Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / scene-manager / src / SceneMemberResource.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 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 "SceneMemberResource.h"
22
23 #include <atomic>
24 #include "OCPlatform.h"
25
26 namespace OIC
27 {
28     namespace Service
29     {
30         namespace
31         {
32             std::atomic_int g_numOfSceneMember(0);
33         }
34
35         SceneMemberResource::Ptr
36         SceneMemberResource::createSceneMemberResource(
37                 RCSRemoteResourceObject::Ptr remoteObject)
38         {
39             SceneMemberResource::Ptr sceneMemberResource(new SceneMemberResource());
40
41             sceneMemberResource->m_uri = PREFIX_SCENE_MEMBER_URI + "/" +
42                 std::to_string(g_numOfSceneMember++);
43
44             sceneMemberResource->m_remoteMemberObj = remoteObject;
45
46             sceneMemberResource->createResourceObject();
47             sceneMemberResource->initSetRequestHandler();
48             sceneMemberResource->setDefaultAttributes();
49
50             return sceneMemberResource;
51         }
52
53         SceneMemberResource::Ptr
54         SceneMemberResource::createSceneMemberResource(const RCSResourceAttributes & link)
55         {
56             return createSceneMemberResource(RCSResourceAttributes(link));
57         }
58
59         SceneMemberResource::Ptr
60         SceneMemberResource::createSceneMemberResource(RCSResourceAttributes && link)
61         {
62             auto href = link.at(SCENE_KEY_HREF).get<std::string>();
63
64             std::string address;
65             std::string uri;
66
67             SceneUtils::getHostUriString(href, &address, &uri);
68
69             auto ocResourcePtr
70                 = OC::OCPlatform::constructResourceObject(
71                     address, uri, OCConnectivityType::CT_ADAPTER_IP, false,
72                     link.at(SCENE_KEY_RT).get<std::vector<std::string>>(),
73                     link.at(SCENE_KEY_IF).get<std::vector<std::string>>());
74
75             return createSceneMemberResource(RCSRemoteResourceObject::fromOCResource(ocResourcePtr));
76         }
77
78         void SceneMemberResource::createResourceObject()
79         {
80             m_sceneMemberResourceObj
81                 = RCSResourceObject::Builder(
82                         m_uri, SCENE_MEMBER_RT, OC_RSRVD_INTERFACE_DEFAULT).
83                         setDiscoverable(true).setObservable(false).build();
84         }
85
86         void SceneMemberResource::setDefaultAttributes()
87         {
88             m_sceneMemberResourceObj->setAttribute(SCENE_KEY_ID, SceneUtils::OICGenerateUUIDStr());
89             m_sceneMemberResourceObj->setAttribute(SCENE_KEY_NAME, std::string());
90
91             RCSResourceAttributes subAtt;
92             subAtt[SCENE_KEY_HREF]
93                     = RCSResourceAttributes::Value(
94                             m_remoteMemberObj->getAddress() + m_remoteMemberObj->getUri());
95             subAtt[SCENE_KEY_IF] = RCSResourceAttributes::Value(m_remoteMemberObj->getInterfaces());
96             subAtt[SCENE_KEY_RT] = RCSResourceAttributes::Value(m_remoteMemberObj->getTypes());
97             m_sceneMemberResourceObj->setAttribute(SCENE_KEY_PAYLOAD_LINK, subAtt);
98
99             m_sceneMemberResourceObj->setAttribute(
100                     SCENE_KEY_SCENEMAPPINGS, std::vector<RCSResourceAttributes>());
101             m_sceneMemberResourceObj->setAttribute(SCENE_KEY_URI, m_uri);
102         }
103
104         void SceneMemberResource::initSetRequestHandler()
105         {
106             m_requestHandler.m_owner = std::weak_ptr<SceneMemberResource>(shared_from_this());
107             m_sceneMemberResourceObj->setSetRequestHandler(std::bind(
108                     &SceneMemberResource::SceneMemberRequestHandler::onSetRequest,
109                     m_requestHandler, std::placeholders::_1, std::placeholders::_2));
110         }
111
112         void SceneMemberResource::addMappingInfo(MappingInfo && mInfo)
113         {
114             RCSResourceAttributes newAtt;
115             {
116                 RCSResourceObject::LockGuard guard(m_sceneMemberResourceObj);
117                 newAtt = m_sceneMemberResourceObj->getAttributes();
118             }
119
120             auto mappingInfo = newAtt.at(SCENE_KEY_SCENEMAPPINGS).
121                     get<std::vector<RCSResourceAttributes>>();
122
123             auto foundMInfo = std::find_if(mappingInfo.begin(), mappingInfo.end(),
124                     [& mInfo](const RCSResourceAttributes & att) -> bool
125                     {
126                         return (att.at(SCENE_KEY_SCENE).get<std::string>() == mInfo.sceneName) &&
127                                 (att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>() == mInfo.key);
128                     });
129
130             if (foundMInfo != mappingInfo.end())
131             {
132                 mappingInfo.erase(foundMInfo);
133             }
134             RCSResourceAttributes newMapInfo;
135             newMapInfo[SCENE_KEY_SCENE] = RCSResourceAttributes::Value(std::move(mInfo.sceneName));
136             newMapInfo[SCENE_KEY_MEMBERPROPERTY] = RCSResourceAttributes::Value(std::move(mInfo.key));
137             newMapInfo[SCENE_KEY_MEMBERVALUE] = std::move(mInfo.value);
138             mappingInfo.push_back(newMapInfo);
139
140             m_sceneMemberResourceObj->setAttribute(SCENE_KEY_SCENEMAPPINGS, mappingInfo);
141         }
142
143         void SceneMemberResource::addMappingInfo(const MappingInfo & mInfo)
144         {
145             addMappingInfo(MappingInfo(mInfo));
146         }
147
148         std::vector<SceneMemberResource::MappingInfo> SceneMemberResource::getMappingInfos() const
149         {
150             std::vector<MappingInfo> retMInfo;
151
152             auto mInfo = m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_SCENEMAPPINGS).
153                     get<std::vector<RCSResourceAttributes>>();
154             std::for_each(mInfo.begin(), mInfo.end(),
155                     [& retMInfo](const RCSResourceAttributes & att)
156                     {
157                         retMInfo.push_back(MappingInfo::create(att));
158                     });
159
160             return retMInfo;
161         }
162
163         std::string SceneMemberResource::getId() const
164         {
165             return m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_ID).get<std::string>();
166         }
167
168         std::string SceneMemberResource::getFullUri() const
169         {
170             return std::string(COAP_TAG + SceneUtils::getNetAddress() + m_uri);
171         }
172
173         std::string SceneMemberResource::getTargetUri() const
174         {
175             return std::string(m_remoteMemberObj->getAddress() + m_remoteMemberObj->getUri());
176         }
177
178         RCSRemoteResourceObject::Ptr SceneMemberResource::getRemoteResourceObject() const
179         {
180             return m_remoteMemberObj;
181         }
182
183         RCSResourceObject::Ptr SceneMemberResource::getRCSResourceObject() const
184         {
185             return m_sceneMemberResourceObj;
186         }
187
188         void SceneMemberResource::execute(std::string && sceneName)
189         {
190             execute(std::move(sceneName), nullptr);
191         }
192
193         void SceneMemberResource::execute(const std::string & sceneName)
194         {
195             execute(std::string(sceneName));
196         }
197
198         void SceneMemberResource::execute(std::string && sceneName, MemberexecuteCallback executeCB)
199         {
200             RCSResourceAttributes setAtt;
201
202             auto mInfo = getMappingInfos();
203             std::for_each(mInfo.begin(), mInfo.end(),
204                     [& setAtt, & sceneName](const MappingInfo & info)
205                     {
206                         if(info.sceneName == sceneName)
207                         {
208                             setAtt[info.key] = info.value;
209                         }
210                     });
211
212             if (setAtt.empty() && executeCB != nullptr)
213             {
214                 executeCB(RCSResourceAttributes(), SCENE_RESPONSE_SUCCESS);
215             }
216
217             m_remoteMemberObj->setRemoteAttributes(setAtt, executeCB);
218         }
219
220         void SceneMemberResource::execute(
221                 const std::string & sceneName, MemberexecuteCallback executeCB)
222         {
223             execute(std::string(sceneName), std::move(executeCB));
224         }
225
226         void SceneMemberResource::setName(const std::string & name)
227         {
228             setName(std::string(name));
229         }
230
231         void SceneMemberResource::setName(std::string && name)
232         {
233             m_sceneMemberResourceObj->setAttribute(SCENE_KEY_NAME, std::move(name));
234         }
235
236         std::string SceneMemberResource::getName() const
237         {
238             return m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_NAME).toString();
239         }
240
241         std::vector<SceneMemberResource::MappingInfo> SceneMemberResource::findMappingInfos(
242                 const std::string & sceneValue) const
243         {
244             auto mInfo = getMappingInfos();
245             std::vector<MappingInfo> retMInfo;
246
247             std::for_each(mInfo.begin(), mInfo.end(),
248                     [& retMInfo, & sceneValue](const MappingInfo & info)
249                     {
250                         if(info.sceneName == sceneValue)
251                         {
252                             retMInfo.push_back(MappingInfo(info));
253                         }
254                     });
255             return retMInfo;
256         }
257
258         bool SceneMemberResource::hasSceneValue(const std::string & sceneValue) const
259         {
260             auto mInfo = getMappingInfos();
261             if (std::find_if(mInfo.begin(), mInfo.end(),
262                     [& sceneValue](const MappingInfo & info) -> bool
263                     {
264                         return info.sceneName == sceneValue;
265                     }) != mInfo.end())
266             {
267                 return true;
268             }
269             return false;
270         }
271
272         SceneMemberResource::MappingInfo
273         SceneMemberResource::MappingInfo::create(const RCSResourceAttributes & att)
274         {
275             return MappingInfo(att.at(SCENE_KEY_SCENE).get<std::string>(),
276                     att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
277                     att.at(SCENE_KEY_MEMBERVALUE));
278         }
279
280         RCSSetResponse SceneMemberResource::SceneMemberRequestHandler::
281         onSetRequest(const RCSRequest & request, RCSResourceAttributes & attributes)
282         {
283             if (attributes.contains(SCENE_KEY_SCENEMAPPINGS))
284             {
285                 addMappingInfos(request, attributes);
286             }
287
288             if (attributes.contains(SCENE_KEY_NAME))
289             {
290                 setSceneMemberName(request, attributes);
291             }
292
293             return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
294                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
295         }
296
297         RCSSetResponse
298         SceneMemberResource::SceneMemberRequestHandler::addMappingInfos(
299                 const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
300         {
301             int eCode = SCENE_RESPONSE_SUCCESS;
302             auto ptr = m_owner.lock();
303             if (!ptr)
304             {
305                 eCode = SCENE_CLIENT_BADREQUEST;
306             }
307             else
308             {
309                 auto mInfo = attributes.at(SCENE_KEY_SCENEMAPPINGS).
310                         get<std::vector<RCSResourceAttributes>>();
311                 std::for_each(mInfo.begin(), mInfo.end(),
312                         [& ptr](const RCSResourceAttributes & att)
313                         {
314                             ptr->addMappingInfo(SceneMemberResource::MappingInfo::create(att));
315                         });
316             }
317
318             return RCSSetResponse::create(attributes, eCode).
319                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
320         }
321
322         RCSSetResponse
323         SceneMemberResource::SceneMemberRequestHandler::setSceneMemberName(
324                 const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
325         {
326             int eCode = SCENE_RESPONSE_SUCCESS;
327             auto ptr = m_owner.lock();
328             if (!ptr)
329             {
330                 eCode = SCENE_CLIENT_BADREQUEST;
331             }
332             else
333             {
334                 ptr->setName(attributes.at(SCENE_KEY_NAME).get<std::string>());
335             }
336
337             return RCSSetResponse::create(attributes, eCode).
338                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
339         }
340     }
341 }