Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.device.DeviceService; |
| 24 | import org.onosproject.net.group.DefaultGroupDescription; |
| 25 | import org.onosproject.net.group.DefaultGroupKey; |
| 26 | import org.onosproject.net.group.Group; |
| 27 | import org.onosproject.net.group.GroupDescription; |
| 28 | import org.onosproject.net.group.GroupKey; |
| 29 | import org.onosproject.net.group.GroupService; |
| 30 | import org.onosproject.rest.AbstractWebResource; |
| 31 | |
| 32 | import javax.ws.rs.Consumes; |
| 33 | import javax.ws.rs.DELETE; |
| 34 | import javax.ws.rs.GET; |
| 35 | import javax.ws.rs.POST; |
| 36 | import javax.ws.rs.Path; |
| 37 | import javax.ws.rs.PathParam; |
| 38 | import javax.ws.rs.Produces; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 39 | import javax.ws.rs.core.Context; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 40 | import javax.ws.rs.core.MediaType; |
| 41 | import javax.ws.rs.core.Response; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 42 | import javax.ws.rs.core.UriBuilder; |
| 43 | import javax.ws.rs.core.UriInfo; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 44 | import java.io.IOException; |
| 45 | import java.io.InputStream; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 46 | |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 47 | import org.onlab.util.HexString; |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 48 | import static org.onlab.util.Tools.nullIsNotFound; |
| 49 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 50 | /** |
| 51 | * Query and program group rules. |
| 52 | */ |
| 53 | |
| 54 | @Path("groups") |
| 55 | public class GroupsWebResource extends AbstractWebResource { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 56 | |
| 57 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 58 | private UriInfo uriInfo; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 59 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 60 | private static final String DEVICE_INVALID = "Invalid deviceId in group creation request"; |
| 61 | private static final String GROUP_NOT_FOUND = "Group was not found"; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 62 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 63 | private final GroupService groupService = get(GroupService.class); |
| 64 | private final ObjectNode root = mapper().createObjectNode(); |
| 65 | private final ArrayNode groupsNode = root.putArray("groups"); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Returns all groups of all devices. |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 69 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 70 | * @return 200 OK with array of all the groups in the system |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 71 | * @onos.rsModel Groups |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 72 | */ |
| 73 | @GET |
| 74 | @Produces(MediaType.APPLICATION_JSON) |
| 75 | public Response getGroups() { |
| 76 | final Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 77 | devices.forEach(device -> { |
| 78 | final Iterable<Group> groups = groupService.getGroups(device.id()); |
| 79 | if (groups != null) { |
| 80 | groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this))); |
| 81 | } |
| 82 | }); |
| 83 | |
| 84 | return ok(root).build(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns all groups associated with the given device. |
| 89 | * |
| 90 | * @param deviceId device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 91 | * @return 200 OK with array of all the groups in the system |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 92 | * @onos.rsModel Groups |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 93 | */ |
| 94 | @GET |
| 95 | @Produces(MediaType.APPLICATION_JSON) |
| 96 | @Path("{deviceId}") |
| 97 | public Response getGroupsByDeviceId(@PathParam("deviceId") String deviceId) { |
| 98 | final Iterable<Group> groups = groupService.getGroups(DeviceId.deviceId(deviceId)); |
| 99 | |
| 100 | groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this))); |
| 101 | |
| 102 | return ok(root).build(); |
| 103 | } |
| 104 | |
| 105 | /** |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 106 | * Returns a group with the given deviceId and appCookie. |
| 107 | * |
| 108 | * @param deviceId device identifier |
| 109 | * @param appCookie group key |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 110 | * @return 200 OK with a group entry in the system |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 111 | * @onos.rsModel Group |
| 112 | */ |
| 113 | @GET |
| 114 | @Produces(MediaType.APPLICATION_JSON) |
| 115 | @Path("{deviceId}/{appCookie}") |
| 116 | public Response getGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId, |
| 117 | @PathParam("appCookie") String appCookie) { |
| 118 | final DeviceId deviceIdInstance = DeviceId.deviceId(deviceId); |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 119 | |
| 120 | if (!appCookie.startsWith("0x")) { |
| 121 | throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x"); |
| 122 | } |
| 123 | final GroupKey appCookieInstance = new DefaultGroupKey(HexString.fromHexString( |
| 124 | appCookie.split("0x")[1], "")); |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 125 | |
| 126 | Group group = nullIsNotFound(groupService.getGroup(deviceIdInstance, appCookieInstance), |
| 127 | GROUP_NOT_FOUND); |
| 128 | |
| 129 | groupsNode.add(codec(Group.class).encode(group, this)); |
| 130 | return ok(root).build(); |
| 131 | } |
| 132 | |
| 133 | /** |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 134 | * Create new group rule. Creates and installs a new group rule for the |
| 135 | * specified device. |
| 136 | * |
| 137 | * @param deviceId device identifier |
| 138 | * @param stream group rule JSON |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 139 | * @return status of the request - CREATED if the JSON is correct, |
| 140 | * BAD_REQUEST if the JSON is invalid |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 141 | * @onos.rsModel GroupsPost |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 142 | */ |
| 143 | @POST |
| 144 | @Path("{deviceId}") |
| 145 | @Consumes(MediaType.APPLICATION_JSON) |
| 146 | @Produces(MediaType.APPLICATION_JSON) |
| 147 | public Response createGroup(@PathParam("deviceId") String deviceId, |
| 148 | InputStream stream) { |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 149 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 150 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 151 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 152 | JsonNode specifiedDeviceId = jsonTree.get("deviceId"); |
| 153 | |
| 154 | if (specifiedDeviceId != null && |
| 155 | !specifiedDeviceId.asText().equals(deviceId)) { |
| 156 | throw new IllegalArgumentException(DEVICE_INVALID); |
| 157 | } |
| 158 | jsonTree.put("deviceId", deviceId); |
| 159 | Group group = codec(Group.class).decode(jsonTree, this); |
| 160 | GroupDescription description = new DefaultGroupDescription( |
| 161 | group.deviceId(), group.type(), group.buckets(), |
| 162 | group.appCookie(), group.id().id(), group.appId()); |
| 163 | groupService.addGroup(description); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 164 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 165 | .path("groups") |
| 166 | .path(deviceId) |
| 167 | .path(Long.toString(group.id().id())); |
| 168 | return Response |
| 169 | .created(locationBuilder.build()) |
| 170 | .build(); |
| 171 | } catch (IOException ex) { |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 172 | throw new IllegalArgumentException(ex); |
| 173 | } |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Removes the specified group. |
| 178 | * |
| 179 | * @param deviceId device identifier |
| 180 | * @param appCookie application cookie to be used for lookup |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 181 | * @return 204 NO CONTENT |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 182 | */ |
| 183 | @DELETE |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 184 | @Path("{deviceId}/{appCookie}") |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 185 | public Response deleteGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId, |
| 186 | @PathParam("appCookie") String appCookie) { |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 187 | DeviceId deviceIdInstance = DeviceId.deviceId(deviceId); |
Varun Sharma | 1853b3f | 2016-07-18 14:37:13 +0530 | [diff] [blame] | 188 | |
| 189 | if (!appCookie.startsWith("0x")) { |
| 190 | throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x"); |
| 191 | } |
| 192 | GroupKey appCookieInstance = new DefaultGroupKey(HexString.fromHexString( |
| 193 | appCookie.split("0x")[1], "")); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 194 | |
| 195 | groupService.removeGroup(deviceIdInstance, appCookieInstance, null); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 196 | return Response.noContent().build(); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 197 | } |
| 198 | } |