Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.rest.resources; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.net.DeviceId; |
| 21 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 22 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 23 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 24 | import org.onosproject.net.flowobjective.NextObjective; |
| 25 | import org.onosproject.rest.AbstractWebResource; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.PathParam; |
| 32 | import javax.ws.rs.Produces; |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 33 | import javax.ws.rs.QueryParam; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 34 | import javax.ws.rs.core.Context; |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 37 | import javax.ws.rs.core.UriBuilder; |
| 38 | import javax.ws.rs.core.UriInfo; |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 41 | |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 42 | import static org.onlab.util.Tools.readTreeFromStream; |
| 43 | |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Manage flow objectives. |
| 46 | */ |
| 47 | @Path("flowobjectives") |
| 48 | public class FlowObjectiveWebResource extends AbstractWebResource { |
| 49 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 50 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 51 | private UriInfo uriInfo; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 52 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 53 | private static final String DEVICE_INVALID = |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 54 | "Invalid deviceId in objective creation request"; |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 55 | private static final String POLICY_INVALID = "Invalid policy"; |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 56 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 57 | private final FlowObjectiveService flowObjectiveService = get(FlowObjectiveService.class); |
| 58 | private final ObjectNode root = mapper().createObjectNode(); |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Creates and installs a new filtering objective for the specified device. |
| 62 | * |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 63 | * @param appId application identifier |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 64 | * @param deviceId device identifier |
| 65 | * @param stream filtering objective JSON |
| 66 | * @return status of the request - CREATED if the JSON is correct, |
| 67 | * BAD_REQUEST if the JSON is invalid |
| 68 | * @onos.rsModel FilteringObjective |
| 69 | */ |
| 70 | @POST |
| 71 | @Path("{deviceId}/filter") |
| 72 | @Consumes(MediaType.APPLICATION_JSON) |
| 73 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 74 | public Response createFilteringObjective(@QueryParam("appId") String appId, |
| 75 | @PathParam("deviceId") String deviceId, |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 76 | InputStream stream) { |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 77 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 78 | UriBuilder locationBuilder = null; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 79 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 80 | validateDeviceId(deviceId, jsonTree); |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 81 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 82 | if (appId != null) { |
| 83 | jsonTree.put("appId", appId); |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 84 | } |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 85 | |
| 86 | DeviceId did = DeviceId.deviceId(deviceId); |
| 87 | FilteringObjective filteringObjective = |
| 88 | codec(FilteringObjective.class).decode(jsonTree, this); |
| 89 | flowObjectiveService.filter(did, filteringObjective); |
| 90 | locationBuilder = uriInfo.getBaseUriBuilder() |
| 91 | .path("flowobjectives") |
| 92 | .path(did.toString()) |
| 93 | .path("filter") |
| 94 | .path(Integer.toString(filteringObjective.id())); |
| 95 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 96 | return Response |
| 97 | .created(locationBuilder.build()) |
| 98 | .build(); |
| 99 | } catch (IOException e) { |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 100 | throw new IllegalArgumentException(e); |
| 101 | } |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Creates and installs a new forwarding objective for the specified device. |
| 106 | * |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 107 | * @param appId application identifier |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 108 | * @param deviceId device identifier |
| 109 | * @param stream forwarding objective JSON |
| 110 | * @return status of the request - CREATED if the JSON is correct, |
| 111 | * BAD_REQUEST if the JSON is invalid |
| 112 | * @onos.rsModel ForwardingObjective |
| 113 | */ |
| 114 | @POST |
| 115 | @Path("{deviceId}/forward") |
| 116 | @Consumes(MediaType.APPLICATION_JSON) |
| 117 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 118 | public Response createForwardingObjective(@QueryParam("appId") String appId, |
| 119 | @PathParam("deviceId") String deviceId, |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 120 | InputStream stream) { |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 121 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 122 | UriBuilder locationBuilder = null; |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 123 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 124 | validateDeviceId(deviceId, jsonTree); |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 125 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 126 | if (appId != null) { |
| 127 | jsonTree.put("appId", appId); |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 128 | } |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 129 | |
| 130 | DeviceId did = DeviceId.deviceId(deviceId); |
| 131 | ForwardingObjective forwardingObjective = |
| 132 | codec(ForwardingObjective.class).decode(jsonTree, this); |
| 133 | flowObjectiveService.forward(did, forwardingObjective); |
| 134 | locationBuilder = uriInfo.getBaseUriBuilder() |
| 135 | .path("flowobjectives") |
| 136 | .path(did.toString()) |
| 137 | .path("forward") |
| 138 | .path(Integer.toString(forwardingObjective.id())); |
| 139 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 140 | return Response |
| 141 | .created(locationBuilder.build()) |
| 142 | .build(); |
| 143 | } catch (IOException e) { |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 144 | throw new IllegalArgumentException(e); |
| 145 | } |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Creates and installs a new next objective for the specified device. |
| 150 | * |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 151 | * @param appId application identifier |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 152 | * @param deviceId device identifier |
| 153 | * @param stream next objective JSON |
| 154 | * @return status of the request - CREATED if the JSON is correct, |
| 155 | * BAD_REQUEST if the JSON is invalid |
| 156 | * @onos.rsModel NextObjective |
| 157 | */ |
| 158 | @POST |
| 159 | @Path("{deviceId}/next") |
| 160 | @Consumes(MediaType.APPLICATION_JSON) |
| 161 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 162 | public Response createNextObjective(@QueryParam("appId") String appId, |
| 163 | @PathParam("deviceId") String deviceId, |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 164 | InputStream stream) { |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 165 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 166 | UriBuilder locationBuilder = null; |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 167 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 168 | validateDeviceId(deviceId, jsonTree); |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 169 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 170 | if (appId != null) { |
| 171 | jsonTree.put("appId", appId); |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 172 | } |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 173 | |
| 174 | DeviceId did = DeviceId.deviceId(deviceId); |
| 175 | NextObjective nextObjective = |
| 176 | codec(NextObjective.class).decode(jsonTree, this); |
| 177 | flowObjectiveService.next(did, nextObjective); |
| 178 | locationBuilder = uriInfo.getBaseUriBuilder() |
| 179 | .path("flowobjectives") |
| 180 | .path(did.toString()) |
| 181 | .path("next") |
| 182 | .path(Integer.toString(nextObjective.id())); |
| 183 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 184 | return Response |
| 185 | .created(locationBuilder.build()) |
| 186 | .build(); |
| 187 | } catch (IOException e) { |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 188 | throw new IllegalArgumentException(e); |
| 189 | } |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Returns the globally unique nextId. |
| 194 | * |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 195 | * @return 200 OK with next identifier |
Jian Li | d79d39e | 2016-02-01 12:53:47 -0800 | [diff] [blame] | 196 | * @onos.rsModel NextId |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 197 | */ |
| 198 | @GET |
| 199 | @Path("next") |
| 200 | @Produces(MediaType.APPLICATION_JSON) |
| 201 | public Response getNextId() { |
| 202 | root.put("nextId", flowObjectiveService.allocateNextId()); |
| 203 | return ok(root).build(); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Installs the filtering rules onto the specified device. |
| 208 | * |
| 209 | * @param stream filtering rule JSON |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 210 | * @return 200 OK |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 211 | * @onos.rsModel ObjectivePolicy |
| 212 | */ |
| 213 | @POST |
| 214 | @Path("policy") |
| 215 | @Consumes(MediaType.APPLICATION_JSON) |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 216 | public Response initPolicy(InputStream stream) { |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 217 | |
| 218 | try { |
| 219 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 220 | JsonNode policyJson = jsonTree.get("policy"); |
| 221 | |
| 222 | if (policyJson == null || policyJson.asText().isEmpty()) { |
| 223 | throw new IllegalArgumentException(POLICY_INVALID); |
| 224 | } |
| 225 | |
| 226 | flowObjectiveService.initPolicy(policyJson.asText()); |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 227 | return Response.ok().build(); |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 228 | } catch (IOException e) { |
| 229 | throw new IllegalArgumentException(e); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 234 | * Validates the deviceId that is contained in json string against the |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 235 | * input deviceId. |
| 236 | * |
| 237 | * @param deviceId device identifier |
| 238 | * @param node object node |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 239 | * @throws IllegalArgumentException if the device id is invalid |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 240 | */ |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 241 | private void validateDeviceId(String deviceId, ObjectNode node) { |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 242 | JsonNode specifiedDeviceId = node.get("deviceId"); |
| 243 | |
| 244 | if (specifiedDeviceId != null && |
| 245 | !specifiedDeviceId.asText().equals(deviceId)) { |
| 246 | throw new IllegalArgumentException(DEVICE_INVALID); |
| 247 | } |
Jian Li | e9ac2c5 | 2016-01-13 17:42:05 -0800 | [diff] [blame] | 248 | } |
| 249 | } |