[IOT-1420] Solve that if, rt properties ommited
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / serverBuilder / src / InterfaceHandler.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 "InterfaceHandler.h"
22
23 #include "OCResourceRequest.h"
24
25 #include "RCSRepresentation.h"
26 #include "RCSRequest.h"
27 #include "RCSResourceObject.h"
28 #include "RCSResourceAttributes.h"
29 #include "ResourceAttributesConverter.h"
30
31 #include <unordered_map>
32
33 namespace
34 {
35     using namespace OIC::Service;
36
37     RCSRepresentation toRepresentation(const RCSResourceObject& resource)
38     {
39         RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
40
41         return RCSRepresentation{ resource.getUri(), resource.getInterfaces(), resource.getTypes(),
42             resource.getAttributes() };
43     }
44
45     RCSRepresentation buildGetBaselineResponse(const RCSRequest&, const RCSResourceObject& resource)
46     {
47         return toRepresentation(resource);
48     }
49
50     RCSRepresentation buildSetBaselineResponse(const RCSRequest& rcsRequest,
51             const RCSResourceObject& resource)
52     {
53         return buildGetBaselineResponse(rcsRequest, resource);
54     }
55
56     RCSRepresentation buildGetRequestResponse(const RCSRequest&, const RCSResourceObject& resource)
57     {
58         RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
59
60         return RCSRepresentation{ resource.getAttributes() };
61     }
62
63     RCSRepresentation buildSetRequestResponse(const RCSRequest& rcsRequest,
64             const RCSResourceObject& resource)
65     {
66         auto requestAttr = ResourceAttributesConverter::fromOCRepresentation(
67                 rcsRequest.getOCRequest()->getResourceRepresentation());
68
69         RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
70
71         const RCSResourceAttributes& updatedAttr = resource.getAttributes();
72
73         for (auto it = requestAttr.begin(); it != requestAttr.end();)
74         {
75             if(updatedAttr.contains(it->key()))
76             {
77                 it->value() = updatedAttr.at(it->key());
78                 ++it;
79             }
80             else
81             {
82                 it = requestAttr.erase(it);
83             }
84         }
85
86         return RCSRepresentation{ requestAttr };
87     }
88
89     RCSRepresentation buildGetBatchResponse(RCSRequest, const RCSResourceObject& resource)
90     {
91         RCSRepresentation rcsRep;
92
93         RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER };
94         rcsRep.setAttributes(resource.getAttributes());
95
96         for (const auto& bound : resource.getBoundResources())
97         {
98             rcsRep.addChild(toRepresentation(*bound));
99         }
100
101         return rcsRep;
102     }
103
104     const std::unordered_map< std::string, InterfaceHandler > g_defaultHandlers {
105
106             { BASELINE_INTERFACE,
107                 InterfaceHandler(buildGetBaselineResponse, buildSetBaselineResponse) },
108
109             { ACTUATOR_INTERFACE,
110                 InterfaceHandler(buildGetRequestResponse, buildSetRequestResponse) },
111
112             { SENSOR_INTERFACE,
113                 InterfaceHandler(buildGetRequestResponse, nullptr) },
114
115             { BATCH_INTERFACE,
116                 InterfaceHandler(buildGetBatchResponse, buildSetBaselineResponse) }
117     };
118 }
119
120 namespace OIC
121 {
122     namespace Service
123     {
124
125         InterfaceHandler::InterfaceHandler(GetResponseBuilder getBuilder,
126                 SetResponseBuilder setBuilder) :
127                 m_getBuilder{ std::move(getBuilder) },
128                 m_setBuilder{ std::move(setBuilder) }
129         {
130         }
131
132         bool InterfaceHandler::isGetSupported() const
133         {
134             return m_getBuilder != nullptr;
135         }
136
137         bool InterfaceHandler::isSetSupported() const
138         {
139             return m_setBuilder != nullptr;
140         }
141
142         InterfaceHandler::GetResponseBuilder InterfaceHandler::getGetResponseBuilder() const
143         {
144             return m_getBuilder;
145         }
146
147         InterfaceHandler::SetResponseBuilder InterfaceHandler::getSetResponseBuilder() const
148         {
149             return m_setBuilder;
150         }
151
152         InterfaceHandler getDefaultInterfaceHandler(const std::string& interfaceName,
153                 const std::string& defaultInterfaceName)
154         {
155             auto it = g_defaultHandlers.find(interfaceName);
156             if (it != g_defaultHandlers.end()) return it->second;
157
158             it = g_defaultHandlers.find(defaultInterfaceName);
159             if (it != g_defaultHandlers.end()) return it->second;
160
161             return g_defaultHandlers.find(OIC::Service::BASELINE_INTERFACE)->second;
162         }
163     }
164 }