コード例 #1
0
std::vector<std::shared_ptr<SimulatorResource> > SimulatorResourceFactory::createResource(
    const std::string &configPath, unsigned int count)
{
    std::vector<std::shared_ptr<SimulatorResource>> resources;

    // Parse the RAML file
    std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(configPath);
    RAML::RamlPtr raml = ramlParser->getRamlPtr();

    // Get the first resource model from RAML
    RAML::RamlResourcePtr ramlResource;
    if (0 == raml->getResources().size()
        || nullptr == (ramlResource = raml->getResources().begin()->second))
    {
        OIC_LOG(ERROR, TAG, "Zero resources detected from RAML!");
        return resources;
    }

    while (count--)
    {
        std::shared_ptr<SimulatorResource> resource = buildResource(ramlResource);
        if (!resource)
        {
            OIC_LOG(ERROR, TAG, "Failed to create resource!");
            return resources;
        }

        resources.push_back(resource);
    }

    return resources;
}
コード例 #2
0
std::shared_ptr<SimulatorResource> SimulatorResourceFactory::createResource(
    const std::string &configPath)
{
    // Parse the RAML file
    std::shared_ptr<RAML::RamlParser> ramlParser = std::make_shared<RAML::RamlParser>(configPath);
    RAML::RamlPtr raml = ramlParser->getRamlPtr();

    // Get the first resource model from RAML
    RAML::RamlResourcePtr ramlResource;
    if (0 == raml->getResources().size()
        || nullptr == (ramlResource = raml->getResources().begin()->second))
    {
        OIC_LOG(ERROR, TAG, "Zero resources detected from RAML!");
        return nullptr;
    }

    return buildResource(ramlResource);
}
コード例 #3
0
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;
}