blob: 47e249fc55d6fba85c9cff58fc0017ae558b01c1 [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;
Ray Milkey86ee5e82018-04-02 15:33:07 -070020import static org.onlab.util.Tools.readTreeFromStream;
Bharat saraswald25a30a2015-10-26 18:35:47 +053021
22import java.io.IOException;
23import java.io.InputStream;
Bharat saraswald25a30a2015-10-26 18:35:47 +053024
25import javax.ws.rs.Consumes;
26import javax.ws.rs.DELETE;
27import javax.ws.rs.GET;
28import javax.ws.rs.POST;
29import javax.ws.rs.PUT;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35
Bharat saraswalf38e2262015-11-18 22:57:05 +053036import org.onosproject.rest.AbstractWebResource;
Bharat saraswald25a30a2015-10-26 18:35:47 +053037import org.onosproject.vtnrsc.FlowClassifier;
38import org.onosproject.vtnrsc.FlowClassifierId;
Jonathan Hart317f4762015-11-09 16:05:36 -080039import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
Bharat saraswalf38e2262015-11-18 22:57:05 +053040import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
Bharat saraswald25a30a2015-10-26 18:35:47 +053042
Bharat saraswalf38e2262015-11-18 22:57:05 +053043import com.fasterxml.jackson.databind.JsonNode;
Bharat saraswalf38e2262015-11-18 22:57:05 +053044import com.fasterxml.jackson.databind.node.ArrayNode;
Bharat saraswald25a30a2015-10-26 18:35:47 +053045import com.fasterxml.jackson.databind.node.ObjectNode;
46
47/**
48 * Query and program flow classifier.
49 */
50@Path("flow_classifiers")
51public class FlowClassifierWebResource extends AbstractWebResource {
52
Bharat saraswalf38e2262015-11-18 22:57:05 +053053 private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class);
54
Bharat saraswald25a30a2015-10-26 18:35:47 +053055 public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found";
56
57 /**
Bharat saraswalf38e2262015-11-18 22:57:05 +053058 * Get all flow classifiers created.
Bharat saraswald25a30a2015-10-26 18:35:47 +053059 *
Bharat saraswala9929a92015-11-19 00:33:55 +053060 * @return 200 OK
Bharat saraswald25a30a2015-10-26 18:35:47 +053061 */
62 @GET
Wu wenbind0b119f2016-05-11 18:03:41 +080063 @Consumes(MediaType.APPLICATION_JSON)
Bharat saraswald25a30a2015-10-26 18:35:47 +053064 @Produces(MediaType.APPLICATION_JSON)
65 public Response getFlowClassifiers() {
Mahesh Poojary Sfac02262015-11-20 19:13:22 +053066 Iterable<FlowClassifier> flowClassifiers = get(FlowClassifierService.class).getFlowClassifiers();
Phaneendra Mandad66dca42015-12-01 20:24:10 +053067 ObjectNode result = mapper().createObjectNode();
Bharat saraswalf38e2262015-11-18 22:57:05 +053068 ArrayNode flowClassifierEntry = result.putArray("flow_classifiers");
69 if (flowClassifiers != null) {
70 for (final FlowClassifier flowClassifier : flowClassifiers) {
Phaneendra Mandad66dca42015-12-01 20:24:10 +053071 flowClassifierEntry.add(codec(FlowClassifier.class).encode(flowClassifier, this));
Bharat saraswalf38e2262015-11-18 22:57:05 +053072 }
73 }
Bharat saraswald25a30a2015-10-26 18:35:47 +053074 return ok(result.toString()).build();
75 }
76
77 /**
Bharat saraswalf38e2262015-11-18 22:57:05 +053078 * Get details of a flow classifier.
Bharat saraswald25a30a2015-10-26 18:35:47 +053079 *
Mahesh Poojary Sfac02262015-11-20 19:13:22 +053080 * @param id
81 * flow classifier id
Bharat saraswalf38e2262015-11-18 22:57:05 +053082 * @return 200 OK , 404 if given identifier does not exist
Bharat saraswald25a30a2015-10-26 18:35:47 +053083 */
84 @GET
85 @Path("{flow_id}")
Wu wenbind0b119f2016-05-11 18:03:41 +080086 @Consumes(MediaType.APPLICATION_JSON)
Bharat saraswald25a30a2015-10-26 18:35:47 +053087 @Produces(MediaType.APPLICATION_JSON)
88 public Response getFlowClassifier(@PathParam("flow_id") String id) {
Phaneendra Mandad66dca42015-12-01 20:24:10 +053089 FlowClassifier flowClassifier = nullIsNotFound(get(FlowClassifierService.class)
90 .getFlowClassifier(FlowClassifierId.of(id)), FLOW_CLASSIFIER_NOT_FOUND);
Bharat saraswald25a30a2015-10-26 18:35:47 +053091
Phaneendra Mandad66dca42015-12-01 20:24:10 +053092 ObjectNode result = mapper().createObjectNode();
93 result.set("flow_classifier", codec(FlowClassifier.class).encode(flowClassifier, this));
Bharat saraswalf38e2262015-11-18 22:57:05 +053094
Bharat saraswald25a30a2015-10-26 18:35:47 +053095 return ok(result.toString()).build();
96 }
97
98 /**
99 * Creates and stores a new flow classifier.
100 *
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530101 * @param stream
102 * flow classifier from JSON
Bharat saraswald25a30a2015-10-26 18:35:47 +0530103 * @return status of the request - CREATED if the JSON is correct,
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530104 * BAD_REQUEST if the JSON is invalid
Bharat saraswald25a30a2015-10-26 18:35:47 +0530105 */
106 @POST
107 @Consumes(MediaType.APPLICATION_JSON)
108 @Produces(MediaType.APPLICATION_JSON)
109 public Response createFlowClassifier(InputStream stream) {
Bharat saraswald25a30a2015-10-26 18:35:47 +0530110 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700111 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530112 JsonNode flow = jsonTree.get("flow_classifier");
Bharat saraswald25a30a2015-10-26 18:35:47 +0530113
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530114 FlowClassifier flowClassifier = codec(FlowClassifier.class).decode((ObjectNode) flow, this);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530115 Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).createFlowClassifier(flowClassifier),
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530116 FLOW_CLASSIFIER_NOT_FOUND);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530117 return Response.status(OK).entity(issuccess.toString()).build();
118 } catch (IOException ex) {
119 log.error("Exception while creating flow classifier {}.", ex.toString());
Bharat saraswald25a30a2015-10-26 18:35:47 +0530120 throw new IllegalArgumentException(ex);
121 }
Bharat saraswald25a30a2015-10-26 18:35:47 +0530122 }
123
124 /**
Bharat saraswalf38e2262015-11-18 22:57:05 +0530125 * Update details of a flow classifier.
Bharat saraswald25a30a2015-10-26 18:35:47 +0530126 *
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530127 * @param id
128 * flow classifier id
129 * @param stream
130 * InputStream
Bharat saraswalf38e2262015-11-18 22:57:05 +0530131 * @return 200 OK, 404 if given identifier does not exist
Bharat saraswald25a30a2015-10-26 18:35:47 +0530132 */
133 @PUT
134 @Path("{flow_id}")
135 @Produces(MediaType.APPLICATION_JSON)
136 @Consumes(MediaType.APPLICATION_JSON)
137 public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) {
138 try {
Bharat saraswalf38e2262015-11-18 22:57:05 +0530139
Ray Milkey86ee5e82018-04-02 15:33:07 -0700140 JsonNode jsonTree = readTreeFromStream(mapper(), stream);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530141 JsonNode flow = jsonTree.get("flow_classifier");
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530142 FlowClassifier flowClassifier = codec(FlowClassifier.class).decode((ObjectNode) flow, this);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530143 Boolean result = nullIsNotFound(get(FlowClassifierService.class).updateFlowClassifier(flowClassifier),
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530144 FLOW_CLASSIFIER_NOT_FOUND);
Bharat saraswalf38e2262015-11-18 22:57:05 +0530145 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);
Bharat saraswald25a30a2015-10-26 18:35:47 +0530149 }
150 }
151
152 /**
Bharat saraswalf38e2262015-11-18 22:57:05 +0530153 * Delete details of a flow classifier.
Bharat saraswald25a30a2015-10-26 18:35:47 +0530154 *
Jian Lic2a542b2016-05-10 11:48:19 -0700155 * @param id flow classifier id
156 * @return 204 NO CONTENT
Bharat saraswald25a30a2015-10-26 18:35:47 +0530157 */
158 @Path("{flow_id}")
159 @DELETE
Wu wenbind0b119f2016-05-11 18:03:41 +0800160 @Produces(MediaType.APPLICATION_JSON)
161 @Consumes(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700162 public Response deleteFlowClassifier(@PathParam("flow_id") String id) {
Bharat saraswalf38e2262015-11-18 22:57:05 +0530163 log.debug("Deletes flow classifier by identifier {}.", id);
164 FlowClassifierId flowClassifierId = FlowClassifierId.of(id);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530165 Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).removeFlowClassifier(flowClassifierId),
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530166 FLOW_CLASSIFIER_NOT_FOUND);
Jian Lic2a542b2016-05-10 11:48:19 -0700167 return Response.noContent().build();
Bharat saraswald25a30a2015-10-26 18:35:47 +0530168 }
169}