blob: 20b59f7943c51c0e5c87b7d6048a4e8db14b588f [file] [log] [blame]
Ray Milkey2b217142014-12-15 09:24:24 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070034import org.onosproject.rest.AbstractWebResource;
Ray Milkey67c22722015-03-09 15:48:57 -070035import org.slf4j.Logger;
Ray Milkey2b217142014-12-15 09:24:24 -080036
Jian Lic2a542b2016-05-10 11:48:19 -070037import javax.ws.rs.Consumes;
38import javax.ws.rs.DELETE;
39import javax.ws.rs.GET;
40import javax.ws.rs.POST;
41import javax.ws.rs.Path;
42import javax.ws.rs.PathParam;
43import javax.ws.rs.Produces;
44import javax.ws.rs.core.Context;
45import javax.ws.rs.core.MediaType;
46import javax.ws.rs.core.Response;
47import javax.ws.rs.core.UriBuilder;
48import javax.ws.rs.core.UriInfo;
49import java.io.IOException;
50import java.io.InputStream;
Author Namee252a002016-09-26 22:42:24 +053051import java.util.List;
Jian Lic2a542b2016-05-10 11:48:19 -070052import java.util.Objects;
53import java.util.concurrent.CountDownLatch;
54import java.util.concurrent.TimeUnit;
Ray Milkey2b217142014-12-15 09:24:24 -080055
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070056import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey67c22722015-03-09 15:48:57 -070057import static org.onosproject.net.intent.IntentState.FAILED;
58import static org.onosproject.net.intent.IntentState.WITHDRAWN;
59import static org.slf4j.LoggerFactory.getLogger;
60
Ray Milkey2b217142014-12-15 09:24:24 -080061/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070062 * Query, submit and withdraw network intents.
Ray Milkey2b217142014-12-15 09:24:24 -080063 */
Ray Milkey2b217142014-12-15 09:24:24 -080064@Path("intents")
65public class IntentsWebResource extends AbstractWebResource {
Ray Milkey303e6712015-07-17 14:06:21 -070066
Ray Milkey67c22722015-03-09 15:48:57 -070067 private static final Logger log = getLogger(IntentsWebResource.class);
68 private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5;
69
Kavitha Alagesane6840cf2016-10-21 10:58:05 +053070 private static final String APP_ID_NOT_FOUND = "Application Id not found";
Author Namee252a002016-09-26 22:42:24 +053071 private static final String HOST_TO_HOST_INTENT = "HostToHostIntent";
72 private static final String POINT_TO_POINT_INTENT = "PointToPointIntent";
73 private static final String SINGLE_TO_MULTI_POINT_INTENT =
74 "SinglePointToMultiPointIntent";
75 private static final String INTENT = "Intent";
76 private static final String APP_ID = "appId";
77 private static final String ID = "id";
78 private static final String INTENT_PATHS = "paths";
79 private static final String INTENT_TYPE = "type";
Jian Licc730a62016-05-10 16:36:16 -070080 private static final String INTENT_NOT_FOUND = "Intent is not found";
Ray Milkey2b217142014-12-15 09:24:24 -080081
Author Namee252a002016-09-26 22:42:24 +053082 @Context
83 private UriInfo uriInfo;
84
Ray Milkey2b217142014-12-15 09:24:24 -080085 /**
Jian Licc730a62016-05-10 16:36:16 -070086 * Gets all intents.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070087 * Returns array containing all the intents in the system.
Jian Licc730a62016-05-10 16:36:16 -070088 *
89 * @return 200 OK with array of all the intents in the system
Andrea Campanella10c4adc2015-12-03 15:27:54 -080090 * @onos.rsModel Intents
Ray Milkey2b217142014-12-15 09:24:24 -080091 */
92 @GET
93 @Produces(MediaType.APPLICATION_JSON)
94 public Response getIntents() {
95 final Iterable<Intent> intents = get(IntentService.class).getIntents();
96 final ObjectNode root = encodeArray(Intent.class, "intents", intents);
Ray Milkey3f025692015-01-26 11:15:41 -080097 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -080098 }
99
100 /**
Jian Licc730a62016-05-10 16:36:16 -0700101 * Gets intent by application and key.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700102 * Returns details of the specified intent.
Jian Licc730a62016-05-10 16:36:16 -0700103 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700104 * @param appId application identifier
105 * @param key intent key
Jian Licc730a62016-05-10 16:36:16 -0700106 * @return 200 OK with intent data
107 * @onos.rsModel Intents
Ray Milkey2b217142014-12-15 09:24:24 -0800108 */
109 @GET
110 @Produces(MediaType.APPLICATION_JSON)
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800111 @Path("{appId}/{key}")
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700112 public Response getIntentById(@PathParam("appId") String appId,
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800113 @PathParam("key") String key) {
114 final ApplicationId app = get(CoreService.class).getAppId(appId);
Kavitha Alagesane6840cf2016-10-21 10:58:05 +0530115 nullIsNotFound(app, APP_ID_NOT_FOUND);
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800116 Intent intent = get(IntentService.class).getIntent(Key.of(key, app));
117 if (intent == null) {
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700118 long numericalKey = Long.decode(key);
119 intent = get(IntentService.class).getIntent(Key.of(numericalKey, app));
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800120 }
121 nullIsNotFound(intent, INTENT_NOT_FOUND);
122
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800123 final ObjectNode root;
124 if (intent instanceof HostToHostIntent) {
125 root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this);
126 } else if (intent instanceof PointToPointIntent) {
127 root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this);
Chiara Contolia8f69ff2016-07-28 01:06:07 +0900128 } else if (intent instanceof SinglePointToMultiPointIntent) {
129 root = codec(SinglePointToMultiPointIntent.class).encode((SinglePointToMultiPointIntent) intent, this);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800130 } else {
131 root = codec(Intent.class).encode(intent, this);
132 }
Ray Milkey3f025692015-01-26 11:15:41 -0800133 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -0800134 }
Ray Milkey67c22722015-03-09 15:48:57 -0700135
Jian Licc730a62016-05-10 16:36:16 -0700136 /**
Author Namee252a002016-09-26 22:42:24 +0530137 * Gets all related flow entries created by a particular intent.
138 * Returns all flow entries of the specified intent.
139 *
140 * @param appId application identifier
141 * @param key intent key
142 * @return 200 OK with intent data
143 * @onos.rsModel Relatedflows
144 */
145 @GET
146 @Produces(MediaType.APPLICATION_JSON)
147 @Path("relatedflows/{appId}/{key}")
148 public Response getIntentFlowsById(@PathParam("appId") String appId,
149 @PathParam("key") String key) {
150 ApplicationId applicationId = get(CoreService.class).getAppId(appId);
151 nullIsNotFound(applicationId, APP_ID_NOT_FOUND);
152 IntentService intentService = get(IntentService.class);
153 FlowRuleService flowService = get(FlowRuleService.class);
154
155 Intent intent = intentService.getIntent(Key.of(key, applicationId));
156 if (intent == null) {
157 long numericalKey = Long.decode(key);
158 intent = intentService.getIntent(
159 Key.of(numericalKey, applicationId));
160 }
161 nullIsNotFound(intent, INTENT_NOT_FOUND);
162
163 ObjectNode root = mapper().createObjectNode();
164 root.put(APP_ID, appId);
165 root.put(ID, key);
166
167 IntentFilter intentFilter = new IntentFilter(intentService, flowService);
168
169 List<Intent> installables =
170 intentService.getInstallableIntents(intent.key());
171
172 if (intent instanceof HostToHostIntent) {
173 root.put(INTENT_TYPE, HOST_TO_HOST_INTENT);
174 } else if (intent instanceof PointToPointIntent) {
175 root.put(INTENT_TYPE, POINT_TO_POINT_INTENT);
176 } else if (intent instanceof SinglePointToMultiPointIntent) {
177 root.put(INTENT_TYPE, SINGLE_TO_MULTI_POINT_INTENT);
178 } else {
179 root.put(INTENT_TYPE, INTENT);
180 }
181
182 ArrayNode pathsNode = root.putArray(INTENT_PATHS);
183
184 for (List<FlowEntry> flowEntries :
185 intentFilter.readIntentFlows(installables)) {
186 ArrayNode flowNode = pathsNode.addArray();
187
188 for (FlowEntry entry : flowEntries) {
189 flowNode.add(codec(FlowEntry.class).encode(entry, this));
190 }
191 }
192 return ok(root).build();
193 }
194
195 /**
Jian Licc730a62016-05-10 16:36:16 -0700196 * Internal listener for tracking the intent deletion events.
197 */
198 private class DeleteListener implements IntentListener {
Ray Milkey67c22722015-03-09 15:48:57 -0700199 final Key key;
200 final CountDownLatch latch;
201
Jian Licc730a62016-05-10 16:36:16 -0700202 /**
203 * Default constructor.
204 *
205 * @param key key
206 * @param latch count down latch
207 */
Ray Milkey67c22722015-03-09 15:48:57 -0700208 DeleteListener(Key key, CountDownLatch latch) {
209 this.key = key;
210 this.latch = latch;
211 }
212
213 @Override
214 public void event(IntentEvent event) {
215 if (Objects.equals(event.subject().key(), key) &&
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700216 (event.type() == IntentEvent.Type.WITHDRAWN ||
217 event.type() == IntentEvent.Type.FAILED)) {
Ray Milkey67c22722015-03-09 15:48:57 -0700218 latch.countDown();
219 }
220 }
221 }
222
223 /**
Jian Licc730a62016-05-10 16:36:16 -0700224 * Submits a new intent.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700225 * Creates and submits intent from the JSON request.
Jian Licc730a62016-05-10 16:36:16 -0700226 *
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700227 * @param stream input JSON
228 * @return status of the request - CREATED if the JSON is correct,
229 * BAD_REQUEST if the JSON is invalid
Jian Licc730a62016-05-10 16:36:16 -0700230 * @onos.rsModel IntentHost
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700231 */
232 @POST
233 @Consumes(MediaType.APPLICATION_JSON)
234 @Produces(MediaType.APPLICATION_JSON)
235 public Response createIntent(InputStream stream) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700236 try {
237 IntentService service = get(IntentService.class);
238 ObjectNode root = (ObjectNode) mapper().readTree(stream);
239 Intent intent = codec(Intent.class).decode(root, this);
240 service.submit(intent);
Ray Milkey303e6712015-07-17 14:06:21 -0700241 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
242 .path("intents")
Ray Milkey8d076402015-08-31 15:43:18 -0700243 .path(intent.appId().name())
Ray Milkey303e6712015-07-17 14:06:21 -0700244 .path(Long.toString(intent.id().fingerprint()));
245 return Response
246 .created(locationBuilder.build())
247 .build();
248 } catch (IOException ioe) {
249 throw new IllegalArgumentException(ioe);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700250 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700251 }
252
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700253 /**
Jian Licc730a62016-05-10 16:36:16 -0700254 * Withdraws intent.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700255 * Withdraws the specified intent from the system.
256 *
257 * @param appId application identifier
258 * @param key intent key
Jian Lic2a542b2016-05-10 11:48:19 -0700259 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700260 */
261 @DELETE
262 @Path("{appId}/{key}")
Jian Lic2a542b2016-05-10 11:48:19 -0700263 public Response deleteIntentById(@PathParam("appId") String appId,
Jian Licc730a62016-05-10 16:36:16 -0700264 @PathParam("key") String key) {
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700265 final ApplicationId app = get(CoreService.class).getAppId(appId);
Kavitha Alagesane6840cf2016-10-21 10:58:05 +0530266 nullIsNotFound(app, APP_ID_NOT_FOUND);
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700267 Intent intent = get(IntentService.class).getIntent(Key.of(key, app));
268 IntentService service = get(IntentService.class);
269
270 if (intent == null) {
271 intent = service
272 .getIntent(Key.of(Long.decode(key), app));
273 }
274 if (intent == null) {
275 // No such intent. REST standards recommend a positive status code
276 // in this case.
Jian Lic2a542b2016-05-10 11:48:19 -0700277 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700278 }
279
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700280 Key k = intent.key();
281
282 // set up latch and listener to track uninstall progress
283 CountDownLatch latch = new CountDownLatch(1);
284
285 IntentListener listener = new DeleteListener(k, latch);
286 service.addListener(listener);
287
288 try {
289 // request the withdraw
290 service.withdraw(intent);
291
292 try {
293 latch.await(WITHDRAW_EVENT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
294 } catch (InterruptedException e) {
295 log.info("REST Delete operation timed out waiting for intent {}", k);
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -0700296 Thread.currentThread().interrupt();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700297 }
298 // double check the state
299 IntentState state = service.getIntentState(k);
300 if (state == WITHDRAWN || state == FAILED) {
301 service.purge(intent);
302 }
303
304 } finally {
305 // clean up the listener
306 service.removeListener(listener);
307 }
Jian Lic2a542b2016-05-10 11:48:19 -0700308 return Response.noContent().build();
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700309 }
Ray Milkey2b217142014-12-15 09:24:24 -0800310}