Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 18 | import java.io.IOException; |
| 19 | import java.io.InputStream; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 20 | import java.net.URI; |
| 21 | import java.net.URISyntaxException; |
Thomas Vachuska | 57a3099 | 2016-03-28 10:36:30 -0700 | [diff] [blame^] | 22 | import java.util.List; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 23 | import java.util.stream.StreamSupport; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 24 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 25 | import javax.ws.rs.Consumes; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 26 | import javax.ws.rs.DELETE; |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 27 | import javax.ws.rs.GET; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 28 | import javax.ws.rs.POST; |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.PathParam; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 34 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 35 | import org.onlab.util.ItemNotFoundException; |
| 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.device.DeviceService; |
| 39 | import org.onosproject.net.flow.FlowEntry; |
| 40 | import org.onosproject.net.flow.FlowRule; |
| 41 | import org.onosproject.net.flow.FlowRuleService; |
| 42 | import org.onosproject.rest.AbstractWebResource; |
| 43 | |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 44 | import com.fasterxml.jackson.databind.JsonNode; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 45 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 46 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 47 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 48 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 49 | * Query and program flow rules. |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | @Path("flows") |
| 53 | public class FlowsWebResource extends AbstractWebResource { |
Thomas Vachuska | 57a3099 | 2016-03-28 10:36:30 -0700 | [diff] [blame^] | 54 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 55 | public static final String DEVICE_NOT_FOUND = "Device is not found"; |
Thomas Vachuska | 57a3099 | 2016-03-28 10:36:30 -0700 | [diff] [blame^] | 56 | public static final String FLOWS = "flows"; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 57 | |
| 58 | final FlowRuleService service = get(FlowRuleService.class); |
| 59 | final ObjectNode root = mapper().createObjectNode(); |
Thomas Vachuska | 57a3099 | 2016-03-28 10:36:30 -0700 | [diff] [blame^] | 60 | final ArrayNode flowsNode = root.putArray(FLOWS); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 61 | |
| 62 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 63 | * Get all flow entries. Returns array of all flow rules in the system. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 64 | * @onos.rsModel Flows |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 65 | * @return array of all the intents in the system |
| 66 | */ |
| 67 | @GET |
| 68 | @Produces(MediaType.APPLICATION_JSON) |
| 69 | public Response getFlows() { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 70 | final Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 71 | for (final Device device : devices) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 72 | final Iterable<FlowEntry> flowEntries = service.getFlowEntries(device.id()); |
| 73 | if (flowEntries != null) { |
| 74 | for (final FlowEntry entry : flowEntries) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 75 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 80 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Thomas Vachuska | 57a3099 | 2016-03-28 10:36:30 -0700 | [diff] [blame^] | 84 | * Create new flow rules. Creates and installs a new flow rules.<br> |
| 85 | * Instructions description: |
| 86 | * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions |
| 87 | * <br> |
| 88 | * Criteria description: |
| 89 | * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria |
| 90 | * |
| 91 | * @onos.rsModel FlowsBatchPost |
| 92 | * @param stream flow rules JSON |
| 93 | * @return status of the request - CREATED if the JSON is correct, |
| 94 | * BAD_REQUEST if the JSON is invalid |
| 95 | */ |
| 96 | @POST |
| 97 | @Consumes(MediaType.APPLICATION_JSON) |
| 98 | @Produces(MediaType.APPLICATION_JSON) |
| 99 | public Response createFlows(InputStream stream) { |
| 100 | try { |
| 101 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 102 | ArrayNode flowsArray = (ArrayNode) jsonTree.get(FLOWS); |
| 103 | List<FlowRule> rules = codec(FlowRule.class).decode(flowsArray, this); |
| 104 | service.applyFlowRules(rules.toArray(new FlowRule[rules.size()])); |
| 105 | } catch (IOException ex) { |
| 106 | throw new IllegalArgumentException(ex); |
| 107 | } |
| 108 | return Response.ok().build(); |
| 109 | } |
| 110 | |
| 111 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 112 | * Get flow entries of a device. Returns array of all flow rules for the |
| 113 | * specified device. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 114 | * @onos.rsModel Flows |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 115 | * @param deviceId device identifier |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 116 | * @return flow data as an array |
| 117 | */ |
| 118 | @GET |
| 119 | @Produces(MediaType.APPLICATION_JSON) |
| 120 | @Path("{deviceId}") |
| 121 | public Response getFlowByDeviceId(@PathParam("deviceId") String deviceId) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 122 | final Iterable<FlowEntry> flowEntries = |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 123 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 124 | |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 125 | if (!flowEntries.iterator().hasNext()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 126 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 127 | } |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 128 | for (final FlowEntry entry : flowEntries) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 129 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 130 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 131 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 135 | * Get flow rule. Returns the flow entry specified by the device id and |
| 136 | * flow rule id. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 137 | * @onos.rsModel Flows |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 138 | * @param deviceId device identifier |
| 139 | * @param flowId flow rule identifier |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 140 | * @return flow data as an array |
| 141 | */ |
| 142 | @GET |
| 143 | @Produces(MediaType.APPLICATION_JSON) |
| 144 | @Path("{deviceId}/{flowId}") |
| 145 | public Response getFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId, |
| 146 | @PathParam("flowId") long flowId) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 147 | final Iterable<FlowEntry> flowEntries = |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 148 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 149 | |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 150 | if (!flowEntries.iterator().hasNext()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 151 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 152 | } |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 153 | for (final FlowEntry entry : flowEntries) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 154 | if (entry.id().value() == flowId) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 155 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 158 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 159 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 162 | * Create new flow rule. Creates and installs a new flow rule for the |
Andrea Campanella | 881f29f | 2016-03-03 19:18:42 -0800 | [diff] [blame] | 163 | * specified device. <br> |
| 164 | * Instructions description: |
| 165 | * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions |
| 166 | * <br> |
| 167 | * Criteria description: |
| 168 | * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria |
| 169 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 170 | * @onos.rsModel FlowsPost |
Madan Jampani | 0dbac7a | 2015-06-25 10:37:45 -0700 | [diff] [blame] | 171 | * @param deviceId device identifier |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 172 | * @param stream flow rule JSON |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 173 | * @return status of the request - CREATED if the JSON is correct, |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 174 | * BAD_REQUEST if the JSON is invalid |
| 175 | */ |
| 176 | @POST |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 177 | @Path("{deviceId}") |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 178 | @Consumes(MediaType.APPLICATION_JSON) |
| 179 | @Produces(MediaType.APPLICATION_JSON) |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 180 | public Response createFlow(@PathParam("deviceId") String deviceId, |
| 181 | InputStream stream) { |
| 182 | URI location; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 183 | try { |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 184 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 185 | JsonNode specifiedDeviceId = jsonTree.get("deviceId"); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 186 | if (specifiedDeviceId != null && |
| 187 | !specifiedDeviceId.asText().equals(deviceId)) { |
| 188 | throw new IllegalArgumentException( |
| 189 | "Invalid deviceId in flow creation request"); |
| 190 | } |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 191 | jsonTree.put("deviceId", deviceId); |
| 192 | FlowRule rule = codec(FlowRule.class).decode(jsonTree, this); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 193 | service.applyFlowRules(rule); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 194 | location = new URI(Long.toString(rule.id().value())); |
| 195 | } catch (IOException | URISyntaxException ex) { |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 196 | throw new IllegalArgumentException(ex); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 197 | } |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 198 | |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 199 | return Response |
| 200 | .created(location) |
| 201 | .build(); |
| 202 | } |
| 203 | |
| 204 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 205 | * Remove flow rule. Removes the specified flow rule. |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 206 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 207 | * @param deviceId device identifier |
| 208 | * @param flowId flow rule identifier |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 209 | */ |
| 210 | @DELETE |
| 211 | @Produces(MediaType.APPLICATION_JSON) |
| 212 | @Path("{deviceId}/{flowId}") |
| 213 | public void deleteFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId, |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 214 | @PathParam("flowId") long flowId) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 215 | final Iterable<FlowEntry> flowEntries = |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 216 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 217 | |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 218 | if (!flowEntries.iterator().hasNext()) { |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 219 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 220 | } |
| 221 | |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 222 | StreamSupport.stream(flowEntries.spliterator(), false) |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 223 | .filter(entry -> entry.id().value() == flowId) |
| 224 | .forEach(service::removeFlowRules); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 227 | } |