blob: 2c88f6fe9adef975e02e52f7ddd47348ea05a8b7 [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;
Ray Milkey86ee5e82018-04-02 15:33:07 -070021import static org.onlab.util.Tools.readTreeFromStream;
Phaneendra Manda052d56f2015-10-29 16:52:46 +053022
23import java.io.IOException;
24import java.io.InputStream;
25
26import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
28import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.PUT;
31import javax.ws.rs.Path;
32import javax.ws.rs.PathParam;
33import javax.ws.rs.Produces;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36
37import org.onosproject.rest.AbstractWebResource;
38import org.onosproject.vtnrsc.PortPair;
39import org.onosproject.vtnrsc.PortPairId;
40import org.onosproject.vtnrsc.portpair.PortPairService;
Phaneendra Manda052d56f2015-10-29 16:52:46 +053041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
Phaneendra Manda63d24702015-11-14 14:56:42 +053044import com.fasterxml.jackson.databind.JsonNode;
Phaneendra Manda63d24702015-11-14 14:56:42 +053045import com.fasterxml.jackson.databind.node.ArrayNode;
Phaneendra Manda052d56f2015-10-29 16:52:46 +053046import com.fasterxml.jackson.databind.node.ObjectNode;
47
48/**
49 * Query and program port pair.
50 */
51@Path("port_pairs")
52public class PortPairWebResource extends AbstractWebResource {
53
54 private final Logger log = LoggerFactory.getLogger(PortPairWebResource.class);
Phaneendra Manda052d56f2015-10-29 16:52:46 +053055 public static final String PORT_PAIR_NOT_FOUND = "Port pair not found";
56 public static final String PORT_PAIR_ID_EXIST = "Port pair exists";
57 public static final String PORT_PAIR_ID_NOT_EXIST = "Port pair does not exist with identifier";
58
59 /**
60 * Get details of all port pairs created.
61 *
62 * @return 200 OK
63 */
64 @GET
65 @Produces(MediaType.APPLICATION_JSON)
Wu wenbind0b119f2016-05-11 18:03:41 +080066 @Consumes(MediaType.APPLICATION_JSON)
Phaneendra Manda052d56f2015-10-29 16:52:46 +053067 public Response getPortPairs() {
Phaneendra Manda568734d2015-12-01 20:30:39 +053068 Iterable<PortPair> portPairs = get(PortPairService.class).getPortPairs();
69 ObjectNode result = mapper().createObjectNode();
Phaneendra Manda63d24702015-11-14 14:56:42 +053070 ArrayNode portPairEntry = result.putArray("port_pairs");
71 if (portPairs != null) {
72 for (final PortPair portPair : portPairs) {
Phaneendra Manda568734d2015-12-01 20:30:39 +053073 portPairEntry.add(codec(PortPair.class).encode(portPair, this));
Phaneendra Manda63d24702015-11-14 14:56:42 +053074 }
75 }
76 return ok(result.toString()).build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +053077 }
78
79 /**
80 * Get details of a specified port pair id.
81 *
82 * @param id port pair id
83 * @return 200 OK, 404 if given identifier does not exist
84 */
85 @GET
86 @Path("{pair_id}")
87 @Produces(MediaType.APPLICATION_JSON)
Wu wenbind0b119f2016-05-11 18:03:41 +080088 @Consumes(MediaType.APPLICATION_JSON)
Phaneendra Manda63d24702015-11-14 14:56:42 +053089 public Response getPortPair(@PathParam("pair_id") String id) {
Phaneendra Manda568734d2015-12-01 20:30:39 +053090 PortPair portPair = nullIsNotFound(get(PortPairService.class).getPortPair(PortPairId.of(id)),
91 PORT_PAIR_NOT_FOUND);
92 ObjectNode result = mapper().createObjectNode();
93 result.set("port_pair", codec(PortPair.class).encode(portPair, this));
Phaneendra Manda63d24702015-11-14 14:56:42 +053094 return ok(result.toString()).build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +053095 }
96
97 /**
98 * Creates a new port pair.
99 *
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
103 */
104 @POST
105 @Consumes(MediaType.APPLICATION_JSON)
106 @Produces(MediaType.APPLICATION_JSON)
107 public Response createPortPair(InputStream stream) {
108 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700109 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530110 JsonNode port = jsonTree.get("port_pair");
Phaneendra Manda568734d2015-12-01 20:30:39 +0530111 PortPair portPair = codec(PortPair.class).decode((ObjectNode) port, this);
112 Boolean isSuccess = nullIsNotFound(get(PortPairService.class).createPortPair(portPair),
113 PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530114 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);
118 }
119 }
120
121 /**
122 * Update details of a specified port pair id.
123 *
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
127 */
128 @PUT
129 @Path("{pair_id}")
130 @Produces(MediaType.APPLICATION_JSON)
131 @Consumes(MediaType.APPLICATION_JSON)
132 public Response updatePortPair(@PathParam("pair_id") String id,
133 final InputStream stream) {
134 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700135 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530136 JsonNode port = jsonTree.get("port_pair");
Phaneendra Manda568734d2015-12-01 20:30:39 +0530137 PortPair portPair = codec(PortPair.class).decode((ObjectNode) port, this);
138 Boolean isSuccess = nullIsNotFound(get(PortPairService.class).updatePortPair(portPair),
139 PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530140 return Response.status(OK).entity(isSuccess.toString()).build();
141 } catch (IOException e) {
142 log.error("Update port pair failed because of exception {}.", e.toString());
143 throw new IllegalArgumentException(e);
144 }
145 }
146
147 /**
148 * Delete details of a specified port pair id.
149 *
150 * @param id port pair id
Jian Lic2a542b2016-05-10 11:48:19 -0700151 * @return 204 NO CONTENT
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530152 */
153 @Path("{pair_id}")
154 @DELETE
Wu wenbind0b119f2016-05-11 18:03:41 +0800155 @Produces(MediaType.APPLICATION_JSON)
156 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700157 public Response deletePortPair(@PathParam("pair_id") String id) {
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530158
Mahesh Poojary Huaweid0b1d132015-11-05 11:00:18 +0530159 PortPairId portPairId = PortPairId.of(id);
Phaneendra Manda568734d2015-12-01 20:30:39 +0530160 Boolean isSuccess = nullIsNotFound(get(PortPairService.class).removePortPair(portPairId), PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530161 if (!isSuccess) {
162 log.debug("Port pair identifier {} does not exist", id);
163 }
Jian Lic2a542b2016-05-10 11:48:19 -0700164 return Response.noContent().build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530165 }
166}