blob: 96cf9ac3d9909da7c0afc2143a6d4ba9ed80d618 [file] [log] [blame]
Phaneendra Manda052d56f2015-10-29 16:52:46 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Phaneendra Manda052d56f2015-10-29 16:52:46 +05303 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package org.onosproject.vtnweb.resources;
18
Phaneendra Manda052d56f2015-10-29 16:52:46 +053019import static javax.ws.rs.core.Response.Status.OK;
20import static org.onlab.util.Tools.nullIsNotFound;
21
22import java.io.IOException;
23import java.io.InputStream;
24
25import javax.ws.rs.Consumes;
26import javax.ws.rs.DELETE;
27import javax.ws.rs.GET;
28import javax.ws.rs.POST;
29import javax.ws.rs.PUT;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35
36import org.onosproject.rest.AbstractWebResource;
37import org.onosproject.vtnrsc.PortPair;
38import org.onosproject.vtnrsc.PortPairId;
39import org.onosproject.vtnrsc.portpair.PortPairService;
Phaneendra Manda052d56f2015-10-29 16:52:46 +053040import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
42
Phaneendra Manda63d24702015-11-14 14:56:42 +053043import com.fasterxml.jackson.databind.JsonNode;
Phaneendra Manda63d24702015-11-14 14:56:42 +053044import com.fasterxml.jackson.databind.node.ArrayNode;
Phaneendra Manda052d56f2015-10-29 16:52:46 +053045import com.fasterxml.jackson.databind.node.ObjectNode;
46
47/**
48 * Query and program port pair.
49 */
50@Path("port_pairs")
51public class PortPairWebResource extends AbstractWebResource {
52
53 private final Logger log = LoggerFactory.getLogger(PortPairWebResource.class);
Phaneendra Manda052d56f2015-10-29 16:52:46 +053054 public static final String PORT_PAIR_NOT_FOUND = "Port pair not found";
55 public static final String PORT_PAIR_ID_EXIST = "Port pair exists";
56 public static final String PORT_PAIR_ID_NOT_EXIST = "Port pair does not exist with identifier";
57
58 /**
59 * Get details of all port pairs created.
60 *
61 * @return 200 OK
62 */
63 @GET
64 @Produces(MediaType.APPLICATION_JSON)
Wu wenbind0b119f2016-05-11 18:03:41 +080065 @Consumes(MediaType.APPLICATION_JSON)
Phaneendra Manda052d56f2015-10-29 16:52:46 +053066 public Response getPortPairs() {
Phaneendra Manda568734d2015-12-01 20:30:39 +053067 Iterable<PortPair> portPairs = get(PortPairService.class).getPortPairs();
68 ObjectNode result = mapper().createObjectNode();
Phaneendra Manda63d24702015-11-14 14:56:42 +053069 ArrayNode portPairEntry = result.putArray("port_pairs");
70 if (portPairs != null) {
71 for (final PortPair portPair : portPairs) {
Phaneendra Manda568734d2015-12-01 20:30:39 +053072 portPairEntry.add(codec(PortPair.class).encode(portPair, this));
Phaneendra Manda63d24702015-11-14 14:56:42 +053073 }
74 }
75 return ok(result.toString()).build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +053076 }
77
78 /**
79 * Get details of a specified port pair id.
80 *
81 * @param id port pair id
82 * @return 200 OK, 404 if given identifier does not exist
83 */
84 @GET
85 @Path("{pair_id}")
86 @Produces(MediaType.APPLICATION_JSON)
Wu wenbind0b119f2016-05-11 18:03:41 +080087 @Consumes(MediaType.APPLICATION_JSON)
Phaneendra Manda63d24702015-11-14 14:56:42 +053088 public Response getPortPair(@PathParam("pair_id") String id) {
Phaneendra Manda568734d2015-12-01 20:30:39 +053089 PortPair portPair = nullIsNotFound(get(PortPairService.class).getPortPair(PortPairId.of(id)),
90 PORT_PAIR_NOT_FOUND);
91 ObjectNode result = mapper().createObjectNode();
92 result.set("port_pair", codec(PortPair.class).encode(portPair, this));
Phaneendra Manda63d24702015-11-14 14:56:42 +053093 return ok(result.toString()).build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +053094 }
95
96 /**
97 * Creates a new port pair.
98 *
99 * @param stream port pair from JSON
100 * @return status of the request - CREATED if the JSON is correct,
101 * BAD_REQUEST if the JSON is invalid
102 */
103 @POST
104 @Consumes(MediaType.APPLICATION_JSON)
105 @Produces(MediaType.APPLICATION_JSON)
106 public Response createPortPair(InputStream stream) {
107 try {
Phaneendra Manda568734d2015-12-01 20:30:39 +0530108 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530109 JsonNode port = jsonTree.get("port_pair");
Phaneendra Manda568734d2015-12-01 20:30:39 +0530110 PortPair portPair = codec(PortPair.class).decode((ObjectNode) port, this);
111 Boolean isSuccess = nullIsNotFound(get(PortPairService.class).createPortPair(portPair),
112 PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530113 return Response.status(OK).entity(isSuccess.toString()).build();
114 } catch (IOException e) {
115 log.error("Exception while creating port pair {}.", e.toString());
116 throw new IllegalArgumentException(e);
117 }
118 }
119
120 /**
121 * Update details of a specified port pair id.
122 *
123 * @param id port pair id
124 * @param stream port pair from json
125 * @return 200 OK, 404 if the given identifier does not exist
126 */
127 @PUT
128 @Path("{pair_id}")
129 @Produces(MediaType.APPLICATION_JSON)
130 @Consumes(MediaType.APPLICATION_JSON)
131 public Response updatePortPair(@PathParam("pair_id") String id,
132 final InputStream stream) {
133 try {
Phaneendra Manda568734d2015-12-01 20:30:39 +0530134 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530135 JsonNode port = jsonTree.get("port_pair");
Phaneendra Manda568734d2015-12-01 20:30:39 +0530136 PortPair portPair = codec(PortPair.class).decode((ObjectNode) port, this);
137 Boolean isSuccess = nullIsNotFound(get(PortPairService.class).updatePortPair(portPair),
138 PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530139 return Response.status(OK).entity(isSuccess.toString()).build();
140 } catch (IOException e) {
141 log.error("Update port pair failed because of exception {}.", e.toString());
142 throw new IllegalArgumentException(e);
143 }
144 }
145
146 /**
147 * Delete details of a specified port pair id.
148 *
149 * @param id port pair id
Jian Lic2a542b2016-05-10 11:48:19 -0700150 * @return 204 NO CONTENT
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530151 */
152 @Path("{pair_id}")
153 @DELETE
Wu wenbind0b119f2016-05-11 18:03:41 +0800154 @Produces(MediaType.APPLICATION_JSON)
155 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700156 public Response deletePortPair(@PathParam("pair_id") String id) {
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530157
Mahesh Poojary Huaweid0b1d132015-11-05 11:00:18 +0530158 PortPairId portPairId = PortPairId.of(id);
Phaneendra Manda568734d2015-12-01 20:30:39 +0530159 Boolean isSuccess = nullIsNotFound(get(PortPairService.class).removePortPair(portPairId), PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530160 if (!isSuccess) {
161 log.debug("Port pair identifier {} does not exist", id);
162 }
Jian Lic2a542b2016-05-10 11:48:19 -0700163 return Response.noContent().build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530164 }
165}