Refactor logging to use slf4j
[iotivity.git] / cloud / resourcedirectory / src / main / java / org / iotivity / cloud / rdserver / resources / directory / RDManager.java
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.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
31 import org.iotivity.cloud.rdserver.Constants;
32 import org.iotivity.cloud.rdserver.db.DBManager;
33 import org.iotivity.cloud.rdserver.resources.directory.rd.InsManager;
34
35 /**
36  * 
37  * This class provides a set of APIs handle requests about resource information
38  *
39  */
40 public class RDManager {
41     private final static Logger                Log               = LoggerFactory.getLogger(RDManager.class);
42     private InsManager                         mInsManager       = new InsManager();
43     private PayloadManager                     mPayloadManager   = new PayloadManager();
44
45     private ArrayList<HashMap<String, Object>> mResourcePresence = new ArrayList<>();
46
47     /**
48      * API for handling resource-publish process
49      * 
50      * @param requestPayload
51      *            request payload
52      * @return response payload
53      */
54     public HashMap<String, Object> publishResource(
55             HashMap<String, Object> requestPayload) {
56
57         HashMap<String, Object> deviceInfo = setResourceDeviceInfo(
58                 requestPayload);
59         ArrayList<HashMap<String, Object>> links = getLinks(requestPayload);
60
61         // check ins and set ins
62         setResourceIns(deviceInfo.get(Constants.DEVICE_ID).toString(), links);
63
64         storeResource(links, deviceInfo);
65
66         return requestPayload;
67
68     }
69
70     // set di, n, lt info
71     private HashMap<String, Object> setResourceDeviceInfo(
72             HashMap<String, Object> payload) {
73         return mPayloadManager.setPayloadData(payload,
74                 mPayloadManager.pubTagKey);
75     }
76
77     private ArrayList<HashMap<String, Object>> getLinks(
78             HashMap<String, Object> requestPayload) {
79         return (ArrayList<HashMap<String, Object>>) requestPayload
80                 .get(Constants.LINKS);
81
82     }
83
84     private void storeResource(ArrayList<HashMap<String, Object>> links,
85             HashMap<String, Object> deviceInfo) {
86
87         ArrayList<HashMap<String, Object>> resourcePresence = new ArrayList<>();
88
89         for (HashMap<String, Object> link : links) {
90             HashMap<String, Object> storeInfo = new HashMap<>();
91             HashMap<String, Object> storeLink = mPayloadManager
92                     .setPayloadData(link, mPayloadManager.pubLinkKey);
93             mPayloadManager.changePolicyTypeToStore(storeLink);
94             storeInfo.putAll(storeLink);
95             storeInfo.putAll(deviceInfo);
96             storeResourceInDB(storeInfo);
97             resourcePresence.add(storeInfo);
98         }
99         setmResourcePresence(resourcePresence);
100
101     }
102
103     private void storeResourceInDB(HashMap<String, Object> rdInfo) {
104         HashMap<String, Object> condition = new HashMap<>();
105         condition.put(Constants.DEVICE_ID, rdInfo.get(Constants.DEVICE_ID));
106         condition.put(Constants.INS, rdInfo.get(Constants.INS));
107         if (checkResourceExist(condition).isEmpty()) {
108             DBManager.getInstance().insertRecord(Constants.RD_TABLE, rdInfo);
109             // set resource presence
110             rdInfo.put(Constants.TRIGGER, Constants.RES_CREATE);
111         } else {
112             DBManager.getInstance().insertAndReplaceRecord(Constants.RD_TABLE,
113                     rdInfo);
114             rdInfo.put(Constants.TRIGGER, Constants.RES_CHANGE);
115         }
116
117     }
118
119     private void setResourceIns(String di,
120             ArrayList<HashMap<String, Object>> links) {
121
122         for (HashMap<String, Object> link : links) {
123             String href = link.get(Constants.HREF).toString();
124             int ins = (int) link.get(Constants.INS);
125             int newIns = checkResourceIns(di, href, ins);
126             if (newIns == -1) {
127                 throw new BadRequestException("resource ins is not correct");
128             }
129             link.put(Constants.INS, newIns);
130         }
131     }
132
133     private int checkResourceIns(String di, String href, int ins) {
134         int storedIns = mInsManager.getIns(di, href);
135         if (ins == 0) {
136             if (storedIns == -1) {
137                 // create ins
138                 ins = mInsManager.createIns(di);
139             } else {
140                 ins = storedIns;
141             }
142         } else {
143             if (ins != storedIns) {
144                 ins = -1;
145             }
146         }
147         return ins;
148     }
149
150     /**
151      * API for handling resource-delete process
152      * 
153      * @param di
154      *            device id
155      * @param ins
156      *            unique id of resource
157      */
158     public void deleteResource(String di, List<String> insList) {
159
160         HashMap<String, Object> condition = new HashMap<>();
161         condition.put(Constants.DEVICE_ID, di);
162
163         ArrayList<HashMap<String, Object>> foundRecord = new ArrayList<>();
164
165         if (insList == null) {
166             foundRecord = checkResourceExist(condition);
167             DBManager.getInstance().deleteRecord(Constants.RD_TABLE, condition);
168         } else {
169             for (String ins : insList) {
170                 condition.put(Constants.INS, Integer.parseInt(ins));
171                 if (!checkResourceExist(condition).isEmpty()) {
172                     foundRecord.add(checkResourceExist(condition).get(0));
173                     DBManager.getInstance().deleteRecord(Constants.RD_TABLE,
174                             condition);
175                 }
176             }
177         }
178
179         if (!foundRecord.isEmpty()) {
180             // set resource presence
181             for (HashMap<String, Object> record : foundRecord) {
182                 record.put(Constants.TRIGGER, Constants.RES_DELETE);
183             }
184             setmResourcePresence(foundRecord);
185         }
186     }
187
188     private ArrayList<HashMap<String, Object>> checkResourceExist(
189             HashMap<String, Object> condition) {
190         ArrayList<HashMap<String, Object>> records = DBManager.getInstance()
191                 .selectRecord(Constants.RD_TABLE, condition);
192
193         return records;
194     }
195
196     /**
197      * API for handling resource-discover process
198      * 
199      * @param diList
200      *            list of device id
201      * @param rtList
202      *            list of resource type
203      * @param ifList
204      *            list of resource interface
205      * @return response payload
206      */
207     public ArrayList<Object> discoverResource(List<String> diList,
208             List<String> rtList, List<String> ifList) {
209
210         HashMap<String, Object> condition = new HashMap<>();
211
212         ArrayList<Object> response = new ArrayList<>();
213
214         if (diList == null) {
215             return response;
216         }
217
218         if (rtList == null && ifList == null) {
219             readResource(diList, condition, response);
220         }
221
222         if (rtList != null) {
223             for (String rt : rtList) {
224                 condition.put(Constants.RESOURCE_TYPE, rt);
225                 readResource(diList, condition, response);
226             }
227         }
228
229         if (ifList != null) {
230             for (String itf : ifList) {
231                 condition.put(Constants.INTERFACE, itf);
232                 readResource(diList, condition, response);
233             }
234         }
235
236         Log.debug("discovery payload : " + response);
237
238         return response;
239     }
240
241     private void readResource(List<String> diList,
242             HashMap<String, Object> condition, ArrayList<Object> response) {
243
244         ArrayList<String> onDiList = getPresenceOnDevices(diList);
245
246         for (String di : onDiList) {
247             condition.put(Constants.DEVICE_ID, di);
248             ArrayList<HashMap<String, Object>> records = DBManager.getInstance()
249                     .selectRecord(Constants.RD_TABLE, condition);
250
251             if (!records.isEmpty()) {
252                 response.add(makeDiscoverResponseSegment(records));
253             }
254
255         }
256     }
257
258     private ArrayList<String> getPresenceOnDevices(List<String> diList) {
259         ArrayList<String> onDiList = new ArrayList<>();
260         HashMap<String, Object> condition = new HashMap<>();
261
262         for (String di : diList) {
263             condition.put(Constants.DEVICE_ID, di);
264             HashMap<String, Object> record = DBManager.getInstance()
265                     .selectOneRecord(Constants.PRESENCE_TABLE, condition);
266
267             if (!record.isEmpty() && record.get(Constants.PRESENCE_STATE)
268                     .equals(Constants.PRESENCE_ON)) {
269                 onDiList.add(di);
270             }
271
272         }
273         return onDiList;
274     }
275
276     private HashMap<String, Object> makeDiscoverResponseSegment(
277             ArrayList<HashMap<String, Object>> records) {
278
279         HashMap<String, Object> responseSegment = new HashMap<>();
280
281         // make Tags
282         HashMap<String, Object> discoverTag = mPayloadManager
283                 .setPayloadData(records.get(0), mPayloadManager.discoverTagKey);
284         responseSegment.putAll(discoverTag);
285
286         ArrayList<Object> links = new ArrayList<>();
287         // make links
288         for (HashMap<String, Object> record : records) {
289             HashMap<String, Object> link = mPayloadManager
290                     .setPayloadData(record, mPayloadManager.discoverLinkKey);
291             mPayloadManager.changePolicyTypeToDiscover(link);
292             links.add(link);
293         }
294         responseSegment.put(Constants.LINKS, links);
295
296         return responseSegment;
297
298     }
299
300     private void setmResourcePresence(
301             ArrayList<HashMap<String, Object>> resourcePresence) {
302         this.mResourcePresence = resourcePresence;
303     }
304
305     /**
306      * API for getting resource information to notify
307      * 
308      * @return resource information
309      */
310     public ArrayList<HashMap<String, Object>> getmResourcePresence() {
311         return mResourcePresence;
312     }
313
314 }