blob: effbe26071a7a0975ea9d20315ed382efc8f3292 [file] [log] [blame]
Jian Lie9ac2c52016-01-13 17:42:05 -08001/*
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 */
16package org.onosproject.rest.resources;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.flowobjective.FilteringObjective;
22import org.onosproject.net.flowobjective.FlowObjectiveService;
23import org.onosproject.net.flowobjective.ForwardingObjective;
24import org.onosproject.net.flowobjective.NextObjective;
25import org.onosproject.rest.AbstractWebResource;
26
27import javax.ws.rs.Consumes;
28import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
Jian Li9d616492016-03-09 10:52:49 -080033import javax.ws.rs.core.Context;
Jian Lie9ac2c52016-01-13 17:42:05 -080034import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
Jian Li9d616492016-03-09 10:52:49 -080036import javax.ws.rs.core.UriBuilder;
37import javax.ws.rs.core.UriInfo;
Jian Lie9ac2c52016-01-13 17:42:05 -080038import java.io.IOException;
39import java.io.InputStream;
Jian Lie9ac2c52016-01-13 17:42:05 -080040
41/**
42 * Manage flow objectives.
43 */
44@Path("flowobjectives")
45public class FlowObjectiveWebResource extends AbstractWebResource {
46
Jian Li9d616492016-03-09 10:52:49 -080047 @Context
48 UriInfo uriInfo;
49
Jian Lie9ac2c52016-01-13 17:42:05 -080050 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 Lie9ac2c52016-01-13 17:42:05 -080072 try {
Jian Li9d616492016-03-09 10:52:49 -080073 UriBuilder locationBuilder = null;
Jian Lie9ac2c52016-01-13 17:42:05 -080074 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 Li9d616492016-03-09 10:52:49 -080080 locationBuilder = uriInfo.getBaseUriBuilder()
81 .path("flowobjectives")
82 .path(did.toString())
83 .path("filter")
84 .path(Integer.toString(filteringObjective.id()));
Jian Lie9ac2c52016-01-13 17:42:05 -080085 }
Jian Li9d616492016-03-09 10:52:49 -080086 return Response
87 .created(locationBuilder.build())
88 .build();
89 } catch (IOException e) {
Jian Lie9ac2c52016-01-13 17:42:05 -080090 throw new IllegalArgumentException(e);
91 }
Jian Lie9ac2c52016-01-13 17:42:05 -080092 }
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 Lie9ac2c52016-01-13 17:42:05 -0800109 try {
Jian Li9d616492016-03-09 10:52:49 -0800110 UriBuilder locationBuilder = null;
Jian Lie9ac2c52016-01-13 17:42:05 -0800111 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 Li9d616492016-03-09 10:52:49 -0800117 locationBuilder = uriInfo.getBaseUriBuilder()
118 .path("flowobjectives")
119 .path(did.toString())
120 .path("forward")
121 .path(Integer.toString(forwardingObjective.id()));
Jian Lie9ac2c52016-01-13 17:42:05 -0800122 }
Jian Li9d616492016-03-09 10:52:49 -0800123 return Response
124 .created(locationBuilder.build())
125 .build();
126 } catch (IOException e) {
Jian Lie9ac2c52016-01-13 17:42:05 -0800127 throw new IllegalArgumentException(e);
128 }
Jian Lie9ac2c52016-01-13 17:42:05 -0800129 }
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 Lie9ac2c52016-01-13 17:42:05 -0800146 try {
Jian Li9d616492016-03-09 10:52:49 -0800147 UriBuilder locationBuilder = null;
Jian Lie9ac2c52016-01-13 17:42:05 -0800148 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 Li9d616492016-03-09 10:52:49 -0800154 locationBuilder = uriInfo.getBaseUriBuilder()
155 .path("flowobjectives")
156 .path(did.toString())
157 .path("next")
158 .path(Integer.toString(nextObjective.id()));
Jian Lie9ac2c52016-01-13 17:42:05 -0800159 }
Jian Li9d616492016-03-09 10:52:49 -0800160 return Response
161 .created(locationBuilder.build())
162 .build();
163 } catch (IOException e) {
Jian Lie9ac2c52016-01-13 17:42:05 -0800164 throw new IllegalArgumentException(e);
165 }
Jian Lie9ac2c52016-01-13 17:42:05 -0800166 }
167
168 /**
169 * Returns the globally unique nextId.
170 *
171 * @return nextId
Jian Lid79d39e2016-02-01 12:53:47 -0800172 * @onos.rsModel NextId
Jian Lie9ac2c52016-01-13 17:42:05 -0800173 */
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 Lie9ac2c52016-01-13 17:42:05 -0800186 * @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}