b9012898ef68a9fc49339034f72b96077d9e7e5f
[onosfw.git] /
1 /*
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
17 package org.onosproject.vtnweb.resources;
18
19 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
20 import static javax.ws.rs.core.Response.Status.OK;
21 import static org.onlab.util.Tools.nullIsNotFound;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36
37 import org.onosproject.rest.AbstractWebResource;
38 import org.onosproject.vtnrsc.PortPair;
39 import org.onosproject.vtnrsc.PortPairId;
40 import org.onosproject.vtnrsc.portpair.PortPairService;
41 import org.onosproject.vtnweb.web.PortPairCodec;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.node.ArrayNode;
48 import com.fasterxml.jackson.databind.node.ObjectNode;
49
50 /**
51  * Query and program port pair.
52  */
53 @Path("port_pairs")
54 public 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();
72         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();
79     }
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)
90     public Response getPortPair(@PathParam("pair_id") String id) {
91
92         if (!service.exists(PortPairId.of(id))) {
93             return Response.status(NOT_FOUND).entity(PORT_PAIR_NOT_FOUND).build();
94         }
95         PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.of(id)), PORT_PAIR_NOT_FOUND);
96         ObjectNode result = new ObjectMapper().createObjectNode();
97         result.set("port_pair", new PortPairCodec().encode(portPair, this));
98         return ok(result.toString()).build();
99     }
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 {
113             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);
118             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 {
139             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);
143             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
160         PortPairId portPairId = PortPairId.of(id);
161         Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), PORT_PAIR_NOT_FOUND);
162         if (!isSuccess) {
163             log.debug("Port pair identifier {} does not exist", id);
164         }
165     }
166 }