2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.rdserver.resources.directory;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
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;
36 * This class provides a set of APIs handle requests about resource information
39 public class RDManager {
41 private InsManager mInsManager = new InsManager();
42 private PayloadManager mPayloadManager = new PayloadManager();
44 private ArrayList<HashMap<String, Object>> mResourcePresence = new ArrayList<>();
47 * API for handling resource-publish process
49 * @param requestPayload
51 * @return response payload
53 public HashMap<String, Object> publishResource(
54 HashMap<String, Object> requestPayload) {
56 HashMap<String, Object> deviceInfo = setResourceDeviceInfo(
58 ArrayList<HashMap<String, Object>> links = getLinks(requestPayload);
60 // check ins and set ins
61 setResourceIns(deviceInfo.get(Constants.DEVICE_ID).toString(), links);
63 storeResource(links, deviceInfo);
65 return requestPayload;
70 private HashMap<String, Object> setResourceDeviceInfo(
71 HashMap<String, Object> payload) {
72 return mPayloadManager.setPayloadData(payload,
73 mPayloadManager.pubTagKey);
76 private ArrayList<HashMap<String, Object>> getLinks(
77 HashMap<String, Object> requestPayload) {
78 return (ArrayList<HashMap<String, Object>>) requestPayload
79 .get(Constants.LINKS);
83 private void storeResource(ArrayList<HashMap<String, Object>> links,
84 HashMap<String, Object> deviceInfo) {
86 ArrayList<HashMap<String, Object>> resourcePresence = new ArrayList<>();
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);
98 setmResourcePresence(resourcePresence);
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);
111 DBManager.getInstance().insertAndReplaceRecord(Constants.RD_TABLE,
113 rdInfo.put(Constants.TRIGGER, Constants.RES_CHANGE);
118 private void setResourceIns(String di,
119 ArrayList<HashMap<String, Object>> links) {
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);
126 throw new BadRequestException("resource ins is not correct");
128 link.put(Constants.INS, newIns);
132 private int checkResourceIns(String di, String href, int ins) {
133 int storedIns = mInsManager.getIns(di, href);
135 if (storedIns == -1) {
137 ins = mInsManager.createIns(di);
142 if (ins != storedIns) {
150 * API for handling resource-delete process
155 * unique id of resource
157 public void deleteResource(String di, List<String> insList) {
159 HashMap<String, Object> condition = new HashMap<>();
160 condition.put(Constants.DEVICE_ID, di);
162 ArrayList<HashMap<String, Object>> foundRecord = new ArrayList<>();
164 if (insList == null) {
165 foundRecord = checkResourceExist(condition);
166 DBManager.getInstance().deleteRecord(Constants.RD_TABLE, condition);
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,
178 if (!foundRecord.isEmpty()) {
179 setmResourcePresence(foundRecord);
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);
192 * API for handling resource-discover process
197 * list of resource type
199 * list of resource interface
200 * @return response payload
202 public ArrayList<Object> discoverResource(List<String> diList,
203 List<String> rtList, List<String> ifList) {
205 HashMap<String, Object> condition = new HashMap<>();
207 ArrayList<Object> response = new ArrayList<>();
209 if (diList == null) {
213 if (rtList == null && ifList == null) {
214 readResource(diList, condition, response);
217 if (rtList != null) {
218 for (String rt : rtList) {
219 condition.put(Constants.RESOURCE_TYPE, rt);
220 readResource(diList, condition, response);
224 if (ifList != null) {
225 for (String itf : ifList) {
226 condition.put(Constants.INTERFACE, itf);
227 readResource(diList, condition, response);
231 Log.d("discovery payload : " + response);
236 private void readResource(List<String> diList,
237 HashMap<String, Object> condition, ArrayList<Object> response) {
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);
244 if (!records.isEmpty()) {
245 response.add(makeDiscoverResponseSegment(records));
251 private HashMap<String, Object> makeDiscoverResponseSegment(
252 ArrayList<HashMap<String, Object>> records) {
254 HashMap<String, Object> responseSegment = new HashMap<>();
257 HashMap<String, Object> discoverTag = mPayloadManager
258 .setPayloadData(records.get(0), mPayloadManager.discoverTagKey);
259 responseSegment.putAll(discoverTag);
261 ArrayList<Object> links = new ArrayList<>();
263 for (HashMap<String, Object> record : records) {
264 HashMap<String, Object> link = mPayloadManager
265 .setPayloadData(record, mPayloadManager.discoverLinkKey);
266 mPayloadManager.changePolicyTypeToDiscover(link);
269 responseSegment.put(Constants.LINKS, links);
271 return responseSegment;
275 private void setmResourcePresence(
276 ArrayList<HashMap<String, Object>> resourcePresence) {
277 this.mResourcePresence = resourcePresence;
281 * API for getting resource information to notify
283 * @return resource information
285 public ArrayList<HashMap<String, Object>> getmResourcePresence() {
286 return mResourcePresence;