blob: 49840c4cc841d6e45baea260d722f2910d2a0c1c [file] [log] [blame]
Ray Milkey4f5de002014-12-17 19:26:11 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Ray Milkey4f5de002014-12-17 19:26:11 -08003 *
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 Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Ray Milkey4f5de002014-12-17 19:26:11 -080017
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;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070026import org.onosproject.rest.AbstractWebResource;
Ray Milkey4f5de002014-12-17 19:26:11 -080027
Thomas Vachuska8683e012015-03-18 18:03:33 -070028import javax.ws.rs.GET;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
Ray Milkey4f5de002014-12-17 19:26:11 -080034
35/**
36 * REST resource for interacting with the inventory of flows.
37 */
38
39@Path("flows")
40public class FlowsWebResource extends AbstractWebResource {
41 public static final String DEVICE_NOT_FOUND = "Device is not found";
42
43 final FlowRuleService service = get(FlowRuleService.class);
44 final ObjectNode root = mapper().createObjectNode();
45 final ArrayNode flowsNode = root.putArray("flows");
Ray Milkey4f5de002014-12-17 19:26:11 -080046
47 /**
48 * Gets an array containing all the intents in the system.
49 *
50 * @return array of all the intents in the system
51 */
52 @GET
53 @Produces(MediaType.APPLICATION_JSON)
54 public Response getFlows() {
55
56 final Iterable<Device> devices = get(DeviceService.class).getDevices();
57 for (final Device device : devices) {
58 final Iterable<FlowEntry> deviceEntries = service.getFlowEntries(device.id());
59 if (deviceEntries != null) {
60 for (final FlowEntry entry : deviceEntries) {
Thomas Vachuska8683e012015-03-18 18:03:33 -070061 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080062 }
63 }
64 }
65
Ray Milkey3f025692015-01-26 11:15:41 -080066 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -080067 }
68
69 /**
70 * Gets the flows for a device, where the device is specified by Id.
71 *
72 * @param deviceId Id of device to look up
73 * @return flow data as an array
74 */
75 @GET
76 @Produces(MediaType.APPLICATION_JSON)
77 @Path("{deviceId}")
78 public Response getFlowByDeviceId(@PathParam("deviceId") String deviceId) {
79 final Iterable<FlowEntry> deviceEntries =
80 service.getFlowEntries(DeviceId.deviceId(deviceId));
81
82 if (!deviceEntries.iterator().hasNext()) {
83 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
84 }
85 for (final FlowEntry entry : deviceEntries) {
Thomas Vachuska8683e012015-03-18 18:03:33 -070086 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080087 }
Ray Milkey3f025692015-01-26 11:15:41 -080088 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -080089 }
90
91 /**
92 * Gets the flows for a device, where the device is specified by Id.
93 *
94 * @param deviceId Id of device to look up
Thomas Vachuska8683e012015-03-18 18:03:33 -070095 * @param flowId Id of flow to look up
Ray Milkey4f5de002014-12-17 19:26:11 -080096 * @return flow data as an array
97 */
98 @GET
99 @Produces(MediaType.APPLICATION_JSON)
100 @Path("{deviceId}/{flowId}")
101 public Response getFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId,
102 @PathParam("flowId") long flowId) {
103 final Iterable<FlowEntry> deviceEntries =
104 service.getFlowEntries(DeviceId.deviceId(deviceId));
105
106 if (!deviceEntries.iterator().hasNext()) {
107 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
108 }
109 for (final FlowEntry entry : deviceEntries) {
110 if (entry.id().value() == flowId) {
Thomas Vachuska8683e012015-03-18 18:03:33 -0700111 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -0800112 }
113 }
Ray Milkey3f025692015-01-26 11:15:41 -0800114 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800115 }
116}