blob: 01ebba0b451fd4c89d25f21470a56ec244a64925 [file] [log] [blame]
Jian Liecb3c0f2015-12-15 10:07:49 -08001/*
2 * Copyright 2014-2015 Open Networking Laboratory
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 */
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
Jian Lia7f86ce2015-12-20 13:42:10 -080047import static org.onlab.util.Tools.nullIsNotFound;
48
Jian Liecb3c0f2015-12-15 10:07:49 -080049/**
50 * Query and program group rules.
51 */
52
53@Path("groups")
54public class GroupsWebResource extends AbstractWebResource {
Jian Li9d616492016-03-09 10:52:49 -080055
56 @Context
57 UriInfo uriInfo;
58
Jian Liecb3c0f2015-12-15 10:07:49 -080059 public static final String DEVICE_INVALID = "Invalid deviceId in group creation request";
Jian Lia7f86ce2015-12-20 13:42:10 -080060 public static final String GROUP_NOT_FOUND = "Group was not found";
Jian Liecb3c0f2015-12-15 10:07:49 -080061
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 Lia7f86ce2015-12-20 13:42:10 -080068 *
Jian Liecb3c0f2015-12-15 10:07:49 -080069 * @return array of all the groups in the system
Jian Lia7f86ce2015-12-20 13:42:10 -080070 * @onos.rsModel Groups
Jian Liecb3c0f2015-12-15 10:07:49 -080071 */
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 Liecb3c0f2015-12-15 10:07:49 -080090 * @return array of all the groups in the system
Jian Lia7f86ce2015-12-20 13:42:10 -080091 * @onos.rsModel Groups
Jian Liecb3c0f2015-12-15 10:07:49 -080092 */
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 Lia7f86ce2015-12-20 13:42:10 -0800105 * 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 Liecb3c0f2015-12-15 10:07:49 -0800128 * 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 Liecb3c0f2015-12-15 10:07:49 -0800133 * @return status of the request - CREATED if the JSON is correct,
134 * BAD_REQUEST if the JSON is invalid
Jian Lia7f86ce2015-12-20 13:42:10 -0800135 * @onos.rsModel GroupsPost
Jian Liecb3c0f2015-12-15 10:07:49 -0800136 */
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 Liecb3c0f2015-12-15 10:07:49 -0800143 try {
Jian Li9d616492016-03-09 10:52:49 -0800144
Jian Liecb3c0f2015-12-15 10:07:49 -0800145 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 Li9d616492016-03-09 10:52:49 -0800158 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 Liecb3c0f2015-12-15 10:07:49 -0800166 throw new IllegalArgumentException(ex);
167 }
Jian Liecb3c0f2015-12-15 10:07:49 -0800168 }
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}