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