blob: 260a2b9939945070a2aa84b6c582b270d39408ae [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 Milkeyeb5c7172015-06-23 14:59:27 -070020import java.net.URI;
21import java.net.URISyntaxException;
22import java.util.stream.StreamSupport;
Ray Milkey4f5de002014-12-17 19:26:11 -080023
Ray Milkeyd43fe452015-05-29 09:35:12 -070024import javax.ws.rs.Consumes;
Ray Milkeyeb5c7172015-06-23 14:59:27 -070025import javax.ws.rs.DELETE;
Thomas Vachuska8683e012015-03-18 18:03:33 -070026import javax.ws.rs.GET;
Ray Milkeyd43fe452015-05-29 09:35:12 -070027import javax.ws.rs.POST;
Thomas Vachuska8683e012015-03-18 18:03:33 -070028import 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
Ray Milkeyd43fe452015-05-29 09:35:12 -070034import org.onlab.util.ItemNotFoundException;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.device.DeviceService;
38import org.onosproject.net.flow.FlowEntry;
39import org.onosproject.net.flow.FlowRule;
40import org.onosproject.net.flow.FlowRuleService;
41import org.onosproject.rest.AbstractWebResource;
42
Ray Milkeyeb5c7172015-06-23 14:59:27 -070043import com.fasterxml.jackson.databind.JsonNode;
Ray Milkeyd43fe452015-05-29 09:35:12 -070044import com.fasterxml.jackson.databind.node.ArrayNode;
45import com.fasterxml.jackson.databind.node.ObjectNode;
46
Ray Milkey4f5de002014-12-17 19:26:11 -080047/**
48 * REST resource for interacting with the inventory of flows.
49 */
50
51@Path("flows")
52public class FlowsWebResource extends AbstractWebResource {
53 public static final String DEVICE_NOT_FOUND = "Device is not found";
54
55 final FlowRuleService service = get(FlowRuleService.class);
56 final ObjectNode root = mapper().createObjectNode();
57 final ArrayNode flowsNode = root.putArray("flows");
Ray Milkey4f5de002014-12-17 19:26:11 -080058
59 /**
60 * Gets an array containing all the intents in the system.
61 *
62 * @return array of all the intents in the system
63 */
64 @GET
65 @Produces(MediaType.APPLICATION_JSON)
66 public Response getFlows() {
67
68 final Iterable<Device> devices = get(DeviceService.class).getDevices();
69 for (final Device device : devices) {
70 final Iterable<FlowEntry> deviceEntries = service.getFlowEntries(device.id());
71 if (deviceEntries != null) {
72 for (final FlowEntry entry : deviceEntries) {
Thomas Vachuska8683e012015-03-18 18:03:33 -070073 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080074 }
75 }
76 }
77
Ray Milkey3f025692015-01-26 11:15:41 -080078 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -080079 }
80
81 /**
82 * Gets the flows for a device, where the device is specified by Id.
83 *
84 * @param deviceId Id of device to look up
85 * @return flow data as an array
86 */
87 @GET
88 @Produces(MediaType.APPLICATION_JSON)
89 @Path("{deviceId}")
90 public Response getFlowByDeviceId(@PathParam("deviceId") String deviceId) {
91 final Iterable<FlowEntry> deviceEntries =
92 service.getFlowEntries(DeviceId.deviceId(deviceId));
93
94 if (!deviceEntries.iterator().hasNext()) {
95 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
96 }
97 for (final FlowEntry entry : deviceEntries) {
Thomas Vachuska8683e012015-03-18 18:03:33 -070098 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080099 }
Ray Milkey3f025692015-01-26 11:15:41 -0800100 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800101 }
102
103 /**
104 * Gets the flows for a device, where the device is specified by Id.
105 *
106 * @param deviceId Id of device to look up
Thomas Vachuska8683e012015-03-18 18:03:33 -0700107 * @param flowId Id of flow to look up
Ray Milkey4f5de002014-12-17 19:26:11 -0800108 * @return flow data as an array
109 */
110 @GET
111 @Produces(MediaType.APPLICATION_JSON)
112 @Path("{deviceId}/{flowId}")
113 public Response getFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId,
114 @PathParam("flowId") long flowId) {
115 final Iterable<FlowEntry> deviceEntries =
116 service.getFlowEntries(DeviceId.deviceId(deviceId));
117
118 if (!deviceEntries.iterator().hasNext()) {
119 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
120 }
121 for (final FlowEntry entry : deviceEntries) {
122 if (entry.id().value() == flowId) {
Thomas Vachuska8683e012015-03-18 18:03:33 -0700123 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -0800124 }
125 }
Ray Milkey3f025692015-01-26 11:15:41 -0800126 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800127 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700128
129 /**
130 * Creates a flow rule from a POST of a JSON string and attempts to apply it.
131 *
132 * @param stream input JSON
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700133 * @return status of the request - CREATED if the JSON is correct,
Ray Milkeyd43fe452015-05-29 09:35:12 -0700134 * BAD_REQUEST if the JSON is invalid
135 */
136 @POST
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700137 @Path("{deviceId}")
Ray Milkeyd43fe452015-05-29 09:35:12 -0700138 @Consumes(MediaType.APPLICATION_JSON)
139 @Produces(MediaType.APPLICATION_JSON)
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700140 public Response createFlow(@PathParam("deviceId") String deviceId,
141 InputStream stream) {
142 URI location;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700143 try {
144 FlowRuleService service = get(FlowRuleService.class);
145 ObjectNode root = (ObjectNode) mapper().readTree(stream);
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700146 JsonNode specifiedDeviceId = root.get("deviceId");
147 if (specifiedDeviceId != null &&
148 !specifiedDeviceId.asText().equals(deviceId)) {
149 throw new IllegalArgumentException(
150 "Invalid deviceId in flow creation request");
151 }
152 root.put("deviceId", deviceId);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700153 FlowRule rule = codec(FlowRule.class).decode(root, this);
154 service.applyFlowRules(rule);
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700155 location = new URI(Long.toString(rule.id().value()));
156 } catch (IOException | URISyntaxException ex) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700157 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
158 }
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700159 return Response
160 .created(location)
161 .build();
162 }
163
164 /**
165 * Removes the flows for a given device with the given flow id.
166 *
167 * @param deviceId Id of device to look up
168 * @param flowId Id of flow to look up
169 */
170 @DELETE
171 @Produces(MediaType.APPLICATION_JSON)
172 @Path("{deviceId}/{flowId}")
173 public void deleteFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId,
174 @PathParam("flowId") long flowId) {
175 final Iterable<FlowEntry> deviceEntries =
176 service.getFlowEntries(DeviceId.deviceId(deviceId));
177
178 if (!deviceEntries.iterator().hasNext()) {
179 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
180 }
181
182 StreamSupport.stream(deviceEntries.spliterator(), false)
183 .filter(entry -> entry.id().value() == flowId)
184 .forEach(service::removeFlowRules);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700185 }
186
Ray Milkey4f5de002014-12-17 19:26:11 -0800187}