Merge "Merge branch 'master' into cloud-interface" into cloud-interface
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / serverBuilder / src / InterfaceHandler.cpp
1 #include "InterfaceHandler.h"
2
3 #include "OCResourceRequest.h"
4
5 #include "RCSRepresentation.h"
6 #include "RCSRequest.h"
7 #include "RCSResourceObject.h"
8 #include "RCSResourceAttributes.h"
9 #include "ResourceAttributesConverter.h"
10
11 #include <unordered_map>
12
13 namespace
14 {
15     using namespace OIC::Service;
16
17     RCSRepresentation toRepresentation(const RCSResourceObject& resource)
18     {
19         RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
20
21         return RCSRepresentation{ resource.getUri(), resource.getInterfaces(), resource.getTypes(),
22             resource.getAttributes() };
23     }
24
25     RCSRepresentation buildGetBaselineResponse(const RCSRequest&, const RCSResourceObject& resource)
26     {
27         return toRepresentation(resource);
28     }
29
30     RCSRepresentation buildSetBaselineResponse(const RCSRequest& rcsRequest,
31             const RCSResourceObject& resource)
32     {
33         return buildGetBaselineResponse(rcsRequest, resource);
34     }
35
36     RCSRepresentation buildGetRequestResponse(const RCSRequest&, const RCSResourceObject& resource)
37     {
38         RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
39
40         return RCSRepresentation{ resource.getAttributes() };
41     }
42
43     RCSRepresentation buildSetRequestResponse(const RCSRequest& rcsRequest,
44             const RCSResourceObject& resource)
45     {
46         auto requestAttr = ResourceAttributesConverter::fromOCRepresentation(
47                 rcsRequest.getOCRequest()->getResourceRepresentation());
48
49         RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
50
51         const RCSResourceAttributes& updatedAttr = resource.getAttributes();
52
53         for (auto it = requestAttr.begin(); it != requestAttr.end();)
54         {
55             if(updatedAttr.contains(it->key()))
56             {
57                 it->value() = updatedAttr.at(it->key());
58                 ++it;
59             }
60             else
61             {
62                 it = requestAttr.erase(it);
63             }
64         }
65
66         return RCSRepresentation{ requestAttr };
67     }
68
69     RCSRepresentation buildGetBatchResponse(RCSRequest, const RCSResourceObject& resource)
70     {
71         RCSRepresentation rcsRep;
72
73         for (const auto& bound : resource.getBoundResources())
74         {
75             rcsRep.addChild(toRepresentation(*bound));
76         }
77
78         return rcsRep;
79     }
80
81     const std::unordered_map< std::string, InterfaceHandler > g_defaultHandlers {
82
83             { BASELINE_INTERFACE,
84                 InterfaceHandler(buildGetBaselineResponse, buildSetBaselineResponse) },
85
86             { ACTUATOR_INTERFACE,
87                 InterfaceHandler(buildGetRequestResponse, buildSetRequestResponse) },
88
89             { SENSOR_INTERFACE,
90                 InterfaceHandler(buildGetRequestResponse, nullptr) },
91
92             { BATCH_INTERFACE,
93                 InterfaceHandler(buildGetBatchResponse, buildSetBaselineResponse) }
94     };
95 }
96
97 namespace OIC
98 {
99     namespace Service
100     {
101
102         InterfaceHandler::InterfaceHandler(GetResponseBuilder getBuilder,
103                 SetResponseBuilder setBuilder) :
104                 m_getBuilder{ std::move(getBuilder) },
105                 m_setBuilder{ std::move(setBuilder) }
106         {
107         }
108
109         bool InterfaceHandler::isGetSupported() const
110         {
111             return m_getBuilder != nullptr;
112         }
113
114         bool InterfaceHandler::isSetSupported() const
115         {
116             return m_setBuilder != nullptr;
117         }
118
119         InterfaceHandler::GetResponseBuilder InterfaceHandler::getGetResponseBuilder() const
120         {
121             return m_getBuilder;
122         }
123
124         InterfaceHandler::SetResponseBuilder InterfaceHandler::getSetResponseBuilder() const
125         {
126             return m_setBuilder;
127         }
128
129         InterfaceHandler getDefaultInterfaceHandler(const std::string& interfaceName,
130                 const std::string& defaultInterfaceName)
131         {
132             auto it = g_defaultHandlers.find(interfaceName);
133             if (it != g_defaultHandlers.end()) return it->second;
134
135             it = g_defaultHandlers.find(defaultInterfaceName);
136             if (it != g_defaultHandlers.end()) return it->second;
137
138             return g_defaultHandlers.find(OIC::Service::BASELINE_INTERFACE)->second;
139         }
140     }
141 }