Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
| 16 | package org.onosproject.rest; |
| 17 | |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 20 | import org.onlab.util.ItemNotFoundException; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.device.DeviceService; |
| 24 | import org.onosproject.net.flow.FlowEntry; |
| 25 | import org.onosproject.net.flow.FlowRuleService; |
| 26 | |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 27 | import javax.ws.rs.GET; |
| 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 | |
| 34 | /** |
| 35 | * REST resource for interacting with the inventory of flows. |
| 36 | */ |
| 37 | |
| 38 | @Path("flows") |
| 39 | public class FlowsWebResource extends AbstractWebResource { |
| 40 | public static final String DEVICE_NOT_FOUND = "Device is not found"; |
| 41 | |
| 42 | final FlowRuleService service = get(FlowRuleService.class); |
| 43 | final ObjectNode root = mapper().createObjectNode(); |
| 44 | final ArrayNode flowsNode = root.putArray("flows"); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Gets an array containing all the intents in the system. |
| 48 | * |
| 49 | * @return array of all the intents in the system |
| 50 | */ |
| 51 | @GET |
| 52 | @Produces(MediaType.APPLICATION_JSON) |
| 53 | public Response getFlows() { |
| 54 | |
| 55 | final Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 56 | for (final Device device : devices) { |
| 57 | final Iterable<FlowEntry> deviceEntries = service.getFlowEntries(device.id()); |
| 58 | if (deviceEntries != null) { |
| 59 | for (final FlowEntry entry : deviceEntries) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 60 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 65 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Gets the flows for a device, where the device is specified by Id. |
| 70 | * |
| 71 | * @param deviceId Id of device to look up |
| 72 | * @return flow data as an array |
| 73 | */ |
| 74 | @GET |
| 75 | @Produces(MediaType.APPLICATION_JSON) |
| 76 | @Path("{deviceId}") |
| 77 | public Response getFlowByDeviceId(@PathParam("deviceId") String deviceId) { |
| 78 | final Iterable<FlowEntry> deviceEntries = |
| 79 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 80 | |
| 81 | if (!deviceEntries.iterator().hasNext()) { |
| 82 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 83 | } |
| 84 | for (final FlowEntry entry : deviceEntries) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 85 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 86 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 87 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Gets the flows for a device, where the device is specified by Id. |
| 92 | * |
| 93 | * @param deviceId Id of device to look up |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 94 | * @param flowId Id of flow to look up |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 95 | * @return flow data as an array |
| 96 | */ |
| 97 | @GET |
| 98 | @Produces(MediaType.APPLICATION_JSON) |
| 99 | @Path("{deviceId}/{flowId}") |
| 100 | public Response getFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId, |
| 101 | @PathParam("flowId") long flowId) { |
| 102 | final Iterable<FlowEntry> deviceEntries = |
| 103 | service.getFlowEntries(DeviceId.deviceId(deviceId)); |
| 104 | |
| 105 | if (!deviceEntries.iterator().hasNext()) { |
| 106 | throw new ItemNotFoundException(DEVICE_NOT_FOUND); |
| 107 | } |
| 108 | for (final FlowEntry entry : deviceEntries) { |
| 109 | if (entry.id().value() == flowId) { |
Thomas Vachuska | 8683e01 | 2015-03-18 18:03:33 -0700 | [diff] [blame] | 110 | flowsNode.add(codec(FlowEntry.class).encode(entry, this)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 113 | return ok(root).build(); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 114 | } |
| 115 | } |