blob: 0404967785c19a36d64862b6f3856eb67d696cde [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
Jian Li9d616492016-03-09 10:52:49 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkeyd43fe452015-05-29 09:35:12 -070021import org.onlab.util.ItemNotFoundException;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.device.DeviceService;
25import org.onosproject.net.flow.FlowEntry;
26import org.onosproject.net.flow.FlowRule;
27import org.onosproject.net.flow.FlowRuleService;
28import org.onosproject.rest.AbstractWebResource;
29
Jian Li9d616492016-03-09 10:52:49 -080030import javax.ws.rs.Consumes;
31import javax.ws.rs.DELETE;
32import javax.ws.rs.GET;
33import javax.ws.rs.POST;
34import javax.ws.rs.Path;
35import javax.ws.rs.PathParam;
36import javax.ws.rs.Produces;
37import javax.ws.rs.core.Context;
38import javax.ws.rs.core.MediaType;
39import javax.ws.rs.core.Response;
40import javax.ws.rs.core.UriBuilder;
41import javax.ws.rs.core.UriInfo;
42import java.io.IOException;
43import java.io.InputStream;
44import java.util.stream.StreamSupport;
Ray Milkeyd43fe452015-05-29 09:35:12 -070045
Ray Milkey4f5de002014-12-17 19:26:11 -080046/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070047 * Query and program flow rules.
Ray Milkey4f5de002014-12-17 19:26:11 -080048 */
49
50@Path("flows")
51public class FlowsWebResource extends AbstractWebResource {
Jian Li9d616492016-03-09 10:52:49 -080052
53 @Context
54 UriInfo uriInfo;
55
Ray Milkey4f5de002014-12-17 19:26:11 -080056 public static final String DEVICE_NOT_FOUND = "Device is not found";
57
58 final FlowRuleService service = get(FlowRuleService.class);
59 final ObjectNode root = mapper().createObjectNode();
60 final ArrayNode flowsNode = root.putArray("flows");
Ray Milkey4f5de002014-12-17 19:26:11 -080061
62 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070063 * Get all flow entries. Returns array of all flow rules in the system.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080064 * @onos.rsModel Flows
Ray Milkey4f5de002014-12-17 19:26:11 -080065 * @return array of all the intents in the system
66 */
67 @GET
68 @Produces(MediaType.APPLICATION_JSON)
69 public Response getFlows() {
Ray Milkey4f5de002014-12-17 19:26:11 -080070 final Iterable<Device> devices = get(DeviceService.class).getDevices();
71 for (final Device device : devices) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +053072 final Iterable<FlowEntry> flowEntries = service.getFlowEntries(device.id());
73 if (flowEntries != null) {
74 for (final FlowEntry entry : flowEntries) {
Thomas Vachuska8683e012015-03-18 18:03:33 -070075 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -080076 }
77 }
78 }
79
Ray Milkey3f025692015-01-26 11:15:41 -080080 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -080081 }
82
83 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070084 * Get flow entries of a device. Returns array of all flow rules for the
85 * specified device.
Andrea Campanella10c4adc2015-12-03 15:27:54 -080086 * @onos.rsModel Flows
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070087 * @param deviceId device identifier
Ray Milkey4f5de002014-12-17 19:26:11 -080088 * @return flow data as an array
89 */
90 @GET
91 @Produces(MediaType.APPLICATION_JSON)
92 @Path("{deviceId}")
93 public Response getFlowByDeviceId(@PathParam("deviceId") String deviceId) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +053094 final Iterable<FlowEntry> flowEntries =
Ray Milkey4f5de002014-12-17 19:26:11 -080095 service.getFlowEntries(DeviceId.deviceId(deviceId));
96
Jian Li9d616492016-03-09 10:52:49 -080097 if (flowEntries == null || !flowEntries.iterator().hasNext()) {
Ray Milkey4f5de002014-12-17 19:26:11 -080098 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
99 }
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530100 for (final FlowEntry entry : flowEntries) {
Thomas Vachuska8683e012015-03-18 18:03:33 -0700101 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -0800102 }
Ray Milkey3f025692015-01-26 11:15:41 -0800103 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800104 }
105
106 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700107 * Get flow rule. Returns the flow entry specified by the device id and
108 * flow rule id.
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800109 * @onos.rsModel Flows
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700110 * @param deviceId device identifier
111 * @param flowId flow rule identifier
Ray Milkey4f5de002014-12-17 19:26:11 -0800112 * @return flow data as an array
113 */
114 @GET
115 @Produces(MediaType.APPLICATION_JSON)
116 @Path("{deviceId}/{flowId}")
117 public Response getFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId,
118 @PathParam("flowId") long flowId) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530119 final Iterable<FlowEntry> flowEntries =
Ray Milkey4f5de002014-12-17 19:26:11 -0800120 service.getFlowEntries(DeviceId.deviceId(deviceId));
121
Jian Li9d616492016-03-09 10:52:49 -0800122 if (flowEntries == null || !flowEntries.iterator().hasNext()) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800123 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
124 }
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530125 for (final FlowEntry entry : flowEntries) {
Ray Milkey4f5de002014-12-17 19:26:11 -0800126 if (entry.id().value() == flowId) {
Thomas Vachuska8683e012015-03-18 18:03:33 -0700127 flowsNode.add(codec(FlowEntry.class).encode(entry, this));
Ray Milkey4f5de002014-12-17 19:26:11 -0800128 }
129 }
Ray Milkey3f025692015-01-26 11:15:41 -0800130 return ok(root).build();
Ray Milkey4f5de002014-12-17 19:26:11 -0800131 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700132
133 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700134 * Create new flow rule. Creates and installs a new flow rule for the
Andrea Campanella881f29f2016-03-03 19:18:42 -0800135 * specified device. <br>
136 * Instructions description:
137 * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Instructions
138 * <br>
139 * Criteria description:
140 * https://wiki.onosproject.org/display/ONOS/Flow+Rule+Criteria
141 *
Andrea Campanella10c4adc2015-12-03 15:27:54 -0800142 * @onos.rsModel FlowsPost
Madan Jampani0dbac7a2015-06-25 10:37:45 -0700143 * @param deviceId device identifier
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700144 * @param stream flow rule JSON
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700145 * @return status of the request - CREATED if the JSON is correct,
Ray Milkeyd43fe452015-05-29 09:35:12 -0700146 * BAD_REQUEST if the JSON is invalid
147 */
148 @POST
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700149 @Path("{deviceId}")
Ray Milkeyd43fe452015-05-29 09:35:12 -0700150 @Consumes(MediaType.APPLICATION_JSON)
151 @Produces(MediaType.APPLICATION_JSON)
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700152 public Response createFlow(@PathParam("deviceId") String deviceId,
153 InputStream stream) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700154 try {
Ray Milkey5d915f42015-08-13 10:27:53 -0700155 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
156 JsonNode specifiedDeviceId = jsonTree.get("deviceId");
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700157 if (specifiedDeviceId != null &&
158 !specifiedDeviceId.asText().equals(deviceId)) {
159 throw new IllegalArgumentException(
160 "Invalid deviceId in flow creation request");
161 }
Ray Milkey5d915f42015-08-13 10:27:53 -0700162 jsonTree.put("deviceId", deviceId);
163 FlowRule rule = codec(FlowRule.class).decode(jsonTree, this);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700164 service.applyFlowRules(rule);
Jian Li9d616492016-03-09 10:52:49 -0800165 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
166 .path("flows")
167 .path(deviceId)
168 .path(rule.id().toString());
169
170 return Response
171 .created(locationBuilder.build())
172 .build();
173 } catch (IOException ex) {
Ray Milkey5d915f42015-08-13 10:27:53 -0700174 throw new IllegalArgumentException(ex);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700175 }
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700176 }
177
178 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700179 * Remove flow rule. Removes the specified flow rule.
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700180 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700181 * @param deviceId device identifier
182 * @param flowId flow rule identifier
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700183 */
184 @DELETE
185 @Produces(MediaType.APPLICATION_JSON)
186 @Path("{deviceId}/{flowId}")
187 public void deleteFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId,
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700188 @PathParam("flowId") long flowId) {
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530189 final Iterable<FlowEntry> flowEntries =
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700190 service.getFlowEntries(DeviceId.deviceId(deviceId));
191
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530192 if (!flowEntries.iterator().hasNext()) {
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700193 throw new ItemNotFoundException(DEVICE_NOT_FOUND);
194 }
195
Phaneendra Mandaaec654c2015-09-23 19:02:23 +0530196 StreamSupport.stream(flowEntries.spliterator(), false)
Ray Milkeyeb5c7172015-06-23 14:59:27 -0700197 .filter(entry -> entry.id().value() == flowId)
198 .forEach(service::removeFlowRules);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700199 }
200
Ray Milkey4f5de002014-12-17 19:26:11 -0800201}