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 if (m_xmlDoc.first_node())
100 for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
101 bundle->next_sibling())
103 std::map< std::string, std::string > bundleMap;
104 for (subItem = bundle->first_node(); subItem;
105 subItem = subItem->next_sibling())
107 strKey = subItem->name();
108 strValue = subItem->value();
110 if (strlen(subItem->value()) > 0)
113 std::make_pair(trim_both(strKey), trim_both(strValue)));
116 configOutput->push_back(bundleMap);
121 catch (rapidxml::parse_error &e)
123 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
124 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception : (%s)", e.what());
129 void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
131 rapidxml::xml_node< char > *bundle;
133 string strBundleId, strPath, strVersion;
139 std::map< std::string, std::string > bundleConfigMap;
142 if (m_xmlDoc.first_node())
144 for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
145 bundle->next_sibling())
148 if (bundle->first_node(BUNDLE_ID))
150 strBundleId = bundle->first_node(BUNDLE_ID)->value();
156 if (!strBundleId.compare(bundleId))
158 bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
161 if (bundle->first_node(BUNDLE_PATH)){
162 strPath = bundle->first_node(BUNDLE_PATH)->value();
167 bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
170 if (bundle->first_node(BUNDLE_VERSION)){
171 strVersion = bundle->first_node(BUNDLE_VERSION)->value();
176 bundleConfigMap.insert(
177 std::make_pair(BUNDLE_VERSION, trim_both(strVersion)));
179 configOutput->push_back(bundleConfigMap);
186 catch (rapidxml::parse_error &e)
188 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
189 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
194 void Configuration::getResourceConfiguration(std::string bundleId, std::string resourceName,
195 resourceInfo *resourceInfoOut){
196 rapidxml::xml_node< char > *bundle;
197 rapidxml::xml_node< char > *resource;
198 rapidxml::xml_node< char > *item, *subItem, *subItem2;
201 string strKey, strValue;
202 OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource configuration for %s %s!",
203 bundleId.c_str(), resourceName.c_str());
210 if (m_xmlDoc.first_node())
212 for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
213 bundle->next_sibling())
216 strBundleId = bundle->first_node(BUNDLE_ID)->value();
218 OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle id %s - %s !",
219 strBundleId.c_str(), bundleId.c_str());
221 if (!strBundleId.compare(bundleId))
223 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
225 if (bundle->first_node(OUTPUT_RESOURCES_TAG)){
226 for (resource = bundle->first_node(OUTPUT_RESOURCES_TAG)->
227 first_node(OUTPUT_RESOURCE_INFO);
228 resource; resource = resource->next_sibling())
231 for (item = resource->first_node(); item; item =
232 item->next_sibling())
234 strKey = item->name();
235 strValue = item->value();
237 if (!strKey.compare(OUTPUT_RESOURCE_NAME))
238 resourceInfoOut->name = trim_both(strValue);
240 else if (!strKey.compare(OUTPUT_RESOURCE_URI))
241 resourceInfoOut->uri = trim_both(strValue);
243 else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
244 resourceInfoOut->address = trim_both(strValue);
246 else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
247 resourceInfoOut->resourceType = trim_both(strValue);
251 for (subItem = item->first_node(); subItem; subItem =
252 subItem->next_sibling())
254 map< string, string > propertyMap;
256 strKey = subItem->name();
258 if (strKey.compare(INPUT_RESOURCE))
260 m_mapisHasInput[strBundleId] = true;
261 OIC_LOG_V(INFO, CONTAINER_TAG,
262 "Bundle has input (%s)",
263 strBundleId.c_str());
266 for (subItem2 = subItem->first_node(); subItem2;
267 subItem2 = subItem2->next_sibling())
269 string newStrKey = subItem2->name();
270 string newStrValue = subItem2->value();
271 OIC_LOG_V(INFO, CONTAINER_TAG,
273 newStrKey.c_str(), newStrValue.c_str());
275 propertyMap[trim_both(newStrKey)] =
276 trim_both(newStrValue);
279 resourceInfoOut->resourceProperty[trim_both(strKey)].push_back(
291 catch (rapidxml::parse_error &e)
293 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
294 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
298 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
302 void Configuration::getResourceConfiguration(std::string bundleId,
303 std::vector< resourceInfo > *configOutput)
305 rapidxml::xml_node< char > *bundle;
306 rapidxml::xml_node< char > *resource;
307 rapidxml::xml_node< char > *item, *subItem, *subItem2;
310 string strKey, strValue;
311 OIC_LOG(INFO, CONTAINER_TAG, "Loading resource configuration!");
318 if (m_xmlDoc.first_node())
320 for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
321 bundle->next_sibling())
324 strBundleId = bundle->first_node(BUNDLE_ID)->value();
326 OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle ids %s - %s !",
327 strBundleId.c_str(), bundleId.c_str());
329 if (!strBundleId.compare(bundleId))
331 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
333 if (bundle->first_node(OUTPUT_RESOURCES_TAG))
335 for (resource = bundle->first_node(OUTPUT_RESOURCES_TAG)->
336 first_node(OUTPUT_RESOURCE_INFO);
337 resource; resource = resource->next_sibling())
339 resourceInfo tempResourceInfo;
341 for (item = resource->first_node(); item; item =
342 item->next_sibling())
344 strKey = item->name();
345 strValue = item->value();
347 if (!strKey.compare(OUTPUT_RESOURCE_NAME))
348 tempResourceInfo.name = trim_both(strValue);
350 else if (!strKey.compare(OUTPUT_RESOURCE_URI))
351 tempResourceInfo.uri = trim_both(strValue);
353 else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
354 tempResourceInfo.address = trim_both(strValue);
356 else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
357 tempResourceInfo.resourceType = trim_both(strValue);
361 for (subItem = item->first_node(); subItem; subItem =
362 subItem->next_sibling())
364 map< string, string > propertyMap;
366 strKey = subItem->name();
368 if (strKey.compare(INPUT_RESOURCE))
370 m_mapisHasInput[strBundleId] = true;
371 OIC_LOG_V(INFO, CONTAINER_TAG,
372 "Bundle has input (%s)",
373 strBundleId.c_str());
376 for (subItem2 = subItem->first_node(); subItem2;
377 subItem2 = subItem2->next_sibling())
379 string newStrKey = subItem2->name();
380 string newStrValue = subItem2->value();
381 OIC_LOG_V(INFO, CONTAINER_TAG,
384 newStrValue.c_str());
386 propertyMap[trim_both(newStrKey)] =
387 trim_both(newStrValue);
390 tempResourceInfo.resourceProperty[trim_both(strKey)].
391 push_back(propertyMap);
395 configOutput->push_back(tempResourceInfo);
402 catch (rapidxml::parse_error &e)
404 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
405 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
409 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
413 void Configuration::getConfigDocument(std::string pathConfigFile)
415 std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
419 xmlFile.seekg(0, std::ios::end);
420 unsigned int size = (unsigned int) xmlFile.tellg();
423 std::vector< char > xmlData(size + 1);
426 xmlFile.read(&xmlData.front(), (std::streamsize) size);
428 m_strConfigData = std::string(xmlData.data());
432 m_xmlDoc.parse< 0 >((char *) m_strConfigData.c_str());
435 catch (rapidxml::parse_error &e)
437 OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
438 OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
443 OIC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");