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 | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 20 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame^] | 21 | import javax.ws.rs.Consumes; |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 22 | import javax.ws.rs.GET; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame^] | 23 | import javax.ws.rs.POST; |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 24 | import javax.ws.rs.Path; |
| 25 | import javax.ws.rs.PathParam; |
| 26 | import javax.ws.rs.Produces; |
| 27 | import javax.ws.rs.core.MediaType; |
| 28 | import javax.ws.rs.core.Response; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 29 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame^] | 30 | import org.onlab.util.ItemNotFoundException; |
| 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.device.DeviceService; |
| 34 | import org.onosproject.net.flow.FlowEntry; |
| 35 | import org.onosproject.net.flow.FlowRule; |
| 36 | import org.onosproject.net.flow.FlowRuleService; |
| 37 | import org.onosproject.rest.AbstractWebResource; |
| 38 | |
| 39 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 40 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 41 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 42 | /** |
| 43 | * REST resource for interacting with the inventory of flows. |
| 44 | */ |
| 45 | |
| 46 | @Path("flows") |
| 47 | public class FlowsWebResource extends AbstractWebResource { |
| 48 | public static final String DEVICE_NOT_FOUND = "Device is not found"; |
| 49 | |
| 50 | final FlowRuleService service = get(FlowRuleService.class); |
| 51 | final ObjectNode root = mapper().createObjectNode(); |
| 52 | final ArrayNode flowsNode = root.putArray("flows"); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Gets an array containing all the intents in the system. |
| 56 | * |
| 57 | * @return array of all the intents in the system |
| 58 | */ |
| 59 | @GET |
| 60 | @Produces(MediaType.APPLICATION_JSON) |
| 61 | public Response getFlows() { |
| 62 | |
| 63 | final Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 64 | for (final Device device : devices) { |
| 65 | final Iterable<FlowEntry> deviceEntries = service.getFlowEntries(device.id()); |
| 66 | if (deviceEntries != null) { |
| 67 | for (final FlowEntry entry : deviceEntries) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 68 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 73 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets the flows for a device, where the device is specified by Id. |
| 78 | * |
| 79 | * @param deviceId Id of device to look up |
| 80 | * @return flow data as an array |
| 81 | */ |
| 82 | @GET |
| 83 | @Produces(MediaType.APPLICATION_JSON) |
| 84 | @Path("{deviceId}") |
| 85 | public Response getFlowByDeviceId(@PathParam("deviceId") String deviceId) { |
| 86 | final Iterable<FlowEntry> deviceEntries = |
| 87 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 88 | |
| 89 | if (!deviceEntries.iterator().hasNext()) { |
| 90 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 91 | } |
| 92 | for (final FlowEntry entry : deviceEntries) { |
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 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 95 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Gets the flows for a device, where the device is specified by Id. |
| 100 | * |
| 101 | * @param deviceId Id of device to look up |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 102 | * @param flowId Id of flow to look up |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 103 | * @return flow data as an array |
| 104 | */ |
| 105 | @GET |
| 106 | @Produces(MediaType.APPLICATION_JSON) |
| 107 | @Path("{deviceId}/{flowId}") |
| 108 | public Response getFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId, |
| 109 | @PathParam("flowId") long flowId) { |
| 110 | final Iterable<FlowEntry> deviceEntries = |
| 111 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 112 | |
| 113 | if (!deviceEntries.iterator().hasNext()) { |
| 114 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 115 | } |
| 116 | for (final FlowEntry entry : deviceEntries) { |
| 117 | if (entry.id().value() == flowId) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 118 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 121 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 122 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame^] | 123 | |
| 124 | /** |
| 125 | * Creates a flow rule from a POST of a JSON string and attempts to apply it. |
| 126 | * |
| 127 | * @param stream input JSON |
| 128 | * @return status of the request - ACCEPTED if the JSON is correct, |
| 129 | * BAD_REQUEST if the JSON is invalid |
| 130 | */ |
| 131 | @POST |
| 132 | @Consumes(MediaType.APPLICATION_JSON) |
| 133 | @Produces(MediaType.APPLICATION_JSON) |
| 134 | public Response createFlow(InputStream stream) { |
| 135 | try { |
| 136 | FlowRuleService service = get(FlowRuleService.class); |
| 137 | ObjectNode root = (ObjectNode) mapper().readTree(stream); |
| 138 | FlowRule rule = codec(FlowRule.class).decode(root, this); |
| 139 | service.applyFlowRules(rule); |
| 140 | } catch (IOException ex) { |
| 141 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); |
| 142 | } |
| 143 | return Response.status(Response.Status.ACCEPTED).build(); |
| 144 | } |
| 145 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 146 | } |