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