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.ObjectMapper;
46 import com.fasterxml.jackson.databind.node.ObjectNode;
49 * Query and program port pair.
52 public class PortPairWebResource extends AbstractWebResource {
54 private final Logger log = LoggerFactory.getLogger(PortPairWebResource.class);
55 private final PortPairService service = get(PortPairService.class);
56 public static final String PORT_PAIR_NOT_FOUND = "Port pair not found";
57 public static final String PORT_PAIR_ID_EXIST = "Port pair exists";
58 public static final String PORT_PAIR_ID_NOT_EXIST = "Port pair does not exist with identifier";
61 * Get details of all port pairs created.
66 @Produces(MediaType.APPLICATION_JSON)
67 public Response getPortPairs() {
68 Iterable<PortPair> portPairs = service.getPortPairs();
69 ObjectNode result = new ObjectMapper().createObjectNode();
70 result.set("port_pairs", new PortPairCodec().encode(portPairs, this));
71 return ok(result).build();
75 * Get details of a specified port pair id.
77 * @param id port pair id
78 * @return 200 OK, 404 if given identifier does not exist
82 @Produces(MediaType.APPLICATION_JSON)
83 public Response getPortPair(@PathParam("portPairId") String id) {
85 if (!service.exists(PortPairId.of(id))) {
86 return Response.status(NOT_FOUND)
87 .entity(PORT_PAIR_NOT_FOUND).build();
89 PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.of(id)),
92 ObjectNode result = new ObjectMapper().createObjectNode();
93 result.set("port_pair", new PortPairCodec().encode(portPair, this));
94 return ok(result).build();
98 * Creates a new port pair.
100 * @param stream port pair from JSON
101 * @return status of the request - CREATED if the JSON is correct,
102 * BAD_REQUEST if the JSON is invalid
105 @Consumes(MediaType.APPLICATION_JSON)
106 @Produces(MediaType.APPLICATION_JSON)
107 public Response createPortPair(InputStream stream) {
109 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
111 PortPair portPair = codec(PortPair.class).decode(jsonTree, this);
112 Boolean isSuccess = nullIsNotFound(service.createPortPair(portPair),
113 PORT_PAIR_NOT_FOUND);
114 return Response.status(OK).entity(isSuccess.toString()).build();
115 } catch (IOException e) {
116 log.error("Exception while creating port pair {}.", e.toString());
117 throw new IllegalArgumentException(e);
122 * Update details of a specified port pair id.
124 * @param id port pair id
125 * @param stream port pair from json
126 * @return 200 OK, 404 if the given identifier does not exist
130 @Produces(MediaType.APPLICATION_JSON)
131 @Consumes(MediaType.APPLICATION_JSON)
132 public Response updatePortPair(@PathParam("pair_id") String id,
133 final InputStream stream) {
135 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
136 PortPair portPair = codec(PortPair.class).decode(jsonTree, this);
137 Boolean isSuccess = nullIsNotFound(service.updatePortPair(portPair), PORT_PAIR_NOT_FOUND);
138 return Response.status(OK).entity(isSuccess.toString()).build();
139 } catch (IOException e) {
140 log.error("Update port pair failed because of exception {}.", e.toString());
141 throw new IllegalArgumentException(e);
146 * Delete details of a specified port pair id.
148 * @param id port pair id
152 public void deletePortPair(@PathParam("pair_id") String id) {
154 PortPairId portPairId = PortPairId.of(id);
155 Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId),
156 PORT_PAIR_NOT_FOUND);
158 log.debug("Port pair identifier {} does not exist", id);