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