blob: be3288c17420d998ae1ecc2ce4c76077e1b671bd [file] [log] [blame]
Ray Milkey4f5de002014-12-17 19:26:11 -08001/*
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 */
16package org.onosproject.rest;
17
Thomas Vachuska8683e012015-03-18 18:03:33 -070018import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey4f5de002014-12-17 19:26:11 -080020import org.onlab.util.ItemNotFoundException;
Ray Milkey4f5de002014-12-17 19:26:11 -080021import org.onosproject.net.Device;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.device.DeviceService;
24import org.onosproject.net.flow.FlowEntry;
25import org.onosproject.net.flow.FlowRuleService;
26
Thomas Vachuska8683e012015-03-18 18:03:33 -070027import javax.ws.rs.GET;
28import javax.ws.rs.Path;
29import javax.ws.rs.PathParam;
30import javax.ws.rs.Produces;
31import javax.ws.rs.core.MediaType;
32import javax.ws.rs.core.Response;
Ray Milkey4f5de002014-12-17 19:26:11 -080033
34/**
35 * REST resource for interacting with the inventory of flows.
36 */
37
38@Path("flows")
39public 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 Milkey4f5de002014-12-17 19:26:11 -080045
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 Vachuska8683e012015-03-18 18:03:33 -070060 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080061 }
62 }
63 }
64
Ray Milkey3f025692015-01-26 11:15:41 -080065 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -080066 }
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 Vachuska8683e012015-03-18 18:03:33 -070085 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080086 }
Ray Milkey3f025692015-01-26 11:15:41 -080087 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -080088 }
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 Vachuska8683e012015-03-18 18:03:33 -070094 * @param flowId Id of flow to look up
Ray Milkey4f5de002014-12-17 19:26:11 -080095 * @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 Vachuska8683e012015-03-18 18:03:33 -0700110 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -0800111 }
112 }
Ray Milkey3f025692015-01-26 11:15:41 -0800113 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800114 }
115}