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 | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 60 | import static org.onlab.util.Tools.readTreeFromStream; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 61 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 62 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
| 63 | import static org.slf4j.LoggerFactory.getLogger; |
| 64 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 65 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 66 | * Query, submit and withdraw network intents. |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 67 | */ |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 68 | @Path("intents") |
| 69 | public class IntentsWebResource extends AbstractWebResource { |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 70 | |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 71 | private static final Logger log = getLogger(IntentsWebResource.class); |
| 72 | private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5; |
| 73 | |
Kavitha Alagesan | e6840cf | 2016-10-21 10:58:05 +0530 | [diff] [blame] | 74 | private static final String APP_ID_NOT_FOUND = "Application Id not found"; |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 75 | private static final String HOST_TO_HOST_INTENT = "HostToHostIntent"; |
| 76 | private static final String POINT_TO_POINT_INTENT = "PointToPointIntent"; |
| 77 | private static final String SINGLE_TO_MULTI_POINT_INTENT = |
| 78 | "SinglePointToMultiPointIntent"; |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 79 | private static final String MULTI_TO_SINGLE_POINT_INTENT = |
| 80 | "MultiPointToSinglePointIntent"; |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 81 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 82 | private static final String INTENT = "Intent"; |
| 83 | private static final String APP_ID = "appId"; |
| 84 | private static final String ID = "id"; |
| 85 | private static final String INTENT_PATHS = "paths"; |
| 86 | private static final String INTENT_TYPE = "type"; |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 87 | private static final String INTENT_NOT_FOUND = "Intent is not found"; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 88 | |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 89 | @Context |
| 90 | private UriInfo uriInfo; |
| 91 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 92 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 93 | * Gets all intents. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 94 | * Returns array containing all the intents in the system. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 95 | * |
| 96 | * @return 200 OK with array of all the intents in the system |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 97 | * @onos.rsModel Intents |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 98 | */ |
| 99 | @GET |
| 100 | @Produces(MediaType.APPLICATION_JSON) |
| 101 | public Response getIntents() { |
| 102 | final Iterable<Intent> intents = get(IntentService.class).getIntents(); |
| 103 | final ObjectNode root = encodeArray(Intent.class, "intents", intents); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 104 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 105 | } |
| 106 | |
dvaddire | 95c84ed | 2017-06-14 15:42:24 +0530 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Gets Summary of all intents. |
| 110 | * Returns Summary of the intents in the system. |
| 111 | * |
| 112 | * @return 200 OK with Summary of all the intents in the system |
| 113 | * @onos.rsModel Minisummary |
| 114 | */ |
| 115 | @GET |
| 116 | @Produces(MediaType.APPLICATION_JSON) |
| 117 | @Path("minisummary") |
| 118 | public Response getIntentSummary() { |
| 119 | final Iterable<Intent> intents = get(IntentService.class).getIntents(); |
| 120 | ObjectNode root = mapper().createObjectNode(); |
| 121 | IntentMiniSummary intentminisummary = new IntentMiniSummary(); |
| 122 | Map<String, IntentMiniSummary> map = intentminisummary.summarize(intents, get(IntentService.class)); |
| 123 | map.values().stream().forEach(intentsummary -> { |
| 124 | root.put(intentsummary.getIntentType(), codec(IntentMiniSummary.class).encode(intentsummary, this)); |
| 125 | }); |
| 126 | return ok(root).build(); |
| 127 | } |
| 128 | |
| 129 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 130 | /** |
Aris C. Risdianto | a579c8f | 2017-04-13 19:38:46 +0900 | [diff] [blame] | 131 | * Gets intent installables by application ID and key. |
Rafal Szalecki | f97e0ed | 2017-06-01 13:07:18 +0200 | [diff] [blame] | 132 | * @param appId application identifier |
| 133 | * @param key intent key |
| 134 | * |
| 135 | * @return 200 OK with array of the intent installables |
| 136 | * @onos.rsModel Intents |
| 137 | */ |
| 138 | @GET |
| 139 | @Produces(MediaType.APPLICATION_JSON) |
| 140 | @Path("installables/{appId}/{key}") |
| 141 | public Response getIntentWithInstallable(@PathParam("appId") String appId, |
| 142 | @PathParam("key") String key) { |
| 143 | final IntentService intentService = get(IntentService.class); |
| 144 | final ApplicationId app = get(CoreService.class).getAppId(appId); |
| 145 | nullIsNotFound(app, APP_ID_NOT_FOUND); |
| 146 | |
| 147 | Intent intent = intentService.getIntent(Key.of(key, app)); |
| 148 | if (intent == null) { |
| 149 | long numericalKey = Long.decode(key); |
| 150 | intent = intentService.getIntent(Key.of(numericalKey, app)); |
| 151 | } |
| 152 | nullIsNotFound(intent, INTENT_NOT_FOUND); |
| 153 | |
| 154 | final Iterable<Intent> installables = intentService.getInstallableIntents(intent.key()); |
| 155 | final ObjectNode root = encodeArray(Intent.class, "installables", installables); |
| 156 | return ok(root).build(); |
| 157 | } |
| 158 | |
| 159 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 160 | * Gets intent by application and key. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 161 | * Returns details of the specified intent. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 162 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 163 | * @param appId application identifier |
| 164 | * @param key intent key |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 165 | * @return 200 OK with intent data |
| 166 | * @onos.rsModel Intents |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 167 | */ |
| 168 | @GET |
| 169 | @Produces(MediaType.APPLICATION_JSON) |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 170 | @Path("{appId}/{key}") |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 171 | public Response getIntentById(@PathParam("appId") String appId, |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 172 | @PathParam("key") String key) { |
| 173 | final ApplicationId app = get(CoreService.class).getAppId(appId); |
Kavitha Alagesan | e6840cf | 2016-10-21 10:58:05 +0530 | [diff] [blame] | 174 | nullIsNotFound(app, APP_ID_NOT_FOUND); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 175 | Intent intent = get(IntentService.class).getIntent(Key.of(key, app)); |
| 176 | if (intent == null) { |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 177 | long numericalKey = Long.decode(key); |
| 178 | intent = get(IntentService.class).getIntent(Key.of(numericalKey, app)); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 179 | } |
| 180 | nullIsNotFound(intent, INTENT_NOT_FOUND); |
| 181 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 182 | final ObjectNode root; |
| 183 | if (intent instanceof HostToHostIntent) { |
| 184 | root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this); |
| 185 | } else if (intent instanceof PointToPointIntent) { |
| 186 | root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this); |
Chiara Contoli | a8f69ff | 2016-07-28 01:06:07 +0900 | [diff] [blame] | 187 | } else if (intent instanceof SinglePointToMultiPointIntent) { |
| 188 | root = codec(SinglePointToMultiPointIntent.class).encode((SinglePointToMultiPointIntent) intent, this); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 189 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
| 190 | root = codec(MultiPointToSinglePointIntent.class).encode((MultiPointToSinglePointIntent) intent, this); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 191 | } else { |
| 192 | root = codec(Intent.class).encode(intent, this); |
| 193 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 194 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 195 | } |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 196 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 197 | /** |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 198 | * Gets all related flow entries created by a particular intent. |
| 199 | * Returns all flow entries of the specified intent. |
| 200 | * |
| 201 | * @param appId application identifier |
| 202 | * @param key intent key |
| 203 | * @return 200 OK with intent data |
| 204 | * @onos.rsModel Relatedflows |
| 205 | */ |
| 206 | @GET |
| 207 | @Produces(MediaType.APPLICATION_JSON) |
| 208 | @Path("relatedflows/{appId}/{key}") |
| 209 | public Response getIntentFlowsById(@PathParam("appId") String appId, |
| 210 | @PathParam("key") String key) { |
| 211 | ApplicationId applicationId = get(CoreService.class).getAppId(appId); |
| 212 | nullIsNotFound(applicationId, APP_ID_NOT_FOUND); |
| 213 | IntentService intentService = get(IntentService.class); |
| 214 | FlowRuleService flowService = get(FlowRuleService.class); |
| 215 | |
| 216 | Intent intent = intentService.getIntent(Key.of(key, applicationId)); |
| 217 | if (intent == null) { |
| 218 | long numericalKey = Long.decode(key); |
| 219 | intent = intentService.getIntent( |
| 220 | Key.of(numericalKey, applicationId)); |
| 221 | } |
| 222 | nullIsNotFound(intent, INTENT_NOT_FOUND); |
| 223 | |
| 224 | ObjectNode root = mapper().createObjectNode(); |
| 225 | root.put(APP_ID, appId); |
| 226 | root.put(ID, key); |
| 227 | |
| 228 | IntentFilter intentFilter = new IntentFilter(intentService, flowService); |
| 229 | |
| 230 | List<Intent> installables = |
| 231 | intentService.getInstallableIntents(intent.key()); |
| 232 | |
| 233 | if (intent instanceof HostToHostIntent) { |
| 234 | root.put(INTENT_TYPE, HOST_TO_HOST_INTENT); |
| 235 | } else if (intent instanceof PointToPointIntent) { |
| 236 | root.put(INTENT_TYPE, POINT_TO_POINT_INTENT); |
| 237 | } else if (intent instanceof SinglePointToMultiPointIntent) { |
| 238 | root.put(INTENT_TYPE, SINGLE_TO_MULTI_POINT_INTENT); |
nassima toumi | 9576131 | 2017-05-23 14:00:33 +0300 | [diff] [blame] | 239 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
| 240 | root.put(INTENT_TYPE, MULTI_TO_SINGLE_POINT_INTENT); |
Author Name | e252a00 | 2016-09-26 22:42:24 +0530 | [diff] [blame] | 241 | } else { |
| 242 | root.put(INTENT_TYPE, INTENT); |
| 243 | } |
| 244 | |
| 245 | ArrayNode pathsNode = root.putArray(INTENT_PATHS); |
| 246 | |
| 247 | for (List<FlowEntry> flowEntries : |
| 248 | intentFilter.readIntentFlows(installables)) { |
| 249 | ArrayNode flowNode = pathsNode.addArray(); |
| 250 | |
| 251 | for (FlowEntry entry : flowEntries) { |
| 252 | flowNode.add(codec(FlowEntry.class).encode(entry, this)); |
| 253 | } |
| 254 | } |
| 255 | return ok(root).build(); |
| 256 | } |
| 257 | |
| 258 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 259 | * Internal listener for tracking the intent deletion events. |
| 260 | */ |
| 261 | private class DeleteListener implements IntentListener { |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 262 | final Key key; |
| 263 | final CountDownLatch latch; |
| 264 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 265 | /** |
| 266 | * Default constructor. |
| 267 | * |
| 268 | * @param key key |
| 269 | * @param latch count down latch |
| 270 | */ |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 271 | DeleteListener(Key key, CountDownLatch latch) { |
| 272 | this.key = key; |
| 273 | this.latch = latch; |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void event(IntentEvent event) { |
| 278 | if (Objects.equals(event.subject().key(), key) && |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 279 | (event.type() == IntentEvent.Type.WITHDRAWN || |
| 280 | event.type() == IntentEvent.Type.FAILED)) { |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 281 | latch.countDown(); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 287 | * Submits a new intent. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 288 | * Creates and submits intent from the JSON request. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 289 | * |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 290 | * @param stream input JSON |
| 291 | * @return status of the request - CREATED if the JSON is correct, |
| 292 | * BAD_REQUEST if the JSON is invalid |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 293 | * @onos.rsModel IntentHost |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 294 | */ |
| 295 | @POST |
| 296 | @Consumes(MediaType.APPLICATION_JSON) |
| 297 | @Produces(MediaType.APPLICATION_JSON) |
| 298 | public Response createIntent(InputStream stream) { |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 299 | try { |
| 300 | IntentService service = get(IntentService.class); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 301 | ObjectNode root = readTreeFromStream(mapper(), stream); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 302 | Intent intent = codec(Intent.class).decode(root, this); |
| 303 | service.submit(intent); |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 304 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 305 | .path("intents") |
Ray Milkey | 8d07640 | 2015-08-31 15:43:18 -0700 | [diff] [blame] | 306 | .path(intent.appId().name()) |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 307 | .path(Long.toString(intent.id().fingerprint())); |
| 308 | return Response |
| 309 | .created(locationBuilder.build()) |
| 310 | .build(); |
| 311 | } catch (IOException ioe) { |
| 312 | throw new IllegalArgumentException(ioe); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 313 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 316 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 317 | * Withdraws intent. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 318 | * Withdraws the specified intent from the system. |
| 319 | * |
| 320 | * @param appId application identifier |
| 321 | * @param key intent key |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 322 | * @return 204 NO CONTENT |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 323 | */ |
| 324 | @DELETE |
| 325 | @Path("{appId}/{key}") |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 326 | public Response deleteIntentById(@PathParam("appId") String appId, |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 327 | @PathParam("key") String key) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 328 | final ApplicationId app = get(CoreService.class).getAppId(appId); |
Kavitha Alagesan | e6840cf | 2016-10-21 10:58:05 +0530 | [diff] [blame] | 329 | nullIsNotFound(app, APP_ID_NOT_FOUND); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 330 | Intent intent = get(IntentService.class).getIntent(Key.of(key, app)); |
| 331 | IntentService service = get(IntentService.class); |
| 332 | |
| 333 | if (intent == null) { |
| 334 | intent = service |
| 335 | .getIntent(Key.of(Long.decode(key), app)); |
| 336 | } |
| 337 | if (intent == null) { |
| 338 | // No such intent. REST standards recommend a positive status code |
| 339 | // in this case. |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 340 | return Response.noContent().build(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 343 | Key k = intent.key(); |
| 344 | |
| 345 | // set up latch and listener to track uninstall progress |
| 346 | CountDownLatch latch = new CountDownLatch(1); |
| 347 | |
| 348 | IntentListener listener = new DeleteListener(k, latch); |
| 349 | service.addListener(listener); |
| 350 | |
| 351 | try { |
| 352 | // request the withdraw |
| 353 | service.withdraw(intent); |
| 354 | |
| 355 | try { |
| 356 | latch.await(WITHDRAW_EVENT_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 357 | } catch (InterruptedException e) { |
| 358 | log.info("REST Delete operation timed out waiting for intent {}", k); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 359 | Thread.currentThread().interrupt(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 360 | } |
| 361 | // double check the state |
| 362 | IntentState state = service.getIntentState(k); |
| 363 | if (state == WITHDRAWN || state == FAILED) { |
| 364 | service.purge(intent); |
| 365 | } |
| 366 | |
| 367 | } finally { |
| 368 | // clean up the listener |
| 369 | service.removeListener(listener); |
| 370 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 371 | return Response.noContent().build(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 372 | } |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 373 | } |