Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / service / resource-container / bundle-api / include / BundleResource.h
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 #ifndef BUNDLERESOURCE_H_
22 #define BUNDLERESOURCE_H_
23
24 #include <list>
25 #include <string>
26 #include <map>
27 #include <vector>
28 #include <memory>
29 #include <mutex>
30
31
32 #include "NotificationReceiver.h"
33 #include "RCSResourceAttributes.h"
34 #include "RCSRequest.h"
35
36 namespace OIC
37 {
38     namespace Service
39     {
40
41         /**
42         * @class    BundleResource
43         * @brief    This class represents Basic bundle resource template
44         *               to be registered in the container and make resource server
45         *
46         */
47         class BundleResource
48         {
49             public:
50                 typedef std::shared_ptr< BundleResource > Ptr;
51
52                 /**
53                 * Constructor for BundleResource
54                 */
55                 BundleResource();
56
57                 /**
58                 * Virtual destructor for BundleResource
59                 */
60                 virtual ~BundleResource();
61
62                 /**
63                 * Return the list of attribute names of the resource
64                 *
65                 * @return List of the attribute names
66                 */
67                 std::list<std::string> getAttributeNames();
68
69                 /**
70                 * Initialize attributes of the resource
71                 *
72                 * @return void
73                 */
74                 virtual void initAttributes() = 0;
75
76                 /**
77                 * Register notification receiver(resource container) to notify for the
78                 *     changes of attributes
79                 *
80                 * @param pNotiReceiver Notification Receiver to get notification from
81                 * bundle resource
82                 *
83                 * @return void
84                 */
85                 void registerObserver(NotificationReceiver* pNotiReceiver);
86
87                 /**
88                 * Return all attributes of the resource
89                 *
90                 * @return Attributes of the resource
91                 */
92                 const RCSResourceAttributes getAttributes();
93
94                 /**
95                 * Set attributes of the resource
96                 *
97                 * @param attrs Attributes to set
98                 *
99                 * @return void
100                 */
101                 void setAttributes(const RCSResourceAttributes &attrs);
102
103                 void setAttributes(const RCSResourceAttributes &attrs, bool notify);
104
105                 /**
106                 * Return the value of an attribute
107                 *
108                 * @param key Key of attribute to get
109                 *
110                 * @return Value of the attribute
111                 */
112                 RCSResourceAttributes::Value getAttribute(const std::string &key);
113
114                 /**
115                 * Sets the value of an attribute
116                 *
117                 * @param key Name of attribute to set
118                 *
119                 * @param value Value of attribute to set
120                 *
121                 * @param notify Flag to indicate if OIC clients should be notified about an update
122                 *
123                 * @return void
124                 */
125                 void setAttribute(const std::string &key, RCSResourceAttributes::Value &&value,
126                                   bool notify);
127
128                 /**
129                 * Sets the value of an attribute
130                 *
131                 * @param key Name of attribute to set
132                 *
133                 * @param value Value of attribute to set
134                 *
135                 * @param notify Flag to indicate if OIC clients should be notified about an update
136                 *
137                 * @return void
138                 */
139                 void setAttribute(const std::string &key, RCSResourceAttributes::Value &value,
140                                   bool notify);
141
142                 /**
143                 * Sets the value of an attribute
144                 *
145                 * @param key Name of attribute to set
146                 *
147                 * @param value Value of attribute to set
148                 *
149                 * @return void
150                 */
151                 void setAttribute(const std::string &key, RCSResourceAttributes::Value &&value);
152
153                 /**
154                 * Sets the value of an attribute
155                 *
156                 * @param key Name of attribute to set
157                 *
158                 * @param value Value of attribute to set
159                 *
160                 * @return void
161                 */
162                 void setAttribute(const std::string &key, RCSResourceAttributes::Value &value);
163
164                 /**
165                 * This function should be implemented by the according bundle resource
166                 * and execute the according business logic (e.g., light switch or sensor resource)
167                 * to retrieve a sensor value. If a new sensor value is retrieved, the
168                 * setAttribute data should be called to update the value.
169                 * The implementor of the function can decide weather to notify OIC clients
170                 * about the changed state or not.
171                 *
172                 * @param queryParams Request parameters
173                 *
174                 * @return All attributes
175                 */
176                 virtual RCSResourceAttributes handleGetAttributesRequest(const
177                         std::map< std::string, std::string > &queryParams) = 0;
178
179                 /**
180                 * This function should be implemented by the according bundle resource
181                 * and execute the according business logic (e.g., light switch or sensor resource)
182                 * and write either on soft sensor values or external bridged devices.
183                 *
184                 * The call of this method could for example trigger a HTTP PUT request on
185                 * an external APIs. This method is responsible to update the resource internal
186                 * data and call the setAttribute method.
187                 *
188                 * The implementor of the function can decide weather to notify OIC clients
189                 * about the changed state or not.
190                 *
191                 * @param attrs Attributes to set
192                 * @param queryParams Request parameters
193                 *
194                 * @return void
195                 */
196                 virtual void handleSetAttributesRequest(const RCSResourceAttributes &attrs,
197                                                         const std::map< std::string, std::string > &queryParams) = 0;
198             private:
199
200                 void sendNotification(NotificationReceiver *notficiationRecevier, std::string uri);
201
202             public:
203                 std::string m_bundleId;
204                 std::string m_name, m_uri, m_resourceType, m_interface, m_address;
205                 std::map< std::string,
206                 std::vector< std::map< std::string, std::string > > > m_mapResourceProperty;
207
208             private:
209                 NotificationReceiver* m_pNotiReceiver;
210                 RCSResourceAttributes m_resourceAttributes;
211                 std::mutex m_resourceAttributes_mutex;
212         };
213     }
214 }
215
216 #endif