Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-container / src / Configuration.cpp
index e545c25..c1dd082 100644 (file)
@@ -25,6 +25,7 @@
 #include "InternalTypes.h"
 
 #define CONTAINER_TAG "RESOURCE_CONTAINER"
+#define DISCOVER_TAG "DISCOVER_RESOURCE_UNIT"
 
 namespace OIC
 {
@@ -70,12 +71,15 @@ namespace OIC
 
         bool Configuration::isHasInput(std::string &bundleId) const
         {
+
             try
             {
+                OIC_LOG_V(INFO, CONTAINER_TAG, "isHasInput: (%d) %s",m_mapisHasInput.at(bundleId), bundleId.c_str() );
                 return m_mapisHasInput.at(bundleId);
             }
             catch (std::out_of_range &e)
             {
+                OIC_LOG_V(INFO, CONTAINER_TAG, "isHasInput out of range %s.", bundleId.c_str());
                 return false;
             }
         }
@@ -91,30 +95,34 @@ namespace OIC
             {
                 try
                 {
-                    for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
-                             bundle->next_sibling())
+                    bundle = m_xmlDoc.first_node();
+                    if (bundle)
                     {
-                        std::map< std::string, std::string > bundleMap;
-                        for (subItem = bundle->first_node(); subItem;
-                             subItem = subItem->next_sibling())
+                        for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
+                                 bundle->next_sibling())
                         {
-                            strKey = subItem->name();
-                            strValue = subItem->value();
-
-                            if (strlen(subItem->value()) > 0)
+                            std::map< std::string, std::string > bundleMap;
+                            for (subItem = bundle->first_node(); subItem;
+                                 subItem = subItem->next_sibling())
                             {
-                                bundleMap.insert(
-                                    std::make_pair(trim_both(strKey), trim_both(strValue)));
+                                strKey = subItem->name();
+                                strValue = subItem->value();
+
+                                if (strlen(subItem->value()) > 0)
+                                {
+                                    bundleMap.insert(
+                                        std::make_pair(trim_both(strKey), trim_both(strValue)));
+                                }
                             }
+                            configOutput->push_back(bundleMap);
                         }
-                        configOutput->push_back(bundleMap);
                     }
 
                 }
                 catch (rapidxml::parse_error &e)
                 {
-                    OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
-                    OC_LOG_V(ERROR, CONTAINER_TAG, "Exception : (%s)", e.what());
+                    OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
+                    OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception : (%s)", e.what());
                 }
             }
         }
@@ -132,126 +140,288 @@ namespace OIC
                     std::map< std::string, std::string > bundleConfigMap;
 
                     // <bundle>
-                    for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
-                             bundle->next_sibling())
+                    bundle = m_xmlDoc.first_node();
+                    if (bundle)
                     {
-                        // <id>
-                        strBundleId = bundle->first_node(BUNDLE_ID)->value();
-
-                        if (!strBundleId.compare(bundleId))
+                        for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
+                                 bundle->next_sibling())
                         {
-                            bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
+                            // <id>
+                            if (bundle->first_node(BUNDLE_ID))
+                            {
+                                strBundleId = bundle->first_node(BUNDLE_ID)->value();
+                            }
+                            else{
+                                strBundleId = "";
+                            }
 
-                            // <path>
-                            strPath = bundle->first_node(BUNDLE_PATH)->value();
-                            bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
+                            if (!strBundleId.compare(bundleId))
+                            {
+                                bundleConfigMap.insert(std::make_pair(BUNDLE_ID, trim_both(strBundleId)));
 
-                            // <version>
-                            strVersion = bundle->first_node(BUNDLE_VERSION)->value();
-                            bundleConfigMap.insert(
-                                std::make_pair(BUNDLE_VERSION, trim_both(strVersion)));
+                                // <path>
+                                if (bundle->first_node(BUNDLE_PATH)){
+                                    strPath = bundle->first_node(BUNDLE_PATH)->value();
+                                }
+                                else{
+                                    strPath = "";
+                                }
+                                bundleConfigMap.insert(std::make_pair(BUNDLE_PATH, trim_both(strPath)));
+
+                                // <version>
+                                if (bundle->first_node(BUNDLE_VERSION)){
+                                    strVersion = bundle->first_node(BUNDLE_VERSION)->value();
+                                }
+                                else{
+                                    strVersion = "";
+                                }
+                                bundleConfigMap.insert(
+                                    std::make_pair(BUNDLE_VERSION, trim_both(strVersion)));
 
-                            configOutput->push_back(bundleConfigMap);
+                                configOutput->push_back(bundleConfigMap);
 
-                            break;
+                                break;
+                            }
                         }
                     }
                 }
                 catch (rapidxml::parse_error &e)
                 {
-                    OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
-                    OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
+                    OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
+                    OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
                 }
             }
         }
 
