SimulatorResourceModel SimulatorResourceFactory::buildModelFromResponseBody(
    RAML::RequestResponseBodyPtr responseBody, std::string &resourceType,
    std::vector<std::string> &interfaceType)
{
    SimulatorResourceModel resModel;

    if (!responseBody)
        return resModel;

    // Iterate throgh all resource property and extract information needed for simulating resource.
    RAML::JsonSchemaPtr resourceProperties = responseBody->getSchema()->getProperties();


    for ( auto &propertyElement : resourceProperties->getProperties())
    {
        if (!propertyElement.second)
            continue;

        std::string propName = propertyElement.second->getName();

        // Resource type
        if ("rt" == propName || "resourceType" == propName)
        {
            resourceType = propertyElement.second->getValueString();
            continue;
        }

        // Interface type
        if ("if" == propName)
        {
            if ("string" == propertyElement.second->getType())
            {
                interfaceType.push_back(propertyElement.second->getValueString());
            }
            else if ("array" == propertyElement.second->getType())
            {
                for (auto &item : propertyElement.second->getItems())
                {
                    if ("string" == item->getType())
                    {
                        interfaceType = item->getAllowedValuesString();
                        break;
                    }
                }
            }
            continue;
        }

        // Other Standard properties which should not be part of resource model
        if ("p" == propName || "n" == propName || "id" == propName)
        {
            continue;
        }

        // Add the attribute to resource model
        if ("array" == propertyElement.second->getType())
        {
            std::vector<SimulatorResourceModel> arrayResModel;
            for ( auto &propertyItem : propertyElement.second->getItems())
            {
                arrayResModel.push_back(buildResourceModel(propertyItem));
            }
            resModel.add(propName, arrayResModel);
        }
        else
        {
            resModel.add(buildAttribute(propertyElement.second));
        }
    }

    if ("array" == resourceProperties->getType())
    {
        std::vector<SimulatorResourceModel> arrayResModel;
        for ( auto &propertyItem : resourceProperties->getItems())
        {
            arrayResModel.push_back(buildResourceModel(propertyItem));
        }
        resModel.add("links", arrayResModel);
    }

    return resModel;
}
SimulatorResourceServerImplSP SimulatorResourceCreator::createResource(
    const std::string &configPath)
{
    RAML::RamlPtr raml;

    try
    {
        std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(configPath);
        raml = ramlParser->getRamlPtr();
    }
    catch (RAML::RamlException &e)
    {
        OC_LOG_V(ERROR, TAG, "RAML Exception occured! [%s]", e.what());
        throw;
    }

    std::map<std::string, RAML::RamlResourcePtr> ramlResources = raml->getResources();
    RAML::RamlResourcePtr ramlResource;
    if (0 == ramlResources.size() || (ramlResource = ramlResources.begin()->second) == nullptr)
    {
        OC_LOG(ERROR, TAG, "Zero resources detected from RAML!");
        return nullptr;
    }

    if (ramlResource)
    {
        SimulatorResourceServerImplSP simResource(new SimulatorResourceServerImpl());
        simResource->setName(ramlResource->getDisplayName());
        simResource->setURI(ramlResource->getResourceUri());

        // Get the resource representation schema from GET response body
        RAML::ActionPtr action = ramlResource->getAction(RAML::ActionType::GET);
        if (!action)
        {
            OC_LOG(ERROR, TAG, "Failed to create resource representation schema as it does not"
                   "posess the GET request!");
            return nullptr;
        }

        RAML::ResponsePtr getResponse = action->getResponse("200");
        if (!getResponse)
        {
            OC_LOG(ERROR, TAG, "Resource does not provide valid GET response!");
            return nullptr;
        }

        RAML::RequestResponseBodyPtr responseBody = getResponse->getResponseBody("application/json");
        if (responseBody)
        {
            RAML::JsonSchemaPtr resourceProperties = responseBody->getSchema()->getProperties();
            for ( auto & propertyElement : resourceProperties->getProperties())
            {
                if (!propertyElement.second)
                    continue;

                std::string propName = propertyElement.second->getName();
                if ("rt" == propName || "resourceType" == propName)
                {
                    simResource->setResourceType(propertyElement.second->getValueString());
                    continue;
                }
                else if ("if" == propName)
                {
                    simResource->setInterfaceType(propertyElement.second->getValueString());
                    continue;
                }
                else if ("p" == propName || "n" == propName || "id" == propName)
                {
                    continue;
                }

                // Build representation attribute
                SimulatorResourceModel::Attribute attribute(propName);
                switch (propertyElement.second->getValueType())
                {
                    case 0: // Integer
                        attribute.setValue(propertyElement.second->getValue<int>());
                        break;

                    case 1: // Double
                        attribute.setValue(propertyElement.second->getValue<double>());
                        break;

                    case 2: // Boolean
                        attribute.setValue(propertyElement.second->getValue<bool>());
                        break;

                    case 3: // String
                        attribute.setValue(propertyElement.second->getValue<std::string>());
                        break;
                }

                // Set range/supported values set
                int min = 0, max = 0, multipleof = 0;
                propertyElement.second->getRange(min, max, multipleof);
                attribute.setRange(min, max);

                if (propertyElement.second->getAllowedValuesSize() > 0)
                    attribute.setAllowedValues(propertyElement.second->getAllowedValues());

                simResource->addAttribute(attribute);
            }
        }

        simResource->setURI(constructURI(simResource->getURI()));
        return simResource;
    }

    return nullptr;
}
Ejemplo n.º 3
0
SimulatorResourceModelSP RequestModelBuilder::createRepSchema(const RAML::RequestResponseBodyPtr
        &rep)
{
    if (!rep)
    {
        return nullptr;
    }

    RAML::SchemaPtr schema = rep->getSchema();
    if (!schema)
    {
        return nullptr;
    }

    RAML::JsonSchemaPtr properties = schema->getProperties();
    if (!properties || 0 == properties->getProperties().size())
        return nullptr;

    SimulatorResourceModelSP repSchema = std::make_shared<SimulatorResourceModel>();
    for (auto &propertyEntry : properties->getProperties())
    {
        std::string propName = propertyEntry.second->getName();
        if ("rt" == propName || "resourceType" == propName || "if" == propName
            || "p" == propName || "n" == propName || "id" == propName)
            continue;

        int valueType = propertyEntry.second->getValueType();
        switch (valueType)
        {
            case 0: // Integer
                {
                    // Add the attribute with value
                    repSchema->add(propertyEntry.second->getName(), propertyEntry.second->getValue<int>());

                    // Convert supported values
                    std::vector<int> allowedValues = propertyEntry.second->getAllowedValuesInt();
                    if (allowedValues.size() > 0)
                    {
                        SimulatorResourceModel::AttributeProperty attrProp(allowedValues);
                        repSchema->setAttributeProperty(propName, attrProp);
                    }
                }
                break;

            case 1: // Double
                {
                    // Add the attribute with value
                    repSchema->add(propertyEntry.second->getName(), propertyEntry.second->getValue<double>());

                    // Convert suppoted values
                    std::vector<double> allowedValues = propertyEntry.second->getAllowedValuesDouble();
                    if (allowedValues.size() > 0)
                    {
                        SimulatorResourceModel::AttributeProperty attrProp(allowedValues);
                        repSchema->setAttributeProperty(propName, attrProp);
                    }
                }
                break;

            case 2: // Boolean
                {
                    // Add the attribute with value
                    repSchema->add(propertyEntry.second->getName(), propertyEntry.second->getValue<bool>());
                    // Convert supported values
                    std::vector<bool> allowedValues = propertyEntry.second->getAllowedValuesBool();
                    if (allowedValues.size() > 0)
                    {
                        SimulatorResourceModel::AttributeProperty attrProp(allowedValues);
                        repSchema->setAttributeProperty(propName, attrProp);
                    }
                }
                break;

            case 3: // String
                {
                    // Add the attribute with value
                    repSchema->add(propertyEntry.second->getName(),
                                   propertyEntry.second->getValue<std::string>());

                    // Convert suppored values
                    std::vector<std::string> allowedValues = propertyEntry.second->getAllowedValuesString();
                    if (allowedValues.size() > 0)
                    {
                        SimulatorResourceModel::AttributeProperty attrProp(allowedValues);
                        repSchema->setAttributeProperty(propName, attrProp);
                    }
                }
                break;
        }

        // Set the range property if its present
        double min, max;
        int multipleof;
        propertyEntry.second->getRange(min, max, multipleof);
        if (min != INT_MIN && max != INT_MAX)
        {
            SimulatorResourceModel::AttributeProperty attrProp(min, max);
            repSchema->setAttributeProperty(propName, attrProp);
        }
    }

    return repSchema;
}