1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 #include "simulator_resource_creator.h"
22 #include "simulator_logger.h"
23 #include <RamlParser.h>
26 #define TAG "SIM_RESOURCE_CREATOR"
28 unsigned int SimulatorResourceCreator::s_id;
29 SimulatorResourceServerImplSP SimulatorResourceCreator::createResource(
30 const std::string &configPath)
36 RAML::RamlParser *ramlParser = new RAML::RamlParser(configPath);
37 raml = ramlParser->getRamlPtr();
39 catch (RAML::RamlException &e)
41 OC_LOG_V(ERROR, TAG, "RAML Exception occured! [%s]", e.what());
45 std::map<std::string, RAML::RamlResourcePtr> ramlResources = raml->getResources();
46 RAML::RamlResourcePtr ramlResource;
47 if (0 == ramlResources.size() || (ramlResource = ramlResources.begin()->second) == nullptr)
49 OC_LOG(ERROR, TAG, "Zero resources detected from RAML!");
55 SimulatorResourceServerImplSP simResource(new SimulatorResourceServerImpl());
56 simResource->setName(ramlResource->getDisplayName());
57 simResource->setURI(ramlResource->getResourceUri());
59 // Get the resource representation schema from GET response body
60 RAML::ActionPtr action = ramlResource->getAction(RAML::ActionType::GET);
63 OC_LOG(ERROR, TAG, "Failed to create resource representation schema as it does not"
64 "posess the GET request!");
68 RAML::ResponsePtr getResponse = action->getResponse("200");
71 OC_LOG(ERROR, TAG, "Resource does not provide valid GET response!");
75 RAML::RequestResponseBodyPtr responseBody = getResponse->getResponseBody("application/json");
78 RAML::JsonSchemaPtr resourceProperties = responseBody->getSchema()->getProperties();
79 for ( auto &propertyElement : resourceProperties->getProperties())
81 if (!propertyElement.second)
84 std::string propName = propertyElement.second->getName();
85 if ("rt" == propName || "resourceType" == propName)
87 simResource->setResourceType(propertyElement.second->getValueString());
90 else if ("if" == propName)
92 simResource->setInterfaceType(propertyElement.second->getValueString());
95 else if ("p" == propName || "n" == propName || "id" == propName)
100 // Build representation attribute
101 SimulatorResourceModel::Attribute attribute(propName);
102 switch (propertyElement.second->getValueType())
105 attribute.setValue(propertyElement.second->getValue<int>());
109 attribute.setValue(propertyElement.second->getValue<double>());
113 attribute.setValue(propertyElement.second->getValue<bool>());
117 attribute.setValue(propertyElement.second->getValue<std::string>());
121 // Set attriute update frequency interval
122 attribute.setUpdateFrequencyTime(propertyElement.second->getUpdateFrequencyTime());
124 // Set range/supported values set
125 int min = 0, max = 0, multipleof = 0;
126 propertyElement.second->getRange(min, max, multipleof);
127 attribute.setRange(min, max);
129 if (propertyElement.second->getAllowedValuesSize() > 0)
130 attribute.setAllowedValues(propertyElement.second->getAllowedValues());
132 simResource->addAttribute(attribute);
136 simResource->setURI(constructURI(simResource->getURI()));
144 * This method appends a unique key to the given URI to make the URI unique in simulator.
145 * Example: If input is "/a/light", then the output will be "/a/light/simulator/0" for the first resource
146 * and "/a/light/simulator/1" for the second resource and so on.
148 std::string SimulatorResourceCreator::constructURI(const std::string &uri)
150 std::ostringstream os;
152 if (!uri.empty() && '/' != uri[uri.length() - 1])
154 os << "simulator/" << s_id++;