-        void Configuration::getResourceConfiguration(std::string bundleId,
-                std::vector< resourceInfo > *configOutput)
-        {
+        void Configuration::getResourceConfiguration(std::string bundleId, std::string resourceUri,
+                        resourceInfo *resourceInfoOut){
             rapidxml::xml_node< char > *bundle;
             rapidxml::xml_node< char > *resource;
             rapidxml::xml_node< char > *item, *subItem, *subItem2;
 
             string strBundleId;
             string strKey, strValue;
+            OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource configuration for %s %s!",
+                bundleId.c_str(), resourceUri.c_str());
 
             if (m_loaded)
             {
                 try
                 {
                     // <bundle>
-                    for (bundle = m_xmlDoc.first_node()->first_node(BUNDLE_TAG); bundle; bundle =
-                             bundle->next_sibling())
+                    bundle = m_xmlDoc.first_node();
+                    if (bundle)
                     {
-                        // <id>
-                        strBundleId = bundle->first_node(BUNDLE_ID)->value();
-
-                        if (!strBundleId.compare(bundleId))
+                        for (bundle = bundle->first_node(BUNDLE_TAG); bundle;
+                            bundle = bundle->next_sibling())
                         {
-                            // <resourceInfo>
-                            for (resource = bundle->first_node(OUTPUT_RESOURCES_TAG)->first_node(OUTPUT_RESOURCE_INFO);
-                                 resource; resource = resource->next_sibling())
+                            // <id>
+                            strBundleId = bundle->first_node(BUNDLE_ID)->value();
+
+                            OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle id %s - %s !",
+                                    strBundleId.c_str(), bundleId.c_str());
+
+                            if (!strBundleId.compare(bundleId))
                             {
-                                resourceInfo tempResourceInfo;
+                                OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
+                                // <resourceInfo>
+                                bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
+                                if (bundle){
+                                    for (resource = bundle->first_node(OUTPUT_RESOURCE_INFO);
+                                         resource; resource = resource->next_sibling())
+                                    {
 
-                                for (item = resource->first_node(); item; item =
-                                         item->next_sibling())
-                                {
-                                    strKey = item->name();
-                                    strValue = item->value();
+                                        for (item = resource->first_node(); item; item =
+                                                 item->next_sibling())
+                                        {
+                                            strKey = item->name();
+                                            strValue = item->value();
 
-                                    if (!strKey.compare(OUTPUT_RESOURCE_NAME))
-                                        tempResourceInfo.name = trim_both(strValue);
+                                            if (!strKey.compare(OUTPUT_RESOURCE_NAME))
+                                            {
+                                                
+                                                resourceInfoOut->name = trim_both(strValue);
+                                            }
 
-                                    else if (!strKey.compare(OUTPUT_RESOURCE_URI))
-                                        tempResourceInfo.uri = trim_both(strValue);
+                                            else if (!strKey.compare(OUTPUT_RESOURCE_URI))
+                                            {
+                                                if (trim_both(strValue).compare(resourceUri) != 0)
+                                                {
+                                                    break;
+                                                }
+                                                resourceInfoOut->uri = trim_both(strValue);
+                                            }
 
-                                    else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
-                                        tempResourceInfo.address = trim_both(strValue);
+                                            else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
+                                                resourceInfoOut->address = trim_both(strValue);
 
-                                    else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
-                                        tempResourceInfo.resourceType = trim_both(strValue);
+                                            else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
+                                                resourceInfoOut->resourceType = trim_both(strValue);
 
-                                    else
+                                            else
+                                            {
+                                                for (subItem = item->first_node(); subItem; subItem =
+                                                         subItem->next_sibling())
+                                                {
+                                                    map< string, string > propertyMap;
+
+                                                    strKey = subItem->name();
+
+                                                    if (strKey.compare(INPUT_RESOURCE))
+                                                    {
+                                                        m_mapisHasInput[strBundleId] = true;
+                                                        OIC_LOG_V(INFO, CONTAINER_TAG,
+                                                                "Bundle has input (%s)",
+                                                                strBundleId.c_str());
+                                                    }
+
+                                                    for (subItem2 = subItem->first_node(); subItem2;
+                                                         subItem2 = subItem2->next_sibling())
+                                                    {
+                                                        string newStrKey = subItem2->name();
+                                                        string newStrValue = subItem2->value();
+                                                        OIC_LOG_V(INFO, CONTAINER_TAG,
+                                                                "key: %s, value %s",
+                                                                newStrKey.c_str(), newStrValue.c_str());
+
+                                                        propertyMap[trim_both(newStrKey)] =
+                                                                trim_both(newStrValue);
+                                                    }
+
+                                                    resourceInfoOut->resourceProperty[trim_both(strKey)].push_back(
+                                                        propertyMap);
+                                                }
+                                            }
+                                        }
+
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+                catch (rapidxml::parse_error &e)
+                {
+                    OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
+                    OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
+                }
+            }
+            else{
+                OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
+            }
+        }
+
+        void Configuration::getResourceConfiguration(std::string bundleId,
+                std::vector< resourceInfo > *configOutput)
+        {
+            rapidxml::xml_node< char > *bundle;
+            rapidxml::xml_node< char > *resource;
+            rapidxml::xml_node< char > *item, *subItem, *subItem2;
+
+            string strBundleId;
+            string strKey, strValue;
+            OIC_LOG(INFO, CONTAINER_TAG, "Loading resource configuration!");
+
+            if (m_loaded)
+            {
+                try
+                {
+                    // <bundle>
+                    bundle = m_xmlDoc.first_node();
+                    if (bundle)
+                    {
+                        for (bundle = bundle->first_node(BUNDLE_TAG); bundle; bundle =
+                                 bundle->next_sibling())
+                        {
+                            // <id>
+                            strBundleId = bundle->first_node(BUNDLE_ID)->value();
+
+                            OIC_LOG_V(INFO, CONTAINER_TAG, "Comparing bundle ids %s - %s !",
+                                    strBundleId.c_str(), bundleId.c_str());
+
+                            if (!strBundleId.compare(bundleId))
+                            {
+                                OIC_LOG_V(INFO, CONTAINER_TAG, "Inspecting");
+                                // <resourceInfo>
+                                bundle = bundle->first_node(OUTPUT_RESOURCES_TAG);
+                                if (bundle)
+                                {
+                                    for (resource = bundle->
+                                            first_node(OUTPUT_RESOURCE_INFO);
+                                         resource; resource = resource->next_sibling())
                                     {
-                                        for (subItem = item->first_node(); subItem; subItem =
-                                                 subItem->next_sibling())
+                                        resourceInfo tempResourceInfo;
+
+                                        for (item = resource->first_node(); item; item =
+                                                 item->next_sibling())
                                         {
-                                            map< string, string > propertyMap;
+                                            strKey = item->name();
+                                            strValue = item->value();
 
-                                            strKey = subItem->name();
+                                            if (!strKey.compare(OUTPUT_RESOURCE_NAME))
+                                                tempResourceInfo.name = trim_both(strValue);
 
-                                            if (strKey.compare(INPUT_RESOURCE))
-                                            {
-                                                m_mapisHasInput[strBundleId] = true;
-                                            }
+                                            else if (!strKey.compare(OUTPUT_RESOURCE_URI))
+                                                tempResourceInfo.uri = trim_both(strValue);
 
-                                            for (subItem2 = subItem->first_node(); subItem2;
-                                                 subItem2 = subItem2->next_sibling())
-                                            {
-                                                string newStrKey = subItem2->name();
-                                                string newStrValue = subItem2->value();
+                                            else if (!strKey.compare(OUTPUT_RESOURCE_ADDR))
+                                                tempResourceInfo.address = trim_both(strValue);
 
-                                                propertyMap[trim_both(newStrKey)] = trim_both(
-                                                                                        newStrValue);
-                                            }
+                                            else if (!strKey.compare(OUTPUT_RESOURCE_TYPE))
+                                                tempResourceInfo.resourceType = trim_both(strValue);
 
-                                            tempResourceInfo.resourceProperty[trim_both(strKey)].push_back(
-                                                propertyMap);
+                                            else
+                                            {
+                                                for (subItem = item->first_node(); subItem; subItem =
+                                                         subItem->next_sibling())
+                                                {
+                                                    map< string, string > propertyMap;
+
+                                                    strKey = subItem->name();
+
+                                                    if (strKey.compare(INPUT_RESOURCE))
+                                                    {
+                                                        m_mapisHasInput[strBundleId] = true;
+                                                        OIC_LOG_V(INFO, CONTAINER_TAG,
+                                                                "Bundle has input (%s)",
+                                                                strBundleId.c_str());
+                                                    }
+
+                                                    for (subItem2 = subItem->first_node(); subItem2;
+                                                         subItem2 = subItem2->next_sibling())
+                                                    {
+                                                        string newStrKey = subItem2->name();
+                                                        string newStrValue = subItem2->value();
+                                                        OIC_LOG_V(INFO, CONTAINER_TAG,
+                                                                "key: %s, value %s",
+                                                                newStrKey.c_str(),
+                                                                newStrValue.c_str());
+
+                                                        propertyMap[trim_both(newStrKey)] =
+                                                                trim_both(newStrValue);
+                                                    }
+
+                                                    tempResourceInfo.resourceProperty[trim_both(strKey)].
+                                                    push_back(propertyMap);
+                                                }
+                                            }
                                         }
+                                        configOutput->push_back(tempResourceInfo);
                                     }
                                 }
-                                configOutput->push_back(tempResourceInfo);
                             }
                         }
                     }
                 }
                 catch (rapidxml::parse_error &e)
                 {
-                    OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
-                    OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
+                    OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
+                    OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
                 }
             }
+            else{
+                OIC_LOG(INFO, CONTAINER_TAG, "config is not loaded yet !!");
+            }
         }
 
         void Configuration::getConfigDocument(std::string pathConfigFile)
@@ -278,14 +448,14 @@ namespace OIC
                 }
                 catch (rapidxml::parse_error &e)
                 {
-                    OC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
-                    OC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
+                    OIC_LOG(ERROR, CONTAINER_TAG, "xml parsing failed !!");
+                    OIC_LOG_V(ERROR, CONTAINER_TAG, "Exception (%s)", e.what());
                 }
             }
             else
             {
-                OC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");
+                OIC_LOG(ERROR, CONTAINER_TAG, "Configuration File load failed !!");
             }
         }
     }
-}
\ No newline at end of file
+}