Search in sources :

Example 1 with GroupService

use of org.onosproject.net.group.GroupService in project onos by opennetworkinglab.

the class GroupsWebResource method createGroup.

/**
 * Create new group rule. Creates and installs a new group rule for the
 * specified device.
 *
 * @param deviceId device identifier
 * @param stream   group rule JSON
 * @return status of the request - CREATED if the JSON is correct,
 * BAD_REQUEST if the JSON is invalid
 * @onos.rsModel GroupsPost
 */
@POST
@Path("{deviceId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createGroup(@PathParam("deviceId") String deviceId, InputStream stream) {
    GroupService groupService = get(GroupService.class);
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), 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);
        UriBuilder locationBuilder = uriInfo.getBaseUriBuilder().path("groups").path(deviceId).path(Long.toString(group.id().id()));
        return Response.created(locationBuilder.build()).build();
    } catch (IOException ex) {
        throw new IllegalArgumentException(ex);
    }
}
Also used : DefaultGroupDescription(org.onosproject.net.group.DefaultGroupDescription) GroupDescription(org.onosproject.net.group.GroupDescription) Group(org.onosproject.net.group.Group) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) UriBuilder(javax.ws.rs.core.UriBuilder) GroupService(org.onosproject.net.group.GroupService) DefaultGroupDescription(org.onosproject.net.group.DefaultGroupDescription) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 2 with GroupService

use of org.onosproject.net.group.GroupService in project onos by opennetworkinglab.

the class GroupsWebResource method getGroupByDeviceIdAndAppCookie.

/**
 * Returns a group with the given deviceId and appCookie.
 *
 * @param deviceId device identifier
 * @param appCookie group key
 * @return 200 OK with a group entry in the system
 * @onos.rsModel Group
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{deviceId}/{appCookie}")
public Response getGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId, @PathParam("appCookie") String appCookie) {
    GroupService groupService = get(GroupService.class);
    final DeviceId deviceIdInstance = DeviceId.deviceId(deviceId);
    final GroupKey appCookieInstance = createKey(appCookie);
    Group group = nullIsNotFound(groupService.getGroup(deviceIdInstance, appCookieInstance), GROUP_NOT_FOUND);
    groupsNode.add(codec(Group.class).encode(group, this));
    return ok(root).build();
}
Also used : Group(org.onosproject.net.group.Group) DeviceId(org.onosproject.net.DeviceId) GroupKey(org.onosproject.net.group.GroupKey) DefaultGroupKey(org.onosproject.net.group.DefaultGroupKey) GroupService(org.onosproject.net.group.GroupService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with GroupService

use of org.onosproject.net.group.GroupService in project onos by opennetworkinglab.

the class GroupsWebResource method getGroupsByDeviceId.

/**
 * Returns all groups associated with the given device.
 *
 * @param deviceId device identifier
 * @return 200 OK with array of all the groups in the system
 * @onos.rsModel Groups
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{deviceId}")
public Response getGroupsByDeviceId(@PathParam("deviceId") String deviceId) {
    GroupService groupService = get(GroupService.class);
    final Iterable<Group> groups = groupService.getGroups(DeviceId.deviceId(deviceId));
    groups.forEach(group -> groupsNode.add(codec(Group.class).encode(group, this)));
    return ok(root).build();
}
Also used : Group(org.onosproject.net.group.Group) GroupService(org.onosproject.net.group.GroupService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with GroupService

use of org.onosproject.net.group.GroupService in project onos by opennetworkinglab.

the class GroupsWebResource method deleteGroupByDeviceIdAndAppCookie.

/**
 * Removes the specified group.
 *
 * @param deviceId  device identifier
 * @param appCookie application cookie to be used for lookup
 * @return 204 NO CONTENT
 */
@DELETE
@Path("{deviceId}/{appCookie}")
public Response deleteGroupByDeviceIdAndAppCookie(@PathParam("deviceId") String deviceId, @PathParam("appCookie") String appCookie) {
    GroupService groupService = get(GroupService.class);
    DeviceId deviceIdInstance = DeviceId.deviceId(deviceId);
    final GroupKey appCookieInstance = createKey(appCookie);
    groupService.removeGroup(deviceIdInstance, appCookieInstance, null);
    return Response.noContent().build();
}
Also used : DeviceId(org.onosproject.net.DeviceId) GroupKey(org.onosproject.net.group.GroupKey) DefaultGroupKey(org.onosproject.net.group.DefaultGroupKey) GroupService(org.onosproject.net.group.GroupService) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 5 with GroupService

use of org.onosproject.net.group.GroupService in project onos by opennetworkinglab.

the class GroupsWebResource method updateGroupBuckets.

/**
 * Adds buckets to a group using the group service.
 *
 * @param deviceIdString device Id
 * @param appCookieString application cookie
 * @param stream JSON stream
 */
private void updateGroupBuckets(String deviceIdString, String appCookieString, InputStream stream) throws IOException {
    GroupService groupService = get(GroupService.class);
    DeviceId deviceId = DeviceId.deviceId(deviceIdString);
    final GroupKey groupKey = createKey(appCookieString);
    Group group = nullIsNotFound(groupService.getGroup(deviceId, groupKey), GROUP_NOT_FOUND);
    ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
    GroupBuckets buckets = null;
    List<GroupBucket> groupBucketList = new ArrayList<>();
    JsonNode bucketsJson = jsonTree.get("buckets");
    final JsonCodec<GroupBucket> groupBucketCodec = codec(GroupBucket.class);
    if (bucketsJson != null) {
        IntStream.range(0, bucketsJson.size()).forEach(i -> {
            ObjectNode bucketJson = (ObjectNode) bucketsJson.get(i);
            groupBucketList.add(groupBucketCodec.decode(bucketJson, this));
        });
        buckets = new GroupBuckets(groupBucketList);
    }
    groupService.addBucketsToGroup(deviceId, groupKey, buckets, groupKey, group.appId());
}
Also used : Group(org.onosproject.net.group.Group) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) GroupKey(org.onosproject.net.group.GroupKey) DefaultGroupKey(org.onosproject.net.group.DefaultGroupKey) ArrayList(java.util.ArrayList) GroupBucket(org.onosproject.net.group.GroupBucket) JsonNode(com.fasterxml.jackson.databind.JsonNode) GroupBuckets(org.onosproject.net.group.GroupBuckets) GroupService(org.onosproject.net.group.GroupService)

Aggregations

GroupService (org.onosproject.net.group.GroupService)12 Group (org.onosproject.net.group.Group)9 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 Device (org.onosproject.net.Device)5 DeviceId (org.onosproject.net.DeviceId)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 GET (javax.ws.rs.GET)4 DeviceService (org.onosproject.net.device.DeviceService)4 DefaultGroupKey (org.onosproject.net.group.DefaultGroupKey)4 GroupKey (org.onosproject.net.group.GroupKey)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 List (java.util.List)3 GroupBucket (org.onosproject.net.group.GroupBucket)3 GroupDescription (org.onosproject.net.group.GroupDescription)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Consumes (javax.ws.rs.Consumes)2 DELETE (javax.ws.rs.DELETE)2