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.PortPair;
39 import org.onosproject.vtnrsc.PortPairId;
40 import org.onosproject.vtnrsc.portpair.PortPairService;
41 import org.onosproject.vtnweb.web.PortPairCodec;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.node.ArrayNode;
48 import com.fasterxml.jackson.databind.node.ObjectNode;
51 * Query and program port pair.
54 public class PortPairWebResource extends AbstractWebResource {
56 private final Logger log = LoggerFactory.getLogger(PortPairWebResource.class);
57 private final PortPairService service = get(PortPairService.class);
58 public static final String PORT_PAIR_NOT_FOUND = "Port pair not found";
59 public static final String PORT_PAIR_ID_EXIST = "Port pair exists";
60 public static final String PORT_PAIR_ID_NOT_EXIST = "Port pair does not exist with identifier";
63 * Get details of all port pairs created.
68 @Produces(MediaType.APPLICATION_JSON)
69 public Response getPortPairs() {
70 Iterable<PortPair> portPairs = service.getPortPairs();
71 ObjectNode result = new ObjectMapper().createObjectNode();
72 ArrayNode portPairEntry = result.putArray("port_pairs");
73 if (portPairs != null) {
74 for (final PortPair portPair : portPairs) {
75 portPairEntry.add(new PortPairCodec().encode(portPair, this));
78 return ok(result.toString()).build();
82 * Get details of a specified port pair id.
84 * @param id port pair id
85 * @return 200 OK, 404 if given identifier does not exist
89 @Produces(MediaType.APPLICATION_JSON)
90 public Response getPortPair(@PathParam("pair_id") String id) {
92 if (!service.exists(PortPairId.of(id))) {
93 return Response.status(NOT_FOUND).entity(PORT_PAIR_NOT_FOUND).build();
95 PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.of(id)), PORT_PAIR_NOT_FOUND);
96 ObjectNode result = new ObjectMapper().createObjectNode();
97 result.set("port_pair", new PortPairCodec().encode(portPair, this));
98 return ok(result.toString()).build();
102 * Creates a new port pair.
104 * @param stream port pair from JSON
105 * @return status of the request - CREATED if the JSON is correct,
106 * BAD_REQUEST if the JSON is invalid
109 @Consumes(MediaType.APPLICATION_JSON)
110 @Produces(MediaType.APPLICATION_JSON)
111 public Response createPortPair(InputStream stream) {
113 ObjectMapper mapper = new ObjectMapper();
114 ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
115 JsonNode port = jsonTree.get("port_pair");
116 PortPair portPair = new PortPairCodec().decode((ObjectNode) port, this);
117 Boolean isSuccess = nullIsNotFound(service.createPortPair(portPair), PORT_PAIR_NOT_FOUND);
118 return Response.status(OK).entity(isSuccess.toString()).build();
119 } catch (IOException e) {
120 log.error("Exception while creating port pair {}.", e.toString());
121 throw new IllegalArgumentException(e);
126 * Update details of a specified port pair id.
128 * @param id port pair id
129 * @param stream port pair from json
130 * @return 200 OK, 404 if the given identifier does not exist
134 @Produces(MediaType.APPLICATION_JSON)
135 @Consumes(MediaType.APPLICATION_JSON)
136 public Response updatePortPair(@PathParam("pair_id") String id,
137 final InputStream stream) {
139 ObjectMapper mapper = new ObjectMapper();
140 ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
141 JsonNode port = jsonTree.get("port_pair");
142 PortPair portPair = new PortPairCodec().decode((ObjectNode) port, this);
143 Boolean isSuccess = nullIsNotFound(service.updatePortPair(portPair), PORT_PAIR_NOT_FOUND);
144 return Response.status(OK).entity(isSuccess.toString()).build();
145 } catch (IOException e) {
146 log.error("Update port pair failed because of exception {}.", e.toString());
147 throw new IllegalArgumentException(e);
152 * Delete details of a specified port pair id.
154 * @param id port pair id
158 public void deletePortPair(@PathParam("pair_id") String id) {
160 PortPairId portPairId = PortPairId.of(id);
161 Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), PORT_PAIR_NOT_FOUND);
163 log.debug("Port pair identifier {} does not exist", id);