blob: 9c3926818068c0c84969d75544fe67eaf533dbd7 [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
Ray Milkeyd43fe452015-05-29 09:35:12 -070018import java.io.IOException;
19import java.io.InputStream;
Ray Milkey4f5de002014-12-17 19:26:11 -080020
Ray Milkeyd43fe452015-05-29 09:35:12 -070021import javax.ws.rs.Consumes;
Thomas Vachuska8683e012015-03-18 18:03:33 -070022import javax.ws.rs.GET;
Ray Milkeyd43fe452015-05-29 09:35:12 -070023import javax.ws.rs.POST;
Thomas Vachuska8683e012015-03-18 18:03:33 -070024import javax.ws.rs.Path;
25import javax.ws.rs.PathParam;
26import javax.ws.rs.Produces;
27import javax.ws.rs.core.MediaType;
28import javax.ws.rs.core.Response;
Ray Milkey4f5de002014-12-17 19:26:11 -080029
Ray Milkeyd43fe452015-05-29 09:35:12 -070030import org.onlab.util.ItemNotFoundException;
31import org.onosproject.net.Device;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.device.DeviceService;
34import org.onosproject.net.flow.FlowEntry;
35import org.onosproject.net.flow.FlowRule;
36import org.onosproject.net.flow.FlowRuleService;
37import org.onosproject.rest.AbstractWebResource;
38
39import com.fasterxml.jackson.databind.node.ArrayNode;
40import com.fasterxml.jackson.databind.node.ObjectNode;
41
Ray Milkey4f5de002014-12-17 19:26:11 -080042/**
43 * REST resource for interacting with the inventory of flows.
44 */
45
46@Path("flows")
47public 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 Milkey4f5de002014-12-17 19:26:11 -080053
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 Vachuska8683e012015-03-18 18:03:33 -070068 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080069 }
70 }
71 }
72
Ray Milkey3f025692015-01-26 11:15:41 -080073 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -080074 }
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 Vachuska8683e012015-03-18 18:03:33 -070093 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080094 }
Ray Milkey3f025692015-01-26 11:15:41 -080095 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -080096 }
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 Vachuska8683e012015-03-18 18:03:33 -0700102 * @param flowId Id of flow to look up
Ray Milkey4f5de002014-12-17 19:26:11 -0800103 * @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 Vachuska8683e012015-03-18 18:03:33 -0700118 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -0800119 }
120 }
Ray Milkey3f025692015-01-26 11:15:41 -0800121 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800122 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700123
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 Milkey4f5de002014-12-17 19:26:11 -0800146}