2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.vtnweb.resources;
18 import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
19 import static org.onlab.util.Tools.nullIsNotFound;
20 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
22 import java.io.IOException;
23 import java.io.InputStream;
25 import java.net.URISyntaxException;
26 import java.util.UUID;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
39 import org.onosproject.vtnrsc.FlowClassifier;
40 import org.onosproject.vtnrsc.FlowClassifierId;
41 import org.onosproject.rest.AbstractWebResource;
42 import org.onosproject.vtnrsc.flowClassifier.FlowClassifierService;
43 import org.onosproject.vtnweb.web.FlowClassifierCodec;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import com.fasterxml.jackson.databind.node.ObjectNode;
49 * Query and program flow classifier.
51 @Path("flow_classifiers")
52 public class FlowClassifierWebResource extends AbstractWebResource {
54 final FlowClassifierService service = get(FlowClassifierService.class);
55 final ObjectNode root = mapper().createObjectNode();
56 public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found";
59 * Get all flow classifiers created. Returns list of all flow classifiers
65 @Produces(MediaType.APPLICATION_JSON)
66 public Response getFlowClassifiers() {
67 Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers();
68 ObjectNode result = new ObjectMapper().createObjectNode();
69 result.set("flow_classifiers", new FlowClassifierCodec().encode(flowClassifiers, this));
70 return ok(result.toString()).build();
74 * Get details of a flow classifier. Returns details of a specified flow
77 * @param id flow classifier id
82 @Produces(MediaType.APPLICATION_JSON)
83 public Response getFlowClassifier(@PathParam("flow_id") String id) {
85 if (!service.hasFlowClassifier(FlowClassifierId.of(UUID.fromString(id)))) {
86 return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build();
88 FlowClassifier flowClassifier = nullIsNotFound(
89 service.getFlowClassifier(FlowClassifierId.of(UUID.fromString(id))),
90 FLOW_CLASSIFIER_NOT_FOUND);
92 ObjectNode result = new ObjectMapper().createObjectNode();
93 result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this));
94 return ok(result.toString()).build();
98 * Creates and stores a new flow classifier.
100 * @param flowClassifierId flow classifier identifier
101 * @param stream flow classifier from JSON
102 * @return status of the request - CREATED if the JSON is correct,
103 * BAD_REQUEST if the JSON is invalid
107 @Consumes(MediaType.APPLICATION_JSON)
108 @Produces(MediaType.APPLICATION_JSON)
109 public Response createFlowClassifier(@PathParam("flow_id") String flowClassifierId, InputStream stream) {
112 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
114 FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this);
115 service.createFlowClassifier(flowClassifier);
116 location = new URI(flowClassifierId);
117 } catch (IOException | URISyntaxException ex) {
118 throw new IllegalArgumentException(ex);
120 return Response.created(location).build();
124 * Creates and stores a new flow classifier.
126 * @param stream flow classifier from JSON
127 * @return status of the request - CREATED if the JSON is correct,
128 * BAD_REQUEST if the JSON is invalid
131 @Consumes(MediaType.APPLICATION_JSON)
132 @Produces(MediaType.APPLICATION_JSON)
133 public Response createFlowClassifier(InputStream stream) {
136 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
138 FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this);
139 service.createFlowClassifier(flowClassifier);
140 location = new URI(flowClassifier.flowClassifierId().toString());
141 } catch (IOException | URISyntaxException ex) {
142 throw new IllegalArgumentException(ex);
144 return Response.created(location).build();
148 * Update details of a flow classifier. Update details of a specified flow
151 * @param id flow classifier id
152 * @param stream InputStream
157 @Produces(MediaType.APPLICATION_JSON)
158 @Consumes(MediaType.APPLICATION_JSON)
159 public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) {
161 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
162 FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this);
163 Boolean result = nullIsNotFound(service.updateFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND);
165 return Response.status(204).entity(FLOW_CLASSIFIER_NOT_FOUND).build();
167 return Response.status(203).entity(result.toString()).build();
168 } catch (Exception e) {
169 return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build();
174 * Delete details of a flow classifier. Delete details of a specified flow
177 * @param id flow classifier id
179 * @throws IOException when input doesn't match.
183 public Response deleteFlowClassifier(@PathParam("flow_id") String id) throws IOException {
185 FlowClassifierId flowClassifierId = FlowClassifierId.of(UUID.fromString(id));
186 service.removeFlowClassifier(flowClassifierId);
187 return Response.status(201).entity("SUCCESS").build();
188 } catch (Exception e) {
189 return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build();