blob: 733fc4161d7564f6a3f6c35f4912fe1c14a6287d [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
46/**
47 * Query and program group rules.
48 */
49
50@Path("groups")
51public class GroupsWebResource extends AbstractWebResource {
52 public static final String DEVICE_INVALID = "Invalid deviceId in group creation request";
53
54 final GroupService groupService = get(GroupService.class);
55 final ObjectNode root = mapper().createObjectNode();
56 final ArrayNode groupsNode = root.putArray("groups");
57
58 /**
59 * Returns all groups of all devices.
60 * @onos.rsModel Groups
61 * @return array of all the groups in the system
62 */
63 @GET
64 @Produces(MediaType.APPLICATION_JSON)
65 public Response getGroups() {
66 final Iterable<Device> devices = get(DeviceService.class).getDevices();
67 devices.forEach(device -> {
68 final Iterable<Group> groups = groupService.getGroups(device.id());
69 if (groups != null) {
70 groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this)));
71 }
72 });
73
74 return ok(root).build();
75 }
76
77 /**
78 * Returns all groups associated with the given device.
79 *
80 * @param deviceId device identifier
81 * @onos.rsModel Groups
82 * @return array of all the groups in the system
83 */
84 @GET
85 @Produces(MediaType.APPLICATION_JSON)
86 @Path("{deviceId}")
87 public Response getGroupsByDeviceId(@PathParam("deviceId") String deviceId) {
88 final Iterable<Group> groups = groupService.getGroups(DeviceId.deviceId(deviceId));
89
90 groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this)));
91
92 return ok(root).build();
93 }
94
95 /**
96 * Create new group rule. Creates and installs a new group rule for the
97 * specified device.
98 *
99 * @param deviceId device identifier
100 * @param stream group rule JSON
101 * @onos.rsModel GroupsPost
102 * @return status of the request - CREATED if the JSON is correct,
103 * BAD_REQUEST if the JSON is invalid
104 */
105 @POST
106 @Path("{deviceId}")
107 @Consumes(MediaType.APPLICATION_JSON)
108 @Produces(MediaType.APPLICATION_JSON)
109 public Response createGroup(@PathParam("deviceId") String deviceId,
110 InputStream stream) {
111 URI location;
112 try {
113 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
114 JsonNode specifiedDeviceId = jsonTree.get("deviceId");
115
116 if (specifiedDeviceId != null &&
117 !specifiedDeviceId.asText().equals(deviceId)) {
118 throw new IllegalArgumentException(DEVICE_INVALID);
119 }
120 jsonTree.put("deviceId", deviceId);
121 Group group = codec(Group.class).decode(jsonTree, this);
122 GroupDescription description = new DefaultGroupDescription(
123 group.deviceId(), group.type(), group.buckets(),
124 group.appCookie(), group.id().id(), group.appId());
125 groupService.addGroup(description);
126 location = new URI(Long.toString(group.id().id()));
127 } catch (IOException | URISyntaxException ex) {
128 throw new IllegalArgumentException(ex);
129 }
130
131 return Response
132 .created(location)
133 .build();
134 }
135
136 /**
137 * Removes the specified group.
138 *
139 * @param deviceId device identifier
140 * @param appCookie application cookie to be used for lookup
141 */
142 @DELETE
143 @Produces(MediaType.APPLICATION_JSON)
144 @Path("{deviceId}/{appCookie}")
145 public void deleteGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId,
146 @PathParam("appCookie") String appCookie) {
147 DeviceId deviceIdInstance = DeviceId.deviceId(deviceId);
148 GroupKey appCookieInstance = new DefaultGroupKey(appCookie.getBytes());
149
150 groupService.removeGroup(deviceIdInstance, appCookieInstance, null);
151 }
152}