blob: 29e67c53a0d0bce669585b5bea8db4567039dbe8 [file] [log] [blame]
Bharat saraswald25a30a2015-10-26 18:35:47 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Bharat saraswald25a30a2015-10-26 18:35:47 +05303 *
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 */
16package org.onosproject.vtnweb.resources;
17
Bharat saraswalf38e2262015-11-18 22:57:05 +053018import static javax.ws.rs.core.Response.Status.OK;
19import static org.onlab.util.Tools.nullIsNotFound;
Bharat saraswald25a30a2015-10-26 18:35:47 +053020
21import java.io.IOException;
22import java.io.InputStream;
Bharat saraswald25a30a2015-10-26 18:35:47 +053023
24import javax.ws.rs.Consumes;
25import javax.ws.rs.DELETE;
26import javax.ws.rs.GET;
27import javax.ws.rs.POST;
28import javax.ws.rs.PUT;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34
Bharat saraswalf38e2262015-11-18 22:57:05 +053035import org.onosproject.rest.AbstractWebResource;
Bharat saraswald25a30a2015-10-26 18:35:47 +053036import org.onosproject.vtnrsc.FlowClassifier;
37import org.onosproject.vtnrsc.FlowClassifierId;
Jonathan Hart317f4762015-11-09 16:05:36 -080038import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
Bharat saraswalf38e2262015-11-18 22:57:05 +053039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
Bharat saraswald25a30a2015-10-26 18:35:47 +053041
Bharat saraswalf38e2262015-11-18 22:57:05 +053042import com.fasterxml.jackson.databind.JsonNode;
Bharat saraswalf38e2262015-11-18 22:57:05 +053043import com.fasterxml.jackson.databind.node.ArrayNode;
Bharat saraswald25a30a2015-10-26 18:35:47 +053044import com.fasterxml.jackson.databind.node.ObjectNode;
45
46/**
47 * Query and program flow classifier.
48 */
49@Path("flow_classifiers")
50public class FlowClassifierWebResource extends AbstractWebResource {
51
Bharat saraswalf38e2262015-11-18 22:57:05 +053052 private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class);
53
Bharat saraswald25a30a2015-10-26 18:35:47 +053054 public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found";
55
56 /**
Bharat saraswalf38e2262015-11-18 22:57:05 +053057 * Get all flow classifiers created.
Bharat saraswald25a30a2015-10-26 18:35:47 +053058 *
Bharat saraswala9929a92015-11-19 00:33:55 +053059 * @return 200 OK
Bharat saraswald25a30a2015-10-26 18:35:47 +053060 */
61 @GET
Wu wenbind0b119f2016-05-11 18:03:41 +080062 @Consumes(MediaType.APPLICATION_JSON)
Bharat saraswald25a30a2015-10-26 18:35:47 +053063 @Produces(MediaType.APPLICATION_JSON)
64 public Response getFlowClassifiers() {
Mahesh Poojary Sfac02262015-11-20 19:13:22 +053065 Iterable<FlowClassifier> flowClassifiers = get(FlowClassifierService.class).getFlowClassifiers();
Phaneendra Mandad66dca42015-12-01 20:24:10 +053066 ObjectNode result = mapper().createObjectNode();
Bharat saraswalf38e2262015-11-18 22:57:05 +053067 ArrayNode flowClassifierEntry = result.putArray("flow_classifiers");
68 if (flowClassifiers != null) {
69 for (final FlowClassifier flowClassifier : flowClassifiers) {
Phaneendra Mandad66dca42015-12-01 20:24:10 +053070 flowClassifierEntry.add(codec(FlowClassifier.class).encode(flowClassifier, this));
Bharat saraswalf38e2262015-11-18 22:57:05 +053071 }
72 }
Bharat saraswald25a30a2015-10-26 18:35:47 +053073 return ok(result.toString()).build();
74 }
75
76 /**
Bharat saraswalf38e2262015-11-18 22:57:05 +053077 * Get details of a flow classifier.
Bharat saraswald25a30a2015-10-26 18:35:47 +053078 *
Mahesh Poojary Sfac02262015-11-20 19:13:22 +053079 * @param id
80 * flow classifier id
Bharat saraswalf38e2262015-11-18 22:57:05 +053081 * @return 200 OK , 404 if given identifier does not exist
Bharat saraswald25a30a2015-10-26 18:35:47 +053082 */
83 @GET
84 @Path("{flow_id}")
Wu wenbind0b119f2016-05-11 18:03:41 +080085 @Consumes(MediaType.APPLICATION_JSON)
Bharat saraswald25a30a2015-10-26 18:35:47 +053086 @Produces(MediaType.APPLICATION_JSON)
87 public Response getFlowClassifier(@PathParam("flow_id") String id) {
Phaneendra Mandad66dca42015-12-01 20:24:10 +053088 FlowClassifier flowClassifier = nullIsNotFound(get(FlowClassifierService.class)
89 .getFlowClassifier(FlowClassifierId.of(id)), FLOW_CLASSIFIER_NOT_FOUND);
Bharat saraswald25a30a2015-10-26 18:35:47 +053090
Phaneendra Mandad66dca42015-12-01 20:24:10 +053091 ObjectNode result = mapper().createObjectNode();
92 result.set("flow_classifier", codec(FlowClassifier.class).encode(flowClassifier, this));
Bharat saraswalf38e2262015-11-18 22:57:05 +053093
Bharat saraswald25a30a2015-10-26 18:35:47 +053094 return ok(result.toString()).build();
95 }
96
97 /**
98 * Creates and stores a new flow classifier.
99 *
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530100 * @param stream
101 * flow classifier from JSON
Bharat saraswald25a30a2015-10-26 18:35:47 +0530102 * @return status of the request - CREATED if the JSON is correct,
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530103 * BAD_REQUEST if the JSON is invalid
Bharat saraswald25a30a2015-10-26 18:35:47 +0530104 */
105 @POST
106 @Consumes(MediaType.APPLICATION_JSON)
107 @Produces(MediaType.APPLICATION_JSON)
108 public Response createFlowClassifier(InputStream stream) {
Bharat saraswald25a30a2015-10-26 18:35:47 +0530109 try {
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530110 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530111 JsonNode flow = jsonTree.get("flow_classifier");
Bharat saraswald25a30a2015-10-26 18:35:47 +0530112
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530113 FlowClassifier flowClassifier = codec(FlowClassifier.class).decode((ObjectNode) flow, this);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530114 Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).createFlowClassifier(flowClassifier),
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530115 FLOW_CLASSIFIER_NOT_FOUND);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530116 return Response.status(OK).entity(issuccess.toString()).build();
117 } catch (IOException ex) {
118 log.error("Exception while creating flow classifier {}.", ex.toString());
Bharat saraswald25a30a2015-10-26 18:35:47 +0530119 throw new IllegalArgumentException(ex);
120 }
Bharat saraswald25a30a2015-10-26 18:35:47 +0530121 }
122
123 /**
Bharat saraswalf38e2262015-11-18 22:57:05 +0530124 * Update details of a flow classifier.
Bharat saraswald25a30a2015-10-26 18:35:47 +0530125 *
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530126 * @param id
127 * flow classifier id
128 * @param stream
129 * InputStream
Bharat saraswalf38e2262015-11-18 22:57:05 +0530130 * @return 200 OK, 404 if given identifier does not exist
Bharat saraswald25a30a2015-10-26 18:35:47 +0530131 */
132 @PUT
133 @Path("{flow_id}")
134 @Produces(MediaType.APPLICATION_JSON)
135 @Consumes(MediaType.APPLICATION_JSON)
136 public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) {
137 try {
Bharat saraswalf38e2262015-11-18 22:57:05 +0530138
139 JsonNode jsonTree = mapper().readTree(stream);
140 JsonNode flow = jsonTree.get("flow_classifier");
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530141 FlowClassifier flowClassifier = codec(FlowClassifier.class).decode((ObjectNode) flow, this);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530142 Boolean result = nullIsNotFound(get(FlowClassifierService.class).updateFlowClassifier(flowClassifier),
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530143 FLOW_CLASSIFIER_NOT_FOUND);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530144 return Response.status(OK).entity(result.toString()).build();
145 } catch (IOException e) {
146 log.error("Update flow classifier failed because of exception {}.", e.toString());
147 throw new IllegalArgumentException(e);
Bharat saraswald25a30a2015-10-26 18:35:47 +0530148 }
149 }
150
151 /**
Bharat saraswalf38e2262015-11-18 22:57:05 +0530152 * Delete details of a flow classifier.
Bharat saraswald25a30a2015-10-26 18:35:47 +0530153 *
Jian Lic2a542b2016-05-10 11:48:19 -0700154 * @param id flow classifier id
155 * @return 204 NO CONTENT
Bharat saraswald25a30a2015-10-26 18:35:47 +0530156 */
157 @Path("{flow_id}")
158 @DELETE
Wu wenbind0b119f2016-05-11 18:03:41 +0800159 @Produces(MediaType.APPLICATION_JSON)
160 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700161 public Response deleteFlowClassifier(@PathParam("flow_id") String id) {
Bharat saraswalf38e2262015-11-18 22:57:05 +0530162 log.debug("Deletes flow classifier by identifier {}.", id);
163 FlowClassifierId flowClassifierId = FlowClassifierId.of(id);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530164 Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).removeFlowClassifier(flowClassifierId),
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530165 FLOW_CLASSIFIER_NOT_FOUND);
Jian Lic2a542b2016-05-10 11:48:19 -0700166 return Response.noContent().build();
Bharat saraswald25a30a2015-10-26 18:35:47 +0530167 }
168}