Refactor logging to use slf4j
[iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / acl / id / Acl.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.accountserver.resources.acl.id;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.ListIterator;
29 import java.util.UUID;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.iotivity.cloud.accountserver.Constants;
34 import org.iotivity.cloud.accountserver.db.AccountDBManager;
35 import org.iotivity.cloud.accountserver.db.AclTable;
36 import org.iotivity.cloud.accountserver.util.TypeCastingManager;
37 import org.iotivity.cloud.base.device.Device;
38 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
39 import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException;
40 import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException;
41 import org.iotivity.cloud.base.protocols.IRequest;
42 import org.iotivity.cloud.base.protocols.MessageBuilder;
43 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
44 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
45 import org.iotivity.cloud.util.Cbor;
46
47 public class Acl {
48     private final static Logger           Log      = LoggerFactory.getLogger(Acl.class);
49     private Cbor<HashMap<String, Object>> mCbor    = new Cbor<>();
50     private String                        mAclid   = null;
51     private String                        mOid     = null;
52     private String                        mDi      = null;
53     private TypeCastingManager<AclTable>  mTypeAcl = new TypeCastingManager<>();
54
55     public Acl(String aclid) {
56         mAclid = aclid;
57     }
58
59     public Acl(String aclid, String oid, String di) {
60         mAclid = aclid;
61         mOid = oid;
62         mDi = di;
63     }
64
65     private static class AclSubscriber {
66         AclSubscriber(Device subscriber, IRequest request) {
67             mSubscriber = subscriber;
68             mRequest = request;
69         }
70
71         public Device   mSubscriber;
72         public IRequest mRequest;
73     }
74
75     private HashMap<String, AclSubscriber> mSubscribers = new HashMap<>();
76
77     public static String valueOf(Object object) {
78         return (object == null) ? "" : object.toString();
79     }
80
81     @SuppressWarnings("unchecked")
82     public static AclTable convertMaptoAclObject(
83             HashMap<String, Object> aclMap) {
84         AclTable aclTable = new AclTable();
85         try {
86             aclTable.setAclid(valueOf(aclMap.get(Constants.KEYFIELD_ACLID)));
87             aclTable.setDi(valueOf(aclMap.get(Constants.KEYFIELD_DI)));
88             aclTable.setOid(valueOf(aclMap.get(Constants.REQ_OWNER_ID)));
89             aclTable.setRowneruuid(
90                     valueOf(aclMap.get(Constants.REQ_ROWNER_ID)));
91
92             List<Ace> aceList = new ArrayList<Ace>();
93
94             List<HashMap<String, Object>> aclist = (List<HashMap<String, Object>>) aclMap
95                     .get(Constants.REQ_ACL_LIST);
96
97             if (aclist == null) {
98                 return aclTable;
99             }
100
101             for (HashMap<String, Object> eachAce : aclist) {
102
103                 Ace ace = new Ace();
104
105                 ace.setAceid(valueOf(eachAce.get(Constants.REQ_ACE_ID)));
106                 ace.setSubjectuuid(valueOf(
107                         eachAce.get(Constants.KEYFIELD_ACE_SUBJECT_ID)));
108                 ace.setStype(Integer.valueOf(eachAce
109                         .get(Constants.KEYFIELD_ACE_SUBJECT_TYPE).toString()));
110                 ace.setPermission(Integer.valueOf(eachAce
111                         .get(Constants.KEYFIELD_ACE_PERMISSION).toString()));
112
113                 Object validity = eachAce.get(Constants.KEYFIELD_ACE_VALIDITY);
114                 if (validity != null) {
115                     ace.setValidity((List<String>) validity);
116                 }
117
118                 List<AceResource> resourceLst = new ArrayList<AceResource>();
119                 List<HashMap<String, Object>> resourceList = (List<HashMap<String, Object>>) eachAce
120                         .get(Constants.KEYFIELD_ACE_RESOURCE);
121                 for (HashMap<String, Object> resrouce : resourceList) {
122
123                     AceResource aceResource = new AceResource();
124                     aceResource.setHref(valueOf(resrouce
125                             .get(Constants.KEYFIELD_ACE_RESOURCE_HREF)));
126                     List<String> rtList = (List<String>) resrouce
127                             .get(Constants.KEYFIELD_ACE_RESOURCE_RT);
128                     aceResource.setRt(rtList);
129                     List<String> ifList = (List<String>) resrouce
130                             .get(Constants.KEYFIELD_ACE_RESOURCE_IF);
131                     aceResource.setOicif(ifList);
132
133                     resourceLst.add(aceResource);
134                 }
135                 ace.setResources(resourceLst);
136
137                 aceList.add(ace);
138
139             }
140             aclTable.setAclist(aceList);
141         } catch (Exception e) {
142             throw new InternalServerErrorException(
143                     "Map to Acl Object casting error " + e.getMessage());
144         }
145         return aclTable;
146
147     }
148
149     @SuppressWarnings("unchecked")
150
151     public List<HashMap<String, Object>> addACE(
152             List<HashMap<String, Object>> aclist) {
153         Log.trace("IN addACE");
154         HashMap<String, Object> hashmap = AccountDBManager.getInstance()
155                 .selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
156         if (hashmap == null) {
157             throw new BadRequestException("aclid is invalid");
158         }
159         List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
160                 .get(Constants.REQ_ACL_LIST);
161
162         ListIterator<HashMap<String, Object>> iterator = aclist.listIterator();
163         while (iterator.hasNext()) {
164             HashMap<String, Object> aceMap = iterator.next();
165             if (aceMap.get(Constants.KEYFIELD_ACE_SUBJECT_ID)
166                     .equals(hashmap.get(Constants.REQ_OWNER_ID))) {
167                 // remove current iterator
168                 iterator.remove();
169                 continue;
170             }
171             aceMap.put(Constants.REQ_ACE_ID, UUID.randomUUID().toString());
172         }
173
174         List<HashMap<String, Object>> newAcList = new ArrayList<HashMap<String, Object>>(
175                 aclist);
176
177         if (aclDbList != null) {
178             newAcList.addAll(aclDbList);
179         }
180         hashmap.put(Constants.REQ_ACL_LIST, newAcList);
181         AccountDBManager.getInstance().updateRecord(Constants.ACL_TABLE,
182                 hashmap);
183         notifyToSubscriber(getResponsePayload(true));
184         Log.trace("OUT addACE");
185         return aclist;
186     }
187
188     public HashMap<String, Object> getACE(String aceid) {
189         HashMap<String, Object> hashmap = AccountDBManager.getInstance()
190                 .selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
191         if (hashmap == null) {
192             throw new BadRequestException("aclid is invalid");
193         }
194
195         List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
196                 .get(Constants.REQ_ACL_LIST);
197         ListIterator<HashMap<String, Object>> iterator = aclDbList
198                 .listIterator();
199         while (iterator.hasNext()) {
200             HashMap<String, Object> aceMap = iterator.next();
201             if (aceMap.get(Constants.REQ_ACE_ID).equals(aceid)) {
202                 // Return the current element from the iterator
203                 return aceMap;
204             }
205         }
206         throw new BadRequestException("aceid is invalid");
207     }
208
209     public boolean isValidAceId(String aceid) {
210         HashMap<String, Object> hashmap = AccountDBManager.getInstance()
211                 .selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
212         if (hashmap == null) {
213             return false;
214         }
215
216         List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
217                 .get(Constants.REQ_ACL_LIST);
218         ListIterator<HashMap<String, Object>> iterator = aclDbList
219                 .listIterator();
220         while (iterator.hasNext()) {
221             HashMap<String, Object> aceMap = iterator.next();
222             if (aceMap.get(Constants.REQ_ACE_ID).equals(aceid)) {
223                 return true;
224             }
225         }
226         return false;
227     }
228
229     public void updateACE(String aceid, HashMap<String, Object> ace) {
230         Log.trace("IN updateACE");
231
232         HashMap<String, Object> hashmap = AccountDBManager.getInstance()
233                 .selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
234
235         List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
236                 .get(Constants.REQ_ACL_LIST);
237
238         ace.put(Constants.REQ_ACE_ID, aceid);
239
240         ListIterator<HashMap<String, Object>> iterator = aclDbList
241                 .listIterator();
242         while (iterator.hasNext()) {
243             HashMap<String, Object> aceMap = iterator.next();
244             if (aceMap.get(Constants.REQ_ACE_ID).equals(aceid)) {
245                 // replace current iterator with new element
246                 iterator.set(ace);
247                 break;
248             }
249         }
250
251         hashmap.put(Constants.REQ_ACL_LIST, aclDbList);
252         AccountDBManager.getInstance().updateRecord(Constants.ACL_TABLE,
253                 hashmap);
254         notifyToSubscriber(getResponsePayload(true));
255         Log.trace("OUT updateACE");
256
257     }
258
259     public void deleteACE(String aceid) {
260
261         HashMap<String, Object> hashmap = AccountDBManager.getInstance()
262                 .selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
263
264         List<HashMap<String, Object>> aclDbList = (List<HashMap<String, Object>>) hashmap
265                 .get(Constants.REQ_ACL_LIST);
266
267         ListIterator<HashMap<String, Object>> iterator = aclDbList
268                 .listIterator();
269         while (iterator.hasNext()) {
270             HashMap<String, Object> aceMap = iterator.next();
271             if (aceMap.get(Constants.REQ_ACE_ID).equals(aceid)) {
272                 // Remove the current element from the iterator
273                 iterator.remove();
274                 break;
275             }
276         }
277
278         hashmap.put(Constants.REQ_ACL_LIST, aclDbList);
279         AccountDBManager.getInstance().updateRecord(Constants.ACL_TABLE,
280                 hashmap);
281         notifyToSubscriber(getResponsePayload(true));
282     }
283
284     public void deleteAclist() {
285         AclTable aclTable = getAclTable();
286         aclTable.setAclist(null);
287         AccountDBManager.getInstance().updateRecord(Constants.ACL_TABLE,
288                 mTypeAcl.convertObjectToMap(aclTable));
289         notifyToSubscriber(getResponsePayload(true));
290     }
291
292     public HashMap<String, Object> getInfo() {
293         return getResponsePayload(true);
294     }
295
296     public HashMap<String, Object> addSubscriber(String di, Device subscriber,
297             IRequest request) {
298
299         verifyAclTableDi(di);
300         AclSubscriber newSubscriber = new AclSubscriber(subscriber, request);
301         mSubscribers.put(di, newSubscriber);
302         return getInfo();
303     }
304
305     public HashMap<String, Object> removeSubscriber(String di) {
306
307         HashMap<String, Object> responsePayload = getResponsePayload(true);
308         if (mSubscribers.containsKey(di)) {
309             mSubscribers.remove(di);
310         }
311         return responsePayload;
312     }
313
314     private void verifyAclTableDi(String di) {
315         AclTable aclTable = getAclTable();
316         if (aclTable.getDi() == null) {
317             throw new BadRequestException("di is invalid in Acl");
318         }
319         String mDi = aclTable.getDi();
320         if (!mDi.equals(di)) {
321             throw new UnAuthorizedException(
322                     di + "is not Device ID of this ACL");
323         }
324     }
325
326     private void notifyToSubscriber(
327             HashMap<String, Object> notifyBytePayloadData) {
328         synchronized (mSubscribers) {
329
330             Iterator<String> iterator = mSubscribers.keySet().iterator();
331             while (iterator.hasNext()) {
332                 String key = iterator.next();
333                 AclSubscriber aclSubscriber = mSubscribers.get(key);
334                 aclSubscriber.mSubscriber.sendResponse(
335                         MessageBuilder.createResponse(aclSubscriber.mRequest,
336                                 ResponseStatus.CONTENT,
337                                 ContentFormat.APPLICATION_CBOR,
338                                 mCbor.encodingPayloadToCbor(
339                                         notifyBytePayloadData)));
340             }
341         }
342     }
343
344     private AclTable getAclTable() {
345         AclTable getAclTable = new AclTable();
346         getAclTable = convertMaptoAclObject(AccountDBManager.getInstance()
347                 .selectRecord(Constants.ACL_TABLE, getCondition()).get(0));
348         return getAclTable;
349     }
350
351     private HashMap<String, Object> getAclTablePayLoad() {
352         HashMap<String, Object> aclPayload = new HashMap<>();
353         aclPayload = AccountDBManager.getInstance()
354                 .selectRecord(Constants.ACL_TABLE, getCondition()).get(0);
355         return aclPayload;
356     }
357
358     private HashMap<String, Object> getResponsePayload(boolean isAliveAcl) {
359         return isAliveAcl ? getAclTablePayLoad() : null;
360     }
361
362     private HashMap<String, Object> getCondition() {
363         HashMap<String, Object> condition = new HashMap<>();
364         condition.put(Constants.REQ_ACL_ID, mAclid);
365         return condition;
366     }
367
368 }