blob: 7a57c0abb522e5d36e1ab92b50243968196ca61f [file] [log] [blame]
Bharat saraswald25a30a2015-10-26 18:35:47 +05301/*
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 */
16package org.onosproject.vtnweb.resources;
17
18import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
19import static org.onlab.util.Tools.nullIsNotFound;
20import static javax.ws.rs.core.Response.Status.NOT_FOUND;
21
22import java.io.IOException;
23import java.io.InputStream;
24import java.net.URI;
25import java.net.URISyntaxException;
26import java.util.UUID;
27
28import javax.ws.rs.Consumes;
29import javax.ws.rs.DELETE;
30import javax.ws.rs.GET;
31import javax.ws.rs.POST;
32import javax.ws.rs.PUT;
33import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
35import javax.ws.rs.Produces;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38
39import org.onosproject.vtnrsc.FlowClassifier;
40import org.onosproject.vtnrsc.FlowClassifierId;
41import org.onosproject.rest.AbstractWebResource;
Jonathan Hart317f4762015-11-09 16:05:36 -080042import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
samueljcce3d5b6a2015-10-27 15:52:52 -070043import org.onosproject.vtnweb.web.FlowClassifierCodec;
Bharat saraswald25a30a2015-10-26 18:35:47 +053044
45import com.fasterxml.jackson.databind.ObjectMapper;
46import com.fasterxml.jackson.databind.node.ObjectNode;
47
48/**
49 * Query and program flow classifier.
50 */
51@Path("flow_classifiers")
52public class FlowClassifierWebResource extends AbstractWebResource {
53
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";
57
58 /**
59 * Get all flow classifiers created. Returns list of all flow classifiers
60 * created.
61 *
62 * @return 200 OK
63 */
64 @GET
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();
71 }
72
73 /**
74 * Get details of a flow classifier. Returns details of a specified flow
75 * classifier id.
76 *
77 * @param id flow classifier id
78 * @return 200 OK
79 */
80 @GET
81 @Path("{flow_id}")
82 @Produces(MediaType.APPLICATION_JSON)
83 public Response getFlowClassifier(@PathParam("flow_id") String id) {
84
Mahesh Poojary Huawei321cc502015-11-03 13:23:32 +053085 if (!service.hasFlowClassifier(FlowClassifierId.of(UUID.fromString(id)))) {
Bharat saraswald25a30a2015-10-26 18:35:47 +053086 return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build();
87 }
88 FlowClassifier flowClassifier = nullIsNotFound(
Mahesh Poojary Huawei321cc502015-11-03 13:23:32 +053089 service.getFlowClassifier(FlowClassifierId.of(UUID.fromString(id))),
Bharat saraswald25a30a2015-10-26 18:35:47 +053090 FLOW_CLASSIFIER_NOT_FOUND);
91
92 ObjectNode result = new ObjectMapper().createObjectNode();
93 result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this));
94 return ok(result.toString()).build();
95 }
96
97 /**
98 * Creates and stores a new flow classifier.
99 *
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
104 */
105 @POST
106 @Path("{flow_id}")
107 @Consumes(MediaType.APPLICATION_JSON)
108 @Produces(MediaType.APPLICATION_JSON)
109 public Response createFlowClassifier(@PathParam("flow_id") String flowClassifierId, InputStream stream) {
110 URI location;
111 try {
112 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
113
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);
119 }
120 return Response.created(location).build();
121 }
122
123 /**
124 * Creates and stores a new flow classifier.
125 *
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
129 */
130 @POST
131 @Consumes(MediaType.APPLICATION_JSON)
132 @Produces(MediaType.APPLICATION_JSON)
133 public Response createFlowClassifier(InputStream stream) {
134 URI location;
135 try {
136 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
137
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);
143 }
144 return Response.created(location).build();
145 }
146
147 /**
148 * Update details of a flow classifier. Update details of a specified flow
149 * classifier id.
150 *
151 * @param id flow classifier id
152 * @param stream InputStream
153 * @return 200 OK
154 */
155 @PUT
156 @Path("{flow_id}")
157 @Produces(MediaType.APPLICATION_JSON)
158 @Consumes(MediaType.APPLICATION_JSON)
159 public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) {
160 try {
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);
164 if (!result) {
165 return Response.status(204).entity(FLOW_CLASSIFIER_NOT_FOUND).build();
166 }
167 return Response.status(203).entity(result.toString()).build();
168 } catch (Exception e) {
169 return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build();
170 }
171 }
172
173 /**
174 * Delete details of a flow classifier. Delete details of a specified flow
175 * classifier id.
176 *
177 * @param id flow classifier id
178 * @return 200 OK
179 * @throws IOException when input doesn't match.
180 */
181 @Path("{flow_id}")
182 @DELETE
183 public Response deleteFlowClassifier(@PathParam("flow_id") String id) throws IOException {
184 try {
Mahesh Poojary Huawei321cc502015-11-03 13:23:32 +0530185 FlowClassifierId flowClassifierId = FlowClassifierId.of(UUID.fromString(id));
Bharat saraswald25a30a2015-10-26 18:35:47 +0530186 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();
190 }
191 }
192}