int MpiSetDesired()

in src/platform/ModulesManager.c [605:716]


int MpiSetDesired(MPI_HANDLE handle, const MPI_JSON_STRING payload, const int payloadSizeBytes)
{
    int status = MPI_OK;
    const char* uuid = (const char*)handle;
    char* json = NULL;
    const char* component = NULL;
    const char* object = NULL;
    char* objectJson = NULL;
    int componentCount = 0;
    int objectCount = 0;
    SESSION* session = NULL;
    MODULE_SESSION* moduleSession = NULL;
    JSON_Value* rootValue = NULL;
    JSON_Object* rootObject = NULL;
    JSON_Object* componentObject = NULL;
    JSON_Value* objectValue = NULL;
    int i = 0;
    int j = 0;

    if ((NULL == handle) || (NULL == payload) || (0 >= payloadSizeBytes))
    {
        OsConfigLogError(GetPlatformLog(), "MpiSetDesired(%p, %p, %d) called with invalid arguments", handle, payload, payloadSizeBytes);
        status = EINVAL;
    }
    else if (NULL == (session = FindSession(uuid)))
    {
        OsConfigLogError(GetPlatformLog(), "MpiSetDesired: no session exists with UUID '%s'", uuid);
        status = EINVAL;
    }
    else if (NULL == (json = (char*)malloc(payloadSizeBytes + 1)))
    {
        OsConfigLogError(GetPlatformLog(), "MpiSetDesired: failed to allocate memory for JSON");
        status = ENOMEM;
    }
    else
    {
        memcpy(json, payload, payloadSizeBytes + 1);

        if (NULL == (rootValue = json_parse_string(json)))
        {
            OsConfigLogError(GetPlatformLog(), "MpiSetDesired: failed to parse JSON");
            status = EINVAL;
        }
        else
        {
            // Iterate over the "keys" in the root object
            rootObject = json_value_get_object(rootValue);
            componentCount = (int)json_object_get_count(rootObject);

            for (i = 0; i < componentCount; i++)
            {
                component = json_object_get_name(rootObject, i);
                componentObject = json_object_get_object(rootObject, component);
                moduleSession = FindModuleSession(session->modules, component);

                if (NULL == moduleSession)
                {
                    OsConfigLogError(GetPlatformLog(), "MpiSetDesired: no module exists with component '%s'", component);
                    status = EINVAL;
                }
                else
                {
                    // Iterate over the "keys" in the component object
                    objectCount = (int)json_object_get_count(componentObject);

                    for (j = 0; j < objectCount; j++)
                    {
                        object = json_object_get_name(componentObject, j);
                        objectValue = json_object_get_value(componentObject, object);
                        objectJson = json_serialize_to_string(objectValue);

                        if (NULL == objectJson)
                        {
                            OsConfigLogError(GetPlatformLog(), "MpiSetDesired: failed to serialize JSON");
                        }
                        else if (NULL == moduleSession->module)
                        {
                            OsConfigLogError(GetPlatformLog(), "MpiSetDesired: no module is loaded for session '%s'", uuid);
                        }
                        else
                        {
                            if (MMI_OK != (status = moduleSession->module->set(moduleSession->handle, component, object, objectJson, (int)strlen(objectJson))))
                            {
                                OsConfigLogError(GetPlatformLog(), "MpiSetDesired: MmiSet(%p, %s, %s) failed with %d", moduleSession->handle, component, object, status);
                            }

                            FREE_MEMORY(objectJson);
                        }
                    }
                }
            }

            json_value_free(rootValue);
        }

        FREE_MEMORY(json);
    }

    if (IsDebugLoggingEnabled())
    {
        if (MMI_OK == status)
        {
            OsConfigLogDebug(GetPlatformLog(), "MpiSetDesired(%p, %p, %d) succeeded", handle, payload, payloadSizeBytes);
        }
        else
        {
            OsConfigLogError(GetPlatformLog(), "MpiSetDesired(%p, %p, %d) failed with %d", handle, payload, payloadSizeBytes, status);
        }
    }

    return status;
}