Fix for issue of server updates its model on both PUT and POST requests even
authorspurthi.segu <spurthi.segu@samsung.com>
Wed, 18 Nov 2015 09:56:45 +0000 (15:26 +0530)
committerMadan Lanka <lanka.madan@samsung.com>
Thu, 19 Nov 2015 01:58:46 +0000 (01:58 +0000)
its respective raml file doest not support either PUT or POST.

Providing the request and response information from raml to simulated resource,
so it can validate the requests it can support whenever it receives one from client.

Change-Id: I42ea78d963dd2c11e7ab945cb6a75bb8e3cd0864
Signed-off-by: spurthi.segu <spurthi.segu@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4259
Reviewed-by: RadhaBhavani <radha.p@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/simulator/examples/client/simulator_client.cpp
service/simulator/src/server/simulator_collection_resource_impl.cpp
service/simulator/src/server/simulator_collection_resource_impl.h
service/simulator/src/server/simulator_resource_factory.cpp
service/simulator/src/server/simulator_single_resource_impl.cpp
service/simulator/src/server/simulator_single_resource_impl.h

index f0f3148329e2a5f927d3c758bb6c9edcbba3ce2e..db7fca28fd79ef8d90460f36809087ae08aa5fdd 100644 (file)
@@ -315,9 +315,9 @@ class ClientController
             try
             {
                 SimulatorResourceModelSP rep = std::make_shared<SimulatorResourceModel>();
-                std::string value = "off";
+                bool value = false;
                 rep->add("power", value);
-                rep->add("intensity", 5);
+                rep->add("intensity", 15);
 
                 resource->put(std::map <std::string, std::string>(), rep, callback);
                 std::cout << "PUT is successful!" << std::endl;
@@ -358,9 +358,9 @@ class ClientController
             try
             {
                 SimulatorResourceModelSP rep = std::make_shared<SimulatorResourceModel>();
-                std::string value = "on";
+                bool value = true;
                 rep->add("power", value);
-                rep->add("intensity", 7);
+                rep->add("intensity", 17);
 
                 resource->post(std::map <std::string, std::string>(), rep, callback);
                 std::cout << "POST is successful!" << std::endl;
index ff673fd17a5291309025b309c70edecef25b5cae..b519c071078b4d641262540887807f6395d80b76 100755 (executable)
@@ -229,6 +229,11 @@ void SimulatorCollectionResourceImpl::setResourceModel(const SimulatorResourceMo
     m_resModel = resModel;
 }
 
+void SimulatorCollectionResourceImpl::setActionType(std::map<RAML::ActionType, RAML::ActionPtr> &actionType)
+{
+    m_actionTypes = actionType;
+}
+
 std::vector<ObserverInfo> SimulatorCollectionResourceImpl::getObserversList()
 {
     return m_observersList;
@@ -426,6 +431,15 @@ std::shared_ptr<OC::OCResourceResponse> SimulatorCollectionResourceImpl::request
     std::shared_ptr<OC::OCResourceRequest> request)
 {
     std::shared_ptr<OC::OCResourceResponse> response;
+
+    RAML::ActionType type = getActionType(request->getRequestType());
+
+    if (!m_actionTypes.empty())
+    {
+        if (m_actionTypes.end() == m_actionTypes.find(type))
+            return response;
+    }
+
     if ("GET" == request->getRequestType())
     {
         // Construct the representation
@@ -455,6 +469,15 @@ std::shared_ptr<OC::OCResourceResponse> SimulatorCollectionResourceImpl::request
 {
     std::lock_guard<std::mutex> lock(m_childResourcesLock);
     std::shared_ptr<OC::OCResourceResponse> response;
+
+    RAML::ActionType type = getActionType(request->getRequestType());
+
+    if (!m_actionTypes.empty())
+    {
+        if (m_actionTypes.end() == m_actionTypes.find(type))
+            return response;
+    }
+
     if ("GET" == request->getRequestType())
     {
         // Construct the representation
@@ -562,3 +585,20 @@ void SimulatorCollectionResourceImpl::removeLink(std::string uri)
         }
     }
 }
+
+RAML::ActionType SimulatorCollectionResourceImpl::getActionType(std::string requestType)
+{
+    if (!requestType.compare("GET"))
+        return RAML::ActionType::GET;
+
+    if (!requestType.compare("PUT"))
+        return RAML::ActionType::PUT;
+
+    if (!requestType.compare("POST"))
+        return RAML::ActionType::POST;
+
+    if (!requestType.compare("DELETE"))
+        return RAML::ActionType::DELETE;
+
+    return RAML::ActionType::NONE;
+}
\ No newline at end of file
index 0b114405e91e2e7482dbe81741300dd40544ebdc..ea31ac60f4c91d3f49d5140fcdacaaf30ed43743 100755 (executable)
@@ -22,6 +22,7 @@
 #define SIMULATOR_COLLECTION_RESOURCE_IMPL_H_
 
 #include "simulator_collection_resource.h"
+#include "RamlParser.h"
 
 class SimulatorResourceFactory;
 class SimulatorCollectionResourceImpl : public SimulatorCollectionResource
@@ -59,6 +60,10 @@ class SimulatorCollectionResourceImpl : public SimulatorCollectionResource
 
         void setResourceModel(const SimulatorResourceModel &resModel);
 
+        void setActionType(std::map<RAML::ActionType, RAML::ActionPtr> &actionType);
+
+        RAML::ActionType getActionType(std::string requestType);
+
     private:
         SimulatorCollectionResourceImpl();
 
@@ -88,6 +93,7 @@ class SimulatorCollectionResourceImpl : public SimulatorCollectionResource
         std::vector<ObserverInfo> m_observersList;
         ObserverCallback m_observeCallback;
         ResourceModelChangedCallback m_modelCallback;
+        std::map<RAML::ActionType , RAML::ActionPtr> m_actionTypes;
 
         OCResourceProperty m_property;
         OCResourceHandle m_resourceHandle;
index 2c862ed7c5224e5762bf23cb458aa4ecb929f1d7..321d4251af0a78ba021cc15988f59c76a36ef3fd 100644 (file)
@@ -331,6 +331,7 @@ std::shared_ptr<SimulatorResource> SimulatorResourceFactory::buildResource(
 \r
     name = ramlResource->getDisplayName();\r
     uri = ramlResource->getResourceUri();\r
+    std::map<RAML::ActionType, RAML::ActionPtr> actionType = ramlResource->getActions();\r
 \r
     RAML::RequestResponseBodyPtr successResponseBody = getRAMLResponseBody(\r
                 ramlResource, RAML::ActionType::GET, "200");\r
@@ -359,6 +360,7 @@ std::shared_ptr<SimulatorResource> SimulatorResourceFactory::buildResource(
             collectionRes->setResourceType(resourceType);\r
             collectionRes->setInterface(interfaceType);\r
             collectionRes->setURI(ResourceURIFactory::getInstance()->constructURI(uri));\r
+            collectionRes->setActionType(actionType);\r
 \r
             collectionRes->setResourceModel(successResponseModel);\r
             simResource = std::dynamic_pointer_cast<SimulatorResource>(collectionRes);\r
@@ -376,6 +378,7 @@ std::shared_ptr<SimulatorResource> SimulatorResourceFactory::buildResource(
             singleRes->setResourceType(resourceType);\r
             singleRes->setInterface(interfaceType);\r
             singleRes->setURI(ResourceURIFactory::getInstance()->constructURI(uri));\r
+            singleRes->setActionType(actionType);\r
 \r
             singleRes->setResourceModel(successResponseModel);\r
             singleRes->setPutErrorResponseModel(putErrorResponseModel);\r
index 16d6a6570e1065003d466bba3a84a1acd0826d8f..94903725359a0682331773898a68a3d53cbb4356 100755 (executable)
@@ -426,6 +426,11 @@ void SimulatorSingleResourceImpl::setPostErrorResponseModel(const SimulatorResou
     m_postErrorResModel = resModel;
 }
 
+void SimulatorSingleResourceImpl::setActionType(std::map<RAML::ActionType, RAML::ActionPtr> &actionType)
+{
+    m_actionTypes = actionType;
+}
+
 void SimulatorSingleResourceImpl::notifyApp(SimulatorResourceModel &resModel)
 {
     if (m_modelCallback)
@@ -467,17 +472,10 @@ OCEntityHandlerResult SimulatorSingleResourceImpl::handleRequests(
                     << " request received. \n**Payload details**\n" << payload)
         }
 
-        // Handover the request to appropriate interface handler
-        std::string interfaceType(OC::DEFAULT_INTERFACE);
-        OC::QueryParamsMap queryParams = request->getQueryParameters();
-        if (queryParams.end() != queryParams.find("if"))
-            interfaceType = queryParams["if"];
+        // TODO: Handover the request to appropriate interface handler
 
         std::shared_ptr<OC::OCResourceResponse> response;
-        if (interfaceType == OC::DEFAULT_INTERFACE)
-        {
-            response = requestOnBaseLineInterface(request);
-        }
+        response = requestOnBaseLineInterface(request);
 
         // Send response if the request handled by resource
         if (response)
@@ -522,6 +520,15 @@ std::shared_ptr<OC::OCResourceResponse> SimulatorSingleResourceImpl::requestOnBa
     std::shared_ptr<OC::OCResourceRequest> request)
 {
     std::shared_ptr<OC::OCResourceResponse> response;
+
+    RAML::ActionType type = getActionType(request->getRequestType());
+
+    if (!m_actionTypes.empty())
+    {
+        if (m_actionTypes.end() == m_actionTypes.find(type))
+            return response;
+    }
+
     if ("GET" == request->getRequestType())
     {
         OC::OCRepresentation ocRep = m_resModel.getOCRepresentation();
@@ -621,4 +628,21 @@ void SimulatorSingleResourceImpl::removeAllObservers()
         if (m_observeCallback)
             m_observeCallback(m_uri, ObservationStatus::UNREGISTER, observerList[index]);
     }
-}
\ No newline at end of file
+}
+
+RAML::ActionType SimulatorSingleResourceImpl::getActionType(std::string requestType)
+{
+    if (!requestType.compare("GET"))
+        return RAML::ActionType::GET;
+
+    if (!requestType.compare("PUT"))
+        return RAML::ActionType::PUT;
+
+    if (!requestType.compare("POST"))
+        return RAML::ActionType::POST;
+
+    if (!requestType.compare("DELETE"))
+        return RAML::ActionType::DELETE;
+
+    return RAML::ActionType::NONE;
+}
index d25c4b530de2fa84d19efc4e3ae80fc995b1358f..645507e37def423fa3d0efd9509d41e08301f246 100755 (executable)
@@ -23,6 +23,7 @@
 
 #include "simulator_single_resource.h"
 #include "resource_update_automation_mngr.h"
+#include "RamlParser.h"
 
 class SimulatorResourceFactory;
 class SimulatorSingleResourceImpl : public SimulatorSingleResource
@@ -74,6 +75,8 @@ class SimulatorSingleResourceImpl : public SimulatorSingleResource
         void setResourceModel(const SimulatorResourceModel &resModel);
         void setPutErrorResponseModel(const SimulatorResourceModel &resModel);
         void setPostErrorResponseModel(const SimulatorResourceModel &resModel);
+        void setActionType(std::map<RAML::ActionType, RAML::ActionPtr> &actionType);
+        RAML::ActionType getActionType(std::string requestType);
         void notifyApp();
         void notifyApp(SimulatorResourceModel &resModel);
 
@@ -103,6 +106,7 @@ class SimulatorSingleResourceImpl : public SimulatorSingleResource
         ObserverCallback m_observeCallback;
         UpdateAutomationMngr m_updateAutomationMgr;
         std::vector<ObserverInfo> m_observersList;
+        std::map<RAML::ActionType , RAML::ActionPtr> m_actionTypes;
 
         OCResourceProperty m_property;
         OCResourceHandle m_resourceHandle;