relocate singleton object(getInstance) to GroupManager
[platform/upstream/iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / acl / group / GroupResource.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.group;
23
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28
29 import org.iotivity.cloud.accountserver.Constants;
30 import org.iotivity.cloud.base.device.Device;
31 import org.iotivity.cloud.base.exception.ServerException;
32 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
33 import org.iotivity.cloud.base.exception.ServerException.PreconditionFailedException;
34 import org.iotivity.cloud.base.protocols.IRequest;
35 import org.iotivity.cloud.base.protocols.IResponse;
36 import org.iotivity.cloud.base.protocols.MessageBuilder;
37 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
38 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
39 import org.iotivity.cloud.base.resource.Resource;
40 import org.iotivity.cloud.util.Cbor;
41
42 public class GroupResource extends Resource {
43     private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
44
45     public GroupResource() {
46         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACL_URI,
47                 Constants.GROUP_URI));
48     }
49
50     @Override
51     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
52             throws ServerException {
53
54         IResponse response = null;
55
56         if (request.getUriPathSegments().size() > getUriPathSegments().size()
57                 + 1) {
58             throw new BadRequestException("uriPath is invalid");
59         }
60
61         switch (request.getMethod()) {
62             case POST:
63                 response = handlePostRequest(request);
64                 break;
65             case GET:
66                 response = handleGetRequest(srcDevice, request);
67                 break;
68             case DELETE:
69                 response = handleDeleteRequest(request);
70                 break;
71             default:
72                 throw new BadRequestException(
73                         request.getMethod() + " request type is not support");
74         }
75         srcDevice.sendResponse(response);
76     }
77
78     private IResponse handlePostRequest(IRequest request)
79             throws ServerException {
80         HashMap<String, Object> payloadData = mCbor
81                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
82
83         if (payloadData == null) {
84             throw new BadRequestException("payload is null");
85         }
86
87         if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
88             String uuid = payloadData.get(Constants.REQ_GROUP_MASTER_ID)
89                     .toString();
90             String gtype = (String) payloadData.get(Constants.REQ_GROUP_TYPE)
91                     .toString();
92             if (uuid == null || gtype == null) {
93                 throw new PreconditionFailedException(
94                         "value of group property is invalid");
95             }
96             return MessageBuilder.createResponse(request,
97                     ResponseStatus.CHANGED, ContentFormat.APPLICATION_CBOR,
98                     mCbor.encodingPayloadToCbor(GroupManager.getInstance()
99                             .createGroup(uuid, gtype)));
100         } else {
101             String gid = request.getUriPathSegments()
102                     .get(getUriPathSegments().size());
103
104             if (payloadData.containsKey(Constants.REQ_MEMBER_LIST)) {
105                 List<String> midList = (List<String>) payloadData
106                         .get(Constants.REQ_MEMBER_LIST);
107                 if (midList == null) {
108                     throw new PreconditionFailedException(
109                             "midList property is invalid");
110                 }
111                 GroupManager.getInstance().addGroupMember(gid,
112                         new HashSet<String>(midList));
113             }
114
115             if (payloadData.containsKey(Constants.REQ_DEVICE_ID_LIST)) {
116                 List<String> diList = (List<String>) payloadData
117                         .get(Constants.REQ_DEVICE_ID_LIST);
118                 if (diList == null) {
119                     throw new PreconditionFailedException(
120                             "diList property is invalid");
121                 }
122                 GroupManager.getInstance().addGroupDevice(gid,
123                         new HashSet<String>(diList));
124             }
125         }
126         return MessageBuilder.createResponse(request, ResponseStatus.CHANGED);
127
128     }
129
130     private IResponse handleGetRequest(Device srcDevice, IRequest request)
131             throws ServerException {
132         HashMap<String, Object> responsePayload = null;
133         String mid = null;
134
135         if (!request.getUriQueryMap().containsKey(Constants.REQ_MEMBER)) {
136             throw new PreconditionFailedException("mid property is invalid");
137         }
138
139         mid = request.getUriQueryMap().get(Constants.REQ_MEMBER).get(0);
140
141         if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
142             responsePayload = GroupManager.getInstance().getGroupList(mid);
143         } else {
144             String gid = request.getUriPathSegments()
145                     .get(getUriPathSegments().size());
146             switch (request.getObserve()) {
147                 case NOTHING:
148                     responsePayload = GroupManager.getInstance()
149                             .getGroupInfo(gid, mid);
150                     break;
151                 case SUBSCRIBE:
152                     responsePayload = GroupManager.getInstance()
153                             .addGroupSubscriber(gid, mid, srcDevice, request);
154                     break;
155                 case UNSUBSCRIBE:
156                     responsePayload = GroupManager.getInstance()
157                             .removeGroupSubscriber(gid, mid);
158                     break;
159                 default:
160                     throw new BadRequestException(request.getObserve()
161                             + " observe type is not support");
162             }
163         }
164         return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
165                 ContentFormat.APPLICATION_CBOR,
166                 mCbor.encodingPayloadToCbor(responsePayload));
167     }
168
169     private IResponse handleDeleteRequest(IRequest request)
170             throws ServerException {
171         if (getUriPathSegments().containsAll(request.getUriPathSegments())) {
172
173             checkQueryException(Arrays.asList(Constants.REQ_GROUP_MASTER_ID,
174                     Constants.REQ_GROUP_ID), request.getUriQueryMap());
175
176             String gmid = request.getUriQueryMap()
177                     .get(Constants.REQ_GROUP_MASTER_ID).get(0);
178             String gid = request.getUriQueryMap().get(Constants.REQ_GROUP_ID)
179                     .get(0);
180
181             GroupManager.getInstance().deleteGroup(gmid, gid);
182         } else {
183             String gid = request.getUriPathSegments()
184                     .get(getUriPathSegments().size());
185             if (request.getUriQueryMap()
186                     .containsKey(Constants.REQ_MEMBER_LIST)) {
187                 List<String> midList = request.getUriQueryMap()
188                         .get(Constants.REQ_MEMBER_LIST);
189                 if (midList == null) {
190                     throw new PreconditionFailedException(
191                             "midList property is invalid");
192                 }
193                 GroupManager.getInstance().removeGroupMember(gid,
194                         new HashSet<String>(midList));
195             }
196             if (request.getUriQueryMap()
197                     .containsKey(Constants.REQ_DEVICE_ID_LIST)) {
198                 List<String> diList = request.getUriQueryMap()
199                         .get(Constants.REQ_DEVICE_ID_LIST);
200                 if (diList == null) {
201                     throw new PreconditionFailedException(
202                             "diList property is invalid");
203                 }
204                 GroupManager.getInstance().removeGroupDevice(gid,
205                         new HashSet<String>(diList));
206             }
207         }
208         return MessageBuilder.createResponse(request, ResponseStatus.DELETED);
209     }
210 }