blob: 5b4945cbd3cb85e5d6c339ba9ee75685e0852ce7 [file] [log] [blame]
Phaneendra Manda052d56f2015-10-29 16:52:46 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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)
65 public Response getPortPairs() {
Phaneendra Manda568734d2015-12-01 20:30:39 +053066 Iterable<PortPair> portPairs = get(PortPairService.class).getPortPairs();
67 ObjectNode result = mapper().createObjectNode();
Phaneendra Manda63d24702015-11-14 14:56:42 +053068 ArrayNode portPairEntry = result.putArray("port_pairs");
69 if (portPairs != null) {
70 for (final PortPair portPair : portPairs) {
Phaneendra Manda568734d2015-12-01 20:30:39 +053071 portPairEntry.add(codec(PortPair.class).encode(portPair, this));
Phaneendra Manda63d24702015-11-14 14:56:42 +053072 }
73 }
74 return ok(result.toString()).build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +053075 }
76
77 /**
78 * Get details of a specified port pair id.
79 *
80 * @param id port pair id
81 * @return 200 OK, 404 if given identifier does not exist
82 */
83 @GET
84 @Path("{pair_id}")
85 @Produces(MediaType.APPLICATION_JSON)
Phaneendra Manda63d24702015-11-14 14:56:42 +053086 public Response getPortPair(@PathParam("pair_id") String id) {
Phaneendra Manda568734d2015-12-01 20:30:39 +053087 PortPair portPair = nullIsNotFound(get(PortPairService.class).getPortPair(PortPairId.of(id)),
88 PORT_PAIR_NOT_FOUND);
89 ObjectNode result = mapper().createObjectNode();
90 result.set("port_pair", codec(PortPair.class).encode(portPair, this));
Phaneendra Manda63d24702015-11-14 14:56:42 +053091 return ok(result.toString()).build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +053092 }
93
94 /**
95 * Creates a new port pair.
96 *
97 * @param stream port pair from JSON
98 * @return status of the request - CREATED if the JSON is correct,
99 * BAD_REQUEST if the JSON is invalid
100 */
101 @POST
102 @Consumes(MediaType.APPLICATION_JSON)
103 @Produces(MediaType.APPLICATION_JSON)
104 public Response createPortPair(InputStream stream) {
105 try {
Phaneendra Manda568734d2015-12-01 20:30:39 +0530106 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530107 JsonNode port = jsonTree.get("port_pair");
Phaneendra Manda568734d2015-12-01 20:30:39 +0530108 PortPair portPair = codec(PortPair.class).decode((ObjectNode) port, this);
109 Boolean isSuccess = nullIsNotFound(get(PortPairService.class).createPortPair(portPair),
110 PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530111 return Response.status(OK).entity(isSuccess.toString()).build();
112 } catch (IOException e) {
113 log.error("Exception while creating port pair {}.", e.toString());
114 throw new IllegalArgumentException(e);
115 }
116 }
117
118 /**
119 * Update details of a specified port pair id.
120 *
121 * @param id port pair id
122 * @param stream port pair from json
123 * @return 200 OK, 404 if the given identifier does not exist
124 */
125 @PUT
126 @Path("{pair_id}")
127 @Produces(MediaType.APPLICATION_JSON)
128 @Consumes(MediaType.APPLICATION_JSON)
129 public Response updatePortPair(@PathParam("pair_id") String id,
130 final InputStream stream) {
131 try {
Phaneendra Manda568734d2015-12-01 20:30:39 +0530132 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530133 JsonNode port = jsonTree.get("port_pair");
Phaneendra Manda568734d2015-12-01 20:30:39 +0530134 PortPair portPair = codec(PortPair.class).decode((ObjectNode) port, this);
135 Boolean isSuccess = nullIsNotFound(get(PortPairService.class).updatePortPair(portPair),
136 PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530137 return Response.status(OK).entity(isSuccess.toString()).build();
138 } catch (IOException e) {
139 log.error("Update port pair failed because of exception {}.", e.toString());
140 throw new IllegalArgumentException(e);
141 }
142 }
143
144 /**
145 * Delete details of a specified port pair id.
146 *
147 * @param id port pair id
148 */
149 @Path("{pair_id}")
150 @DELETE
151 public void deletePortPair(@PathParam("pair_id") String id) {
152
Mahesh Poojary Huaweid0b1d132015-11-05 11:00:18 +0530153 PortPairId portPairId = PortPairId.of(id);
Phaneendra Manda568734d2015-12-01 20:30:39 +0530154 Boolean isSuccess = nullIsNotFound(get(PortPairService.class).removePortPair(portPairId), PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530155 if (!isSuccess) {
156 log.debug("Port pair identifier {} does not exist", id);
157 }
158 }
159}