blob: 675581db1893dde76b94e978d50a1cf8d1e964e5 [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;
25import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080026import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080027import org.onosproject.net.intent.Intent;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090028import org.onosproject.net.intent.IntentState;
Ray Milkey67c22722015-03-09 15:48:57 -070029import org.onosproject.net.intent.IntentEvent;
30import org.onosproject.net.intent.IntentListener;
Ray Milkey2b217142014-12-15 09:24:24 -080031import org.onosproject.net.intent.IntentService;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080032import org.onosproject.net.intent.Key;
Author Namee252a002016-09-26 22:42:24 +053033import org.onosproject.net.intent.util.IntentFilter;
dvaddire95c84ed2017-06-14 15:42:24 +053034import org.onosproject.net.intent.util.IntentMiniSummary;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070035import org.onosproject.rest.AbstractWebResource;
Ray Milkey67c22722015-03-09 15:48:57 -070036import org.slf4j.Logger;
Ray Milkey2b217142014-12-15 09:24:24 -080037
Jian Lic2a542b2016-05-10 11:48:19 -070038import javax.ws.rs.Consumes;
39import javax.ws.rs.DELETE;
40import javax.ws.rs.GET;
41import javax.ws.rs.POST;
42import javax.ws.rs.Path;
43import javax.ws.rs.PathParam;
44import javax.ws.rs.Produces;
45import javax.ws.rs.core.Context;
46import javax.ws.rs.core.MediaType;
47import javax.ws.rs.core.Response;
48import javax.ws.rs.core.UriBuilder;
49import javax.ws.rs.core.UriInfo;
50import java.io.IOException;
51import java.io.InputStream;
Author Namee252a002016-09-26 22:42:24 +053052import java.util.List;
dvaddire95c84ed2017-06-14 15:42:24 +053053import java.util.Map;
Jian Lic2a542b2016-05-10 11:48:19 -070054import java.util.Objects;
55import java.util.concurrent.CountDownLatch;
56import java.util.concurrent.TimeUnit;
Ray Milkey2b217142014-12-15 09:24:24 -080057
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070058import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey67c22722015-03-09 15:48:57 -070059import static org.onosproject.net.intent.IntentState.FAILED;
60import static org.onosproject.net.intent.IntentState.WITHDRAWN;
61import static org.slf4j.LoggerFactory.getLogger;
62
Ray Milkey2b217142014-12-15 09:24:24 -080063/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070064 * Query, submit and withdraw network intents.
Ray Milkey2b217142014-12-15 09:24:24 -080065 */
Ray Milkey2b217142014-12-15 09:24:24 -080066@Path("intents")
67public class IntentsWebResource extends AbstractWebResource {
Ray Milkey303e6712015-07-17 14:06:21 -070068
Ray Milkey67c22722015-03-09 15:48:57 -070069 private static final Logger log = getLogger(IntentsWebResource.class);
70 private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5;
71
Kavitha Alagesane6840cf2016-10-21 10:58:05 +053072 private static final String APP_ID_NOT_FOUND = "Application Id not found";
Author Namee252a002016-09-26 22:42:24 +053073 private static final String HOST_TO_HOST_INTENT = "HostToHostIntent";
74 private static final String POINT_TO_POINT_INTENT = "PointToPointIntent";
75 private static final String SINGLE_TO_MULTI_POINT_INTENT =
76 "SinglePointToMultiPointIntent";
77 private static final String INTENT = "Intent";
78 private static final String APP_ID = "appId";
79 private static final String ID = "id";
80 private static final String INTENT_PATHS = "paths";
81 private static final String INTENT_TYPE = "type";
Jian Licc730a62016-05-10 16:36:16 -070082 private static final String INTENT_NOT_FOUND = "Intent is not found";
Ray Milkey2b217142014-12-15 09:24:24 -080083
Author Namee252a002016-09-26 22:42:24 +053084 @Context
85 private UriInfo uriInfo;
86
Ray Milkey2b217142014-12-15 09:24:24 -080087 /**
Jian Licc730a62016-05-10 16:36:16 -070088 * Gets all intents.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070089 * Returns array containing all the intents in the system.
Jian Licc730a62016-05-10 16:36:16 -070090 *
91 * @return 200 OK with array of all the intents in the system
Andrea Campanella10c4adc2015-12-03 15:27:54 -080092 * @onos.rsModel Intents
Ray Milkey2b217142014-12-15 09:24:24 -080093 */
94 @GET
95 @Produces(MediaType.APPLICATION_JSON)
96 public Response getIntents() {
97 final Iterable<Intent> intents = get(IntentService.class).getIntents();
98 final ObjectNode root = encodeArray(Intent.class, "intents", intents);
Ray Milkey3f025692015-01-26 11:15:41 -080099 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -0800100 }
101
dvaddire95c84ed2017-06-14 15:42:24 +0530102
103 /**
104 * Gets Summary of all intents.
105 * Returns Summary of the intents in the system.
106 *
107 * @return 200 OK with Summary of all the intents in the system
108 * @onos.rsModel Minisummary
109 */
110 @GET
111 @Produces(MediaType.APPLICATION_JSON)
112 @Path("minisummary")
113 public Response getIntentSummary() {
114 final Iterable<Intent> intents = get(IntentService.class).getIntents();
115 ObjectNode root = mapper().createObjectNode();
116 IntentMiniSummary intentminisummary = new IntentMiniSummary();
117 Map<String, IntentMiniSummary> map = intentminisummary.summarize(intents, get(IntentService.class));
118 map.values().stream().forEach(intentsummary -> {
119 root.put(intentsummary.getIntentType(), codec(IntentMiniSummary.class).encode(intentsummary, this));
120 });
121 return ok(root).build();
122 }
123
124
Ray Milkey2b217142014-12-15 09:24:24 -0800125 /**
Rafal Szaleckif97e0ed2017-06-01 13:07:18 +0200126 * Gets intent intallables by application ID and key.
127 * @param appId application identifier
128 * @param key intent key
129 *
130 * @return 200 OK with array of the intent installables
131 * @onos.rsModel Intents
132 */
133 @GET
134 @Produces(MediaType.APPLICATION_JSON)
135 @Path("installables/{appId}/{key}")
136 public Response getIntentWithInstallable(@PathParam("appId") String appId,
137 @PathParam("key") String key) {
138 final IntentService intentService = get(IntentService.class);
139 final ApplicationId app = get(CoreService.class).getAppId(appId);
140 nullIsNotFound(app, APP_ID_NOT_FOUND);
141
142 Intent intent = intentService.getIntent(Key.of(key, app));
143 if (intent == null) {
144 long numericalKey = Long.decode(key);
145 intent = intentService.getIntent(Key.of(numericalKey, app));
146 }
147 nullIsNotFound(intent, INTENT_NOT_FOUND);
148
149 final Iterable<Intent> installables = intentService.getInstallableIntents(intent.key());
150 final ObjectNode root = encodeArray(Intent.class, "installables", installables);
151 return ok(root).build();
152 }
153
154 /**
Jian Licc730a62016-05-10 16:36:16 -0700155 * Gets intent by application and key.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700156 * Returns details of the specified intent.
Jian Licc730a62016-05-10 16:36:16 -0700157 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700158 * @param appId application identifier
159 * @param key intent key
Jian Licc730a62016-05-10 16:36:16 -0700160 * @return 200 OK with intent data
161 * @onos.rsModel Intents
Ray Milkey2b217142014-12-15 09:24:24 -0800162 */
163 @GET
164 @Produces(MediaType.APPLICATION_JSON)
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800165 @Path("{appId}/{key}")
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700166 public Response getIntentById(@PathParam("appId") String appId,
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800167 @PathParam("key") String key) {
168 final ApplicationId app = get(CoreService.class).getAppId(appId);
Kavitha Alagesane6840cf2016-10-21 10:58:05 +0530169 nullIsNotFound(app, APP_ID_NOT_FOUND);
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800170 Intent intent = get(IntentService.class).getIntent(Key.of(key, app));
171 if (intent == null) {
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700172 long numericalKey = Long.decode(key);
173 intent = get(IntentService.class).getIntent(Key.of(numericalKey, app));
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800174 }
175 nullIsNotFound(intent, INTENT_NOT_FOUND);
176
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800177 final ObjectNode root;
178 if (intent instanceof HostToHostIntent) {
179 root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this);
180 } else if (intent instanceof PointToPointIntent) {
181 root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this);
Chiara Contolia8f69ff2016-07-28 01:06:07 +0900182 } else if (intent instanceof SinglePointToMultiPointIntent) {
183 root = codec(SinglePointToMultiPointIntent.class).encode((SinglePointToMultiPointIntent) intent, this);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800184 } else {
185 root = codec(Intent.class).encode(intent, this);
186 }
Ray Milkey3f025692015-01-26 11:15:41 -0800187 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -0800188 }
Ray Milkey67c22722015-03-09 15:48:57 -0700189
Jian Licc730a62016-05-10 16:36:16 -0700190 /**
Author Namee252a002016-09-26 22:42:24 +0530191 * Gets all related flow entries created by a particular intent.
192 * Returns all flow entries of the specified intent.
193 *
194 * @param appId application identifier
195 * @param key intent key
196 * @return 200 OK with intent data
197 * @onos.rsModel Relatedflows
198 */
199 @GET
200 @Produces(MediaType.APPLICATION_JSON)
201 @Path("relatedflows/{appId}/{key}")
202 public Response getIntentFlowsById(@PathParam("appId") String appId,
203 @PathParam("key") String key) {
204 ApplicationId applicationId = get(CoreService.class).getAppId(appId);
205 nullIsNotFound(applicationId, APP_ID_NOT_FOUND);
206 IntentService intentService = get(IntentService.class);
207 FlowRuleService flowService = get(FlowRuleService.class);
208
209 Intent intent = intentService.getIntent(Key.of(key, applicationId));
210 if (intent == null) {
211 long numericalKey = Long.decode(key);
212 intent = intentService.getIntent(
213 Key.of(numericalKey, applicationId));
214 }
215 nullIsNotFound(intent, INTENT_NOT_FOUND);
216
217 ObjectNode root = mapper().createObjectNode();
218 root.put(APP_ID, appId);
219 root.put(ID, key);
220
221 IntentFilter intentFilter = new IntentFilter(intentService, flowService);
222
223 List<Intent> installables =
224 intentService.getInstallableIntents(intent.key());
225
226 if (intent instanceof HostToHostIntent) {
227 root.put(INTENT_TYPE, HOST_TO_HOST_INTENT);
228 } else if (intent instanceof PointToPointIntent) {
229 root.put(INTENT_TYPE, POINT_TO_POINT_INTENT);
230 } else if (intent instanceof SinglePointToMultiPointIntent) {
231 root.put(INTENT_TYPE, SINGLE_TO_MULTI_POINT_INTENT);
232 } else {
233 root.put(INTENT_TYPE, INTENT);
234 }
235
236 ArrayNode pathsNode = root.putArray(INTENT_PATHS);
237
238 for (List<FlowEntry> flowEntries :
239 intentFilter.readIntentFlows(installables)) {
240 ArrayNode flowNode = pathsNode.addArray();
241
242 for (FlowEntry entry : flowEntries) {
243 flowNode.add(codec(FlowEntry.class).encode(entry, this));
244 }
245 }
246 return ok(root).build();
247 }
248
249 /**
Jian Licc730a62016-05-10 16:36:16 -0700250 * Internal listener for tracking the intent deletion events.
251 */
252 private class DeleteListener implements IntentListener {
Ray Milkey67c22722015-03-09 15:48:57 -0700253 final Key key;
254 final CountDownLatch latch;
255
Jian Licc730a62016-05-10 16:36:16 -0700256 /**
257 * Default constructor.
258 *
259 * @param key key
260 * @param latch count down latch
261 */
Ray Milkey67c22722015-03-09 15:48:57 -0700262 DeleteListener(Key key, CountDownLatch latch) {
263 this.key = key;
264 this.latch = latch;
265 }
266
267 @Override
268 public void event(IntentEvent event) {
269 if (Objects.equals(event.subject().key(), key) &&
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700270 (event.type() == IntentEvent.Type.WITHDRAWN ||
271 event.type() == IntentEvent.Type.FAILED)) {
Ray Milkey67c22722015-03-09 15:48:57 -0700272 latch.countDown();
273 }
274 }
275 }
276
277 /**
Jian Licc730a62016-05-10 16:36:16 -0700278 * Submits a new intent.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700279 * Creates and submits intent from the JSON request.
Jian Licc730a62016-05-10 16:36:16 -0700280 *
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700281 * @param stream input JSON
282 * @return status of the request - CREATED if the JSON is correct,
283 * BAD_REQUEST if the JSON is invalid
Jian Licc730a62016-05-10 16:36:16 -0700284 * @onos.rsModel IntentHost
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700285 */
286 @POST
287 @Consumes(MediaType.APPLICATION_JSON)
288 @Produces(MediaType.APPLICATION_JSON)
289 public Response createIntent(InputStream stream) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700290 try {
291 IntentService service = get(IntentService.class);
292 ObjectNode root = (ObjectNode) mapper().readTree(stream);
293 Intent intent = codec(Intent.class).decode(root, this);
294 service.submit(intent);
Ray Milkey303e6712015-07-17 14:06:21 -0700295 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
296 .path("intents")
Ray Milkey8d076402015-08-31 15:43:18 -0700297 .path(intent.appId().name())
Ray Milkey303e6712015-07-17 14:06:21 -0700298 .path(Long.toString(intent.id().fingerprint()));
299 return Response
300 .created(locationBuilder.build())
301 .build();
302 } catch (IOException ioe) {
303 throw new IllegalArgumentException(ioe);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700304 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700305 }
306
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700307 /**
Jian Licc730a62016-05-10 16:36:16 -0700308 * Withdraws intent.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700309 * Withdraws the specified intent from the system.
310 *
311 * @param appId application identifier
312 * @param key intent key
Jian Lic2a542b2016-05-10 11:48:19 -0700313 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700314 */
315 @DELETE
316 @Path("{appId}/{key}")
Jian Lic2a542b2016-05-10 11:48:19 -0700317 public Response deleteIntentById(@PathParam("appId") String appId,
Jian Licc730a62016-05-10 16:36:16 -0700318 @PathParam("key") String key) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700319 final ApplicationId app = get(CoreService.class).getAppId(appId);
Kavitha Alagesane6840cf2016-10-21 10:58:05 +0530320 nullIsNotFound(app, APP_ID_NOT_FOUND);
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700321 Intent intent = get(IntentService.class).getIntent(Key.of(key, app));
322 IntentService service = get(IntentService.class);
323
324 if (intent == null) {
325 intent = service
326 .getIntent(Key.of(Long.decode(key), app));
327 }
328 if (intent == null) {
329 // No such intent. REST standards recommend a positive status code
330 // in this case.
Jian Lic2a542b2016-05-10 11:48:19 -0700331 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700332 }
333
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700334 Key k = intent.key();
335
336 // set up latch and listener to track uninstall progress
337 CountDownLatch latch = new CountDownLatch(1);
338
339 IntentListener listener = new DeleteListener(k, latch);
340 service.addListener(listener);
341
342 try {
343 // request the withdraw
344 service.withdraw(intent);
345
346 try {
347 latch.await(WITHDRAW_EVENT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
348 } catch (InterruptedException e) {
349 log.info("REST Delete operation timed out waiting for intent {}", k);
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -0700350 Thread.currentThread().interrupt();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700351 }
352 // double check the state
353 IntentState state = service.getIntentState(k);
354 if (state == WITHDRAWN || state == FAILED) {
355 service.purge(intent);
356 }
357
358 } finally {
359 // clean up the listener
360 service.removeListener(listener);
361 }
Jian Lic2a542b2016-05-10 11:48:19 -0700362 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700363 }
Ray Milkey2b217142014-12-15 09:24:24 -0800364}