Cloud ACE update
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / acl / id / AclManager.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.List;
27 import java.util.UUID;
28
29 import org.iotivity.cloud.accountserver.Constants;
30 import org.iotivity.cloud.accountserver.db.AccountDBManager;
31 import org.iotivity.cloud.accountserver.db.AclTable;
32 import org.iotivity.cloud.accountserver.util.TypeCastingManager;
33
34 import org.iotivity.cloud.base.device.Device;
35 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
36 import org.iotivity.cloud.base.protocols.IRequest;
37
38 import org.iotivity.cloud.util.Log;
39
40 public class AclManager {
41     public HashMap<String, Acl>          mAcls    = new HashMap<>();
42     private TypeCastingManager<AclTable> mTypeAcl = new TypeCastingManager<AclTable>();
43
44     public HashMap<String, Object> createAcl(String oid, String di) {
45         HashMap<String, Object> responsePayload = new HashMap<>();
46         String aclid = null;
47         aclid = UUID.randomUUID().toString();
48
49         AclTable newAclTable = new AclTable(aclid, oid, di, null, null);
50
51         AccountDBManager.getInstance().insertRecord(Constants.ACL_TABLE,
52                 mTypeAcl.convertObjectToMap(newAclTable));
53         mAcls.put(aclid, new Acl(aclid));
54         responsePayload.put(Constants.REQ_ACL_ID, aclid);
55         return responsePayload;
56     }
57
58     public Acl getAcl(String aclid) {
59         return mAcls.get(aclid);
60     }
61
62     public HashMap<String, Object> getAclid(String di) {
63         HashMap<String, Object> responsePayload = new HashMap<>();
64         String aclid = null;
65         HashMap<String, Object> condition = new HashMap<>();
66         condition.put(Constants.KEYFIELD_DI, di);
67         ArrayList<HashMap<String, Object>> result = AccountDBManager
68             .getInstance().selectRecord(Constants.ACL_TABLE, condition);
69         if (result != null)
70         {
71             for (HashMap<String, Object> element : result) {
72                 AclTable getAclTable = new AclTable();
73                 getAclTable = Acl.convertMaptoAclObject(element);
74                 aclid = getAclTable.getAclid();
75                 responsePayload.put(Constants.KEYFIELD_ACLID, aclid);
76                 return responsePayload;
77             }
78         }
79         return null;
80     }
81
82     public void deleteAcl(String aclid) {
83         HashMap<String, Object> condition = new HashMap<>();
84         condition.put(Constants.REQ_ACL_ID, aclid);
85         AccountDBManager.getInstance().deleteRecord(Constants.ACL_TABLE,
86             condition);
87         mAcls.remove(aclid);
88     }
89
90     public List<HashMap<String, Object>> addAclACE(String aclid, List<HashMap<String, Object>> aclist) {
91         return getAcl(aclid).addACE(aclist);
92     }
93
94     public HashMap<String, Object> getAclACE(String aclid, String aceid) {
95         return getAcl(aclid).getACE(aceid);
96     }
97
98     public void updateACE(String aclid, String aceid, HashMap<String, Object> ace) {
99         if(getAcl(aclid).isValidAceId(aceid))
100         {
101             getAcl(aclid).updateACE(aceid, ace);
102         }
103         else
104         {
105             throw new BadRequestException("Invalid parameters");
106         }
107     }
108
109     public void deleteAclACE(String aclid, String aceid) {
110         if(getAcl(aclid).isValidAceId(aceid))
111         {
112             getAcl(aclid).deleteACE(aceid);
113         }
114         else
115         {
116             throw new BadRequestException("Invalid parameters");
117         }
118     }
119     public void deleteAclAclist(String aclid) {
120         getAcl(aclid).deleteAclist();
121     }
122
123     public HashMap<String, Object> addAclSubscriber(String aclid, String di,
124         Device srcDevice, IRequest request) {
125         return getAcl(aclid).addSubscriber(di, srcDevice, request);
126     }
127
128     public HashMap<String, Object> removeAclSubscriber(String aclid, String di) {
129         return getAcl(aclid).removeSubscriber(di);
130     }
131
132     public HashMap<String, Object> getAclInfo(String aclid) {
133         return getAcl(aclid).getInfo();
134     }
135
136 }