blob: d2a361e5b9dd535c8db7859408b28b1bb23e1b7f [file] [log] [blame]
Jian Liecb3c0f2015-12-15 10:07:49 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jian Liecb3c0f2015-12-15 10:07:49 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.rest.resources;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey957bb372018-04-03 15:39:46 -070021import com.google.common.collect.ImmutableList;
22import org.onlab.util.HexString;
23import org.onosproject.codec.JsonCodec;
Jian Liecb3c0f2015-12-15 10:07:49 -080024import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.group.DefaultGroupDescription;
28import org.onosproject.net.group.DefaultGroupKey;
29import org.onosproject.net.group.Group;
Ray Milkey957bb372018-04-03 15:39:46 -070030import org.onosproject.net.group.GroupBucket;
31import org.onosproject.net.group.GroupBuckets;
Jian Liecb3c0f2015-12-15 10:07:49 -080032import org.onosproject.net.group.GroupDescription;
33import org.onosproject.net.group.GroupKey;
34import org.onosproject.net.group.GroupService;
35import org.onosproject.rest.AbstractWebResource;
36
37import javax.ws.rs.Consumes;
38import javax.ws.rs.DELETE;
39import javax.ws.rs.GET;
40import javax.ws.rs.POST;
41import javax.ws.rs.Path;
42import javax.ws.rs.PathParam;
43import javax.ws.rs.Produces;
Jian Li9d616492016-03-09 10:52:49 -080044import javax.ws.rs.core.Context;
Jian Liecb3c0f2015-12-15 10:07:49 -080045import javax.ws.rs.core.MediaType;
46import javax.ws.rs.core.Response;
Jian Li9d616492016-03-09 10:52:49 -080047import javax.ws.rs.core.UriBuilder;
48import javax.ws.rs.core.UriInfo;
Jian Liecb3c0f2015-12-15 10:07:49 -080049import java.io.IOException;
50import java.io.InputStream;
Ray Milkey957bb372018-04-03 15:39:46 -070051import java.util.ArrayList;
52import java.util.List;
53import java.util.stream.IntStream;
Jian Liecb3c0f2015-12-15 10:07:49 -080054
Jian Lia7f86ce2015-12-20 13:42:10 -080055import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey86ee5e82018-04-02 15:33:07 -070056import static org.onlab.util.Tools.readTreeFromStream;
Jian Lia7f86ce2015-12-20 13:42:10 -080057
Jian Liecb3c0f2015-12-15 10:07:49 -080058/**
59 * Query and program group rules.
60 */
61
62@Path("groups")
63public class GroupsWebResource extends AbstractWebResource {
Jian Li9d616492016-03-09 10:52:49 -080064
65 @Context
Jian Licc730a62016-05-10 16:36:16 -070066 private UriInfo uriInfo;
Jian Li9d616492016-03-09 10:52:49 -080067
Jian Licc730a62016-05-10 16:36:16 -070068 private static final String DEVICE_INVALID = "Invalid deviceId in group creation request";
69 private static final String GROUP_NOT_FOUND = "Group was not found";
Jian Liecb3c0f2015-12-15 10:07:49 -080070
Jian Licc730a62016-05-10 16:36:16 -070071 private final GroupService groupService = get(GroupService.class);
72 private final ObjectNode root = mapper().createObjectNode();
73 private final ArrayNode groupsNode = root.putArray("groups");
Jian Liecb3c0f2015-12-15 10:07:49 -080074
Ray Milkey957bb372018-04-03 15:39:46 -070075 private GroupKey createKey(String appCookieString) {
76 if (!appCookieString.startsWith("0x")) {
77 throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x");
78 }
79 return new DefaultGroupKey(HexString.fromHexString(
80 appCookieString.split("0x")[1], ""));
81 }
82
Jian Liecb3c0f2015-12-15 10:07:49 -080083 /**
84 * Returns all groups of all devices.
Jian Lia7f86ce2015-12-20 13:42:10 -080085 *
Jian Licc730a62016-05-10 16:36:16 -070086 * @return 200 OK with array of all the groups in the system
Jian Lia7f86ce2015-12-20 13:42:10 -080087 * @onos.rsModel Groups
Jian Liecb3c0f2015-12-15 10:07:49 -080088 */
89 @GET
90 @Produces(MediaType.APPLICATION_JSON)
91 public Response getGroups() {
92 final Iterable<Device> devices = get(DeviceService.class).getDevices();
93 devices.forEach(device -> {
94 final Iterable<Group> groups = groupService.getGroups(device.id());
95 if (groups != null) {
96 groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this)));
97 }
98 });
99
100 return ok(root).build();
101 }
102
103 /**
104 * Returns all groups associated with the given device.
105 *
106 * @param deviceId device identifier
Jian Licc730a62016-05-10 16:36:16 -0700107 * @return 200 OK with array of all the groups in the system
Jian Lia7f86ce2015-12-20 13:42:10 -0800108 * @onos.rsModel Groups
Jian Liecb3c0f2015-12-15 10:07:49 -0800109 */
110 @GET
111 @Produces(MediaType.APPLICATION_JSON)
112 @Path("{deviceId}")
113 public Response getGroupsByDeviceId(@PathParam("deviceId") String deviceId) {
114 final Iterable<Group> groups = groupService.getGroups(DeviceId.deviceId(deviceId));
115
116 groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this)));
117
118 return ok(root).build();
119 }
120
121 /**
Jian Lia7f86ce2015-12-20 13:42:10 -0800122 * Returns a group with the given deviceId and appCookie.
123 *
124 * @param deviceId device identifier
125 * @param appCookie group key
Jian Licc730a62016-05-10 16:36:16 -0700126 * @return 200 OK with a group entry in the system
Jian Lia7f86ce2015-12-20 13:42:10 -0800127 * @onos.rsModel Group
128 */
129 @GET
130 @Produces(MediaType.APPLICATION_JSON)
131 @Path("{deviceId}/{appCookie}")
132 public Response getGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId,
133 @PathParam("appCookie") String appCookie) {
134 final DeviceId deviceIdInstance = DeviceId.deviceId(deviceId);
Varun Sharma1853b3f2016-07-18 14:37:13 +0530135
Ray Milkey957bb372018-04-03 15:39:46 -0700136 final GroupKey appCookieInstance = createKey(appCookie);
Jian Lia7f86ce2015-12-20 13:42:10 -0800137
138 Group group = nullIsNotFound(groupService.getGroup(deviceIdInstance, appCookieInstance),
139 GROUP_NOT_FOUND);
140
141 groupsNode.add(codec(Group.class).encode(group, this));
142 return ok(root).build();
143 }
144
145 /**
Jian Liecb3c0f2015-12-15 10:07:49 -0800146 * Create new group rule. Creates and installs a new group rule for the
147 * specified device.
148 *
149 * @param deviceId device identifier
150 * @param stream group rule JSON
Jian Liecb3c0f2015-12-15 10:07:49 -0800151 * @return status of the request - CREATED if the JSON is correct,
152 * BAD_REQUEST if the JSON is invalid
Jian Lia7f86ce2015-12-20 13:42:10 -0800153 * @onos.rsModel GroupsPost
Jian Liecb3c0f2015-12-15 10:07:49 -0800154 */
155 @POST
156 @Path("{deviceId}")
157 @Consumes(MediaType.APPLICATION_JSON)
158 @Produces(MediaType.APPLICATION_JSON)
159 public Response createGroup(@PathParam("deviceId") String deviceId,
160 InputStream stream) {
Jian Liecb3c0f2015-12-15 10:07:49 -0800161 try {
Jian Li9d616492016-03-09 10:52:49 -0800162
Ray Milkey86ee5e82018-04-02 15:33:07 -0700163 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Jian Liecb3c0f2015-12-15 10:07:49 -0800164 JsonNode specifiedDeviceId = jsonTree.get("deviceId");
165
166 if (specifiedDeviceId != null &&
167 !specifiedDeviceId.asText().equals(deviceId)) {
168 throw new IllegalArgumentException(DEVICE_INVALID);
169 }
170 jsonTree.put("deviceId", deviceId);
171 Group group = codec(Group.class).decode(jsonTree, this);
172 GroupDescription description = new DefaultGroupDescription(
173 group.deviceId(), group.type(), group.buckets(),
174 group.appCookie(), group.id().id(), group.appId());
175 groupService.addGroup(description);
Jian Li9d616492016-03-09 10:52:49 -0800176 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
177 .path("groups")
178 .path(deviceId)
179 .path(Long.toString(group.id().id()));
180 return Response
181 .created(locationBuilder.build())
182 .build();
183 } catch (IOException ex) {
Jian Liecb3c0f2015-12-15 10:07:49 -0800184 throw new IllegalArgumentException(ex);
185 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800186 }
187
188 /**
189 * Removes the specified group.
190 *
191 * @param deviceId device identifier
192 * @param appCookie application cookie to be used for lookup
Jian Lic2a542b2016-05-10 11:48:19 -0700193 * @return 204 NO CONTENT
Jian Liecb3c0f2015-12-15 10:07:49 -0800194 */
195 @DELETE
Jian Liecb3c0f2015-12-15 10:07:49 -0800196 @Path("{deviceId}/{appCookie}")
Jian Lic2a542b2016-05-10 11:48:19 -0700197 public Response deleteGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId,
198 @PathParam("appCookie") String appCookie) {
Jian Liecb3c0f2015-12-15 10:07:49 -0800199 DeviceId deviceIdInstance = DeviceId.deviceId(deviceId);
Varun Sharma1853b3f2016-07-18 14:37:13 +0530200
Ray Milkey957bb372018-04-03 15:39:46 -0700201 final GroupKey appCookieInstance = createKey(appCookie);
Jian Liecb3c0f2015-12-15 10:07:49 -0800202
203 groupService.removeGroup(deviceIdInstance, appCookieInstance, null);
Jian Lic2a542b2016-05-10 11:48:19 -0700204 return Response.noContent().build();
Jian Liecb3c0f2015-12-15 10:07:49 -0800205 }
Ray Milkey957bb372018-04-03 15:39:46 -0700206
207 /**
208 * Adds buckets to a group using the group service.
209 *
210 * @param deviceIdString device Id
211 * @param appCookieString application cookie
212 * @param stream JSON stream
213 */
214 private void updateGroupBuckets(String deviceIdString, String appCookieString, InputStream stream)
215 throws IOException {
216 DeviceId deviceId = DeviceId.deviceId(deviceIdString);
217 final GroupKey groupKey = createKey(appCookieString);
218
219 Group group = nullIsNotFound(groupService.getGroup(deviceId, groupKey), GROUP_NOT_FOUND);
220
221 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
222
223 GroupBuckets buckets = null;
224 List<GroupBucket> groupBucketList = new ArrayList<>();
225 JsonNode bucketsJson = jsonTree.get("buckets");
226 final JsonCodec<GroupBucket> groupBucketCodec = codec(GroupBucket.class);
227 if (bucketsJson != null) {
228 IntStream.range(0, bucketsJson.size())
229 .forEach(i -> {
230 ObjectNode bucketJson = (ObjectNode) bucketsJson.get(i);
231 groupBucketList.add(groupBucketCodec.decode(bucketJson, this));
232 });
233 buckets = new GroupBuckets(groupBucketList);
234 }
235 groupService.addBucketsToGroup(deviceId, groupKey, buckets, groupKey, group.appId());
236 }
237
238 /**
239 * Adds buckets to an existing group.
240 *
241 * @param deviceIdString device identifier
242 * @param appCookieString application cookie
243 * @param stream buckets JSON
244 * @return status of the request - NO_CONTENT if the JSON is correct,
245 * BAD_REQUEST if the JSON is invalid
246 * @onos.rsModel GroupsBucketsPost
247 */
248 @POST
249 @Path("{deviceId}/{appCookie}/buckets")
250 @Consumes(MediaType.APPLICATION_JSON)
251 @Produces(MediaType.APPLICATION_JSON)
252 public Response addBucket(@PathParam("deviceId") String deviceIdString,
253 @PathParam("appCookie") String appCookieString,
254 InputStream stream) {
255 try {
256 updateGroupBuckets(deviceIdString, appCookieString, stream);
257
258 return Response
259 .noContent()
260 .build();
261 } catch (IOException ex) {
262 throw new IllegalArgumentException(ex);
263 }
264 }
265
266 /**
267 * Removes buckets from a group using the group service.
268 *
269 * @param deviceIdString device Id
270 * @param appCookieString application cookie
271 * @param bucketIds comma separated list of bucket Ids to remove
272 */
273 private void removeGroupBuckets(String deviceIdString, String appCookieString, String bucketIds) {
274 DeviceId deviceId = DeviceId.deviceId(deviceIdString);
275 final GroupKey groupKey = createKey(appCookieString);
276
277 Group group = nullIsNotFound(groupService.getGroup(deviceId, groupKey), GROUP_NOT_FOUND);
278
279 List<GroupBucket> groupBucketList = new ArrayList<>();
280
281 List<String> bucketsToRemove = ImmutableList.copyOf(bucketIds.split(","));
282
283 bucketsToRemove.forEach(
284 bucketIdToRemove -> {
285 group.buckets().buckets().stream()
286 .filter(bucket -> Integer.toString(bucket.hashCode()).equals(bucketIdToRemove))
287 .forEach(groupBucketList::add);
288 }
289 );
290 groupService.removeBucketsFromGroup(deviceId, groupKey,
291 new GroupBuckets(groupBucketList), groupKey,
292 group.appId());
293 }
294
295 /**
296 * Removes buckets from an existing group.
297 *
298 * @param deviceIdString device identifier
299 * @param appCookieString application cookie
300 * @param bucketIds comma separated list of identifiers of buckets to remove from this group
301 * @return status of the request - NO_CONTENT if the JSON is correct,
302 * BAD_REQUEST if the JSON is invalid
303 */
304 @DELETE
305 @Path("{deviceId}/{appCookie}/buckets/{bucketIds}")
306 @Consumes(MediaType.APPLICATION_JSON)
307 @Produces(MediaType.APPLICATION_JSON)
308 public Response deleteBuckets(@PathParam("deviceId") String deviceIdString,
309 @PathParam("appCookie") String appCookieString,
310 @PathParam("bucketIds") String bucketIds) {
311 removeGroupBuckets(deviceIdString, appCookieString, bucketIds);
312
313 return Response
314 .noContent()
315 .build();
316 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800317}