blob: 1af502b75e76895bdcc9673ade1c8c0545d95986 [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;
21import org.onosproject.net.Device;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.device.DeviceService;
24import org.onosproject.net.group.DefaultGroupDescription;
25import org.onosproject.net.group.DefaultGroupKey;
26import org.onosproject.net.group.Group;
27import org.onosproject.net.group.GroupDescription;
28import org.onosproject.net.group.GroupKey;
29import org.onosproject.net.group.GroupService;
30import org.onosproject.rest.AbstractWebResource;
31
32import javax.ws.rs.Consumes;
33import javax.ws.rs.DELETE;
34import javax.ws.rs.GET;
35import javax.ws.rs.POST;
36import javax.ws.rs.Path;
37import javax.ws.rs.PathParam;
38import javax.ws.rs.Produces;
Jian Li9d616492016-03-09 10:52:49 -080039import javax.ws.rs.core.Context;
Jian Liecb3c0f2015-12-15 10:07:49 -080040import javax.ws.rs.core.MediaType;
41import javax.ws.rs.core.Response;
Jian Li9d616492016-03-09 10:52:49 -080042import javax.ws.rs.core.UriBuilder;
43import javax.ws.rs.core.UriInfo;
Jian Liecb3c0f2015-12-15 10:07:49 -080044import java.io.IOException;
45import java.io.InputStream;
Jian Liecb3c0f2015-12-15 10:07:49 -080046
Varun Sharma1853b3f2016-07-18 14:37:13 +053047import org.onlab.util.HexString;
Jian Lia7f86ce2015-12-20 13:42:10 -080048import static org.onlab.util.Tools.nullIsNotFound;
49
Jian Liecb3c0f2015-12-15 10:07:49 -080050/**
51 * Query and program group rules.
52 */
53
54@Path("groups")
55public class GroupsWebResource extends AbstractWebResource {
Jian Li9d616492016-03-09 10:52:49 -080056
57 @Context
Jian Licc730a62016-05-10 16:36:16 -070058 private UriInfo uriInfo;
Jian Li9d616492016-03-09 10:52:49 -080059
Jian Licc730a62016-05-10 16:36:16 -070060 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 Liecb3c0f2015-12-15 10:07:49 -080062
Jian Licc730a62016-05-10 16:36:16 -070063 private final GroupService groupService = get(GroupService.class);
64 private final ObjectNode root = mapper().createObjectNode();
65 private final ArrayNode groupsNode = root.putArray("groups");
Jian Liecb3c0f2015-12-15 10:07:49 -080066
67 /**
68 * Returns all groups of all devices.
Jian Lia7f86ce2015-12-20 13:42:10 -080069 *
Jian Licc730a62016-05-10 16:36:16 -070070 * @return 200 OK with array of all the groups in the system
Jian Lia7f86ce2015-12-20 13:42:10 -080071 * @onos.rsModel Groups
Jian Liecb3c0f2015-12-15 10:07:49 -080072 */
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 Licc730a62016-05-10 16:36:16 -070091 * @return 200 OK with array of all the groups in the system
Jian Lia7f86ce2015-12-20 13:42:10 -080092 * @onos.rsModel Groups
Jian Liecb3c0f2015-12-15 10:07:49 -080093 */
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 Lia7f86ce2015-12-20 13:42:10 -0800106 * Returns a group with the given deviceId and appCookie.
107 *
108 * @param deviceId device identifier
109 * @param appCookie group key
Jian Licc730a62016-05-10 16:36:16 -0700110 * @return 200 OK with a group entry in the system
Jian Lia7f86ce2015-12-20 13:42:10 -0800111 * @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 Sharma1853b3f2016-07-18 14:37:13 +0530119
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 Lia7f86ce2015-12-20 13:42:10 -0800125
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 Liecb3c0f2015-12-15 10:07:49 -0800134 * 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 Liecb3c0f2015-12-15 10:07:49 -0800139 * @return status of the request - CREATED if the JSON is correct,
140 * BAD_REQUEST if the JSON is invalid
Jian Lia7f86ce2015-12-20 13:42:10 -0800141 * @onos.rsModel GroupsPost
Jian Liecb3c0f2015-12-15 10:07:49 -0800142 */
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 Liecb3c0f2015-12-15 10:07:49 -0800149 try {
Jian Li9d616492016-03-09 10:52:49 -0800150
Jian Liecb3c0f2015-12-15 10:07:49 -0800151 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 Li9d616492016-03-09 10:52:49 -0800164 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 Liecb3c0f2015-12-15 10:07:49 -0800172 throw new IllegalArgumentException(ex);
173 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800174 }
175
176 /**
177 * Removes the specified group.
178 *
179 * @param deviceId device identifier
180 * @param appCookie application cookie to be used for lookup
Jian Lic2a542b2016-05-10 11:48:19 -0700181 * @return 204 NO CONTENT
Jian Liecb3c0f2015-12-15 10:07:49 -0800182 */
183 @DELETE
Jian Liecb3c0f2015-12-15 10:07:49 -0800184 @Path("{deviceId}/{appCookie}")
Jian Lic2a542b2016-05-10 11:48:19 -0700185 public Response deleteGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId,
186 @PathParam("appCookie") String appCookie) {
Jian Liecb3c0f2015-12-15 10:07:49 -0800187 DeviceId deviceIdInstance = DeviceId.deviceId(deviceId);
Varun Sharma1853b3f2016-07-18 14:37:13 +0530188
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 Liecb3c0f2015-12-15 10:07:49 -0800194
195 groupService.removeGroup(deviceIdInstance, appCookieInstance, null);
Jian Lic2a542b2016-05-10 11:48:19 -0700196 return Response.noContent().build();
Jian Liecb3c0f2015-12-15 10:07:49 -0800197 }
198}