Merge "Merge branch 'master' into group-manager" into group-manager
[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 numOfSceneMember(0);
33         }
34
35         SceneMemberResource::Ptr
36         SceneMemberResource::createSceneMemberResource(
37                 RCSRemoteResourceObject::Ptr remoteObject)
38         {
39             SceneMemberResource::Ptr newSceneMemberObject(new SceneMemberResource());
40
41             newSceneMemberObject->m_uri = PREFIX_SCENE_MEMBER_URI + "/" +
42                 std::to_string(numOfSceneMember++);
43
44             newSceneMemberObject->m_remoteMemberObj = remoteObject;
45
46             newSceneMemberObject->createResourceObject();
47             newSceneMemberObject->initSetRequestHandler();
48             newSceneMemberObject->setDefaultAttributes();
49
50             return newSceneMemberObject;
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(mInfo.sceneName);
136             newMapInfo[SCENE_KEY_MEMBERPROPERTY] = RCSResourceAttributes::Value(mInfo.key);
137             newMapInfo[SCENE_KEY_MEMBERVALUE] = 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::getMappingInfo()
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                         MappingInfo info(att.at(SCENE_KEY_SCENE).get<std::string>(),
158                                 att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
159                                 att.at(SCENE_KEY_MEMBERVALUE));
160                         retMInfo.push_back(info);
161                     });
162
163             return retMInfo;
164         }
165
166         std::string SceneMemberResource::getId() const
167         {
168             return m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_ID).get<std::string>();
169         }
170
171         std::string SceneMemberResource::getFullUri() const
172         {
173             return std::string(COAP_TAG + SceneUtils::getNetAddress() + m_uri);
174         }
175
176         RCSRemoteResourceObject::Ptr SceneMemberResource::getRemoteResourceObject() const
177         {
178             return m_remoteMemberObj;
179         }
180
181         RCSResourceObject::Ptr SceneMemberResource::getRCSResourceObject() const
182         {
183             return m_sceneMemberResourceObj;
184         }
185
186         void SceneMemberResource::execute(std::string && sceneName)
187         {
188             execute(std::move(sceneName), nullptr);
189         }
190
191         void SceneMemberResource::execute(const std::string & sceneName)
192         {
193             execute(std::string(sceneName));
194         }
195
196         void SceneMemberResource::execute(std::string && sceneName, MemberexecuteCallback executeCB)
197         {
198             RCSResourceAttributes setAtt;
199
200             auto mInfo = getMappingInfo();
201             std::for_each (mInfo.begin(), mInfo.end(),
202                     [& setAtt, & sceneName](const MappingInfo & info)
203                     {
204                         if(info.sceneName == sceneName)
205                         {
206                             setAtt[info.key] = info.value;
207                         }
208                     });
209
210             if (setAtt.empty() && executeCB != nullptr)
211             {
212                 executeCB(RCSResourceAttributes(), SCENE_RESPONSE_SUCCESS);
213             }
214
215             m_remoteMemberObj->setRemoteAttributes(setAtt, executeCB);
216         }
217
218         void SceneMemberResource::execute(
219                 const std::string & sceneName, MemberexecuteCallback executeCB)
220         {
221             execute(std::string(sceneName), std::move(executeCB));
222         }
223
224         void SceneMemberResource::setName(const std::string & name)
225         {
226             setName(std::string(name));
227         }
228
229         void SceneMemberResource::setName(std::string && name)
230         {
231             m_sceneMemberResourceObj->setAttribute(SCENE_KEY_NAME, std::move(name));
232         }
233
234         std::string SceneMemberResource::getName() const
235         {
236             return m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_NAME).toString();
237         }
238
239         RCSSetResponse SceneMemberResource::SceneMemberRequestHandler::
240         onSetRequest(const RCSRequest & request, RCSResourceAttributes & attributes)
241         {
242             if (attributes.contains(SCENE_KEY_SCENEMAPPINGS))
243             {
244                 addMappingInfos(request, attributes);
245             }
246
247             if (attributes.contains(SCENE_KEY_NAME))
248             {
249                 setSceneMemberName(request, attributes);
250             }
251
252             return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
253                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
254         }
255
256         RCSSetResponse
257         SceneMemberResource::SceneMemberRequestHandler::addMappingInfos(
258                 const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
259         {
260             auto ptr = m_owner.lock();
261             if (!ptr)
262             {
263                 return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
264                         setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
265             }
266
267             auto mInfo = attributes.at(SCENE_KEY_SCENEMAPPINGS).
268                     get<std::vector<RCSResourceAttributes>>();
269             std::for_each (mInfo.begin(), mInfo.end(),
270                     [& ptr](const RCSResourceAttributes & att)
271                     {
272                         ptr->addMappingInfo(SceneMemberResource::MappingInfo(
273                                 att.at(SCENE_KEY_SCENE).get<std::string>(),
274                                 att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
275                                 att.at(SCENE_KEY_MEMBERVALUE)));
276                     });
277
278             return RCSSetResponse::create(attributes).
279                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
280         }
281
282         RCSSetResponse
283         SceneMemberResource::SceneMemberRequestHandler::setSceneMemberName(
284                 const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
285         {
286             auto ptr = m_owner.lock();
287             if (!ptr)
288             {
289                 return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
290                         setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
291             }
292
293             ptr->setName(attributes.at(SCENE_KEY_NAME).get<std::string>());
294
295             return RCSSetResponse::create(attributes).
296                     setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
297         }
298     }
299 }