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.accountserver.resources.acl.group;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
29 import org.iotivity.cloud.accountserver.Constants;
30 import org.iotivity.cloud.accountserver.db.AccountDBManager;
31 import org.iotivity.cloud.accountserver.db.GroupTable;
32 import org.iotivity.cloud.accountserver.util.TypeCastingManager;
33 import org.iotivity.cloud.base.device.Device;
34 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
35 import org.iotivity.cloud.base.exception.ServerException.InternalServerErrorException;
36 import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException;
37 import org.iotivity.cloud.base.protocols.IRequest;
38 import org.iotivity.cloud.base.protocols.MessageBuilder;
39 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
40 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
41 import org.iotivity.cloud.util.Cbor;
45 * This class provides a set of APIs to handle group requests
51 private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
52 private String mGid = null;
53 private TypeCastingManager<GroupTable> mTypeGroup = new TypeCastingManager<>();
55 public Group(String gid) {
59 private class GroupSubscriber {
60 GroupSubscriber(Device subscriber, IRequest request) {
61 mSubscriber = subscriber;
65 public Device mSubscriber;
66 public IRequest mRequest;
69 private HashMap<String, GroupSubscriber> mSubscribers = new HashMap<>();
73 * API to add the member user id to the group table in the db
76 * User id list which are provided by Sign-up process
78 public void addMember(HashSet<String> uuid) {
80 GroupTable groupTable = getGroupTable();
82 groupTable.setMidlist(groupTable.getMidlist() == null ? uuid
83 : addGroupListSet(groupTable.getMidlist(), uuid));
85 AccountDBManager.getInstance().updateRecord(Constants.GROUP_TABLE,
86 mTypeGroup.convertObjectToMap(groupTable));
88 notifyToSubscriber(getResponsePayload(true));
92 * API to add the device id to the group table in the db
95 * device id list to be stored
97 public void addDevice(HashSet<String> di) {
99 GroupTable groupTable = getGroupTable();
101 groupTable.setDilist(groupTable.getDilist() == null ? di
102 : addGroupListSet(groupTable.getDilist(), di));
104 AccountDBManager.getInstance().updateRecord(Constants.GROUP_TABLE,
105 mTypeGroup.convertObjectToMap(groupTable));
107 notifyToSubscriber(getResponsePayload(true));
111 * API to remove member user id list from the group table in the db
114 * User id list to be removed from the group table
116 public void removeMember(HashSet<String> uuid) {
118 GroupTable groupTable = getGroupTable();
119 if (groupTable.getGmid() == null) {
120 throw new InternalServerErrorException("group master is empty");
122 if (uuid.contains(groupTable.getGmid())) {
123 GroupResource.getInstance().deleteGroup(groupTable.getGmid(), mGid);
124 notifyToSubscriber(getResponsePayload(false));
126 if (groupTable.getMidlist() == null) {
127 throw new BadRequestException("midList is invalid in Group");
129 groupTable.setMidlist(
130 removeGroupListSet(groupTable.getMidlist(), uuid));
131 AccountDBManager.getInstance().updateRecord(Constants.GROUP_TABLE,
132 mTypeGroup.convertObjectToMap(groupTable));
133 notifyToSubscriber(getResponsePayload(true));
135 Iterator<String> mid = uuid.iterator();
136 while (mid.hasNext()) {
137 removeSubscriber(mid.next());
143 * API to remove device id list from the group table in the db
146 * device id list to be removed from the group table
148 public void removeDevice(HashSet<String> di) {
150 GroupTable groupTable = getGroupTable();
151 if (groupTable.getDilist() == null) {
152 throw new BadRequestException("deviceList is invalid in Group");
154 groupTable.setDilist(removeGroupListSet(groupTable.getDilist(), di));
156 AccountDBManager.getInstance().updateRecord(Constants.GROUP_TABLE,
157 mTypeGroup.convertObjectToMap(groupTable));
159 notifyToSubscriber(getResponsePayload(true));
163 * API to return the group information payload
166 * member id to verify if the id exists in the group table
167 * @return group information payload
169 public HashMap<String, Object> getInfo(String mid) {
171 verifyGroupTableMid(mid);
173 return getResponsePayload(true);
176 public boolean checkDeviceExistance(String di) {
177 return verifyGroupTableDi(di);
181 * API to add group subscriber
184 * member id to verify if the id exists in the group table
189 * @return group information payload
191 public HashMap<String, Object> addSubscriber(String mid, Device subscriber,
194 // Check if the user has privilege to observe
195 verifyGroupTableMid(mid);
197 GroupSubscriber newSubscriber = new GroupSubscriber(subscriber,
200 mSubscribers.put(mid, newSubscriber);
203 request.getUriQueryMap().get(Constants.REQ_MEMBER).get(0));
207 * API to unsubscribe group information
210 * user Id to unscribe group information
211 * @return group information payload
213 public HashMap<String, Object> removeSubscriber(String mid) {
215 HashMap<String, Object> responsePayload = getResponsePayload(true);
217 if (mSubscribers.containsKey(mid)) {
218 mSubscribers.remove(mid);
221 return responsePayload;
224 private void verifyGroupTableMid(String mid) {
226 GroupTable groupTable = getGroupTable();
228 if (groupTable.getMidlist() == null) {
229 throw new BadRequestException("midList is invalid in Group");
231 HashSet<String> midListSet = new HashSet<>(
232 (Collection<? extends String>) groupTable.getMidlist());
234 if (!midListSet.contains(mid)) {
236 throw new UnAuthorizedException(
237 mid + " is not Group member in gid=" + mGid);
241 private boolean verifyGroupTableDi(String di) {
243 GroupTable groupTable = getGroupTable();
245 if (groupTable.getDilist() == null) {
249 HashSet<String> diListSet = new HashSet<>(
250 (Collection<? extends String>) groupTable.getDilist());
252 if (!diListSet.contains(di)) {
258 private void notifyToSubscriber(
259 HashMap<String, Object> notifiyBtyePayloadData) {
260 synchronized (mSubscribers) {
262 Iterator<String> iterator = mSubscribers.keySet().iterator();
264 while (iterator.hasNext()) {
266 String key = iterator.next();
268 GroupSubscriber groupSubscriber = mSubscribers.get(key);
270 groupSubscriber.mSubscriber.sendResponse(
271 MessageBuilder.createResponse(groupSubscriber.mRequest,
272 ResponseStatus.CONTENT,
273 ContentFormat.APPLICATION_CBOR,
274 mCbor.encodingPayloadToCbor(
275 notifiyBtyePayloadData)));
280 private GroupTable getGroupTable() {
282 GroupTable getGroupTable = new GroupTable();
284 getGroupTable = mTypeGroup
286 AccountDBManager.getInstance().selectRecord(
287 Constants.GROUP_TABLE, getContdition()).get(0),
290 return getGroupTable;
293 private HashMap<String, Object> getResponsePayload(boolean isAliveGroup) {
295 GroupTable groupTable = isAliveGroup ? getGroupTable() : null;
297 HashMap<String, Object> responsePayload = new HashMap<>();
299 responsePayload.put(Constants.REQ_GROUP_ID,
300 isAliveGroup ? groupTable.getGid() : null);
301 responsePayload.put(Constants.REQ_GROUP_MASTER_ID,
302 isAliveGroup ? groupTable.getGmid() : null);
303 responsePayload.put(Constants.REQ_MEMBER_LIST,
304 isAliveGroup ? groupTable.getMidlist() : null);
305 responsePayload.put(Constants.REQ_DEVICE_ID_LIST,
306 isAliveGroup ? groupTable.getDilist() : null);
307 responsePayload.put(Constants.REQ_GROUP_TYPE,
308 isAliveGroup ? groupTable.getGtype() : null);
310 return responsePayload;
313 private HashSet<String> addGroupListSet(Object object,
314 HashSet<String> addList) {
316 HashSet<String> groupSet = new HashSet<>(
317 (Collection<? extends String>) object);
319 groupSet.addAll(addList);
324 private HashSet<String> removeGroupListSet(Object object,
325 HashSet<String> removeList) {
327 HashSet<String> groupSet = new HashSet<>(
328 (Collection<? extends String>) object);
330 groupSet.removeAll(removeList);
335 private HashMap<String, Object> getContdition() {
337 HashMap<String, Object> condition = new HashMap<>();
338 condition.put(Constants.REQ_GROUP_ID, mGid);