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