blob: 278fa0708827877c95930affa51bd34fdfd9aa23 [file] [log] [blame]
Ray Milkey2b217142014-12-15 09:24:24 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey2b217142014-12-15 09:24:24 -08003 *
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 */
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Ray Milkey2b217142014-12-15 09:24:24 -080017
Author Namee252a002016-09-26 22:42:24 +053018import com.fasterxml.jackson.databind.node.ArrayNode;
Jian Lic2a542b2016-05-10 11:48:19 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080020import org.onosproject.core.ApplicationId;
21import org.onosproject.core.CoreService;
Author Namee252a002016-09-26 22:42:24 +053022import org.onosproject.net.flow.FlowEntry;
23import org.onosproject.net.flow.FlowRuleService;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090024import org.onosproject.net.intent.SinglePointToMultiPointIntent;
nassima toumi95761312017-05-23 14:00:33 +030025import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090026import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080027import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080028import org.onosproject.net.intent.Intent;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090029import org.onosproject.net.intent.IntentState;
Ray Milkey67c22722015-03-09 15:48:57 -070030import org.onosproject.net.intent.IntentEvent;
31import org.onosproject.net.intent.IntentListener;
Ray Milkey2b217142014-12-15 09:24:24 -080032import org.onosproject.net.intent.IntentService;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080033import org.onosproject.net.intent.Key;
Author Namee252a002016-09-26 22:42:24 +053034import org.onosproject.net.intent.util.IntentFilter;
dvaddire95c84ed2017-06-14 15:42:24 +053035import org.onosproject.net.intent.util.IntentMiniSummary;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070036import org.onosproject.rest.AbstractWebResource;
Ray Milkey67c22722015-03-09 15:48:57 -070037import org.slf4j.Logger;
Ray Milkey2b217142014-12-15 09:24:24 -080038
Jian Lic2a542b2016-05-10 11:48:19 -070039import javax.ws.rs.Consumes;
40import javax.ws.rs.DELETE;
41import javax.ws.rs.GET;
42import javax.ws.rs.POST;
43import javax.ws.rs.Path;
44import javax.ws.rs.PathParam;
45import javax.ws.rs.Produces;
46import javax.ws.rs.core.Context;
47import javax.ws.rs.core.MediaType;
48import javax.ws.rs.core.Response;
49import javax.ws.rs.core.UriBuilder;
50import javax.ws.rs.core.UriInfo;
51import java.io.IOException;
52import java.io.InputStream;
Author Namee252a002016-09-26 22:42:24 +053053import java.util.List;
dvaddire95c84ed2017-06-14 15:42:24 +053054import java.util.Map;
Jian Lic2a542b2016-05-10 11:48:19 -070055import java.util.Objects;
56import java.util.concurrent.CountDownLatch;
57import java.util.concurrent.TimeUnit;
Ray Milkey2b217142014-12-15 09:24:24 -080058
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070059import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey67c22722015-03-09 15:48:57 -070060import static org.onosproject.net.intent.IntentState.FAILED;
61import static org.onosproject.net.intent.IntentState.WITHDRAWN;
62import static org.slf4j.LoggerFactory.getLogger;
63
Ray Milkey2b217142014-12-15 09:24:24 -080064/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070065 * Query, submit and withdraw network intents.
Ray Milkey2b217142014-12-15 09:24:24 -080066 */
Ray Milkey2b217142014-12-15 09:24:24 -080067@Path("intents")
68public class IntentsWebResource extends AbstractWebResource {
Ray Milkey303e6712015-07-17 14:06:21 -070069
Ray Milkey67c22722015-03-09 15:48:57 -070070 private static final Logger log = getLogger(IntentsWebResource.class);
71 private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5;
72
Kavitha Alagesane6840cf2016-10-21 10:58:05 +053073 private static final String APP_ID_NOT_FOUND = "Application Id not found";
Author Namee252a002016-09-26 22:42:24 +053074 private static final String HOST_TO_HOST_INTENT = "HostToHostIntent";
75 private static final String POINT_TO_POINT_INTENT = "PointToPointIntent";
76 private static final String SINGLE_TO_MULTI_POINT_INTENT =
77 "SinglePointToMultiPointIntent";
nassima toumi95761312017-05-23 14:00:33 +030078 private static final String MULTI_TO_SINGLE_POINT_INTENT =
79 "MultiPointToSinglePointIntent";
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +090080
Author Namee252a002016-09-26 22:42:24 +053081 private static final String INTENT = "Intent";
82 private static final String APP_ID = "appId";
83 private static final String ID = "id";
84 private static final String INTENT_PATHS = "paths";
85 private static final String INTENT_TYPE = "type";
Jian Licc730a62016-05-10 16:36:16 -070086 private static final String INTENT_NOT_FOUND = "Intent is not found";
Ray Milkey2b217142014-12-15 09:24:24 -080087
Author Namee252a002016-09-26 22:42:24 +053088 @Context
89 private UriInfo uriInfo;
90
Ray Milkey2b217142014-12-15 09:24:24 -080091 /**
Jian Licc730a62016-05-10 16:36:16 -070092 * Gets all intents.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070093 * Returns array containing all the intents in the system.
Jian Licc730a62016-05-10 16:36:16 -070094 *
95 * @return 200 OK with array of all the intents in the system
Andrea Campanella10c4adc2015-12-03 15:27:54 -080096 * @onos.rsModel Intents
Ray Milkey2b217142014-12-15 09:24:24 -080097 */
98 @GET
99 @Produces(MediaType.APPLICATION_JSON)
100 public Response getIntents() {
101 final Iterable<Intent> intents = get(IntentService.class).getIntents();
102 final ObjectNode root = encodeArray(Intent.class, "intents", intents);
Ray Milkey3f025692015-01-26 11:15:41 -0800103 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -0800104 }
105
dvaddire95c84ed2017-06-14 15:42:24 +0530106
107 /**
108 * Gets Summary of all intents.
109 * Returns Summary of the intents in the system.
110 *
111 * @return 200 OK with Summary of all the intents in the system
112 * @onos.rsModel Minisummary
113 */
114 @GET
115 @Produces(MediaType.APPLICATION_JSON)
116 @Path("minisummary")
117 public Response getIntentSummary() {
118 final Iterable<Intent> intents = get(IntentService.class).getIntents();
119 ObjectNode root = mapper().createObjectNode();
120 IntentMiniSummary intentminisummary = new IntentMiniSummary();
121 Map<String, IntentMiniSummary> map = intentminisummary.summarize(intents, get(IntentService.class));
122 map.values().stream().forEach(intentsummary -> {
123 root.put(intentsummary.getIntentType(), codec(IntentMiniSummary.class).encode(intentsummary, this));
124 });
125 return ok(root).build();
126 }
127
128
Ray Milkey2b217142014-12-15 09:24:24 -0800129 /**
Aris C. Risdiantoa579c8f2017-04-13 19:38:46 +0900130 * Gets intent installables by application ID and key.
Rafal Szaleckif97e0ed2017-06-01 13:07:18 +0200131 * @param appId application identifier
132 * @param key intent key
133 *
134 * @return 200 OK with array of the intent installables
135 * @onos.rsModel Intents
136 */
137 @GET
138 @Produces(MediaType.APPLICATION_JSON)
139 @Path("installables/{appId}/{key}")
140 public Response getIntentWithInstallable(@PathParam("appId") String appId,
141 @PathParam("key") String key) {
142 final IntentService intentService = get(IntentService.class);
143 final ApplicationId app = get(CoreService.class).getAppId(appId);
144 nullIsNotFound(app, APP_ID_NOT_FOUND);
145
146 Intent intent = intentService.getIntent(Key.of(key, app));
147 if (intent == null) {
148 long numericalKey = Long.decode(key);
149 intent = intentService.getIntent(Key.of(numericalKey, app));
150 }
151 nullIsNotFound(intent, INTENT_NOT_FOUND);
152
153 final Iterable<Intent> installables = intentService.getInstallableIntents(intent.key());
154 final ObjectNode root = encodeArray(Intent.class, "installables", installables);
155 return ok(root).build();
156 }
157
158 /**
Jian Licc730a62016-05-10 16:36:16 -0700159 * Gets intent by application and key.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700160 * Returns details of the specified intent.
Jian Licc730a62016-05-10 16:36:16 -0700161 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700162 * @param appId application identifier
163 * @param key intent key
Jian Licc730a62016-05-10 16:36:16 -0700164 * @return 200 OK with intent data
165 * @onos.rsModel Intents
Ray Milkey2b217142014-12-15 09:24:24 -0800166 */
167 @GET
168 @Produces(MediaType.APPLICATION_JSON)
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800169 @Path("{appId}/{key}")
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700170 public Response getIntentById(@PathParam("appId") String appId,
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800171 @PathParam("key") String key) {
172 final ApplicationId app = get(CoreService.class).getAppId(appId);
Kavitha Alagesane6840cf2016-10-21 10:58:05 +0530173 nullIsNotFound(app, APP_ID_NOT_FOUND);
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800174 Intent intent = get(IntentService.class).getIntent(Key.of(key, app));
175 if (intent == null) {
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700176 long numericalKey = Long.decode(key);
177 intent = get(IntentService.class).getIntent(Key.of(numericalKey, app));
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800178 }
179 nullIsNotFound(intent, INTENT_NOT_FOUND);
180
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800181 final ObjectNode root;
182 if (intent instanceof HostToHostIntent) {
183 root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this);
184 } else if (intent instanceof PointToPointIntent) {
185 root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this);
Chiara Contolia8f69ff2016-07-28 01:06:07 +0900186 } else if (intent instanceof SinglePointToMultiPointIntent) {
187 root = codec(SinglePointToMultiPointIntent.class).encode((SinglePointToMultiPointIntent) intent, this);
nassima toumi95761312017-05-23 14:00:33 +0300188 } else if (intent instanceof MultiPointToSinglePointIntent) {
189 root = codec(MultiPointToSinglePointIntent.class).encode((MultiPointToSinglePointIntent) intent, this);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800190 } else {
191 root = codec(Intent.class).encode(intent, this);
192 }
Ray Milkey3f025692015-01-26 11:15:41 -0800193 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -0800194 }
Ray Milkey67c22722015-03-09 15:48:57 -0700195
Jian Licc730a62016-05-10 16:36:16 -0700196 /**
Author Namee252a002016-09-26 22:42:24 +0530197 * Gets all related flow entries created by a particular intent.
198 * Returns all flow entries of the specified intent.
199 *
200 * @param appId application identifier
201 * @param key intent key
202 * @return 200 OK with intent data
203 * @onos.rsModel Relatedflows
204 */
205 @GET
206 @Produces(MediaType.APPLICATION_JSON)
207 @Path("relatedflows/{appId}/{key}")
208 public Response getIntentFlowsById(@PathParam("appId") String appId,
209 @PathParam("key") String key) {
210 ApplicationId applicationId = get(CoreService.class).getAppId(appId);
211 nullIsNotFound(applicationId, APP_ID_NOT_FOUND);
212 IntentService intentService = get(IntentService.class);
213 FlowRuleService flowService = get(FlowRuleService.class);
214
215 Intent intent = intentService.getIntent(Key.of(key, applicationId));
216 if (intent == null) {
217 long numericalKey = Long.decode(key);
218 intent = intentService.getIntent(
219 Key.of(numericalKey, applicationId));
220 }
221 nullIsNotFound(intent, INTENT_NOT_FOUND);
222
223 ObjectNode root = mapper().createObjectNode();
224 root.put(APP_ID, appId);
225 root.put(ID, key);
226
227 IntentFilter intentFilter = new IntentFilter(intentService, flowService);
228
229 List<Intent> installables =
230 intentService.getInstallableIntents(intent.key());
231
232 if (intent instanceof HostToHostIntent) {
233 root.put(INTENT_TYPE, HOST_TO_HOST_INTENT);
234 } else if (intent instanceof PointToPointIntent) {
235 root.put(INTENT_TYPE, POINT_TO_POINT_INTENT);
236 } else if (intent instanceof SinglePointToMultiPointIntent) {
237 root.put(INTENT_TYPE, SINGLE_TO_MULTI_POINT_INTENT);
nassima toumi95761312017-05-23 14:00:33 +0300238 } else if (intent instanceof MultiPointToSinglePointIntent) {
239 root.put(INTENT_TYPE, MULTI_TO_SINGLE_POINT_INTENT);
Author Namee252a002016-09-26 22:42:24 +0530240 } else {
241 root.put(INTENT_TYPE, INTENT);
242 }
243
244 ArrayNode pathsNode = root.putArray(INTENT_PATHS);
245
246 for (List<FlowEntry> flowEntries :
247 intentFilter.readIntentFlows(installables)) {
248 ArrayNode flowNode = pathsNode.addArray();
249
250 for (FlowEntry entry : flowEntries) {
251 flowNode.add(codec(FlowEntry.class).encode(entry, this));
252 }
253 }
254 return ok(root).build();
255 }
256
257 /**
Jian Licc730a62016-05-10 16:36:16 -0700258 * Internal listener for tracking the intent deletion events.
259 */
260 private class DeleteListener implements IntentListener {
Ray Milkey67c22722015-03-09 15:48:57 -0700261 final Key key;
262 final CountDownLatch latch;
263
Jian Licc730a62016-05-10 16:36:16 -0700264 /**
265 * Default constructor.
266 *
267 * @param key key
268 * @param latch count down latch
269 */
Ray Milkey67c22722015-03-09 15:48:57 -0700270 DeleteListener(Key key, CountDownLatch latch) {
271 this.key = key;
272 this.latch = latch;
273 }
274
275 @Override
276 public void event(IntentEvent event) {
277 if (Objects.equals(event.subject().key(), key) &&
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700278 (event.type() == IntentEvent.Type.WITHDRAWN ||
279 event.type() == IntentEvent.Type.FAILED)) {
Ray Milkey67c22722015-03-09 15:48:57 -0700280 latch.countDown();
281 }
282 }
283 }
284
285 /**
Jian Licc730a62016-05-10 16:36:16 -0700286 * Submits a new intent.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700287 * Creates and submits intent from the JSON request.
Jian Licc730a62016-05-10 16:36:16 -0700288 *
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700289 * @param stream input JSON
290 * @return status of the request - CREATED if the JSON is correct,
291 * BAD_REQUEST if the JSON is invalid
Jian Licc730a62016-05-10 16:36:16 -0700292 * @onos.rsModel IntentHost
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700293 */
294 @POST
295 @Consumes(MediaType.APPLICATION_JSON)
296 @Produces(MediaType.APPLICATION_JSON)
297 public Response createIntent(InputStream stream) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700298 try {
299 IntentService service = get(IntentService.class);
300 ObjectNode root = (ObjectNode) mapper().readTree(stream);
301 Intent intent = codec(Intent.class).decode(root, this);
302 service.submit(intent);
Ray Milkey303e6712015-07-17 14:06:21 -0700303 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
304 .path("intents")
Ray Milkey8d076402015-08-31 15:43:18 -0700305 .path(intent.appId().name())
Ray Milkey303e6712015-07-17 14:06:21 -0700306 .path(Long.toString(intent.id().fingerprint()));
307 return Response
308 .created(locationBuilder.build())
309 .build();
310 } catch (IOException ioe) {
311 throw new IllegalArgumentException(ioe);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700312 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700313 }
314
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700315 /**
Jian Licc730a62016-05-10 16:36:16 -0700316 * Withdraws intent.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700317 * Withdraws the specified intent from the system.
318 *
319 * @param appId application identifier
320 * @param key intent key
Jian Lic2a542b2016-05-10 11:48:19 -0700321 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700322 */
323 @DELETE
324 @Path("{appId}/{key}")
Jian Lic2a542b2016-05-10 11:48:19 -0700325 public Response deleteIntentById(@PathParam("appId") String appId,
Jian Licc730a62016-05-10 16:36:16 -0700326 @PathParam("key") String key) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700327 final ApplicationId app = get(CoreService.class).getAppId(appId);
Kavitha Alagesane6840cf2016-10-21 10:58:05 +0530328 nullIsNotFound(app, APP_ID_NOT_FOUND);
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700329 Intent intent = get(IntentService.class).getIntent(Key.of(key, app));
330 IntentService service = get(IntentService.class);
331
332 if (intent == null) {
333 intent = service
334 .getIntent(Key.of(Long.decode(key), app));
335 }
336 if (intent == null) {
337 // No such intent. REST standards recommend a positive status code
338 // in this case.
Jian Lic2a542b2016-05-10 11:48:19 -0700339 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700340 }
341
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700342 Key k = intent.key();
343
344 // set up latch and listener to track uninstall progress
345 CountDownLatch latch = new CountDownLatch(1);
346
347 IntentListener listener = new DeleteListener(k, latch);
348 service.addListener(listener);
349
350 try {
351 // request the withdraw
352 service.withdraw(intent);
353
354 try {
355 latch.await(WITHDRAW_EVENT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
356 } catch (InterruptedException e) {
357 log.info("REST Delete operation timed out waiting for intent {}", k);
Ray Milkey5c7d4882018-02-05 14:50:39 -0800358 Thread.currentThread().interrupt();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700359 }
360 // double check the state
361 IntentState state = service.getIntentState(k);
362 if (state == WITHDRAWN || state == FAILED) {
363 service.purge(intent);
364 }
365
366 } finally {
367 // clean up the listener
368 service.removeListener(listener);
369 }
Jian Lic2a542b2016-05-10 11:48:19 -0700370 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700371 }
Ray Milkey2b217142014-12-15 09:24:24 -0800372}