2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.vtnweb.resources;
19 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
20 import static javax.ws.rs.core.Response.Status.OK;
21 import static org.onlab.util.Tools.nullIsNotFound;
23 import java.io.IOException;
24 import java.io.InputStream;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
37 import org.onosproject.rest.AbstractWebResource;
38 import org.onosproject.vtnrsc.PortPairGroup;
39 import org.onosproject.vtnrsc.PortPairGroupId;
40 import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
41 import org.onosproject.vtnweb.web.PortPairGroupCodec;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import com.fasterxml.jackson.databind.node.ObjectNode;
49 * Query and program port pair group.
52 @Path("port_pair_groups")
53 public class PortPairGroupWebResource extends AbstractWebResource {
55 private final Logger log = LoggerFactory.getLogger(PortPairGroupWebResource.class);
56 private final PortPairGroupService service = get(PortPairGroupService.class);
57 public static final String PORT_PAIR_GROUP_NOT_FOUND = "Port pair group not found";
58 public static final String PORT_PAIR_GROUP_ID_EXIST = "Port pair group exists";
59 public static final String PORT_PAIR_GROUP_ID_NOT_EXIST = "Port pair group does not exist with identifier";
62 * Get details of all port pair groups created.
67 @Produces(MediaType.APPLICATION_JSON)
68 public Response getPortPairGroups() {
69 Iterable<PortPairGroup> portPairGroups = service.getPortPairGroups();
70 ObjectNode result = new ObjectMapper().createObjectNode();
71 result.set("port_pair_groups", new PortPairGroupCodec().encode(portPairGroups, this));
72 return ok(result).build();
76 * Get details of a specified port pair group id.
78 * @param id port pair group id
79 * @return 200 OK, 404 if given identifier does not exist
83 @Produces(MediaType.APPLICATION_JSON)
84 public Response getPortPairGroup(@PathParam("group_id") String id) {
86 if (!service.exists(PortPairGroupId.of(id))) {
87 return Response.status(NOT_FOUND)
88 .entity(PORT_PAIR_GROUP_NOT_FOUND).build();
90 PortPairGroup portPairGroup = nullIsNotFound(service.getPortPairGroup(PortPairGroupId.of(id)),
91 PORT_PAIR_GROUP_NOT_FOUND);
93 ObjectNode result = new ObjectMapper().createObjectNode();
94 result.set("port_pair_group", new PortPairGroupCodec().encode(portPairGroup, this));
95 return ok(result).build();
99 * Creates a new port pair group.
101 * @param stream port pair group from JSON
102 * @return status of the request - CREATED if the JSON is correct,
103 * BAD_REQUEST if the JSON is invalid
106 @Consumes(MediaType.APPLICATION_JSON)
107 @Produces(MediaType.APPLICATION_JSON)
108 public Response createPortPairGroup(InputStream stream) {
111 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
113 PortPairGroup portPairGroup = codec(PortPairGroup.class).decode(jsonTree, this);
114 Boolean issuccess = nullIsNotFound(service.createPortPairGroup(portPairGroup),
115 PORT_PAIR_GROUP_NOT_FOUND);
116 return Response.status(OK).entity(issuccess.toString()).build();
117 } catch (IOException e) {
118 log.error("Exception while creating port pair group {}.", e.toString());
119 throw new IllegalArgumentException(e);
124 * Update details of a specified port pair group id.
126 * @param id port pair group id
127 * @param stream port pair group from json
128 * @return 200 OK, 404 if given identifier does not exist
132 @Produces(MediaType.APPLICATION_JSON)
133 @Consumes(MediaType.APPLICATION_JSON)
134 public Response updatePortPairGroup(@PathParam("group_id") String id,
135 final InputStream stream) {
137 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
138 PortPairGroup portPairGroup = codec(PortPairGroup.class).decode(jsonTree, this);
139 Boolean isSuccess = nullIsNotFound(service.updatePortPairGroup(portPairGroup), PORT_PAIR_GROUP_NOT_FOUND);
140 return Response.status(OK).entity(isSuccess.toString()).build();
141 } catch (IOException e) {
142 log.error("Update port pair group failed because of exception {}.", e.toString());
143 throw new IllegalArgumentException(e);
148 * Delete details of a specified port pair group id.
150 * @param id port pair group id
154 public void deletePortPairGroup(@PathParam("group_id") String id) {
155 log.debug("Deletes port pair group by identifier {}.", id);
156 PortPairGroupId portPairGroupId = PortPairGroupId.of(id);
157 Boolean issuccess = nullIsNotFound(service.removePortPairGroup(portPairGroupId),
158 PORT_PAIR_GROUP_NOT_FOUND);
160 log.debug("Port pair group identifier {} does not exist", id);