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