[ONOS-3603] Implement REST API for Group query, insert, delete
* Implement decoding feature for GroupBucketCodec and GroupCodec
* Implement GroupsWebResource
* Add unit test for GroupBucketCodec and GroupCodec
* Add unit test for GroupsWebResource
* Add group insertion json example
* Add Swagger doc
Change-Id: Ie58cba2e1af996c7b8652a55d9ef0c27207beafc
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java b/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
index cec1faa..afce7f7 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
@@ -37,6 +37,7 @@
HostsWebResource.class,
IntentsWebResource.class,
FlowsWebResource.class,
+ GroupsWebResource.class,
TopologyWebResource.class,
ConfigWebResource.class,
PathsWebResource.class,
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/GroupsWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/GroupsWebResource.java
new file mode 100644
index 0000000..733fc41
--- /dev/null
+++ b/web/api/src/main/java/org/onosproject/rest/resources/GroupsWebResource.java
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.rest.resources;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.group.DefaultGroupDescription;
+import org.onosproject.net.group.DefaultGroupKey;
+import org.onosproject.net.group.Group;
+import org.onosproject.net.group.GroupDescription;
+import org.onosproject.net.group.GroupKey;
+import org.onosproject.net.group.GroupService;
+import org.onosproject.rest.AbstractWebResource;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+/**
+ * Query and program group rules.
+ */
+
+@Path("groups")
+public class GroupsWebResource extends AbstractWebResource {
+ public static final String DEVICE_INVALID = "Invalid deviceId in group creation request";
+
+ final GroupService groupService = get(GroupService.class);
+ final ObjectNode root = mapper().createObjectNode();
+ final ArrayNode groupsNode = root.putArray("groups");
+
+ /**
+ * Returns all groups of all devices.
+ * @onos.rsModel Groups
+ * @return array of all the groups in the system
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getGroups() {
+ final Iterable<Device> devices = get(DeviceService.class).getDevices();
+ devices.forEach(device -> {
+ final Iterable<Group> groups = groupService.getGroups(device.id());
+ if (groups != null) {
+ groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this)));
+ }
+ });
+
+ return ok(root).build();
+ }
+
+ /**
+ * Returns all groups associated with the given device.
+ *
+ * @param deviceId device identifier
+ * @onos.rsModel Groups
+ * @return array of all the groups in the system
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("{deviceId}")
+ public Response getGroupsByDeviceId(@PathParam("deviceId") String deviceId) {
+ final Iterable<Group> groups = groupService.getGroups(DeviceId.deviceId(deviceId));
+
+ groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this)));
+
+ return ok(root).build();
+ }
+
+ /**
+ * Create new group rule. Creates and installs a new group rule for the
+ * specified device.
+ *
+ * @param deviceId device identifier
+ * @param stream group rule JSON
+ * @onos.rsModel GroupsPost
+ * @return status of the request - CREATED if the JSON is correct,
+ * BAD_REQUEST if the JSON is invalid
+ */
+ @POST
+ @Path("{deviceId}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response createGroup(@PathParam("deviceId") String deviceId,
+ InputStream stream) {
+ URI location;
+ try {
+ ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
+ JsonNode specifiedDeviceId = jsonTree.get("deviceId");
+
+ if (specifiedDeviceId != null &&
+ !specifiedDeviceId.asText().equals(deviceId)) {
+ throw new IllegalArgumentException(DEVICE_INVALID);
+ }
+ jsonTree.put("deviceId", deviceId);
+ Group group = codec(Group.class).decode(jsonTree, this);
+ GroupDescription description = new DefaultGroupDescription(
+ group.deviceId(), group.type(), group.buckets(),
+ group.appCookie(), group.id().id(), group.appId());
+ groupService.addGroup(description);
+ location = new URI(Long.toString(group.id().id()));
+ } catch (IOException | URISyntaxException ex) {
+ throw new IllegalArgumentException(ex);
+ }
+
+ return Response
+ .created(location)
+ .build();
+ }
+
+ /**
+ * Removes the specified group.
+ *
+ * @param deviceId device identifier
+ * @param appCookie application cookie to be used for lookup
+ */
+ @DELETE
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("{deviceId}/{appCookie}")
+ public void deleteGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId,
+ @PathParam("appCookie") String appCookie) {
+ DeviceId deviceIdInstance = DeviceId.deviceId(deviceId);
+ GroupKey appCookieInstance = new DefaultGroupKey(appCookie.getBytes());
+
+ groupService.removeGroup(deviceIdInstance, appCookieInstance, null);
+ }
+}
diff --git a/web/api/src/main/resources/definitions/Groups.json b/web/api/src/main/resources/definitions/Groups.json
new file mode 100644
index 0000000..517c564
--- /dev/null
+++ b/web/api/src/main/resources/definitions/Groups.json
@@ -0,0 +1,124 @@
+{
+ "type": "object",
+ "title": "groups",
+ "required": [
+ "groups"
+ ],
+ "properties": {
+ "groups": {
+ "type": "array",
+ "xml": {
+ "name": "groups",
+ "wrapped": true
+ },
+ "items": {
+ "type": "object",
+ "title": "group",
+ "required": [
+ "id",
+ "state",
+ "life",
+ "packets",
+ "bytes",
+ "referenceCount",
+ "type",
+ "deviceId",
+ "buckets"
+ ],
+ "properties": {
+ "id": {
+ "type": "string",
+ "example": "1"
+ },
+ "state": {
+ "type": "string",
+ "example": "PENDING_ADD"
+ },
+ "life": {
+ "type": "integer",
+ "format": "int64",
+ "example": 69889
+ },
+ "packets": {
+ "type": "integer",
+ "format": "int64",
+ "example": 22546
+ },
+ "bytes": {
+ "type": "integer",
+ "format": "int64",
+ "example": 1826226
+ },
+ "referenceCount": {
+ "type": "integer",
+ "format": "int64",
+ "example": 1826226
+ },
+ "type": {
+ "type": "string",
+ "example": "ALL"
+ },
+ "deviceId": {
+ "type": "string",
+ "example": "of:0000000000000003"
+ },
+ "buckets": {
+ "type": "array",
+ "xml": {
+ "name": "buckets",
+ "wrapped": true
+ },
+ "items": {
+ "type": "object",
+ "title": "buckets",
+ "required": [
+ "treatment",
+ "weight",
+ "watchPort",
+ "watchGroup"
+ ],
+ "properties": {
+ "treatment": {
+ "type": "object",
+ "title": "treatment",
+ "required": [
+ "instructions",
+ "deferred"
+ ],
+ "properties": {
+ "instructions": {
+ "type": "array",
+ "title": "treatment",
+ "required": [
+ "properties",
+ "port"
+ ],
+ "items": {
+ "type": "object",
+ "title": "instructions",
+ "required": [
+ "type",
+ "port"
+ ],
+ "properties": {
+ "type": {
+ "type": "string",
+ "example": "OUTPUT"
+ },
+ "port": {
+ "type": "string",
+ "example": "2"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/web/api/src/main/resources/definitions/GroupsPost.json b/web/api/src/main/resources/definitions/GroupsPost.json
new file mode 100644
index 0000000..f096201
--- /dev/null
+++ b/web/api/src/main/resources/definitions/GroupsPost.json
@@ -0,0 +1,84 @@
+{
+ "type": "object",
+ "title": "group",
+ "required": [
+ "type",
+ "deviceId",
+ "appCookie",
+ "groupId",
+ "buckets"
+ ],
+ "properties": {
+ "type": {
+ "type": "string",
+ "example": "ALL"
+ },
+ "deviceId": {
+ "type": "string",
+ "example": "of:0000000000000001"
+ },
+ "appCookie": {
+ "type": "string",
+ "example": "1"
+ },
+ "groupId": {
+ "type": "string",
+ "example": "1"
+ },
+ "buckets": {
+ "type": "array",
+ "xml": {
+ "name": "buckets",
+ "wrapped": true
+ },
+ "items": {
+ "type": "object",
+ "title": "buckets",
+ "required": [
+ "treatment",
+ "weight",
+ "watchPort",
+ "watchGroup"
+ ],
+ "properties": {
+ "treatment": {
+ "type": "object",
+ "title": "treatment",
+ "required": [
+ "instructions",
+ "deferred"
+ ],
+ "properties": {
+ "instructions": {
+ "type": "array",
+ "title": "treatment",
+ "required": [
+ "properties",
+ "port"
+ ],
+ "items": {
+ "type": "object",
+ "title": "instructions",
+ "required": [
+ "type",
+ "port"
+ ],
+ "properties": {
+ "type": {
+ "type": "string",
+ "example": "OUTPUT"
+ },
+ "port": {
+ "type": "string",
+ "example": "2"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file