replace : iotivity -> iotivity-sec
[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 = nullptr;
91             rapidxml::xml_node< char > *subItem = nullptr;
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 = nullptr;
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 = 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;
210
211             string strBundleId;
212             string strKey, strValue;
213             OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource configuration for %s %s!",
214                 bundleId.c_str(), resourceUri.c_str());
215
216             if (m_loaded)
217             {
218                 try
219                 {
220                     // <bundle>
221                     bundle = m_xmlDoc.first_node();
222                     if (bundle)
223                     {
224                         for (bundle = bundle->first_node(BUNDLE_TAG); bundle;
225                             bundle = bundle->next_sibling())
226                         {
227                             // <id>
228                             strBundleId = bundle->first_node(BUNDLE_ID)->value();
229
230                             OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle id %s - %s !",
231                                     strBundleId.c_str(), bundleId.c_str());
232
233                             if (!strBundleId.compare(bundleId))
234                             {
235                                 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
236                                 // <resourceInfo>
237                                 bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
238                                 if (bundle)
239                                 {
240                                     for (resource = bundle->first_node(OUTPUT_RESOURCE_INFO);
241                                          resource; resource = resource->next_sibling())
242                                     {
243
244                                         for (item = resource->first_node(); item; item =
245                                                  item->next_sibling())
246                                         {
247                                             strKey = item->name();
248                                             strValue = item->value();
249
250                                             if (!strKey.compare(OUTPUT_RESOURCE_NAME))
251                                             {
252                                                 
253                                                 resourceInfoOut->name = trim_both(strValue);
254                                             }
255
256                                             else if (!strKey.compare(OUTPUT_RESOURCE_URI))
257                                             {
258                                                 if (trim_both(strValue).compare(resourceUri) != 0)
259                                                 {
260                                                     break;
261                                                 }
262                                                 resourceInfoOut->uri = trim_both(strValue);
263                                             }
264
265                                             else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
266                                             {
267                                                 resourceInfoOut->address = trim_both(strValue);
268                                             }
269
270                                             else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
271                                             {
272                                                 resourceInfoOut->resourceType = trim_both(strValue);
273                                             }
274
275                                             else
276                                             {
277                                                 for (subItem = item->first_node(); subItem; subItem =
278                                                          subItem->next_sibling())
279                                                 {
280                                                     map< string, string > propertyMap;
281
282                                                     strKey = subItem->name();
283
284                                                     if (strKey.compare(INPUT_RESOURCE))
285                                                     {
286                                                         m_mapisHasInput[strBundleId] = true;
287                                                         OIC_LOG_V(INFO, CONTAINER_TAG,
288                                                                 "Bundle has input (%s)",
289                                                                 strBundleId.c_str());
290                                                     }
291
292                                                     for (subItem2 = subItem->first_node(); subItem2;
293                                                          subItem2 = subItem2->next_sibling())
294                                                     {
295                                                         string newStrKey = subItem2->name();
296                                                         string newStrValue = subItem2->value();
297                                                         OIC_LOG_V(INFO, CONTAINER_TAG,
298                                                                 "key: %s, value %s",
299                                                                 newStrKey.c_str(), newStrValue.c_str());
300
301                                                         propertyMap[trim_both(newStrKey)] =
302                                                                 trim_both(newStrValue);
303                                                     }
304
305                                                     resourceInfoOut->resourceProperty[trim_both(strKey)].push_back(
306                                                         propertyMap);
307                                                 }
308                                             }
309                                         }
310
311                                     }
312                                 }
313                             }
314                         }
315                     }
316                 }
317                 catch (rapidxml::parse_error &e)
318                 {
319                     OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
320                     OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
321                 }
322             }
323             else
324             {
325                 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
326             }
327         }
328
329         void Configuration::getResourceConfiguration(std::string bundleId,
330                 std::vector< resourceInfo > *configOutput)
331         {
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;
337
338             string strBundleId;
339             string strKey, strValue;
340             OIC_LOG(INFO, CONTAINER_TAG, "Loading resource configuration!");
341
342             if (m_loaded)
343             {
344                 try
345                 {
346                     // <bundle>
347                     bundle = m_xmlDoc.first_node();
348                     if (bundle)
349                     {
350                         for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
351                                  bundle->next_sibling())
352                         {
353                             // <id>
354                             if (bundle->first_node(BUNDLE_ID))
355                             {
356                                 strBundleId = bundle->first_node(BUNDLE_ID)->value();
357                             }
358
359                             OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle ids %s - %s !",
360                                     strBundleId.c_str(), bundleId.c_str());
361
362                             if (!strBundleId.compare(bundleId))
363                             {
364                                 OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
365                                 // <resourceInfo>
366                                 bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
367                                 if (bundle)
368                                 {
369                                     for (resource = bundle->
370                                             first_node(OUTPUT_RESOURCE_INFO);
371                                          resource; resource = resource->next_sibling())
372                                     {
373                                         resourceInfo tempResourceInfo;
374
375                                         for (item = resource->first_node(); item; item =
376                                                  item->next_sibling())
377                                         {
378                                             strKey = item->name();
379                                             strValue = item->value();
380
381                                             if (!strKey.compare(OUTPUT_RESOURCE_NAME))
382                                             {
383                                                 tempResourceInfo.name = trim_both(strValue);
384                                             }
385
386                                             else if (!strKey.compare(OUTPUT_RESOURCE_URI))
387                                             {
388                                                 tempResourceInfo.uri = trim_both(strValue);
389                                             }
390
391                                             else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
392                                             {
393                                                 tempResourceInfo.address = trim_both(strValue);
394                                             }
395
396                                             else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
397                                             {
398                                                 tempResourceInfo.resourceType = trim_both(strValue);
399                                             }
400
401                                             else
402                                             {
403                                                 for (subItem = item->first_node(); subItem; subItem =
404                                                          subItem->next_sibling())
405                                                 {
406                                                     map< string, string > propertyMap;
407
408                                                     strKey = subItem->name();
409
410                                                     if (strKey.compare(INPUT_RESOURCE))
411                                                     {
412                                                         m_mapisHasInput[strBundleId] = true;
413                                                         OIC_LOG_V(INFO, CONTAINER_TAG,
414                                                                 "Bundle has input (%s)",
415                                                                 strBundleId.c_str());
416                                                     }
417
418                                                     for (subItem2 = subItem->first_node(); subItem2;
419                                                          subItem2 = subItem2->next_sibling())
420                                                     {
421                                                         string newStrKey = subItem2->name();
422                                                         string newStrValue = subItem2->value();
423                                                         OIC_LOG_V(INFO, CONTAINER_TAG,
424                                                                 "key: %s, value %s",
425                                                                 newStrKey.c_str(),
426                                                                 newStrValue.c_str());
427
428                                                         propertyMap[trim_both(newStrKey)] =
429                                                                 trim_both(newStrValue);
430                                                     }
431
432                                                     tempResourceInfo.resourceProperty[trim_both(strKey)].
433                                                     push_back(propertyMap);
434                                                 }
435                                             }
436                                         }
437                                         configOutput->push_back(tempResourceInfo);
438                                     }
439                                 }
440                             }
441                         }
442                     }
443                 }
444                 catch (rapidxml::parse_error &e)
445                 {
446                     OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
447                     OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
448                 }
449             }
450             else
451             {
452                 OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
453             }
454         }
455
456         void Configuration::getConfigDocument(std::string pathConfigFile)
457         {
458             std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
459
460             if (!xmlFile.fail())
461             {
462                 xmlFile.seekg(0, std::ios::end);
463                 unsigned int size = (unsigned int) xmlFile.tellg();
464                 xmlFile.seekg(0);
465
466                 std::vector< char > xmlData(size + 1);
467                 xmlData[size] = 0;
468
469                 xmlFile.read(&xmlData.front(), (std::streamsize) size);
470                 xmlFile.close();
471                 m_strConfigData = std::string(xmlData.data());
472
473                 try
474                 {
475                     m_xmlDoc.parse< 0 >((char *) m_strConfigData.c_str());
476                     m_loaded = true;
477                 }
478                 catch (rapidxml::parse_error &e)
479                 {
480                     OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
481                     OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
482                 }
483             }
484             else
485             {
486                 OIC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");
487             }
488         }
489     }
490 }