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