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 | |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 47 | import static org.onlab.util.Tools.nullIsNotFound; |
| 48 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Query and program group rules. |
| 51 | */ |
| 52 | |
| 53 | @Path("groups") |
| 54 | public class GroupsWebResource extends AbstractWebResource { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 55 | |
| 56 | @Context |
| 57 | UriInfo uriInfo; |
| 58 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 59 | public static final String DEVICE_INVALID = "Invalid deviceId in group creation request"; |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 60 | public static final String GROUP_NOT_FOUND = "Group was not found"; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 61 | |
| 62 | final GroupService groupService = get(GroupService.class); |
| 63 | final ObjectNode root = mapper().createObjectNode(); |
| 64 | final ArrayNode groupsNode = root.putArray("groups"); |
| 65 | |
| 66 | /** |
| 67 | * Returns all groups of all devices. |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 68 | * |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 69 | * @return array of all the groups in the system |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 70 | * @onos.rsModel Groups |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 71 | */ |
| 72 | @GET |
| 73 | @Produces(MediaType.APPLICATION_JSON) |
| 74 | public Response getGroups() { |
| 75 | final Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 76 | devices.forEach(device -> { |
| 77 | final Iterable<Group> groups = groupService.getGroups(device.id()); |
| 78 | if (groups != null) { |
| 79 | groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this))); |
| 80 | } |
| 81 | }); |
| 82 | |
| 83 | return ok(root).build(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns all groups associated with the given device. |
| 88 | * |
| 89 | * @param deviceId device identifier |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 90 | * @return array of all the groups in the system |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 91 | * @onos.rsModel Groups |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 92 | */ |
| 93 | @GET |
| 94 | @Produces(MediaType.APPLICATION_JSON) |
| 95 | @Path("{deviceId}") |
| 96 | public Response getGroupsByDeviceId(@PathParam("deviceId") String deviceId) { |
| 97 | final Iterable<Group> groups = groupService.getGroups(DeviceId.deviceId(deviceId)); |
| 98 | |
| 99 | groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this))); |
| 100 | |
| 101 | return ok(root).build(); |
| 102 | } |
| 103 | |
| 104 | /** |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 105 | * Returns a group with the given deviceId and appCookie. |
| 106 | * |
| 107 | * @param deviceId device identifier |
| 108 | * @param appCookie group key |
| 109 | * @return a group entry in the system |
| 110 | * @onos.rsModel Group |
| 111 | */ |
| 112 | @GET |
| 113 | @Produces(MediaType.APPLICATION_JSON) |
| 114 | @Path("{deviceId}/{appCookie}") |
| 115 | public Response getGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId, |
| 116 | @PathParam("appCookie") String appCookie) { |
| 117 | final DeviceId deviceIdInstance = DeviceId.deviceId(deviceId); |
| 118 | final GroupKey appCookieInstance = new DefaultGroupKey(appCookie.getBytes()); |
| 119 | |
| 120 | Group group = nullIsNotFound(groupService.getGroup(deviceIdInstance, appCookieInstance), |
| 121 | GROUP_NOT_FOUND); |
| 122 | |
| 123 | groupsNode.add(codec(Group.class).encode(group, this)); |
| 124 | return ok(root).build(); |
| 125 | } |
| 126 | |
| 127 | /** |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 128 | * Create new group rule. Creates and installs a new group rule for the |
| 129 | * specified device. |
| 130 | * |
| 131 | * @param deviceId device identifier |
| 132 | * @param stream group rule JSON |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 133 | * @return status of the request - CREATED if the JSON is correct, |
| 134 | * BAD_REQUEST if the JSON is invalid |
Jian Li | a7f86ce | 2015-12-20 13:42:10 -0800 | [diff] [blame] | 135 | * @onos.rsModel GroupsPost |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 136 | */ |
| 137 | @POST |
| 138 | @Path("{deviceId}") |
| 139 | @Consumes(MediaType.APPLICATION_JSON) |
| 140 | @Produces(MediaType.APPLICATION_JSON) |
| 141 | public Response createGroup(@PathParam("deviceId") String deviceId, |
| 142 | InputStream stream) { |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 143 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 144 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 145 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 146 | JsonNode specifiedDeviceId = jsonTree.get("deviceId"); |
| 147 | |
| 148 | if (specifiedDeviceId != null && |
| 149 | !specifiedDeviceId.asText().equals(deviceId)) { |
| 150 | throw new IllegalArgumentException(DEVICE_INVALID); |
| 151 | } |
| 152 | jsonTree.put("deviceId", deviceId); |
| 153 | Group group = codec(Group.class).decode(jsonTree, this); |
| 154 | GroupDescription description = new DefaultGroupDescription( |
| 155 | group.deviceId(), group.type(), group.buckets(), |
| 156 | group.appCookie(), group.id().id(), group.appId()); |
| 157 | groupService.addGroup(description); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 158 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 159 | .path("groups") |
| 160 | .path(deviceId) |
| 161 | .path(Long.toString(group.id().id())); |
| 162 | return Response |
| 163 | .created(locationBuilder.build()) |
| 164 | .build(); |
| 165 | } catch (IOException ex) { |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 166 | throw new IllegalArgumentException(ex); |
| 167 | } |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Removes the specified group. |
| 172 | * |
| 173 | * @param deviceId device identifier |
| 174 | * @param appCookie application cookie to be used for lookup |
| 175 | */ |
| 176 | @DELETE |
| 177 | @Produces(MediaType.APPLICATION_JSON) |
| 178 | @Path("{deviceId}/{appCookie}") |
| 179 | public void deleteGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId, |
| 180 | @PathParam("appCookie") String appCookie) { |
| 181 | DeviceId deviceIdInstance = DeviceId.deviceId(deviceId); |
| 182 | GroupKey appCookieInstance = new DefaultGroupKey(appCookie.getBytes()); |
| 183 | |
| 184 | groupService.removeGroup(deviceIdInstance, appCookieInstance, null); |
| 185 | } |
| 186 | } |