blob: ec32b200c24386cadbf3bb710a956261f1835144 [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;
24import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.flow.FlowEntry;
28import org.onosproject.net.flow.FlowRule;
29import org.onosproject.net.flow.FlowRuleService;
30import org.onosproject.rest.AbstractWebResource;
31
Jian Li9d616492016-03-09 10:52:49 -080032import javax.ws.rs.Consumes;
33import javax.ws.rs.DELETE;
34import javax.ws.rs.GET;
35import javax.ws.rs.POST;
36import javax.ws.rs.Path;
37import javax.ws.rs.PathParam;
38import javax.ws.rs.Produces;
39import javax.ws.rs.core.Context;
40import javax.ws.rs.core.MediaType;
41import javax.ws.rs.core.Response;
42import javax.ws.rs.core.UriBuilder;
43import javax.ws.rs.core.UriInfo;
44import java.io.IOException;
45import java.io.InputStream;
Ray Milkey460f9b02016-03-29 11:56:19 -070046import java.util.ArrayList;
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -070047import java.util.List;
Jian Li9d616492016-03-09 10:52:49 -080048import java.util.stream.StreamSupport;
Ray Milkeyd43fe452015-05-29 09:35:12 -070049
Ray Milkey460f9b02016-03-29 11:56:19 -070050import static org.onlab.util.Tools.nullIsNotFound;
51
Ray Milkey4f5de002014-12-17 19:26:11 -080052/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070053 * Query and program flow rules.
Ray Milkey4f5de002014-12-17 19:26:11 -080054 */
55
56@Path("flows")
57public class FlowsWebResource extends AbstractWebResource {
Jian Li9d616492016-03-09 10:52:49 -080058
59 @Context
60 UriInfo uriInfo;
61
Ray Milkey4f5de002014-12-17 19:26:11 -080062 public static final String DEVICE_NOT_FOUND = "Device is not found";
Ray Milkey460f9b02016-03-29 11:56:19 -070063 public static final String FLOW_NOT_FOUND = "Flow is not found";
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -070064 public static final String FLOWS = "flows";
Ray Milkey460f9b02016-03-29 11:56:19 -070065 public static final String DEVICE_ID = "deviceId";
66 public static final String FLOW_ID = "flowId";
Ray Milkey4f5de002014-12-17 19:26:11 -080067
68 final FlowRuleService service = get(FlowRuleService.class);
69 final ObjectNode root = mapper().createObjectNode();
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -070070 final ArrayNode flowsNode = root.putArray(FLOWS);
Ray Milkey4f5de002014-12-17 19:26:11 -080071
72 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070073 * Get all flow entries. Returns array of all flow rules in the system.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080074 * @onos.rsModel Flows
Ray Milkey4f5de002014-12-17 19:26:11 -080075 * @return array of all the intents in the system
76 */
77 @GET
78 @Produces(MediaType.APPLICATION_JSON)
79 public Response getFlows() {
Ray Milkey4f5de002014-12-17 19:26:11 -080080 final Iterable<Device> devices = get(DeviceService.class).getDevices();
81 for (final Device device : devices) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +053082 final Iterable<FlowEntry> flowEntries = service.getFlowEntries(device.id());
83 if (flowEntries != null) {
84 for (final FlowEntry entry : flowEntries) {
Thomas Vachuska8683e012015-03-18 18:03:33 -070085 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080086 }
87 }
88 }
89
Ray Milkey3f025692015-01-26 11:15:41 -080090 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -080091 }
92
93 /**
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -070094 * Create new flow rules. Creates and installs a new flow rules.<br>
95 * Instructions description:
96 * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions
97 * <br>
98 * Criteria description:
99 * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria
100 *
101 * @onos.rsModel FlowsBatchPost
102 * @param stream flow rules JSON
103 * @return status of the request - CREATED if the JSON is correct,
104 * BAD_REQUEST if the JSON is invalid
105 */
106 @POST
107 @Consumes(MediaType.APPLICATION_JSON)
108 @Produces(MediaType.APPLICATION_JSON)
109 public Response createFlows(InputStream stream) {
110 try {
111 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
112 ArrayNode flowsArray = (ArrayNode) jsonTree.get(FLOWS);
113 List<FlowRule> rules = codec(FlowRule.class).decode(flowsArray, this);
114 service.applyFlowRules(rules.toArray(new FlowRule[rules.size()]));
Ray Milkey460f9b02016-03-29 11:56:19 -0700115 rules.forEach(flowRule -> {
116 ObjectNode flowNode = mapper().createObjectNode();
117 flowNode.put(DEVICE_ID, flowRule.deviceId().toString())
118 .put(FLOW_ID, flowRule.id().value());
119 flowsNode.add(flowNode);
120 });
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -0700121 } catch (IOException ex) {
122 throw new IllegalArgumentException(ex);
123 }
Ray Milkey460f9b02016-03-29 11:56:19 -0700124 return Response.ok(root).build();
Thomas Vachuskaa1ae5e12016-03-28 10:36:30 -0700125 }
126
127 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700128 * Get flow entries of a device. Returns array of all flow rules for the
129 * specified device.
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800130 * @onos.rsModel Flows
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700131 * @param deviceId device identifier
Ray Milkey4f5de002014-12-17 19:26:11 -0800132 * @return flow data as an array
133 */
134 @GET
135 @Produces(MediaType.APPLICATION_JSON)
136 @Path("{deviceId}")
137 public Response getFlowByDeviceId(@PathParam("deviceId") String deviceId) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530138 final Iterable<FlowEntry> flowEntries =
Ray Milkey4f5de002014-12-17 19:26:11 -0800139 service.getFlowEntries(DeviceId.deviceId(deviceId));
140
Jian Li9d616492016-03-09 10:52:49 -0800141 if (flowEntries == null || !flowEntries.iterator().hasNext()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800142 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
143 }
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530144 for (final FlowEntry entry : flowEntries) {
Thomas Vachuska8683e012015-03-18 18:03:33 -0700145 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -0800146 }
Ray Milkey3f025692015-01-26 11:15:41 -0800147 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800148 }
149
150 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700151 * Get flow rule. Returns the flow entry specified by the device id and
152 * flow rule id.
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800153 * @onos.rsModel Flows
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700154 * @param deviceId device identifier
155 * @param flowId flow rule identifier
Ray Milkey4f5de002014-12-17 19:26:11 -0800156 * @return flow data as an array
157 */
158 @GET
159 @Produces(MediaType.APPLICATION_JSON)
160 @Path("{deviceId}/{flowId}")
161 public Response getFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId,
162 @PathParam("flowId") long flowId) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530163 final Iterable<FlowEntry> flowEntries =
Ray Milkey4f5de002014-12-17 19:26:11 -0800164 service.getFlowEntries(DeviceId.deviceId(deviceId));
165
Jian Li9d616492016-03-09 10:52:49 -0800166 if (flowEntries == null || !flowEntries.iterator().hasNext()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800167 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
168 }
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530169 for (final FlowEntry entry : flowEntries) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800170 if (entry.id().value() == flowId) {
Thomas Vachuska8683e012015-03-18 18:03:33 -0700171 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -0800172 }
173 }
Ray Milkey3f025692015-01-26 11:15:41 -0800174 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800175 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700176
177 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700178 * Create new flow rule. Creates and installs a new flow rule for the
Andrea Campanella881f29f2016-03-03 19:18:42 -0800179 * specified device. <br>
180 * Instructions description:
181 * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions
182 * <br>
183 * Criteria description:
184 * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria
185 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800186 * @onos.rsModel FlowsPost
Madan Jampani0dbac7a2015-06-25 10:37:45 -0700187 * @param deviceId device identifier
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700188 * @param stream flow rule JSON
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700189 * @return status of the request - CREATED if the JSON is correct,
Ray Milkeyd43fe452015-05-29 09:35:12 -0700190 * BAD_REQUEST if the JSON is invalid
191 */
192 @POST
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700193 @Path("{deviceId}")
Ray Milkeyd43fe452015-05-29 09:35:12 -0700194 @Consumes(MediaType.APPLICATION_JSON)
195 @Produces(MediaType.APPLICATION_JSON)
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700196 public Response createFlow(@PathParam("deviceId") String deviceId,
197 InputStream stream) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700198 try {
Ray Milkey5d915f42015-08-13 10:27:53 -0700199 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
200 JsonNode specifiedDeviceId = jsonTree.get("deviceId");
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700201 if (specifiedDeviceId != null &&
202 !specifiedDeviceId.asText().equals(deviceId)) {
203 throw new IllegalArgumentException(
204 "Invalid deviceId in flow creation request");
205 }
Ray Milkey5d915f42015-08-13 10:27:53 -0700206 jsonTree.put("deviceId", deviceId);
207 FlowRule rule = codec(FlowRule.class).decode(jsonTree, this);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700208 service.applyFlowRules(rule);
Jian Li9d616492016-03-09 10:52:49 -0800209 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
210 .path("flows")
211 .path(deviceId)
Ray Milkey90289b02016-03-31 15:23:28 -0700212 .path(Long.toString(rule.id().value()));
Jian Li9d616492016-03-09 10:52:49 -0800213
214 return Response
215 .created(locationBuilder.build())
216 .build();
217 } catch (IOException ex) {
Ray Milkey5d915f42015-08-13 10:27:53 -0700218 throw new IllegalArgumentException(ex);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700219 }
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700220 }
221
222 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700223 * Remove flow rule. Removes the specified flow rule.
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700224 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700225 * @param deviceId device identifier
226 * @param flowId flow rule identifier
Jian Lic2a542b2016-05-10 11:48:19 -0700227 * @return 204 NO CONTENT
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700228 */
229 @DELETE
230 @Produces(MediaType.APPLICATION_JSON)
231 @Path("{deviceId}/{flowId}")
Jian Lic2a542b2016-05-10 11:48:19 -0700232 public Response deleteFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId,
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700233 @PathParam("flowId") long flowId) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530234 final Iterable<FlowEntry> flowEntries =
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700235 service.getFlowEntries(DeviceId.deviceId(deviceId));
236
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530237 if (!flowEntries.iterator().hasNext()) {
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700238 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
239 }
240
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530241 StreamSupport.stream(flowEntries.spliterator(), false)
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700242 .filter(entry -> entry.id().value() == flowId)
243 .forEach(service::removeFlowRules);
Jian Lic2a542b2016-05-10 11:48:19 -0700244 return Response.noContent().build();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700245 }
246
Ray Milkey460f9b02016-03-29 11:56:19 -0700247 /**
248 * Removes a batch of flow rules.
Ray Milkeybee35092016-04-12 10:01:26 -0700249 *
250 * @param stream stream for posted JSON
Jian Lic2a542b2016-05-10 11:48:19 -0700251 * @return NO CONTENT
Ray Milkey460f9b02016-03-29 11:56:19 -0700252 */
253 @DELETE
254 @Produces(MediaType.APPLICATION_JSON)
Jian Lic2a542b2016-05-10 11:48:19 -0700255 public Response deleteFlows(InputStream stream) {
Ray Milkey460f9b02016-03-29 11:56:19 -0700256 ListMultimap<DeviceId, Long> deviceMap = ArrayListMultimap.create();
257 List<FlowEntry> rulesToRemove = new ArrayList<>();
258
259 try {
260 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
261
262 JsonNode jsonFlows = jsonTree.get("flows");
263
264 jsonFlows.forEach(node -> {
265 DeviceId deviceId =
266 DeviceId.deviceId(
267 nullIsNotFound(node.get(DEVICE_ID),
268 DEVICE_NOT_FOUND).asText());
269 long flowId = nullIsNotFound(node.get(FLOW_ID),
270 FLOW_NOT_FOUND).asLong();
271 deviceMap.put(deviceId, flowId);
272
273 });
274 } catch (IOException ex) {
275 throw new IllegalArgumentException(ex);
276 }
277
278 deviceMap.keySet().forEach(deviceId -> {
279 List<Long> flowIds = deviceMap.get(deviceId);
280 Iterable<FlowEntry> entries = service.getFlowEntries(deviceId);
281 flowIds.forEach(flowId -> {
282 StreamSupport.stream(entries.spliterator(), false)
283 .filter(entry -> flowId == entry.id().value())
284 .forEach(rulesToRemove::add);
285 });
286 });
287
288 service.removeFlowRules(rulesToRemove.toArray(new FlowEntry[0]));
Jian Lic2a542b2016-05-10 11:48:19 -0700289 return Response.noContent().build();
Ray Milkey460f9b02016-03-29 11:56:19 -0700290 }
291
Ray Milkey4f5de002014-12-17 19:26:11 -0800292}