blob: 36b8d4cbed6021ef47804dd3f45c4cc07432b48e [file] [log] [blame]
Ray Milkey4f5de002014-12-17 19:26:11 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey4f5de002014-12-17 19:26:11 -08003 *
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 */
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Ray Milkey4f5de002014-12-17 19:26:11 -080017
Jian Li9d616492016-03-09 10:52:49 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey460f9b02016-03-29 11:56:19 -070021import com.google.common.collect.ArrayListMultimap;
22import com.google.common.collect.ListMultimap;
Ray Milkeyd43fe452015-05-29 09:35:12 -070023import org.onlab.util.ItemNotFoundException;
Jian Li2907ad22016-05-12 23:08:54 -070024import org.onosproject.app.ApplicationService;
25import org.onosproject.core.ApplicationId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070026import org.onosproject.net.Device;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.flow.FlowEntry;
30import org.onosproject.net.flow.FlowRule;
31import org.onosproject.net.flow.FlowRuleService;
32import org.onosproject.rest.AbstractWebResource;
33
Jian Li9d616492016-03-09 10:52:49 -080034import javax.ws.rs.Consumes;
35import javax.ws.rs.DELETE;
36import javax.ws.rs.GET;
37import javax.ws.rs.POST;
38import javax.ws.rs.Path;
39import javax.ws.rs.PathParam;
40import javax.ws.rs.Produces;
Jian Li2907ad22016-05-12 23:08:54 -070041import javax.ws.rs.QueryParam;
Jian Li9d616492016-03-09 10:52:49 -080042import javax.ws.rs.core.Context;
43import javax.ws.rs.core.MediaType;
44import javax.ws.rs.core.Response;
45import javax.ws.rs.core.UriBuilder;
46import javax.ws.rs.core.UriInfo;
47import java.io.IOException;
48import java.io.InputStream;
Ray Milkey460f9b02016-03-29 11:56:19 -070049import java.util.ArrayList;
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -070050import java.util.List;
Jian Li9d616492016-03-09 10:52:49 -080051import java.util.stream.StreamSupport;
Ray Milkeyd43fe452015-05-29 09:35:12 -070052
Ray Milkey131c0682016-10-18 11:12:50 -070053import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkey460f9b02016-03-29 11:56:19 -070054import static org.onlab.util.Tools.nullIsNotFound;
55
Ray Milkey4f5de002014-12-17 19:26:11 -080056/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070057 * Query and program flow rules.
Ray Milkey4f5de002014-12-17 19:26:11 -080058 */
59
60@Path("flows")
61public class FlowsWebResource extends AbstractWebResource {
Jian Li9d616492016-03-09 10:52:49 -080062
63 @Context
Jian Licc730a62016-05-10 16:36:16 -070064 private UriInfo uriInfo;
Jian Li9d616492016-03-09 10:52:49 -080065
Jian Licc730a62016-05-10 16:36:16 -070066 private static final String DEVICE_NOT_FOUND = "Device is not found";
67 private static final String FLOW_NOT_FOUND = "Flow is not found";
Jian Li2907ad22016-05-12 23:08:54 -070068 private static final String APP_ID_NOT_FOUND = "Application Id is not found";
Ray Milkey131c0682016-10-18 11:12:50 -070069 private static final String FLOW_ARRAY_REQUIRED = "Flows array was not specified";
Jian Licc730a62016-05-10 16:36:16 -070070 private static final String FLOWS = "flows";
71 private static final String DEVICE_ID = "deviceId";
72 private static final String FLOW_ID = "flowId";
Ray Milkey4f5de002014-12-17 19:26:11 -080073
Jian Licc730a62016-05-10 16:36:16 -070074 private final FlowRuleService service = get(FlowRuleService.class);
75 private final ObjectNode root = mapper().createObjectNode();
76 private final ArrayNode flowsNode = root.putArray(FLOWS);
Ray Milkey4f5de002014-12-17 19:26:11 -080077
78 /**
Jian Licc730a62016-05-10 16:36:16 -070079 * Gets all flow entries. Returns array of all flow rules in the system.
80 *
81 * @return 200 OK with a collection of flows
Jian Li2907ad22016-05-12 23:08:54 -070082 * @onos.rsModel FlowEntries
Ray Milkey4f5de002014-12-17 19:26:11 -080083 */
84 @GET
85 @Produces(MediaType.APPLICATION_JSON)
86 public Response getFlows() {
Ray Milkey4f5de002014-12-17 19:26:11 -080087 final Iterable<Device> devices = get(DeviceService.class).getDevices();
88 for (final Device device : devices) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +053089 final Iterable<FlowEntry> flowEntries = service.getFlowEntries(device.id());
90 if (flowEntries != null) {
91 for (final FlowEntry entry : flowEntries) {
Thomas Vachuska8683e012015-03-18 18:03:33 -070092 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080093 }
94 }
95 }
96
Ray Milkey3f025692015-01-26 11:15:41 -080097 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -080098 }
99
100 /**
Jian Licc730a62016-05-10 16:36:16 -0700101 * Creates new flow rules. Creates and installs a new flow rules.<br>
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -0700102 * Instructions description:
103 * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions
104 * <br>
105 * Criteria description:
106 * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria
107 *
Ray Milkeybb23e0b2016-08-02 17:00:21 -0700108 * @param appId application id
Jian Licc730a62016-05-10 16:36:16 -0700109 * @param stream flow rules JSON
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -0700110 * @return status of the request - CREATED if the JSON is correct,
111 * BAD_REQUEST if the JSON is invalid
Jian Licc730a62016-05-10 16:36:16 -0700112 * @onos.rsModel FlowsBatchPost
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -0700113 */
114 @POST
115 @Consumes(MediaType.APPLICATION_JSON)
116 @Produces(MediaType.APPLICATION_JSON)
Jian Li2907ad22016-05-12 23:08:54 -0700117 public Response createFlows(@QueryParam("appId") String appId, InputStream stream) {
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -0700118 try {
119 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Ray Milkey131c0682016-10-18 11:12:50 -0700120 ArrayNode flowsArray = nullIsIllegal((ArrayNode) jsonTree.get(FLOWS),
121 FLOW_ARRAY_REQUIRED);
Jian Li2907ad22016-05-12 23:08:54 -0700122
Jonathan Hart9b80da82016-11-10 21:46:15 +0000123 if (appId != null) {
124 flowsArray.forEach(flowJson -> ((ObjectNode) flowJson).put("appId", appId));
125 }
Jian Li2907ad22016-05-12 23:08:54 -0700126
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -0700127 List<FlowRule> rules = codec(FlowRule.class).decode(flowsArray, this);
Ray Milkey131c0682016-10-18 11:12:50 -0700128
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -0700129 service.applyFlowRules(rules.toArray(new FlowRule[rules.size()]));
Ray Milkey460f9b02016-03-29 11:56:19 -0700130 rules.forEach(flowRule -> {
131 ObjectNode flowNode = mapper().createObjectNode();
132 flowNode.put(DEVICE_ID, flowRule.deviceId().toString())
Jayasree Ghosh28e1b3e2016-06-16 05:38:50 +0530133 .put(FLOW_ID, Long.toString(flowRule.id().value()));
Ray Milkey460f9b02016-03-29 11:56:19 -0700134 flowsNode.add(flowNode);
135 });
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -0700136 } catch (IOException ex) {
137 throw new IllegalArgumentException(ex);
138 }
Ray Milkey460f9b02016-03-29 11:56:19 -0700139 return Response.ok(root).build();
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -0700140 }
141
142 /**
Jian Licc730a62016-05-10 16:36:16 -0700143 * Gets flow entries of a device. Returns array of all flow rules for the
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700144 * specified device.
Jian Licc730a62016-05-10 16:36:16 -0700145 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700146 * @param deviceId device identifier
Jian Licc730a62016-05-10 16:36:16 -0700147 * @return 200 OK with a collection of flows of given device
Jian Li2907ad22016-05-12 23:08:54 -0700148 * @onos.rsModel FlowEntries
Ray Milkey4f5de002014-12-17 19:26:11 -0800149 */
150 @GET
151 @Produces(MediaType.APPLICATION_JSON)
Jian Li2907ad22016-05-12 23:08:54 -0700152 // TODO: we need to add "/device" suffix to the path to differentiate with appId
Ray Milkey4f5de002014-12-17 19:26:11 -0800153 @Path("{deviceId}")
154 public Response getFlowByDeviceId(@PathParam("deviceId") String deviceId) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530155 final Iterable<FlowEntry> flowEntries =
Ray Milkey4f5de002014-12-17 19:26:11 -0800156 service.getFlowEntries(DeviceId.deviceId(deviceId));
157
Jian Li9d616492016-03-09 10:52:49 -0800158 if (flowEntries == null || !flowEntries.iterator().hasNext()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800159 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
160 }
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530161 for (final FlowEntry entry : flowEntries) {
Thomas Vachuska8683e012015-03-18 18:03:33 -0700162 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -0800163 }
Ray Milkey3f025692015-01-26 11:15:41 -0800164 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800165 }
166
167 /**
Jian Li2907ad22016-05-12 23:08:54 -0700168 * Gets flow rules. Returns the flow entry specified by the device id and
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700169 * flow rule id.
Jian Licc730a62016-05-10 16:36:16 -0700170 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700171 * @param deviceId device identifier
172 * @param flowId flow rule identifier
Jian Li2907ad22016-05-12 23:08:54 -0700173 * @return 200 OK with a collection of flows of given device and flow
174 * @onos.rsModel FlowEntries
Ray Milkey4f5de002014-12-17 19:26:11 -0800175 */
176 @GET
177 @Produces(MediaType.APPLICATION_JSON)
178 @Path("{deviceId}/{flowId}")
179 public Response getFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId,
180 @PathParam("flowId") long flowId) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530181 final Iterable<FlowEntry> flowEntries =
Ray Milkey4f5de002014-12-17 19:26:11 -0800182 service.getFlowEntries(DeviceId.deviceId(deviceId));
183
Jian Li9d616492016-03-09 10:52:49 -0800184 if (flowEntries == null || !flowEntries.iterator().hasNext()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800185 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
186 }
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530187 for (final FlowEntry entry : flowEntries) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800188 if (entry.id().value() == flowId) {
Thomas Vachuska8683e012015-03-18 18:03:33 -0700189 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -0800190 }
191 }
Ray Milkey3f025692015-01-26 11:15:41 -0800192 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800193 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700194
195 /**
Jian Li2907ad22016-05-12 23:08:54 -0700196 * Gets flow rules generated by an application.
197 * Returns the flow rule specified by the application id.
198 *
199 * @param appId application identifier
200 * @return 200 OK with a collection of flows of given application id
201 * @onos.rsModel FlowRules
202 */
203 @GET
204 @Produces(MediaType.APPLICATION_JSON)
205 @Path("application/{appId}")
206 public Response getFlowByAppId(@PathParam("appId") String appId) {
207 final ApplicationService appService = get(ApplicationService.class);
208 final ApplicationId idInstant = nullIsNotFound(appService.getId(appId), APP_ID_NOT_FOUND);
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530209 final Iterable<FlowEntry> flowEntries = service.getFlowEntriesById(idInstant);
Jian Li2907ad22016-05-12 23:08:54 -0700210
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530211 flowEntries.forEach(flow -> flowsNode.add(codec(FlowEntry.class).encode(flow, this)));
Jian Li2907ad22016-05-12 23:08:54 -0700212 return ok(root).build();
213 }
214
Bharath Thiruveedula99849dc2016-11-17 22:04:38 +0530215
Jian Li2907ad22016-05-12 23:08:54 -0700216 /**
217 * Removes flow rules by application ID.
218 * Removes a collection of flow rules generated by the given application.
219 *
220 * @param appId application identifier
221 * @return 204 NO CONTENT
222 */
223 @DELETE
224 @Produces(MediaType.APPLICATION_JSON)
225 @Path("application/{appId}")
226 public Response removeFlowByAppId(@PathParam("appId") String appId) {
227 final ApplicationService appService = get(ApplicationService.class);
228 final ApplicationId idInstant = nullIsNotFound(appService.getId(appId), APP_ID_NOT_FOUND);
229 service.removeFlowRulesById(idInstant);
230 return Response.noContent().build();
231 }
232
233 /**
Jian Licc730a62016-05-10 16:36:16 -0700234 * Creates new flow rule. Creates and installs a new flow rule for the
Andrea Campanella881f29f2016-03-03 19:18:42 -0800235 * specified device. <br>
236 * Instructions description:
237 * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions
238 * <br>
239 * Criteria description:
240 * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria
241 *
Madan Jampani0dbac7a2015-06-25 10:37:45 -0700242 * @param deviceId device identifier
Jian Li2907ad22016-05-12 23:08:54 -0700243 * @param appId application identifier
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700244 * @param stream flow rule JSON
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700245 * @return status of the request - CREATED if the JSON is correct,
Ray Milkeyd43fe452015-05-29 09:35:12 -0700246 * BAD_REQUEST if the JSON is invalid
Jian Licc730a62016-05-10 16:36:16 -0700247 * @onos.rsModel FlowsPost
Ray Milkeyd43fe452015-05-29 09:35:12 -0700248 */
249 @POST
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700250 @Path("{deviceId}")
Ray Milkeyd43fe452015-05-29 09:35:12 -0700251 @Consumes(MediaType.APPLICATION_JSON)
252 @Produces(MediaType.APPLICATION_JSON)
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700253 public Response createFlow(@PathParam("deviceId") String deviceId,
Jian Li2907ad22016-05-12 23:08:54 -0700254 @QueryParam("appId") String appId,
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700255 InputStream stream) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700256 try {
Ray Milkey5d915f42015-08-13 10:27:53 -0700257 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
258 JsonNode specifiedDeviceId = jsonTree.get("deviceId");
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700259 if (specifiedDeviceId != null &&
260 !specifiedDeviceId.asText().equals(deviceId)) {
261 throw new IllegalArgumentException(
262 "Invalid deviceId in flow creation request");
263 }
Ray Milkey5d915f42015-08-13 10:27:53 -0700264 jsonTree.put("deviceId", deviceId);
Jian Li2907ad22016-05-12 23:08:54 -0700265
Jonathan Hart9b80da82016-11-10 21:46:15 +0000266 if (appId != null) {
267 jsonTree.put("appId", appId);
268 }
Jian Li2907ad22016-05-12 23:08:54 -0700269
Ray Milkey5d915f42015-08-13 10:27:53 -0700270 FlowRule rule = codec(FlowRule.class).decode(jsonTree, this);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700271 service.applyFlowRules(rule);
Jian Li9d616492016-03-09 10:52:49 -0800272 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
273 .path("flows")
274 .path(deviceId)
Ray Milkey90289b02016-03-31 15:23:28 -0700275 .path(Long.toString(rule.id().value()));
Jian Li9d616492016-03-09 10:52:49 -0800276
277 return Response
278 .created(locationBuilder.build())
279 .build();
280 } catch (IOException ex) {
Ray Milkey5d915f42015-08-13 10:27:53 -0700281 throw new IllegalArgumentException(ex);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700282 }
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700283 }
284
285 /**
Jian Li2907ad22016-05-12 23:08:54 -0700286 * Removes flow rule. Removes the specified flow rule.
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700287 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700288 * @param deviceId device identifier
289 * @param flowId flow rule identifier
Jian Lic2a542b2016-05-10 11:48:19 -0700290 * @return 204 NO CONTENT
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700291 */
292 @DELETE
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700293 @Path("{deviceId}/{flowId}")
Jian Lic2a542b2016-05-10 11:48:19 -0700294 public Response deleteFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId,
Jian Licc730a62016-05-10 16:36:16 -0700295 @PathParam("flowId") long flowId) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530296 final Iterable<FlowEntry> flowEntries =
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700297 service.getFlowEntries(DeviceId.deviceId(deviceId));
298
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530299 if (!flowEntries.iterator().hasNext()) {
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700300 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
301 }
302
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530303 StreamSupport.stream(flowEntries.spliterator(), false)
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700304 .filter(entry -> entry.id().value() == flowId)
305 .forEach(service::removeFlowRules);
Jian Lic2a542b2016-05-10 11:48:19 -0700306 return Response.noContent().build();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700307 }
308
Ray Milkey460f9b02016-03-29 11:56:19 -0700309 /**
310 * Removes a batch of flow rules.
Ray Milkeybee35092016-04-12 10:01:26 -0700311 *
312 * @param stream stream for posted JSON
Jian Licc730a62016-05-10 16:36:16 -0700313 * @return 204 NO CONTENT
Ray Milkey460f9b02016-03-29 11:56:19 -0700314 */
315 @DELETE
Jian Lic2a542b2016-05-10 11:48:19 -0700316 public Response deleteFlows(InputStream stream) {
Ray Milkey460f9b02016-03-29 11:56:19 -0700317 ListMultimap<DeviceId, Long> deviceMap = ArrayListMultimap.create();
318 List<FlowEntry> rulesToRemove = new ArrayList<>();
319
320 try {
321 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
322
323 JsonNode jsonFlows = jsonTree.get("flows");
324
325 jsonFlows.forEach(node -> {
326 DeviceId deviceId =
327 DeviceId.deviceId(
328 nullIsNotFound(node.get(DEVICE_ID),
Jian Licc730a62016-05-10 16:36:16 -0700329 DEVICE_NOT_FOUND).asText());
Ray Milkey460f9b02016-03-29 11:56:19 -0700330 long flowId = nullIsNotFound(node.get(FLOW_ID),
Jian Licc730a62016-05-10 16:36:16 -0700331 FLOW_NOT_FOUND).asLong();
Ray Milkey460f9b02016-03-29 11:56:19 -0700332 deviceMap.put(deviceId, flowId);
333
334 });
335 } catch (IOException ex) {
336 throw new IllegalArgumentException(ex);
337 }
338
339 deviceMap.keySet().forEach(deviceId -> {
340 List<Long> flowIds = deviceMap.get(deviceId);
341 Iterable<FlowEntry> entries = service.getFlowEntries(deviceId);
342 flowIds.forEach(flowId -> {
343 StreamSupport.stream(entries.spliterator(), false)
344 .filter(entry -> flowId == entry.id().value())
345 .forEach(rulesToRemove::add);
346 });
347 });
348
349 service.removeFlowRules(rulesToRemove.toArray(new FlowEntry[0]));
Jian Lic2a542b2016-05-10 11:48:19 -0700350 return Response.noContent().build();
Ray Milkey460f9b02016-03-29 11:56:19 -0700351 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800352}