Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.rest.resources; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
| 22 | import org.onlab.util.HexString; |
| 23 | import org.onosproject.codec.JsonCodec; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 24 | import org.onosproject.net.Device; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.group.DefaultGroupDescription; |
| 28 | import org.onosproject.net.group.DefaultGroupKey; |
| 29 | import org.onosproject.net.group.Group; |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 30 | import org.onosproject.net.group.GroupBucket; |
| 31 | import org.onosproject.net.group.GroupBuckets; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 32 | import org.onosproject.net.group.GroupDescription; |
| 33 | import org.onosproject.net.group.GroupKey; |
| 34 | import org.onosproject.net.group.GroupService; |
| 35 | import org.onosproject.rest.AbstractWebResource; |
| 36 | |
| 37 | import javax.ws.rs.Consumes; |
| 38 | import javax.ws.rs.DELETE; |
| 39 | import javax.ws.rs.GET; |
| 40 | import javax.ws.rs.POST; |
| 41 | import javax.ws.rs.Path; |
| 42 | import javax.ws.rs.PathParam; |
| 43 | import javax.ws.rs.Produces; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 44 | import javax.ws.rs.core.Context; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 45 | import javax.ws.rs.core.MediaType; |
| 46 | import javax.ws.rs.core.Response; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 47 | import javax.ws.rs.core.UriBuilder; |
| 48 | import javax.ws.rs.core.UriInfo; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 49 | import java.io.IOException; |
| 50 | import java.io.InputStream; |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 51 | import java.util.ArrayList; |
| 52 | import java.util.List; |
| 53 | import java.util.stream.IntStream; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 54 | |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 55 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 56 | import static org.onlab.util.Tools.readTreeFromStream; |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 57 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Query and program group rules. |
| 60 | */ |
| 61 | |
| 62 | @Path("groups") |
| 63 | public class GroupsWebResource extends AbstractWebResource { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 64 | |
| 65 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 66 | private UriInfo uriInfo; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 67 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 68 | 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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 70 | private final ObjectNode root = mapper().createObjectNode(); |
| 71 | private final ArrayNode groupsNode = root.putArray("groups"); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 72 | |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 73 | private GroupKey createKey(String appCookieString) { |
| 74 | if (!appCookieString.startsWith("0x")) { |
| 75 | throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x"); |
| 76 | } |
| 77 | return new DefaultGroupKey(HexString.fromHexString( |
| 78 | appCookieString.split("0x")[1], "")); |
| 79 | } |
| 80 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 81 | /** |
| 82 | * Returns all groups of all devices. |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 83 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 84 | * @return 200 OK with array of all the groups in the system |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 85 | * @onos.rsModel Groups |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 86 | */ |
| 87 | @GET |
| 88 | @Produces(MediaType.APPLICATION_JSON) |
| 89 | public Response getGroups() { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame] | 90 | GroupService groupService = get(GroupService.class); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 91 | final Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 92 | devices.forEach(device -> { |
| 93 | final Iterable<Group> groups = groupService.getGroups(device.id()); |
| 94 | if (groups != null) { |
| 95 | groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this))); |
| 96 | } |
| 97 | }); |
| 98 | |
| 99 | return ok(root).build(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns all groups associated with the given device. |
| 104 | * |
| 105 | * @param deviceId device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 106 | * @return 200 OK with array of all the groups in the system |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 107 | * @onos.rsModel Groups |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 108 | */ |
| 109 | @GET |
| 110 | @Produces(MediaType.APPLICATION_JSON) |
| 111 | @Path("{deviceId}") |
| 112 | public Response getGroupsByDeviceId(@PathParam("deviceId") String deviceId) { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame] | 113 | GroupService groupService = get(GroupService.class); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 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 Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 122 | * Returns a group with the given deviceId and appCookie. |
| 123 | * |
| 124 | * @param deviceId device identifier |
| 125 | * @param appCookie group key |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 126 | * @return 200 OK with a group entry in the system |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 127 | * @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) { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame] | 134 | GroupService groupService = get(GroupService.class); |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 135 | final DeviceId deviceIdInstance = DeviceId.deviceId(deviceId); |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 136 | |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 137 | final GroupKey appCookieInstance = createKey(appCookie); |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 138 | |
| 139 | Group group = nullIsNotFound(groupService.getGroup(deviceIdInstance, appCookieInstance), |
| 140 | GROUP_NOT_FOUND); |
| 141 | |
| 142 | groupsNode.add(codec(Group.class).encode(group, this)); |
| 143 | return ok(root).build(); |
| 144 | } |
| 145 | |
| 146 | /** |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 147 | * Create new group rule. Creates and installs a new group rule for the |
| 148 | * specified device. |
| 149 | * |
| 150 | * @param deviceId device identifier |
| 151 | * @param stream group rule JSON |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 152 | * @return status of the request - CREATED if the JSON is correct, |
| 153 | * BAD_REQUEST if the JSON is invalid |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 154 | * @onos.rsModel GroupsPost |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 155 | */ |
| 156 | @POST |
| 157 | @Path("{deviceId}") |
| 158 | @Consumes(MediaType.APPLICATION_JSON) |
| 159 | @Produces(MediaType.APPLICATION_JSON) |
| 160 | public Response createGroup(@PathParam("deviceId") String deviceId, |
| 161 | InputStream stream) { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame] | 162 | GroupService groupService = get(GroupService.class); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 163 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 164 | |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 165 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 166 | JsonNode specifiedDeviceId = jsonTree.get("deviceId"); |
| 167 | |
| 168 | if (specifiedDeviceId != null && |
| 169 | !specifiedDeviceId.asText().equals(deviceId)) { |
| 170 | throw new IllegalArgumentException(DEVICE_INVALID); |
| 171 | } |
| 172 | jsonTree.put("deviceId", deviceId); |
| 173 | Group group = codec(Group.class).decode(jsonTree, this); |
| 174 | GroupDescription description = new DefaultGroupDescription( |
| 175 | group.deviceId(), group.type(), group.buckets(), |
| 176 | group.appCookie(), group.id().id(), group.appId()); |
| 177 | groupService.addGroup(description); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 178 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 179 | .path("groups") |
| 180 | .path(deviceId) |
| 181 | .path(Long.toString(group.id().id())); |
| 182 | return Response |
| 183 | .created(locationBuilder.build()) |
| 184 | .build(); |
| 185 | } catch (IOException ex) { |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 186 | throw new IllegalArgumentException(ex); |
| 187 | } |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Removes the specified group. |
| 192 | * |
| 193 | * @param deviceId device identifier |
| 194 | * @param appCookie application cookie to be used for lookup |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 195 | * @return 204 NO CONTENT |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 196 | */ |
| 197 | @DELETE |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 198 | @Path("{deviceId}/{appCookie}") |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 199 | public Response deleteGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId, |
| 200 | @PathParam("appCookie") String appCookie) { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame] | 201 | GroupService groupService = get(GroupService.class); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 202 | DeviceId deviceIdInstance = DeviceId.deviceId(deviceId); |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 203 | |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 204 | final GroupKey appCookieInstance = createKey(appCookie); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 205 | |
| 206 | groupService.removeGroup(deviceIdInstance, appCookieInstance, null); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 207 | return Response.noContent().build(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 208 | } |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * Adds buckets to a group using the group service. |
| 212 | * |
| 213 | * @param deviceIdString device Id |
| 214 | * @param appCookieString application cookie |
| 215 | * @param stream JSON stream |
| 216 | */ |
| 217 | private void updateGroupBuckets(String deviceIdString, String appCookieString, InputStream stream) |
| 218 | throws IOException { |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame] | 219 | GroupService groupService = get(GroupService.class); |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 220 | DeviceId deviceId = DeviceId.deviceId(deviceIdString); |
| 221 | final GroupKey groupKey = createKey(appCookieString); |
| 222 | |
| 223 | Group group = nullIsNotFound(groupService.getGroup(deviceId, groupKey), GROUP_NOT_FOUND); |
| 224 | |
| 225 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
| 226 | |
| 227 | GroupBuckets buckets = null; |
| 228 | List<GroupBucket> groupBucketList = new ArrayList<>(); |
| 229 | JsonNode bucketsJson = jsonTree.get("buckets"); |
| 230 | final JsonCodec<GroupBucket> groupBucketCodec = codec(GroupBucket.class); |
| 231 | if (bucketsJson != null) { |
| 232 | IntStream.range(0, bucketsJson.size()) |
| 233 | .forEach(i -> { |
| 234 | ObjectNode bucketJson = (ObjectNode) bucketsJson.get(i); |
| 235 | groupBucketList.add(groupBucketCodec.decode(bucketJson, this)); |
| 236 | }); |
| 237 | buckets = new GroupBuckets(groupBucketList); |
| 238 | } |
| 239 | groupService.addBucketsToGroup(deviceId, groupKey, buckets, groupKey, group.appId()); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Adds buckets to an existing group. |
| 244 | * |
| 245 | * @param deviceIdString device identifier |
| 246 | * @param appCookieString application cookie |
| 247 | * @param stream buckets JSON |
| 248 | * @return status of the request - NO_CONTENT if the JSON is correct, |
| 249 | * BAD_REQUEST if the JSON is invalid |
| 250 | * @onos.rsModel GroupsBucketsPost |
| 251 | */ |
| 252 | @POST |
| 253 | @Path("{deviceId}/{appCookie}/buckets") |
| 254 | @Consumes(MediaType.APPLICATION_JSON) |
| 255 | @Produces(MediaType.APPLICATION_JSON) |
| 256 | public Response addBucket(@PathParam("deviceId") String deviceIdString, |
| 257 | @PathParam("appCookie") String appCookieString, |
| 258 | InputStream stream) { |
| 259 | try { |
| 260 | updateGroupBuckets(deviceIdString, appCookieString, stream); |
| 261 | |
| 262 | return Response |
| 263 | .noContent() |
| 264 | .build(); |
| 265 | } catch (IOException ex) { |
| 266 | throw new IllegalArgumentException(ex); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Removes buckets from a group using the group service. |
| 272 | * |
| 273 | * @param deviceIdString device Id |
| 274 | * @param appCookieString application cookie |
| 275 | * @param bucketIds comma separated list of bucket Ids to remove |
| 276 | */ |
| 277 | private void removeGroupBuckets(String deviceIdString, String appCookieString, String bucketIds) { |
| 278 | DeviceId deviceId = DeviceId.deviceId(deviceIdString); |
| 279 | final GroupKey groupKey = createKey(appCookieString); |
Harshada Chaundkar | 4f62a1e | 2019-02-18 12:27:34 -0500 | [diff] [blame] | 280 | GroupService groupService = get(GroupService.class); |
Ray Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 281 | |
| 282 | Group group = nullIsNotFound(groupService.getGroup(deviceId, groupKey), GROUP_NOT_FOUND); |
| 283 | |
| 284 | List<GroupBucket> groupBucketList = new ArrayList<>(); |
| 285 | |
| 286 | List<String> bucketsToRemove = ImmutableList.copyOf(bucketIds.split(",")); |
| 287 | |
| 288 | bucketsToRemove.forEach( |
| 289 | bucketIdToRemove -> { |
| 290 | group.buckets().buckets().stream() |
| 291 | .filter(bucket -> Integer.toString(bucket.hashCode()).equals(bucketIdToRemove)) |
| 292 | .forEach(groupBucketList::add); |
| 293 | } |
| 294 | ); |
| 295 | groupService.removeBucketsFromGroup(deviceId, groupKey, |
| 296 | new GroupBuckets(groupBucketList), groupKey, |
| 297 | group.appId()); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Removes buckets from an existing group. |
| 302 | * |
| 303 | * @param deviceIdString device identifier |
| 304 | * @param appCookieString application cookie |
| 305 | * @param bucketIds comma separated list of identifiers of buckets to remove from this group |
| 306 | * @return status of the request - NO_CONTENT if the JSON is correct, |
| 307 | * BAD_REQUEST if the JSON is invalid |
| 308 | */ |
| 309 | @DELETE |
| 310 | @Path("{deviceId}/{appCookie}/buckets/{bucketIds}") |
| 311 | @Consumes(MediaType.APPLICATION_JSON) |
| 312 | @Produces(MediaType.APPLICATION_JSON) |
| 313 | public Response deleteBuckets(@PathParam("deviceId") String deviceIdString, |
| 314 | @PathParam("appCookie") String appCookieString, |
| 315 | @PathParam("bucketIds") String bucketIds) { |
| 316 | removeGroupBuckets(deviceIdString, appCookieString, bucketIds); |
| 317 | |
| 318 | return Response |
| 319 | .noContent() |
| 320 | .build(); |
| 321 | } |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 322 | } |