55b74b6c3e09a2ddf8841acd320086d0b615bbd5
[platform/upstream/iotivity.git] /
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.rdserver.resources.directory;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27
28 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
29 import org.iotivity.cloud.rdserver.Constants;
30 import org.iotivity.cloud.rdserver.db.DBManager;
31 import org.iotivity.cloud.rdserver.resources.directory.rd.InsManager;
32 import org.iotivity.cloud.util.Log;
33
34 /**
35  * 
36  * This class provides a set of APIs handle requests about resource information
37  *
38  */
39 public class RDManager {
40
41     private InsManager                         mInsManager       = new InsManager();
42     private PayloadManager                     mPayloadManager   = new PayloadManager();
43
44     private ArrayList<HashMap<String, Object>> mResourcePresence = new ArrayList<>();
45
46     /**
47      * API for handling resource-publish process
48      * 
49      * @param requestPayload
50      *            request payload
51      * @return response payload
52      */
53     public HashMap<String, Object> publishResource(
54             HashMap<String, Object> requestPayload) {
55
56         HashMap<String, Object> deviceInfo = setResourceDeviceInfo(
57                 requestPayload);
58         ArrayList<HashMap<String, Object>> links = getLinks(requestPayload);
59
60         // check ins and set ins
61         setResourceIns(deviceInfo.get(Constants.DEVICE_ID).toString(), links);
62
63         storeResource(links, deviceInfo);
64
65         return requestPayload;
66
67     }
68
69     // set di, n, lt info
70     private HashMap<String, Object> setResourceDeviceInfo(
71             HashMap<String, Object> payload) {
72         return mPayloadManager.setPayloadData(payload,
73                 mPayloadManager.pubTagKey);
74     }
75
76     private ArrayList<HashMap<String, Object>> getLinks(
77             HashMap<String, Object> requestPayload) {
78         return (ArrayList<HashMap<String, Object>>) requestPayload
79                 .get(Constants.LINKS);
80
81     }
82
83     private void storeResource(ArrayList<HashMap<String, Object>> links,
84             HashMap<String, Object> deviceInfo) {
85
86         ArrayList<HashMap<String, Object>> resourcePresence = new ArrayList<>();
87
88         for (HashMap<String, Object> link : links) {
89             HashMap<String, Object> storeInfo = new HashMap<>();
90             HashMap<String, Object> storeLink = mPayloadManager
91                     .setPayloadData(link, mPayloadManager.pubLinkKey);
92             mPayloadManager.changePolicyTypeToStore(storeLink);
93             storeInfo.putAll(storeLink);
94             storeInfo.putAll(deviceInfo);
95             storeResourceInDB(storeInfo);
96             resourcePresence.add(storeInfo);
97         }
98         setmResourcePresence(resourcePresence);
99
100     }
101
102     private void storeResourceInDB(HashMap<String, Object> rdInfo) {
103         HashMap<String, Object> condition = new HashMap<>();
104         condition.put(Constants.DEVICE_ID, rdInfo.get(Constants.DEVICE_ID));
105         condition.put(Constants.INS, rdInfo.get(Constants.INS));
106         if (checkResourceExist(condition).isEmpty()) {
107             DBManager.getInstance().insertRecord(Constants.RD_TABLE, rdInfo);
108             // set resource presence
109             rdInfo.put(Constants.TRIGGER, Constants.RES_CREATE);
110         } else {
111             DBManager.getInstance().insertAndReplaceRecord(Constants.RD_TABLE,
112                     rdInfo);
113             rdInfo.put(Constants.TRIGGER, Constants.RES_CHANGE);
114         }
115
116     }
117
118     private void setResourceIns(String di,
119             ArrayList<HashMap<String, Object>> links) {
120
121         for (HashMap<String, Object> link : links) {
122             String href = link.get(Constants.HREF).toString();
123             int ins = (int) link.get(Constants.INS);
124             int newIns = checkResourceIns(di, href, ins);
125             if (newIns == -1) {
126                 throw new BadRequestException("resource ins is not correct");
127             }
128             link.put(Constants.INS, newIns);
129         }
130     }
131
132     private int checkResourceIns(String di, String href, int ins) {
133         int storedIns = mInsManager.getIns(di, href);
134         if (ins == 0) {
135             if (storedIns == -1) {
136                 // create ins
137                 ins = mInsManager.createIns(di);
138             } else {
139                 ins = storedIns;
140             }
141         } else {
142             if (ins != storedIns) {
143                 ins = -1;
144             }
145         }
146         return ins;
147     }
148
149     /**
150      * API for handling resource-delete process
151      * 
152      * @param di
153      *            device id
154      * @param ins
155      *            unique id of resource
156      */
157     public void deleteResource(String di, List<String> insList) {
158
159         HashMap<String, Object> condition = new HashMap<>();
160         condition.put(Constants.DEVICE_ID, di);
161
162         ArrayList<HashMap<String, Object>> foundRecord = new ArrayList<>();
163
164         if (insList == null) {
165             foundRecord = checkResourceExist(condition);
166             DBManager.getInstance().deleteRecord(Constants.RD_TABLE, condition);
167         } else {
168             for (String ins : insList) {
169                 condition.put(Constants.INS, Integer.parseInt(ins));
170                 if (!checkResourceExist(condition).isEmpty()) {
171                     foundRecord.add(checkResourceExist(condition).get(0));
172                     DBManager.getInstance().deleteRecord(Constants.RD_TABLE,
173                             condition);
174                 }
175             }
176         }
177
178         if (!foundRecord.isEmpty()) {
179             setmResourcePresence(foundRecord);
180         }
181     }
182
183     private ArrayList<HashMap<String, Object>> checkResourceExist(
184             HashMap<String, Object> condition) {
185         ArrayList<HashMap<String, Object>> records = DBManager.getInstance()
186                 .selectRecord(Constants.RD_TABLE, condition);
187
188         return records;
189     }
190
191     /**
192      * API for handling resource-discover process
193      * 
194      * @param diList
195      *            list of device id
196      * @param rtList
197      *            list of resource type
198      * @param ifList
199      *            list of resource interface
200      * @return response payload
201      */
202     public ArrayList<Object> discoverResource(List<String> diList,
203             List<String> rtList, List<String> ifList) {
204
205         HashMap<String, Object> condition = new HashMap<>();
206
207         ArrayList<Object> response = new ArrayList<>();
208
209         if (diList == null) {
210             return response;
211         }
212
213         if (rtList == null && ifList == null) {
214             readResource(diList, condition, response);
215         }
216
217         if (rtList != null) {
218             for (String rt : rtList) {
219                 condition.put(Constants.RESOURCE_TYPE, rt);
220                 readResource(diList, condition, response);
221             }
222         }
223
224         if (ifList != null) {
225             for (String itf : ifList) {
226                 condition.put(Constants.INTERFACE, itf);
227                 readResource(diList, condition, response);
228             }
229         }
230
231         Log.d("discovery payload : " + response);
232
233         return response;
234     }
235
236     private void readResource(List<String> diList,
237             HashMap<String, Object> condition, ArrayList<Object> response) {
238
239         for (String di : diList) {
240             condition.put(Constants.DEVICE_ID, di);
241             ArrayList<HashMap<String, Object>> records = DBManager.getInstance()
242                     .selectRecord(Constants.RD_TABLE, condition);
243
244             if (!records.isEmpty()) {
245                 response.add(makeDiscoverResponseSegment(records));
246             }
247
248         }
249     }
250
251     private HashMap<String, Object> makeDiscoverResponseSegment(
252             ArrayList<HashMap<String, Object>> records) {
253
254         HashMap<String, Object> responseSegment = new HashMap<>();
255
256         // make Tags
257         HashMap<String, Object> discoverTag = mPayloadManager
258                 .setPayloadData(records.get(0), mPayloadManager.discoverTagKey);
259         responseSegment.putAll(discoverTag);
260
261         ArrayList<Object> links = new ArrayList<>();
262         // make links
263         for (HashMap<String, Object> record : records) {
264             HashMap<String, Object> link = mPayloadManager
265                     .setPayloadData(record, mPayloadManager.discoverLinkKey);
266             mPayloadManager.changePolicyTypeToDiscover(link);
267             links.add(link);
268         }
269         responseSegment.put(Constants.LINKS, links);
270
271         return responseSegment;
272
273     }
274
275     private void setmResourcePresence(
276             ArrayList<HashMap<String, Object>> resourcePresence) {
277         this.mResourcePresence = resourcePresence;
278     }
279
280     /**
281      * API for getting resource information to notify
282      * 
283      * @return resource information
284      */
285     public ArrayList<HashMap<String, Object>> getmResourcePresence() {
286         return mResourcePresence;
287     }
288
289 }