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"
28 #define CONTAINER_TAG "RESOURCE_CONTAINER"
29 #define DISCOVER_TAG "DISCOVER_RESOURCE_UNIT"
35 static inline std::string trim_both(const std::string &str)
37 size_t npos = str.find_first_not_of(" \t\v\n\r");
39 if (npos == std::string::npos)
44 std::string tempString = str.substr(npos, str.length());
45 npos = tempString.find_last_not_of(" \t\v\n\r");
47 return npos == std::string::npos ? tempString : tempString.substr(0, npos + 1);
50 Configuration::Configuration()
55 Configuration::Configuration(string configFile)
59 m_pathConfigFile.append(configFile);
61 getConfigDocument(m_pathConfigFile);
64 Configuration::~Configuration()
68 bool Configuration::isLoaded() const
73 bool Configuration::isHasInput(std::string &bundleId) const
78 OIC_LOG_V(INFO, CONTAINER_TAG, "isHasInput: (%d) %s",m_mapisHasInput.at(bundleId), bundleId.c_str() );
79 return m_mapisHasInput.at(bundleId);
81 catch (std::out_of_range &e)
83 OIC_LOG_V(INFO, CONTAINER_TAG, "isHasInput out of range %s.", bundleId.c_str());
88 void Configuration::getConfiguredBundles(configInfo *configOutput)
90 rapidxml::xml_node< char > *bundle = nullptr;
91 rapidxml::xml_node< char > *subItem = nullptr;
93 string strKey, strValue;
99 bundle = m_xmlDoc.first_node();
102 for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
103 bundle->next_sibling())
105 std::map< std::string, std::string > bundleMap;
106 for (subItem = bundle->first_node(); subItem;
107 subItem = subItem->next_sibling())
109 strKey = subItem->name();
110 strValue = subItem->value();
112 if (strlen(subItem->value()) > 0)
115 std::make_pair(trim_both(strKey), trim_both(strValue)));
118 configOutput->push_back(bundleMap);
123 catch (rapidxml::parse_error &e)
125 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
126 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception : (%s)", e.what());
131 void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
133 rapidxml::xml_node< char > *bundle = nullptr;
135 string strBundleId, strPath, strVersion;
141 std::map< std::string, std::string > bundleConfigMap;
144 bundle = m_xmlDoc.first_node();
147 for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
148 bundle->next_sibling())
151 if (bundle->first_node(BUNDLE_ID))
153 strBundleId = bundle->first_node(BUNDLE_ID)->value();
160 if (!strBundleId.compare(bundleId))
162 bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
165 if (bundle->first_node(BUNDLE_PATH))
167 strPath = bundle->first_node(BUNDLE_PATH)->value();
173 bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
176 if (bundle->first_node(BUNDLE_VERSION))
178 strVersion = bundle->first_node(BUNDLE_VERSION)->value();
184 bundleConfigMap.insert(
185 std::make_pair(BUNDLE_VERSION, trim_both(strVersion)));
187 configOutput->push_back(bundleConfigMap);
194 catch (rapidxml::parse_error &e)
196 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
197 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
202 void Configuration::getResourceConfiguration(std::string bundleId, std::string resourceUri,
203 resourceInfo *resourceInfoOut)
205 rapidxml::xml_node< char > *bundle = nullptr;
206 rapidxml::xml_node< char > *resource = nullptr;
207 rapidxml::xml_node< char > *item = nullptr;
208 rapidxml::xml_node< char > *subItem = nullptr;
209 rapidxml::xml_node< char > *subItem2 = nullptr;
212 string strKey, strValue;
213 OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource configuration for %s %s!",
214 bundleId.c_str(), resourceUri.c_str());
221 bundle = m_xmlDoc.first_node();
224 for (bundle = bundle->first_node(BUNDLE_TAG); bundle;
225 bundle = bundle->next_sibling())
228 strBundleId = bundle->first_node(BUNDLE_ID)->value();
230 OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle id %s - %s !",
231 strBundleId.c_str(), bundleId.c_str());
233 if (!strBundleId.compare(bundleId))
235 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
237 bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
240 for (resource = bundle->first_node(OUTPUT_RESOURCE_INFO);
241 resource; resource = resource->next_sibling())
244 for (item = resource->first_node(); item; item =
245 item->next_sibling())
247 strKey = item->name();
248 strValue = item->value();
250 if (!strKey.compare(OUTPUT_RESOURCE_NAME))
253 resourceInfoOut->name = trim_both(strValue);
256 else if (!strKey.compare(OUTPUT_RESOURCE_URI))
258 if (trim_both(strValue).compare(resourceUri) != 0)
262 resourceInfoOut->uri = trim_both(strValue);
265 else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
267 resourceInfoOut->address = trim_both(strValue);
270 else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
272 resourceInfoOut->resourceType = trim_both(strValue);
277 for (subItem = item->first_node(); subItem; subItem =
278 subItem->next_sibling())
280 map< string, string > propertyMap;
282 strKey = subItem->name();
284 if (strKey.compare(INPUT_RESOURCE))
286 m_mapisHasInput[strBundleId] = true;
287 OIC_LOG_V(INFO, CONTAINER_TAG,
288 "Bundle has input (%s)",
289 strBundleId.c_str());
292 for (subItem2 = subItem->first_node(); subItem2;
293 subItem2 = subItem2->next_sibling())
295 string newStrKey = subItem2->name();
296 string newStrValue = subItem2->value();
297 OIC_LOG_V(INFO, CONTAINER_TAG,
299 newStrKey.c_str(), newStrValue.c_str());
301 propertyMap[trim_both(newStrKey)] =
302 trim_both(newStrValue);
305 resourceInfoOut->resourceProperty[trim_both(strKey)].push_back(
317 catch (rapidxml::parse_error &e)
319 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
320 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
325 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
329 void Configuration::getResourceConfiguration(std::string bundleId,
330 std::vector< resourceInfo > *configOutput)
332 rapidxml::xml_node< char > *bundle = nullptr;
333 rapidxml::xml_node< char > *resource = nullptr;
334 rapidxml::xml_node< char > *item = nullptr;
335 rapidxml::xml_node< char > *subItem = nullptr;
336 rapidxml::xml_node< char > *subItem2 = nullptr;
339 string strKey, strValue;
340 OIC_LOG(INFO, CONTAINER_TAG, "Loading resource configuration!");
347 bundle = m_xmlDoc.first_node();
350 for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
351 bundle->next_sibling())
354 if (bundle->first_node(BUNDLE_ID))
356 strBundleId = bundle->first_node(BUNDLE_ID)->value();
359 OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle ids %s - %s !",
360 strBundleId.c_str(), bundleId.c_str());
362 if (!strBundleId.compare(bundleId))
364 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
366 bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
369 for (resource = bundle->
370 first_node(OUTPUT_RESOURCE_INFO);
371 resource; resource = resource->next_sibling())
373 resourceInfo tempResourceInfo;
375 for (item = resource->first_node(); item; item =
376 item->next_sibling())
378 strKey = item->name();
379 strValue = item->value();
381 if (!strKey.compare(OUTPUT_RESOURCE_NAME))
383 tempResourceInfo.name = trim_both(strValue);
386 else if (!strKey.compare(OUTPUT_RESOURCE_URI))
388 tempResourceInfo.uri = trim_both(strValue);
391 else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
393 tempResourceInfo.address = trim_both(strValue);
396 else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
398 tempResourceInfo.resourceType = trim_both(strValue);
403 for (subItem = item->first_node(); subItem; subItem =
404 subItem->next_sibling())
406 map< string, string > propertyMap;
408 strKey = subItem->name();
410 if (strKey.compare(INPUT_RESOURCE))
412 m_mapisHasInput[strBundleId] = true;
413 OIC_LOG_V(INFO, CONTAINER_TAG,
414 "Bundle has input (%s)",
415 strBundleId.c_str());
418 for (subItem2 = subItem->first_node(); subItem2;
419 subItem2 = subItem2->next_sibling())
421 string newStrKey = subItem2->name();
422 string newStrValue = subItem2->value();
423 OIC_LOG_V(INFO, CONTAINER_TAG,
426 newStrValue.c_str());
428 propertyMap[trim_both(newStrKey)] =
429 trim_both(newStrValue);
432 tempResourceInfo.resourceProperty[trim_both(strKey)].
433 push_back(propertyMap);
437 configOutput->push_back(tempResourceInfo);
444 catch (rapidxml::parse_error &e)
446 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
447 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
452 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
456 void Configuration::getConfigDocument(std::string pathConfigFile)
458 std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
462 xmlFile.seekg(0, std::ios::end);
463 unsigned int size = (unsigned int) xmlFile.tellg();
466 std::vector< char > xmlData(size + 1);
469 xmlFile.read(&xmlData.front(), (std::streamsize) size);
471 m_strConfigData = std::string(xmlData.data());
475 m_xmlDoc.parse< 0 >((char *) m_strConfigData.c_str());
478 catch (rapidxml::parse_error &e)
480 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
481 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
486 OIC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");