blob: 2d917bb5de1f6323ea12a6204db6eb91b424724f [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;
39import javax.ws.rs.core.MediaType;
40import javax.ws.rs.core.Response;
41import java.io.IOException;
42import java.io.InputStream;
43import java.net.URI;
44import java.net.URISyntaxException;
45
Jian Lia7f86ce2015-12-20 13:42:10 -080046import static org.onlab.util.Tools.nullIsNotFound;
47
Jian Liecb3c0f2015-12-15 10:07:49 -080048/**
49 * Query and program group rules.
50 */
51
52@Path("groups")
53public class GroupsWebResource extends AbstractWebResource {
54 public static final String DEVICE_INVALID = "Invalid deviceId in group creation request";
Jian Lia7f86ce2015-12-20 13:42:10 -080055 public static final String GROUP_NOT_FOUND = "Group was not found";
Jian Liecb3c0f2015-12-15 10:07:49 -080056
57 final GroupService groupService = get(GroupService.class);
58 final ObjectNode root = mapper().createObjectNode();
59 final ArrayNode groupsNode = root.putArray("groups");
60
61 /**
62 * Returns all groups of all devices.
Jian Lia7f86ce2015-12-20 13:42:10 -080063 *
Jian Liecb3c0f2015-12-15 10:07:49 -080064 * @return array of all the groups in the system
Jian Lia7f86ce2015-12-20 13:42:10 -080065 * @onos.rsModel Groups
Jian Liecb3c0f2015-12-15 10:07:49 -080066 */
67 @GET
68 @Produces(MediaType.APPLICATION_JSON)
69 public Response getGroups() {
70 final Iterable<Device> devices = get(DeviceService.class).getDevices();
71 devices.forEach(device -> {
72 final Iterable<Group> groups = groupService.getGroups(device.id());
73 if (groups != null) {
74 groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this)));
75 }
76 });
77
78 return ok(root).build();
79 }
80
81 /**
82 * Returns all groups associated with the given device.
83 *
84 * @param deviceId device identifier
Jian Liecb3c0f2015-12-15 10:07:49 -080085 * @return array of all the groups in the system
Jian Lia7f86ce2015-12-20 13:42:10 -080086 * @onos.rsModel Groups
Jian Liecb3c0f2015-12-15 10:07:49 -080087 */
88 @GET
89 @Produces(MediaType.APPLICATION_JSON)
90 @Path("{deviceId}")
91 public Response getGroupsByDeviceId(@PathParam("deviceId") String deviceId) {
92 final Iterable<Group> groups = groupService.getGroups(DeviceId.deviceId(deviceId));
93
94 groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this)));
95
96 return ok(root).build();
97 }
98
99 /**
Jian Lia7f86ce2015-12-20 13:42:10 -0800100 * Returns a group with the given deviceId and appCookie.
101 *
102 * @param deviceId device identifier
103 * @param appCookie group key
104 * @return a group entry in the system
105 * @onos.rsModel Group
106 */
107 @GET
108 @Produces(MediaType.APPLICATION_JSON)
109 @Path("{deviceId}/{appCookie}")
110 public Response getGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId,
111 @PathParam("appCookie") String appCookie) {
112 final DeviceId deviceIdInstance = DeviceId.deviceId(deviceId);
113 final GroupKey appCookieInstance = new DefaultGroupKey(appCookie.getBytes());
114
115 Group group = nullIsNotFound(groupService.getGroup(deviceIdInstance, appCookieInstance),
116 GROUP_NOT_FOUND);
117
118 groupsNode.add(codec(Group.class).encode(group, this));
119 return ok(root).build();
120 }
121
122 /**
Jian Liecb3c0f2015-12-15 10:07:49 -0800123 * Create new group rule. Creates and installs a new group rule for the
124 * specified device.
125 *
126 * @param deviceId device identifier
127 * @param stream group rule JSON
Jian Liecb3c0f2015-12-15 10:07:49 -0800128 * @return status of the request - CREATED if the JSON is correct,
129 * BAD_REQUEST if the JSON is invalid
Jian Lia7f86ce2015-12-20 13:42:10 -0800130 * @onos.rsModel GroupsPost
Jian Liecb3c0f2015-12-15 10:07:49 -0800131 */
132 @POST
133 @Path("{deviceId}")
134 @Consumes(MediaType.APPLICATION_JSON)
135 @Produces(MediaType.APPLICATION_JSON)
136 public Response createGroup(@PathParam("deviceId") String deviceId,
137 InputStream stream) {
138 URI location;
139 try {
140 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
141 JsonNode specifiedDeviceId = jsonTree.get("deviceId");
142
143 if (specifiedDeviceId != null &&
144 !specifiedDeviceId.asText().equals(deviceId)) {
145 throw new IllegalArgumentException(DEVICE_INVALID);
146 }
147 jsonTree.put("deviceId", deviceId);
148 Group group = codec(Group.class).decode(jsonTree, this);
149 GroupDescription description = new DefaultGroupDescription(
150 group.deviceId(), group.type(), group.buckets(),
151 group.appCookie(), group.id().id(), group.appId());
152 groupService.addGroup(description);
153 location = new URI(Long.toString(group.id().id()));
154 } catch (IOException | URISyntaxException ex) {
155 throw new IllegalArgumentException(ex);
156 }
157
158 return Response
159 .created(location)
160 .build();
161 }
162
163 /**
164 * Removes the specified group.
165 *
166 * @param deviceId device identifier
167 * @param appCookie application cookie to be used for lookup
168 */
169 @DELETE
170 @Produces(MediaType.APPLICATION_JSON)
171 @Path("{deviceId}/{appCookie}")
172 public void deleteGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId,
173 @PathParam("appCookie") String appCookie) {
174 DeviceId deviceIdInstance = DeviceId.deviceId(deviceId);
175 GroupKey appCookieInstance = new DefaultGroupKey(appCookie.getBytes());
176
177 groupService.removeGroup(deviceIdInstance, appCookieInstance, null);
178 }
179}