1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 "Configuration.h"
25 #include "InternalTypes.h"
27 #define CONTAINER_TAG "RESOURCE_CONTAINER"
28 #define DISCOVER_TAG "DISCOVER_RESOURCE_UNIT"
34 static inline std::string trim_both(const std::string &str)
36 size_t npos = str.find_first_not_of(" \t\v\n\r");
38 if (npos == std::string::npos)
43 std::string tempString = str.substr(npos, str.length());
44 npos = tempString.find_last_not_of(" \t\v\n\r");
46 return npos == std::string::npos ? tempString : tempString.substr(0, npos + 1);
49 Configuration::Configuration()
54 Configuration::Configuration(string configFile)
58 m_pathConfigFile.append(configFile);
60 getConfigDocument(m_pathConfigFile);
63 Configuration::~Configuration()
67 bool Configuration::isLoaded() const
72 bool Configuration::isHasInput(std::string &bundleId) const
77 OIC_LOG_V(INFO, CONTAINER_TAG, "isHasInput: (%d) %s",m_mapisHasInput.at(bundleId), bundleId.c_str() );
78 return m_mapisHasInput.at(bundleId);
80 catch (std::out_of_range &e)
82 OIC_LOG_V(INFO, CONTAINER_TAG, "isHasInput out of range %s.", bundleId.c_str());
87 void Configuration::getConfiguredBundles(configInfo *configOutput)
89 rapidxml::xml_node< char > *bundle;
90 rapidxml::xml_node< char > *subItem;
92 string strKey, strValue;
98 bundle = m_xmlDoc.first_node();
101 for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
102 bundle->next_sibling())
104 std::map< std::string, std::string > bundleMap;
105 for (subItem = bundle->first_node(); subItem;
106 subItem = subItem->next_sibling())
108 strKey = subItem->name();
109 strValue = subItem->value();
111 if (strlen(subItem->value()) > 0)
114 std::make_pair(trim_both(strKey), trim_both(strValue)));
117 configOutput->push_back(bundleMap);
122 catch (rapidxml::parse_error &e)
124 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
125 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception : (%s)", e.what());
130 void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
132 rapidxml::xml_node< char > *bundle;
134 string strBundleId, strPath, strVersion;
140 std::map< std::string, std::string > bundleConfigMap;
143 bundle = m_xmlDoc.first_node();
146 for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
147 bundle->next_sibling())
150 if (bundle->first_node(BUNDLE_ID))
152 strBundleId = bundle->first_node(BUNDLE_ID)->value();
158 if (!strBundleId.compare(bundleId))
160 bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
163 if (bundle->first_node(BUNDLE_PATH)){
164 strPath = bundle->first_node(BUNDLE_PATH)->value();
169 bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
172 if (bundle->first_node(BUNDLE_VERSION)){
173 strVersion = bundle->first_node(BUNDLE_VERSION)->value();
178 bundleConfigMap.insert(
179 std::make_pair(BUNDLE_VERSION, trim_both(strVersion)));
181 configOutput->push_back(bundleConfigMap);
188 catch (rapidxml::parse_error &e)
190 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
191 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
196 void Configuration::getResourceConfiguration(std::string bundleId, std::string resourceUri,
197 resourceInfo *resourceInfoOut){
198 rapidxml::xml_node< char > *bundle;
199 rapidxml::xml_node< char > *resource;
200 rapidxml::xml_node< char > *item, *subItem, *subItem2;
203 string strKey, strValue;
204 OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource configuration for %s %s!",
205 bundleId.c_str(), resourceUri.c_str());
212 bundle = m_xmlDoc.first_node();
215 for (bundle = bundle->first_node(BUNDLE_TAG); bundle;
216 bundle = bundle->next_sibling())
219 strBundleId = bundle->first_node(BUNDLE_ID)->value();
221 OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle id %s - %s !",
222 strBundleId.c_str(), bundleId.c_str());
224 if (!strBundleId.compare(bundleId))
226 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
228 bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
230 for (resource = bundle->first_node(OUTPUT_RESOURCE_INFO);
231 resource; resource = resource->next_sibling())
234 for (item = resource->first_node(); item; item =
235 item->next_sibling())
237 strKey = item->name();
238 strValue = item->value();
240 if (!strKey.compare(OUTPUT_RESOURCE_NAME))
243 resourceInfoOut->name = trim_both(strValue);
246 else if (!strKey.compare(OUTPUT_RESOURCE_URI))
248 if (trim_both(strValue).compare(resourceUri) != 0)
252 resourceInfoOut->uri = trim_both(strValue);
255 else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
256 resourceInfoOut->address = trim_both(strValue);
258 else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
259 resourceInfoOut->resourceType = trim_both(strValue);
263 for (subItem = item->first_node(); subItem; subItem =
264 subItem->next_sibling())
266 map< string, string > propertyMap;
268 strKey = subItem->name();
270 if (strKey.compare(INPUT_RESOURCE))
272 m_mapisHasInput[strBundleId] = true;
273 OIC_LOG_V(INFO, CONTAINER_TAG,
274 "Bundle has input (%s)",
275 strBundleId.c_str());
278 for (subItem2 = subItem->first_node(); subItem2;
279 subItem2 = subItem2->next_sibling())
281 string newStrKey = subItem2->name();
282 string newStrValue = subItem2->value();
283 OIC_LOG_V(INFO, CONTAINER_TAG,
285 newStrKey.c_str(), newStrValue.c_str());
287 propertyMap[trim_both(newStrKey)] =
288 trim_both(newStrValue);
291 resourceInfoOut->resourceProperty[trim_both(strKey)].push_back(
303 catch (rapidxml::parse_error &e)
305 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
306 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
310 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
314 void Configuration::getResourceConfiguration(std::string bundleId,
315 std::vector< resourceInfo > *configOutput)
317 rapidxml::xml_node< char > *bundle;
318 rapidxml::xml_node< char > *resource;
319 rapidxml::xml_node< char > *item, *subItem, *subItem2;
322 string strKey, strValue;
323 OIC_LOG(INFO, CONTAINER_TAG, "Loading resource configuration!");
330 bundle = m_xmlDoc.first_node();
333 for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
334 bundle->next_sibling())
337 strBundleId = bundle->first_node(BUNDLE_ID)->value();
339 OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle ids %s - %s !",
340 strBundleId.c_str(), bundleId.c_str());
342 if (!strBundleId.compare(bundleId))
344 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
346 bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
349 for (resource = bundle->
350 first_node(OUTPUT_RESOURCE_INFO);
351 resource; resource = resource->next_sibling())
353 resourceInfo tempResourceInfo;
355 for (item = resource->first_node(); item; item =
356 item->next_sibling())
358 strKey = item->name();
359 strValue = item->value();
361 if (!strKey.compare(OUTPUT_RESOURCE_NAME))
362 tempResourceInfo.name = trim_both(strValue);
364 else if (!strKey.compare(OUTPUT_RESOURCE_URI))
365 tempResourceInfo.uri = trim_both(strValue);
367 else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
368 tempResourceInfo.address = trim_both(strValue);
370 else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
371 tempResourceInfo.resourceType = trim_both(strValue);
375 for (subItem = item->first_node(); subItem; subItem =
376 subItem->next_sibling())
378 map< string, string > propertyMap;
380 strKey = subItem->name();
382 if (strKey.compare(INPUT_RESOURCE))
384 m_mapisHasInput[strBundleId] = true;
385 OIC_LOG_V(INFO, CONTAINER_TAG,
386 "Bundle has input (%s)",
387 strBundleId.c_str());
390 for (subItem2 = subItem->first_node(); subItem2;
391 subItem2 = subItem2->next_sibling())
393 string newStrKey = subItem2->name();
394 string newStrValue = subItem2->value();
395 OIC_LOG_V(INFO, CONTAINER_TAG,
398 newStrValue.c_str());
400 propertyMap[trim_both(newStrKey)] =
401 trim_both(newStrValue);
404 tempResourceInfo.resourceProperty[trim_both(strKey)].
405 push_back(propertyMap);
409 configOutput->push_back(tempResourceInfo);
416 catch (rapidxml::parse_error &e)
418 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
419 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
423 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
427 void Configuration::getConfigDocument(std::string pathConfigFile)
429 std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
433 xmlFile.seekg(0, std::ios::end);
434 unsigned int size = (unsigned int) xmlFile.tellg();
437 std::vector< char > xmlData(size + 1);
440 xmlFile.read(&xmlData.front(), (std::streamsize) size);
442 m_strConfigData = std::string(xmlData.data());
446 m_xmlDoc.parse< 0 >((char *) m_strConfigData.c_str());
449 catch (rapidxml::parse_error &e)
451 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
452 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
457 OIC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");