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