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.OK;
19 import static org.onlab.util.Tools.nullIsNotFound;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.DELETE;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.PUT;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.Response;
35 import org.onosproject.rest.AbstractWebResource;
36 import org.onosproject.vtnrsc.FlowClassifier;
37 import org.onosproject.vtnrsc.FlowClassifierId;
38 import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
39 import org.onosproject.vtnweb.web.FlowClassifierCodec;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45 import com.fasterxml.jackson.databind.node.ArrayNode;
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 private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class);
56 public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found";
59 * Get all flow classifiers created.
64 @Produces(MediaType.APPLICATION_JSON)
65 public Response getFlowClassifiers() {
66 Iterable<FlowClassifier> flowClassifiers = get(FlowClassifierService.class).getFlowClassifiers();
67 ObjectNode result = new ObjectMapper().createObjectNode();
68 ArrayNode flowClassifierEntry = result.putArray("flow_classifiers");
69 if (flowClassifiers != null) {
70 for (final FlowClassifier flowClassifier : flowClassifiers) {
71 flowClassifierEntry.add(new FlowClassifierCodec().encode(flowClassifier, this));
74 return ok(result.toString()).build();
78 * Get details of a flow classifier.
82 * @return 200 OK , 404 if given identifier does not exist
86 @Produces(MediaType.APPLICATION_JSON)
87 public Response getFlowClassifier(@PathParam("flow_id") String id) {
88 FlowClassifier flowClassifier = nullIsNotFound(
89 get(FlowClassifierService.class).getFlowClassifier(FlowClassifierId.of(id)), FLOW_CLASSIFIER_NOT_FOUND);
91 ObjectNode result = new ObjectMapper().createObjectNode();
92 result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this));
94 return ok(result.toString()).build();
98 * Creates and stores a new flow classifier.
101 * flow classifier from JSON
102 * @return status of the request - CREATED if the JSON is correct,
103 * BAD_REQUEST if the JSON is invalid
106 @Consumes(MediaType.APPLICATION_JSON)
107 @Produces(MediaType.APPLICATION_JSON)
108 public Response createFlowClassifier(InputStream stream) {
110 ObjectMapper mapper = new ObjectMapper();
111 ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
112 JsonNode flow = jsonTree.get("flow_classifier");
114 FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this);
115 Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).createFlowClassifier(flowClassifier),
116 FLOW_CLASSIFIER_NOT_FOUND);
117 return Response.status(OK).entity(issuccess.toString()).build();
118 } catch (IOException ex) {
119 log.error("Exception while creating flow classifier {}.", ex.toString());
120 throw new IllegalArgumentException(ex);
125 * Update details of a flow classifier.
131 * @return 200 OK, 404 if given identifier does not exist
135 @Produces(MediaType.APPLICATION_JSON)
136 @Consumes(MediaType.APPLICATION_JSON)
137 public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) {
140 JsonNode jsonTree = mapper().readTree(stream);
141 JsonNode flow = jsonTree.get("flow_classifier");
142 FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this);
143 Boolean result = nullIsNotFound(get(FlowClassifierService.class).updateFlowClassifier(flowClassifier),
144 FLOW_CLASSIFIER_NOT_FOUND);
145 return Response.status(OK).entity(result.toString()).build();
146 } catch (IOException e) {
147 log.error("Update flow classifier failed because of exception {}.", e.toString());
148 throw new IllegalArgumentException(e);
153 * Delete details of a flow classifier.
160 public void deleteFlowClassifier(@PathParam("flow_id") String id) {
161 log.debug("Deletes flow classifier by identifier {}.", id);
162 FlowClassifierId flowClassifierId = FlowClassifierId.of(id);
163 Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).removeFlowClassifier(flowClassifierId),
164 FLOW_CLASSIFIER_NOT_FOUND);