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