blob: 9194c14f7bc47a273a1d143d96aee8e5554649b3 [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
45import com.fasterxml.jackson.databind.ObjectMapper;
46import 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);
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";
59
60 /**
61 * Get details of all port pairs created.
62 *
63 * @return 200 OK
64 */
65 @GET
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();
72 }
73
74 /**
75 * Get details of a specified port pair id.
76 *
77 * @param id port pair id
78 * @return 200 OK, 404 if given identifier does not exist
79 */
80 @GET
81 @Path("{pair_id}")
82 @Produces(MediaType.APPLICATION_JSON)
83 public Response getPortPair(@PathParam("portPairId") String id) {
84
85 if (!service.exists(PortPairId.portPairId(id))) {
86 return Response.status(NOT_FOUND)
87 .entity(PORT_PAIR_NOT_FOUND).build();
88 }
89 PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.portPairId(id)),
90 PORT_PAIR_NOT_FOUND);
91
92 ObjectNode result = new ObjectMapper().createObjectNode();
93 result.set("port_pair", new PortPairCodec().encode(portPair, this));
94 return ok(result).build();
95 }
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 {
109 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
110
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);
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 {
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);
142 }
143 }
144
145 /**
146 * Delete details of a specified port pair id.
147 *
148 * @param id port pair id
149 */
150 @Path("{pair_id}")
151 @DELETE
152 public void deletePortPair(@PathParam("pair_id") String id) {
153
154 PortPairId portPairId = PortPairId.portPairId(id);
155 Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId),
156 PORT_PAIR_NOT_FOUND);
157 if (!isSuccess) {
158 log.debug("Port pair identifier {} does not exist", id);
159 }
160 }
161}