blob: b9012898ef68a9fc49339034f72b96077d9e7e5f [file] [log] [blame]
Phaneendra Manda052d56f2015-10-29 16:52:46 +05301/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
19import static javax.ws.rs.core.Response.Status.NOT_FOUND;
20import static javax.ws.rs.core.Response.Status.OK;
21import static org.onlab.util.Tools.nullIsNotFound;
22
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;
41import org.onosproject.vtnweb.web.PortPairCodec;
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
Phaneendra Manda63d24702015-11-14 14:56:42 +053045import com.fasterxml.jackson.databind.JsonNode;
Phaneendra Manda052d56f2015-10-29 16:52:46 +053046import com.fasterxml.jackson.databind.ObjectMapper;
Phaneendra Manda63d24702015-11-14 14:56:42 +053047import com.fasterxml.jackson.databind.node.ArrayNode;
Phaneendra Manda052d56f2015-10-29 16:52:46 +053048import com.fasterxml.jackson.databind.node.ObjectNode;
49
50/**
51 * Query and program port pair.
52 */
53@Path("port_pairs")
54public class PortPairWebResource extends AbstractWebResource {
55
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";
61
62 /**
63 * Get details of all port pairs created.
64 *
65 * @return 200 OK
66 */
67 @GET
68 @Produces(MediaType.APPLICATION_JSON)
69 public Response getPortPairs() {
70 Iterable<PortPair> portPairs = service.getPortPairs();
71 ObjectNode result = new ObjectMapper().createObjectNode();
Phaneendra Manda63d24702015-11-14 14:56:42 +053072 ArrayNode portPairEntry = result.putArray("port_pairs");
73 if (portPairs != null) {
74 for (final PortPair portPair : portPairs) {
75 portPairEntry.add(new PortPairCodec().encode(portPair, this));
76 }
77 }
78 return ok(result.toString()).build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +053079 }
80
81 /**
82 * Get details of a specified port pair id.
83 *
84 * @param id port pair id
85 * @return 200 OK, 404 if given identifier does not exist
86 */
87 @GET
88 @Path("{pair_id}")
89 @Produces(MediaType.APPLICATION_JSON)
Phaneendra Manda63d24702015-11-14 14:56:42 +053090 public Response getPortPair(@PathParam("pair_id") String id) {
Phaneendra Manda052d56f2015-10-29 16:52:46 +053091
Mahesh Poojary Huaweid0b1d132015-11-05 11:00:18 +053092 if (!service.exists(PortPairId.of(id))) {
Phaneendra Manda63d24702015-11-14 14:56:42 +053093 return Response.status(NOT_FOUND).entity(PORT_PAIR_NOT_FOUND).build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +053094 }
Phaneendra Manda63d24702015-11-14 14:56:42 +053095 PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.of(id)), PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +053096 ObjectNode result = new ObjectMapper().createObjectNode();
97 result.set("port_pair", new PortPairCodec().encode(portPair, this));
Phaneendra Manda63d24702015-11-14 14:56:42 +053098 return ok(result.toString()).build();
Phaneendra Manda052d56f2015-10-29 16:52:46 +053099 }
100
101 /**
102 * Creates a new port pair.
103 *
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
107 */
108 @POST
109 @Consumes(MediaType.APPLICATION_JSON)
110 @Produces(MediaType.APPLICATION_JSON)
111 public Response createPortPair(InputStream stream) {
112 try {
Phaneendra Manda63d24702015-11-14 14:56:42 +0530113 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);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530118 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);
122 }
123 }
124
125 /**
126 * Update details of a specified port pair id.
127 *
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
131 */
132 @PUT
133 @Path("{pair_id}")
134 @Produces(MediaType.APPLICATION_JSON)
135 @Consumes(MediaType.APPLICATION_JSON)
136 public Response updatePortPair(@PathParam("pair_id") String id,
137 final InputStream stream) {
138 try {
Phaneendra Manda63d24702015-11-14 14:56:42 +0530139 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);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530143 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);
148 }
149 }
150
151 /**
152 * Delete details of a specified port pair id.
153 *
154 * @param id port pair id
155 */
156 @Path("{pair_id}")
157 @DELETE
158 public void deletePortPair(@PathParam("pair_id") String id) {
159
Mahesh Poojary Huaweid0b1d132015-11-05 11:00:18 +0530160 PortPairId portPairId = PortPairId.of(id);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530161 Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), PORT_PAIR_NOT_FOUND);
Phaneendra Manda052d56f2015-10-29 16:52:46 +0530162 if (!isSuccess) {
163 log.debug("Port pair identifier {} does not exist", id);
164 }
165 }
166}