Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 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 | */ |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 17 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.core.CoreService; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 22 | import org.onosproject.net.flow.FlowEntry; |
| 23 | import org.onosproject.net.flow.FlowRuleService; |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 24 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
| 25 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 26 | import org.onosproject.net.intent.HostToHostIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.Intent; |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 28 | import org.onosproject.net.intent.IntentState; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 29 | import org.onosproject.net.intent.IntentEvent; |
| 30 | import org.onosproject.net.intent.IntentListener; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 31 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.Key; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 33 | import org.onosproject.net.intent.util.IntentFilter; |
dvaddire | 95c84ed | 2017-06-14 15:42:24 +0530 | [diff] [blame] | 34 | import org.onosproject.net.intent.util.IntentMiniSummary; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 35 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 37 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 38 | import javax.ws.rs.Consumes; |
| 39 | import javax.ws.rs.DELETE; |
| 40 | import javax.ws.rs.GET; |
| 41 | import javax.ws.rs.POST; |
| 42 | import javax.ws.rs.Path; |
| 43 | import javax.ws.rs.PathParam; |
| 44 | import javax.ws.rs.Produces; |
| 45 | import javax.ws.rs.core.Context; |
| 46 | import javax.ws.rs.core.MediaType; |
| 47 | import javax.ws.rs.core.Response; |
| 48 | import javax.ws.rs.core.UriBuilder; |
| 49 | import javax.ws.rs.core.UriInfo; |
| 50 | import java.io.IOException; |
| 51 | import java.io.InputStream; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 52 | import java.util.List; |
dvaddire | 95c84ed | 2017-06-14 15:42:24 +0530 | [diff] [blame] | 53 | import java.util.Map; |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 54 | import java.util.Objects; |
| 55 | import java.util.concurrent.CountDownLatch; |
| 56 | import java.util.concurrent.TimeUnit; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 57 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 58 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 59 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 60 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
| 61 | import static org.slf4j.LoggerFactory.getLogger; |
| 62 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 63 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 64 | * Query, submit and withdraw network intents. |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 65 | */ |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 66 | @Path("intents") |
| 67 | public class IntentsWebResource extends AbstractWebResource { |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 68 | |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 69 | private static final Logger log = getLogger(IntentsWebResource.class); |
| 70 | private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5; |
| 71 | |
Kavitha Alagesan | e6840cf | 2016-10-21 10:58:05 +0530 | [diff] [blame] | 72 | private static final String APP_ID_NOT_FOUND = "Application Id not found"; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 73 | 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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 82 | private static final String INTENT_NOT_FOUND = "Intent is not found"; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 83 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 84 | @Context |
| 85 | private UriInfo uriInfo; |
| 86 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 87 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 88 | * Gets all intents. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 89 | * Returns array containing all the intents in the system. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 90 | * |
| 91 | * @return 200 OK with array of all the intents in the system |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 92 | * @onos.rsModel Intents |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 93 | */ |
| 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 Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 99 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 100 | } |
| 101 | |
dvaddire | 95c84ed | 2017-06-14 15:42:24 +0530 | [diff] [blame] | 102 | |
| 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 Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 125 | /** |
Rafal Szalecki | f97e0ed | 2017-06-01 13:07:18 +0200 | [diff] [blame] | 126 | * 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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 155 | * Gets intent by application and key. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 156 | * Returns details of the specified intent. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 157 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 158 | * @param appId application identifier |
| 159 | * @param key intent key |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 160 | * @return 200 OK with intent data |
| 161 | * @onos.rsModel Intents |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 162 | */ |
| 163 | @GET |
| 164 | @Produces(MediaType.APPLICATION_JSON) |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 165 | @Path("{appId}/{key}") |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 166 | public Response getIntentById(@PathParam("appId") String appId, |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 167 | @PathParam("key") String key) { |
| 168 | final ApplicationId app = get(CoreService.class).getAppId(appId); |
Kavitha Alagesan | e6840cf | 2016-10-21 10:58:05 +0530 | [diff] [blame] | 169 | nullIsNotFound(app, APP_ID_NOT_FOUND); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 170 | Intent intent = get(IntentService.class).getIntent(Key.of(key, app)); |
| 171 | if (intent == null) { |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 172 | long numericalKey = Long.decode(key); |
| 173 | intent = get(IntentService.class).getIntent(Key.of(numericalKey, app)); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 174 | } |
| 175 | nullIsNotFound(intent, INTENT_NOT_FOUND); |
| 176 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 177 | 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 Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 182 | } else if (intent instanceof SinglePointToMultiPointIntent) { |
| 183 | root = codec(SinglePointToMultiPointIntent.class).encode((SinglePointToMultiPointIntent) intent, this); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 184 | } else { |
| 185 | root = codec(Intent.class).encode(intent, this); |
| 186 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 187 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 188 | } |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 189 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 190 | /** |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 191 | * 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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 250 | * Internal listener for tracking the intent deletion events. |
| 251 | */ |
| 252 | private class DeleteListener implements IntentListener { |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 253 | final Key key; |
| 254 | final CountDownLatch latch; |
| 255 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 256 | /** |
| 257 | * Default constructor. |
| 258 | * |
| 259 | * @param key key |
| 260 | * @param latch count down latch |
| 261 | */ |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 262 | 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 270 | (event.type() == IntentEvent.Type.WITHDRAWN || |
| 271 | event.type() == IntentEvent.Type.FAILED)) { |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 272 | latch.countDown(); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 278 | * Submits a new intent. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 279 | * Creates and submits intent from the JSON request. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 280 | * |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 281 | * @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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 284 | * @onos.rsModel IntentHost |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 285 | */ |
| 286 | @POST |
| 287 | @Consumes(MediaType.APPLICATION_JSON) |
| 288 | @Produces(MediaType.APPLICATION_JSON) |
| 289 | public Response createIntent(InputStream stream) { |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 290 | 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 Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 295 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 296 | .path("intents") |
Ray Milkey | 8d07640 | 2015-08-31 15:43:18 -0700 | [diff] [blame] | 297 | .path(intent.appId().name()) |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 298 | .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 Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 304 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 307 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 308 | * Withdraws intent. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 309 | * Withdraws the specified intent from the system. |
| 310 | * |
| 311 | * @param appId application identifier |
| 312 | * @param key intent key |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 313 | * @return 204 NO CONTENT |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 314 | */ |
| 315 | @DELETE |
| 316 | @Path("{appId}/{key}") |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 317 | public Response deleteIntentById(@PathParam("appId") String appId, |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 318 | @PathParam("key") String key) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 319 | final ApplicationId app = get(CoreService.class).getAppId(appId); |
Kavitha Alagesan | e6840cf | 2016-10-21 10:58:05 +0530 | [diff] [blame] | 320 | nullIsNotFound(app, APP_ID_NOT_FOUND); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 321 | 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 Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 331 | return Response.noContent().build(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 334 | 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 HIGUCHI | 4f8a377 | 2017-05-16 20:23:49 -0700 | [diff] [blame] | 350 | Thread.currentThread().interrupt(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 351 | } |
| 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 Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 362 | return Response.noContent().build(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 363 | } |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 364 | } |