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; |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 25 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 26 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.HostToHostIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 28 | import org.onosproject.net.intent.Intent; |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 29 | import org.onosproject.net.intent.IntentState; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 30 | import org.onosproject.net.intent.IntentEvent; |
| 31 | import org.onosproject.net.intent.IntentListener; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.Key; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 34 | import org.onosproject.net.intent.util.IntentFilter; |
dvaddire | 95c84ed | 2017-06-14 15:42:24 +0530 | [diff] [blame] | 35 | import org.onosproject.net.intent.util.IntentMiniSummary; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 36 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 38 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 39 | import javax.ws.rs.Consumes; |
| 40 | import javax.ws.rs.DELETE; |
| 41 | import javax.ws.rs.GET; |
| 42 | import javax.ws.rs.POST; |
| 43 | import javax.ws.rs.Path; |
| 44 | import javax.ws.rs.PathParam; |
| 45 | import javax.ws.rs.Produces; |
| 46 | import javax.ws.rs.core.Context; |
| 47 | import javax.ws.rs.core.MediaType; |
| 48 | import javax.ws.rs.core.Response; |
| 49 | import javax.ws.rs.core.UriBuilder; |
| 50 | import javax.ws.rs.core.UriInfo; |
| 51 | import java.io.IOException; |
| 52 | import java.io.InputStream; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 53 | import java.util.List; |
dvaddire | 95c84ed | 2017-06-14 15:42:24 +0530 | [diff] [blame] | 54 | import java.util.Map; |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 55 | import java.util.Objects; |
| 56 | import java.util.concurrent.CountDownLatch; |
| 57 | import java.util.concurrent.TimeUnit; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 58 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 59 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 60 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 61 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
| 63 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 64 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 65 | * Query, submit and withdraw network intents. |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 66 | */ |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 67 | @Path("intents") |
| 68 | public class IntentsWebResource extends AbstractWebResource { |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 69 | |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 70 | private static final Logger log = getLogger(IntentsWebResource.class); |
| 71 | private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5; |
| 72 | |
Kavitha Alagesan | e6840cf | 2016-10-21 10:58:05 +0530 | [diff] [blame] | 73 | private static final String APP_ID_NOT_FOUND = "Application Id not found"; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 74 | 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 toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 78 | private static final String MULTI_TO_SINGLE_POINT_INTENT = |
| 79 | "MultiPointToSinglePointIntent"; |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 80 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 81 | 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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 86 | private static final String INTENT_NOT_FOUND = "Intent is not found"; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 87 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 88 | @Context |
| 89 | private UriInfo uriInfo; |
| 90 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 91 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 92 | * Gets all intents. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 93 | * Returns array containing all the intents in the system. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 94 | * |
| 95 | * @return 200 OK with array of all the intents in the system |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 96 | * @onos.rsModel Intents |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 97 | */ |
| 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 Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 103 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 104 | } |
| 105 | |
dvaddire | 95c84ed | 2017-06-14 15:42:24 +0530 | [diff] [blame] | 106 | |
| 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 Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 129 | /** |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 130 | * Gets intent installables by application ID and key. |
Rafal Szalecki | f97e0ed | 2017-06-01 13:07:18 +0200 | [diff] [blame] | 131 | * @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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 159 | * Gets intent by application and key. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 160 | * Returns details of the specified intent. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 161 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 162 | * @param appId application identifier |
| 163 | * @param key intent key |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 164 | * @return 200 OK with intent data |
| 165 | * @onos.rsModel Intents |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 166 | */ |
| 167 | @GET |
| 168 | @Produces(MediaType.APPLICATION_JSON) |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 169 | @Path("{appId}/{key}") |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 170 | public Response getIntentById(@PathParam("appId") String appId, |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 171 | @PathParam("key") String key) { |
| 172 | final ApplicationId app = get(CoreService.class).getAppId(appId); |
Kavitha Alagesan | e6840cf | 2016-10-21 10:58:05 +0530 | [diff] [blame] | 173 | nullIsNotFound(app, APP_ID_NOT_FOUND); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 174 | Intent intent = get(IntentService.class).getIntent(Key.of(key, app)); |
| 175 | if (intent == null) { |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 176 | long numericalKey = Long.decode(key); |
| 177 | intent = get(IntentService.class).getIntent(Key.of(numericalKey, app)); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 178 | } |
| 179 | nullIsNotFound(intent, INTENT_NOT_FOUND); |
| 180 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 181 | 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 Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 186 | } else if (intent instanceof SinglePointToMultiPointIntent) { |
| 187 | root = codec(SinglePointToMultiPointIntent.class).encode((SinglePointToMultiPointIntent) intent, this); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 188 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
| 189 | root = codec(MultiPointToSinglePointIntent.class).encode((MultiPointToSinglePointIntent) intent, this); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 190 | } else { |
| 191 | root = codec(Intent.class).encode(intent, this); |
| 192 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 193 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 194 | } |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 195 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 196 | /** |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 197 | * 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 toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 238 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
| 239 | root.put(INTENT_TYPE, MULTI_TO_SINGLE_POINT_INTENT); |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 240 | } 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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 258 | * Internal listener for tracking the intent deletion events. |
| 259 | */ |
| 260 | private class DeleteListener implements IntentListener { |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 261 | final Key key; |
| 262 | final CountDownLatch latch; |
| 263 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Default constructor. |
| 266 | * |
| 267 | * @param key key |
| 268 | * @param latch count down latch |
| 269 | */ |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 270 | 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 Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 278 | (event.type() == IntentEvent.Type.WITHDRAWN || |
| 279 | event.type() == IntentEvent.Type.FAILED)) { |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 280 | latch.countDown(); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 286 | * Submits a new intent. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 287 | * Creates and submits intent from the JSON request. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 288 | * |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 289 | * @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 Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 292 | * @onos.rsModel IntentHost |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 293 | */ |
| 294 | @POST |
| 295 | @Consumes(MediaType.APPLICATION_JSON) |
| 296 | @Produces(MediaType.APPLICATION_JSON) |
| 297 | public Response createIntent(InputStream stream) { |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 298 | 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 Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 303 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 304 | .path("intents") |
Ray Milkey | 8d07640 | 2015-08-31 15:43:18 -0700 | [diff] [blame] | 305 | .path(intent.appId().name()) |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 306 | .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 Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 312 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 315 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 316 | * Withdraws intent. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 317 | * Withdraws the specified intent from the system. |
| 318 | * |
| 319 | * @param appId application identifier |
| 320 | * @param key intent key |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 321 | * @return 204 NO CONTENT |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 322 | */ |
| 323 | @DELETE |
| 324 | @Path("{appId}/{key}") |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 325 | public Response deleteIntentById(@PathParam("appId") String appId, |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 326 | @PathParam("key") String key) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 327 | final ApplicationId app = get(CoreService.class).getAppId(appId); |
Kavitha Alagesan | e6840cf | 2016-10-21 10:58:05 +0530 | [diff] [blame] | 328 | nullIsNotFound(app, APP_ID_NOT_FOUND); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 329 | 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 Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 339 | return Response.noContent().build(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 342 | 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); |
| 358 | } |
| 359 | // double check the state |
| 360 | IntentState state = service.getIntentState(k); |
| 361 | if (state == WITHDRAWN || state == FAILED) { |
| 362 | service.purge(intent); |
| 363 | } |
| 364 | |
| 365 | } finally { |
| 366 | // clean up the listener |
| 367 | service.removeListener(listener); |
| 368 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 369 | return Response.noContent().build(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 370 | } |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 371 | } |