Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 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 | */ |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 17 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ArrayListMultimap; |
| 22 | import com.google.common.collect.ListMultimap; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 23 | import org.onlab.util.ItemNotFoundException; |
| 24 | import org.onosproject.net.Device; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.flow.FlowEntry; |
| 28 | import org.onosproject.net.flow.FlowRule; |
| 29 | import org.onosproject.net.flow.FlowRuleService; |
| 30 | import org.onosproject.rest.AbstractWebResource; |
| 31 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 32 | import javax.ws.rs.Consumes; |
| 33 | import javax.ws.rs.DELETE; |
| 34 | import javax.ws.rs.GET; |
| 35 | import javax.ws.rs.POST; |
| 36 | import javax.ws.rs.Path; |
| 37 | import javax.ws.rs.PathParam; |
| 38 | import javax.ws.rs.Produces; |
| 39 | import javax.ws.rs.core.Context; |
| 40 | import javax.ws.rs.core.MediaType; |
| 41 | import javax.ws.rs.core.Response; |
| 42 | import javax.ws.rs.core.UriBuilder; |
| 43 | import javax.ws.rs.core.UriInfo; |
| 44 | import java.io.IOException; |
| 45 | import java.io.InputStream; |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 46 | import java.util.ArrayList; |
Thomas Vachuska | a1ae5e1 | 2016-03-28 10:36:30 -0700 | [diff] [blame] | 47 | import java.util.List; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 48 | import java.util.stream.StreamSupport; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 49 | |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 50 | import static org.onlab.util.Tools.nullIsNotFound; |
| 51 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 52 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 53 | * Query and program flow rules. |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 54 | */ |
| 55 | |
| 56 | @Path("flows") |
| 57 | public class FlowsWebResource extends AbstractWebResource { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 58 | |
| 59 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 60 | private UriInfo uriInfo; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 61 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 62 | private static final String DEVICE_NOT_FOUND = "Device is not found"; |
| 63 | private static final String FLOW_NOT_FOUND = "Flow is not found"; |
| 64 | private static final String FLOWS = "flows"; |
| 65 | private static final String DEVICE_ID = "deviceId"; |
| 66 | private static final String FLOW_ID = "flowId"; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 67 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 68 | private final FlowRuleService service = get(FlowRuleService.class); |
| 69 | private final ObjectNode root = mapper().createObjectNode(); |
| 70 | private final ArrayNode flowsNode = root.putArray(FLOWS); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 73 | * Gets all flow entries. Returns array of all flow rules in the system. |
| 74 | * |
| 75 | * @return 200 OK with a collection of flows |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 76 | * @onos.rsModel Flows |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 77 | */ |
| 78 | @GET |
| 79 | @Produces(MediaType.APPLICATION_JSON) |
| 80 | public Response getFlows() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 81 | final Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 82 | for (final Device device : devices) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 83 | final Iterable<FlowEntry> flowEntries = service.getFlowEntries(device.id()); |
| 84 | if (flowEntries != null) { |
| 85 | for (final FlowEntry entry : flowEntries) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 86 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 91 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 95 | * Creates new flow rules. Creates and installs a new flow rules.<br> |
Thomas Vachuska | a1ae5e1 | 2016-03-28 10:36:30 -0700 | [diff] [blame] | 96 | * Instructions description: |
| 97 | * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions |
| 98 | * <br> |
| 99 | * Criteria description: |
| 100 | * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria |
| 101 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 102 | * @param stream flow rules JSON |
Thomas Vachuska | a1ae5e1 | 2016-03-28 10:36:30 -0700 | [diff] [blame] | 103 | * @return status of the request - CREATED if the JSON is correct, |
| 104 | * BAD_REQUEST if the JSON is invalid |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 105 | * @onos.rsModel FlowsBatchPost |
Thomas Vachuska | a1ae5e1 | 2016-03-28 10:36:30 -0700 | [diff] [blame] | 106 | */ |
| 107 | @POST |
| 108 | @Consumes(MediaType.APPLICATION_JSON) |
| 109 | @Produces(MediaType.APPLICATION_JSON) |
| 110 | public Response createFlows(InputStream stream) { |
| 111 | try { |
| 112 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 113 | ArrayNode flowsArray = (ArrayNode) jsonTree.get(FLOWS); |
| 114 | List<FlowRule> rules = codec(FlowRule.class).decode(flowsArray, this); |
| 115 | service.applyFlowRules(rules.toArray(new FlowRule[rules.size()])); |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 116 | rules.forEach(flowRule -> { |
| 117 | ObjectNode flowNode = mapper().createObjectNode(); |
| 118 | flowNode.put(DEVICE_ID, flowRule.deviceId().toString()) |
| 119 | .put(FLOW_ID, flowRule.id().value()); |
| 120 | flowsNode.add(flowNode); |
| 121 | }); |
Thomas Vachuska | a1ae5e1 | 2016-03-28 10:36:30 -0700 | [diff] [blame] | 122 | } catch (IOException ex) { |
| 123 | throw new IllegalArgumentException(ex); |
| 124 | } |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 125 | return Response.ok(root).build(); |
Thomas Vachuska | a1ae5e1 | 2016-03-28 10:36:30 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 129 | * Gets flow entries of a device. Returns array of all flow rules for the |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 130 | * specified device. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 131 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 132 | * @param deviceId device identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 133 | * @return 200 OK with a collection of flows of given device |
| 134 | * @onos.rsModel Flows |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 135 | */ |
| 136 | @GET |
| 137 | @Produces(MediaType.APPLICATION_JSON) |
| 138 | @Path("{deviceId}") |
| 139 | public Response getFlowByDeviceId(@PathParam("deviceId") String deviceId) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 140 | final Iterable<FlowEntry> flowEntries = |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 141 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 142 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 143 | if (flowEntries == null || !flowEntries.iterator().hasNext()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 144 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 145 | } |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 146 | for (final FlowEntry entry : flowEntries) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 147 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 148 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 149 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 153 | * Gets flow rule. Returns the flow entry specified by the device id and |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 154 | * flow rule id. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 155 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 156 | * @param deviceId device identifier |
| 157 | * @param flowId flow rule identifier |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 158 | * @return 200 OK with a flows of given device and flow |
| 159 | * @onos.rsModel Flows |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 160 | */ |
| 161 | @GET |
| 162 | @Produces(MediaType.APPLICATION_JSON) |
| 163 | @Path("{deviceId}/{flowId}") |
| 164 | public Response getFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId, |
| 165 | @PathParam("flowId") long flowId) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 166 | final Iterable<FlowEntry> flowEntries = |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 167 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 168 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 169 | if (flowEntries == null || !flowEntries.iterator().hasNext()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 170 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 171 | } |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 172 | for (final FlowEntry entry : flowEntries) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 173 | if (entry.id().value() == flowId) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 174 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 177 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 178 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 181 | * Creates new flow rule. Creates and installs a new flow rule for the |
Andrea Campanella | 881f29f | 2016-03-03 19:18:42 -0800 | [diff] [blame] | 182 | * specified device. <br> |
| 183 | * Instructions description: |
| 184 | * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions |
| 185 | * <br> |
| 186 | * Criteria description: |
| 187 | * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria |
| 188 | * |
Madan Jampani | 0dbac7a | 2015-06-25 10:37:45 -0700 | [diff] [blame] | 189 | * @param deviceId device identifier |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 190 | * @param stream flow rule JSON |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 191 | * @return status of the request - CREATED if the JSON is correct, |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 192 | * BAD_REQUEST if the JSON is invalid |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 193 | * @onos.rsModel FlowsPost |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 194 | */ |
| 195 | @POST |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 196 | @Path("{deviceId}") |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 197 | @Consumes(MediaType.APPLICATION_JSON) |
| 198 | @Produces(MediaType.APPLICATION_JSON) |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 199 | public Response createFlow(@PathParam("deviceId") String deviceId, |
| 200 | InputStream stream) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 201 | try { |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 202 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 203 | JsonNode specifiedDeviceId = jsonTree.get("deviceId"); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 204 | if (specifiedDeviceId != null && |
| 205 | !specifiedDeviceId.asText().equals(deviceId)) { |
| 206 | throw new IllegalArgumentException( |
| 207 | "Invalid deviceId in flow creation request"); |
| 208 | } |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 209 | jsonTree.put("deviceId", deviceId); |
| 210 | FlowRule rule = codec(FlowRule.class).decode(jsonTree, this); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 211 | service.applyFlowRules(rule); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 212 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 213 | .path("flows") |
| 214 | .path(deviceId) |
Ray Milkey | 90289b0 | 2016-03-31 15:23:28 -0700 | [diff] [blame] | 215 | .path(Long.toString(rule.id().value())); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 216 | |
| 217 | return Response |
| 218 | .created(locationBuilder.build()) |
| 219 | .build(); |
| 220 | } catch (IOException ex) { |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 221 | throw new IllegalArgumentException(ex); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 222 | } |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 226 | * Remove flow rule. Removes the specified flow rule. |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 227 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 228 | * @param deviceId device identifier |
| 229 | * @param flowId flow rule identifier |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 230 | * @return 204 NO CONTENT |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 231 | */ |
| 232 | @DELETE |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 233 | @Path("{deviceId}/{flowId}") |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 234 | public Response deleteFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId, |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 235 | @PathParam("flowId") long flowId) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 236 | final Iterable<FlowEntry> flowEntries = |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 237 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 238 | |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 239 | if (!flowEntries.iterator().hasNext()) { |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 240 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 241 | } |
| 242 | |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 243 | StreamSupport.stream(flowEntries.spliterator(), false) |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 244 | .filter(entry -> entry.id().value() == flowId) |
| 245 | .forEach(service::removeFlowRules); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 246 | return Response.noContent().build(); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 249 | /** |
| 250 | * Removes a batch of flow rules. |
Ray Milkey | bee3509 | 2016-04-12 10:01:26 -0700 | [diff] [blame] | 251 | * |
| 252 | * @param stream stream for posted JSON |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 253 | * @return 204 NO CONTENT |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 254 | */ |
| 255 | @DELETE |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 256 | public Response deleteFlows(InputStream stream) { |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 257 | ListMultimap<DeviceId, Long> deviceMap = ArrayListMultimap.create(); |
| 258 | List<FlowEntry> rulesToRemove = new ArrayList<>(); |
| 259 | |
| 260 | try { |
| 261 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 262 | |
| 263 | JsonNode jsonFlows = jsonTree.get("flows"); |
| 264 | |
| 265 | jsonFlows.forEach(node -> { |
| 266 | DeviceId deviceId = |
| 267 | DeviceId.deviceId( |
| 268 | nullIsNotFound(node.get(DEVICE_ID), |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 269 | DEVICE_NOT_FOUND).asText()); |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 270 | long flowId = nullIsNotFound(node.get(FLOW_ID), |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame^] | 271 | FLOW_NOT_FOUND).asLong(); |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 272 | deviceMap.put(deviceId, flowId); |
| 273 | |
| 274 | }); |
| 275 | } catch (IOException ex) { |
| 276 | throw new IllegalArgumentException(ex); |
| 277 | } |
| 278 | |
| 279 | deviceMap.keySet().forEach(deviceId -> { |
| 280 | List<Long> flowIds = deviceMap.get(deviceId); |
| 281 | Iterable<FlowEntry> entries = service.getFlowEntries(deviceId); |
| 282 | flowIds.forEach(flowId -> { |
| 283 | StreamSupport.stream(entries.spliterator(), false) |
| 284 | .filter(entry -> flowId == entry.id().value()) |
| 285 | .forEach(rulesToRemove::add); |
| 286 | }); |
| 287 | }); |
| 288 | |
| 289 | service.removeFlowRules(rulesToRemove.toArray(new FlowEntry[0])); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 290 | return Response.noContent().build(); |
Ray Milkey | 460f9b0 | 2016-03-29 11:56:19 -0700 | [diff] [blame] | 291 | } |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 292 | } |