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 | |
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 | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 21 | import org.onlab.util.ItemNotFoundException; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.device.DeviceService; |
| 25 | import org.onosproject.net.flow.FlowEntry; |
| 26 | import org.onosproject.net.flow.FlowRule; |
| 27 | import org.onosproject.net.flow.FlowRuleService; |
| 28 | import org.onosproject.rest.AbstractWebResource; |
| 29 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 30 | import javax.ws.rs.Consumes; |
| 31 | import javax.ws.rs.DELETE; |
| 32 | import javax.ws.rs.GET; |
| 33 | import javax.ws.rs.POST; |
| 34 | import javax.ws.rs.Path; |
| 35 | import javax.ws.rs.PathParam; |
| 36 | import javax.ws.rs.Produces; |
| 37 | import javax.ws.rs.core.Context; |
| 38 | import javax.ws.rs.core.MediaType; |
| 39 | import javax.ws.rs.core.Response; |
| 40 | import javax.ws.rs.core.UriBuilder; |
| 41 | import javax.ws.rs.core.UriInfo; |
| 42 | import java.io.IOException; |
| 43 | import java.io.InputStream; |
| 44 | import java.util.stream.StreamSupport; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 45 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 46 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 47 | * Query and program flow rules. |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | @Path("flows") |
| 51 | public class FlowsWebResource extends AbstractWebResource { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 52 | |
| 53 | @Context |
| 54 | UriInfo uriInfo; |
| 55 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 56 | public static final String DEVICE_NOT_FOUND = "Device is not found"; |
| 57 | |
| 58 | final FlowRuleService service = get(FlowRuleService.class); |
| 59 | final ObjectNode root = mapper().createObjectNode(); |
| 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 | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 84 | * Get flow entries of a device. Returns array of all flow rules for the |
| 85 | * specified device. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 86 | * @onos.rsModel Flows |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 87 | * @param deviceId device identifier |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 88 | * @return flow data as an array |
| 89 | */ |
| 90 | @GET |
| 91 | @Produces(MediaType.APPLICATION_JSON) |
| 92 | @Path("{deviceId}") |
| 93 | public Response getFlowByDeviceId(@PathParam("deviceId") String deviceId) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 94 | final Iterable<FlowEntry> flowEntries = |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 95 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 96 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 97 | if (flowEntries == null || !flowEntries.iterator().hasNext()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 98 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 99 | } |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 100 | for (final FlowEntry entry : flowEntries) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 101 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 102 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 103 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 107 | * Get flow rule. Returns the flow entry specified by the device id and |
| 108 | * flow rule id. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 109 | * @onos.rsModel Flows |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 110 | * @param deviceId device identifier |
| 111 | * @param flowId flow rule identifier |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 112 | * @return flow data as an array |
| 113 | */ |
| 114 | @GET |
| 115 | @Produces(MediaType.APPLICATION_JSON) |
| 116 | @Path("{deviceId}/{flowId}") |
| 117 | public Response getFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId, |
| 118 | @PathParam("flowId") long flowId) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 119 | final Iterable<FlowEntry> flowEntries = |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 120 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 121 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 122 | if (flowEntries == null || !flowEntries.iterator().hasNext()) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 123 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 124 | } |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 125 | for (final FlowEntry entry : flowEntries) { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 126 | if (entry.id().value() == flowId) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 127 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 130 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 131 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 132 | |
| 133 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 134 | * 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] | 135 | * specified device. <br> |
| 136 | * Instructions description: |
| 137 | * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions |
| 138 | * <br> |
| 139 | * Criteria description: |
| 140 | * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria |
| 141 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 142 | * @onos.rsModel FlowsPost |
Madan Jampani | 0dbac7a | 2015-06-25 10:37:45 -0700 | [diff] [blame] | 143 | * @param deviceId device identifier |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 144 | * @param stream flow rule JSON |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 145 | * @return status of the request - CREATED if the JSON is correct, |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 146 | * BAD_REQUEST if the JSON is invalid |
| 147 | */ |
| 148 | @POST |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 149 | @Path("{deviceId}") |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 150 | @Consumes(MediaType.APPLICATION_JSON) |
| 151 | @Produces(MediaType.APPLICATION_JSON) |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 152 | public Response createFlow(@PathParam("deviceId") String deviceId, |
| 153 | InputStream stream) { |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 154 | try { |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 155 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 156 | JsonNode specifiedDeviceId = jsonTree.get("deviceId"); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 157 | if (specifiedDeviceId != null && |
| 158 | !specifiedDeviceId.asText().equals(deviceId)) { |
| 159 | throw new IllegalArgumentException( |
| 160 | "Invalid deviceId in flow creation request"); |
| 161 | } |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 162 | jsonTree.put("deviceId", deviceId); |
| 163 | FlowRule rule = codec(FlowRule.class).decode(jsonTree, this); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 164 | service.applyFlowRules(rule); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 165 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 166 | .path("flows") |
| 167 | .path(deviceId) |
| 168 | .path(rule.id().toString()); |
| 169 | |
| 170 | return Response |
| 171 | .created(locationBuilder.build()) |
| 172 | .build(); |
| 173 | } catch (IOException ex) { |
Ray Milkey | 5d915f4 | 2015-08-13 10:27:53 -0700 | [diff] [blame] | 174 | throw new IllegalArgumentException(ex); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 175 | } |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 179 | * Remove flow rule. Removes the specified flow rule. |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 180 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 181 | * @param deviceId device identifier |
| 182 | * @param flowId flow rule identifier |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 183 | */ |
| 184 | @DELETE |
| 185 | @Produces(MediaType.APPLICATION_JSON) |
| 186 | @Path("{deviceId}/{flowId}") |
| 187 | public void deleteFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId, |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 188 | @PathParam("flowId") long flowId) { |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 189 | final Iterable<FlowEntry> flowEntries = |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 190 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 191 | |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 192 | if (!flowEntries.iterator().hasNext()) { |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 193 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 194 | } |
| 195 | |
Phaneendra Manda | aec654c | 2015-09-23 19:02:23 +0530 | [diff] [blame] | 196 | StreamSupport.stream(flowEntries.spliterator(), false) |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 197 | .filter(entry -> entry.id().value() == flowId) |
| 198 | .forEach(service::removeFlowRules); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 201 | } |