fc5e1ae7ae27391916c5623ad9f832691a7a9556
[platform/upstream/iotivity.git] / service / resource-container / src / Configuration.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "Configuration.h"
22
23 #include <stdexcept>
24 #include <utility>
25 #include "InternalTypes.h"
26 #include <string.h>
27
28 #define CONTAINER_TAG "RESOURCE_CONTAINER"
29 #define DISCOVER_TAG "DISCOVER_RESOURCE_UNIT"
30
31 namespace OIC
32 {
33     namespace Service
34     {
35         static inline std::string trim_both(const std::string &str)
36         {
37             size_t npos = str.find_first_not_of(" \t\v\n\r");
38
39             if (npos == std::string::npos)
40             {
41                 return "";
42             }
43
44             std::string tempString = str.substr(npos, str.length());
45             npos = tempString.find_last_not_of(" \t\v\n\r");
46
47             return npos == std::string::npos ? tempString : tempString.substr(0, npos + 1);
48         }
49
50         Configuration::Configuration()
51         {
52             m_loaded = false;
53         }
54
55         Configuration::Configuration(string configFile)
56         {
57             m_loaded = false;
58
59             m_pathConfigFile.append(configFile);
60
61             getConfigDocument(m_pathConfigFile);
62         }
63
64         Configuration::~Configuration()
65         {
66         }
67
68         bool Configuration::isLoaded() const
69         {
70             return m_loaded;
71         }
72
73         bool Configuration::isHasInput(std::string &bundleId) const
74         {
75
76             try
77             {
78                 OIC_LOG_V(INFO, CONTAINER_TAG, "isHasInput: (%d) %s",m_mapisHasInput.at(bundleId), bundleId.c_str() );
79                 return m_mapisHasInput.at(bundleId);
80             }
81             catch (std::out_of_range &e)
82             {
83                 OIC_LOG_V(INFO, CONTAINER_TAG, "isHasInput out of range %s.", bundleId.c_str());
84                 return false;
85             }
86         }
87
88         void Configuration::getConfiguredBundles(configInfo *configOutput)
89         {
90             rapidxml::xml_node< char > *bundle;
91             rapidxml::xml_node< char > *subItem;
92
93             string strKey, strValue;
94
95             if (m_loaded)
96             {
97                 try
98                 {
99                     bundle = m_xmlDoc.first_node();
100                     if (bundle)
101                     {
102                         for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
103                                  bundle->next_sibling())
104                         {
105                             std::map< std::string, std::string > bundleMap;
106                             for (subItem = bundle->first_node(); subItem;
107                                  subItem = subItem->next_sibling())
108                             {
109                                 strKey = subItem->name();
110                                 strValue = subItem->value();
111
112                                 if (strlen(subItem->value()) > 0)
113                                 {
114                                     bundleMap.insert(
115                                         std::make_pair(trim_both(strKey), trim_both(strValue)));
116                                 }
117                             }
118                             configOutput->push_back(bundleMap);
119                         }
120                     }
121
122                 }
123                 catch (rapidxml::parse_error &e)
124                 {
125                     OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
126                     OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception : (%s)", e.what());
127                 }
128             }
129         }
130
131         void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
132         {
133             rapidxml::xml_node< char > *bundle;
134
135             string strBundleId, strPath, strVersion;
136
137             if (m_loaded)
138             {
139                 try
140                 {
141                     std::map< std::string, std::string > bundleConfigMap;
142
143                     // <bundle>
144                     bundle = m_xmlDoc.first_node();
145                     if (bundle)
146                     {
147                         for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
148                                  bundle->next_sibling())
149                         {
150                             // <id>
151                             if (bundle->first_node(BUNDLE_ID))
152                             {
153                                 strBundleId = bundle->first_node(BUNDLE_ID)->value();
154                             }
155                             else
156                             {
157                                 strBundleId = "";
158                             }
159
160                             if (!strBundleId.compare(bundleId))
161                             {
162                                 bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
163
164                                 // <path>
165                                 if (bundle->first_node(BUNDLE_PATH))
166                                 {
167                                     strPath = bundle->first_node(BUNDLE_PATH)->value();
168                                 }
169                                 else
170                                 {
171                                     strPath = "";
172                                 }
173                                 bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
174
175                                 // <version>
176                                 if (bundle->first_node(BUNDLE_VERSION))
177                                 {
178                                     strVersion = bundle->first_node(BUNDLE_VERSION)->value();
179                                 }
180                                 else
181                                 {
182                                     strVersion = "";
183                                 }
184                                 bundleConfigMap.insert(
185                                     std::make_pair(BUNDLE_VERSION, trim_both(strVersion)));
186
187                                 configOutput->push_back(bundleConfigMap);
188
189                                 break;
190                             }
191                         }
192                     }
193                 }
194                 catch (rapidxml::parse_error &e)
195                 {
196                     OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
197                     OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
198                 }
199             }
200         }
201
202         void Configuration::getResourceConfiguration(std::string bundleId, std::string resourceUri,
203                         resourceInfo *resourceInfoOut)
204         {
205             rapidxml::xml_node< char > *bundle;
206             rapidxml::xml_node< char > *resource;
207             rapidxml::xml_node< char > *item, *subItem, *subItem2;
208
209             string strBundleId;
210             string strKey, strValue;
211             OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource configuration for %s %s!",
212                 bundleId.c_str(), resourceUri.c_str());
213
214             if (m_loaded)
215             {
216                 try
217                 {
218                     // <bundle>
219                     bundle = m_xmlDoc.first_node();
220                     if (bundle)
221                     {
222                         for (bundle = bundle->first_node(BUNDLE_TAG); bundle;
223                             bundle = bundle->next_sibling())
224                         {
225                             // <id>
226                             strBundleId = bundle->first_node(BUNDLE_ID)->value();
227
228                             OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle id %s - %s !",
229                                     strBundleId.c_str(), bundleId.c_str());
230
231                             if (!strBundleId.compare(bundleId))
232                             {
233                                 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
234                                 // <resourceInfo>
235                                 bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
236                                 if (bundle)
237                                 {
238                                     for (resource = bundle->first_node(OUTPUT_RESOURCE_INFO);
239                                          resource; resource = resource->next_sibling())
240                                     {
241
242                                         for (item = resource->first_node(); item; item =
243                                                  item->next_sibling())
244                                         {
245                                             strKey = item->name();
246                                             strValue = item->value();
247
248                                             if (!strKey.compare(OUTPUT_RESOURCE_NAME))
249                                             {
250                                                 
251                                                 resourceInfoOut->name = trim_both(strValue);
252                                             }
253
254                                             else if (!strKey.compare(OUTPUT_RESOURCE_URI))
255                                             {
256                                                 if (trim_both(strValue).compare(resourceUri) != 0)
257                                                 {
258                                                     break;
259                                                 }
260                                                 resourceInfoOut->uri = trim_both(strValue);
261                                             }
262
263                                             else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
264                                             {
265                                                 resourceInfoOut->address = trim_both(strValue);
266                                             }
267
268                                             else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
269                                             {
270                                                 resourceInfoOut->resourceType = trim_both(strValue);
271                                             }
272
273                                             else
274                                             {
275                                                 for (subItem = item->first_node(); subItem; subItem =
276                                                          subItem->next_sibling())
277                                                 {
278                                                     map< string, string > propertyMap;
279
280                                                     strKey = subItem->name();
281
282                                                     if (strKey.compare(INPUT_RESOURCE))
283                                                     {
284                                                         m_mapisHasInput[strBundleId] = true;
285                                                         OIC_LOG_V(INFO, CONTAINER_TAG,
286                                                                 "Bundle has input (%s)",
287                                                                 strBundleId.c_str());
288                                                     }
289
290                                                     for (subItem2 = subItem->first_node(); subItem2;
291                                                          subItem2 = subItem2->next_sibling())
292                                                     {
293                                                         string newStrKey = subItem2->name();
294                                                         string newStrValue = subItem2->value();
295                                                         OIC_LOG_V(INFO, CONTAINER_TAG,
296                                                                 "key: %s, value %s",
297                                                                 newStrKey.c_str(), newStrValue.c_str());
298
299                                                         propertyMap[trim_both(newStrKey)] =
300                                                                 trim_both(newStrValue);
301                                                     }
302
303                                                     resourceInfoOut->resourceProperty[trim_both(strKey)].push_back(
304                                                         propertyMap);
305                                                 }
306                                             }
307                                         }
308
309                                     }
310                                 }
311                             }
312                         }
313                     }
314                 }
315                 catch (rapidxml::parse_error &e)
316                 {
317                     OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
318                     OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
319                 }
320             }
321             else
322             {
323                 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
324             }
325         }
326
327         void Configuration::getResourceConfiguration(std::string bundleId,
328                 std::vector< resourceInfo > *configOutput)
329         {
330             rapidxml::xml_node< char > *bundle;
331             rapidxml::xml_node< char > *resource;
332             rapidxml::xml_node< char > *item, *subItem, *subItem2;
333
334             string strBundleId;
335             string strKey, strValue;
336             OIC_LOG(INFO, CONTAINER_TAG, "Loading resource configuration!");
337
338             if (m_loaded)
339             {
340                 try
341                 {
342                     // <bundle>
343                     bundle = m_xmlDoc.first_node();
344                     if (bundle)
345                     {
346                         for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
347                                  bundle->next_sibling())
348                         {
349                             // <id>
350                             strBundleId = bundle->first_node(BUNDLE_ID)->value();
351
352                             OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle ids %s - %s !",
353                                     strBundleId.c_str(), bundleId.c_str());
354
355                             if (!strBundleId.compare(bundleId))
356                             {
357                                 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
358                                 // <resourceInfo>
359                                 bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
360                                 if (bundle)
361                                 {
362                                     for (resource = bundle->
363                                             first_node(OUTPUT_RESOURCE_INFO);
364                                          resource; resource = resource->next_sibling())
365                                     {
366                                         resourceInfo tempResourceInfo;
367
368                                         for (item = resource->first_node(); item; item =
369                                                  item->next_sibling())
370                                         {
371                                             strKey = item->name();
372                                             strValue = item->value();
373
374                                             if (!strKey.compare(OUTPUT_RESOURCE_NAME))
375                                             {
376                                                 tempResourceInfo.name = trim_both(strValue);
377                                             }
378
379                                             else if (!strKey.compare(OUTPUT_RESOURCE_URI))
380                                             {
381                                                 tempResourceInfo.uri = trim_both(strValue);
382                                             }
383
384                                             else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
385                                             {
386                                                 tempResourceInfo.address = trim_both(strValue);
387                                             }
388
389                                             else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
390                                             {
391                                                 tempResourceInfo.resourceType = trim_both(strValue);
392                                             }
393
394                                             else
395                                             {
396                                                 for (subItem = item->first_node(); subItem; subItem =
397                                                          subItem->next_sibling())
398                                                 {
399                                                     map< string, string > propertyMap;
400
401                                                     strKey = subItem->name();
402
403                                                     if (strKey.compare(INPUT_RESOURCE))
404                                                     {
405                                                         m_mapisHasInput[strBundleId] = true;
406                                                         OIC_LOG_V(INFO, CONTAINER_TAG,
407                                                                 "Bundle has input (%s)",
408                                                                 strBundleId.c_str());
409                                                     }
410
411                                                     for (subItem2 = subItem->first_node(); subItem2;
412                                                          subItem2 = subItem2->next_sibling())
413                                                     {
414                                                         string newStrKey = subItem2->name();
415                                                         string newStrValue = subItem2->value();
416                                                         OIC_LOG_V(INFO, CONTAINER_TAG,
417                                                                 "key: %s, value %s",
418                                                                 newStrKey.c_str(),
419                                                                 newStrValue.c_str());
420
421                                                         propertyMap[trim_both(newStrKey)] =
422                                                                 trim_both(newStrValue);
423                                                     }
424
425                                                     tempResourceInfo.resourceProperty[trim_both(strKey)].
426                                                     push_back(propertyMap);
427                                                 }
428                                             }
429                                         }
430                                         configOutput->push_back(tempResourceInfo);
431                                     }
432                                 }
433                             }
434                         }
435                     }
436                 }
437                 catch (rapidxml::parse_error &e)
438                 {
439                     OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
440                     OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
441                 }
442             }
443             else
444             {
445                 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
446             }
447         }
448
449         void Configuration::getConfigDocument(std::string pathConfigFile)
450         {
451             std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
452
453             if (!xmlFile.fail())
454             {
455                 xmlFile.seekg(0, std::ios::end);
456                 unsigned int size = (unsigned int) xmlFile.tellg();
457                 xmlFile.seekg(0);
458
459                 std::vector< char > xmlData(size + 1);
460                 xmlData[size] = 0;
461
462                 xmlFile.read(&xmlData.front(), (std::streamsize) size);
463                 xmlFile.close();
464                 m_strConfigData = std::string(xmlData.data());
465
466                 try
467                 {
468                     m_xmlDoc.parse< 0 >((char *) m_strConfigData.c_str());
469                     m_loaded = true;
470                 }
471                 catch (rapidxml::parse_error &e)
472                 {
473                     OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
474                     OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
475                 }
476             }
477             else
478             {
479                 OIC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");
480             }
481         }
482     }
483 }