Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.core.CoreService; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 21 | import org.onosproject.net.intent.HostToHostIntent; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 22 | import org.onosproject.net.intent.Intent; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 23 | import org.onosproject.net.intent.IntentEvent; |
| 24 | import org.onosproject.net.intent.IntentListener; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 25 | import org.onosproject.net.intent.IntentService; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 26 | import org.onosproject.net.intent.IntentState; |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.Key; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 28 | import org.onosproject.net.intent.PointToPointIntent; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 29 | import org.onosproject.rest.AbstractWebResource; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 31 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 32 | import javax.ws.rs.Consumes; |
| 33 | import javax.ws.rs.DELETE; |
| 34 | import javax.ws.rs.GET; |
| 35 | import javax.ws.rs.POST; |
| 36 | import javax.ws.rs.Path; |
| 37 | import javax.ws.rs.PathParam; |
| 38 | import javax.ws.rs.Produces; |
| 39 | import javax.ws.rs.core.Context; |
| 40 | import javax.ws.rs.core.MediaType; |
| 41 | import javax.ws.rs.core.Response; |
| 42 | import javax.ws.rs.core.UriBuilder; |
| 43 | import javax.ws.rs.core.UriInfo; |
| 44 | import java.io.IOException; |
| 45 | import java.io.InputStream; |
| 46 | import java.util.Objects; |
| 47 | import java.util.concurrent.CountDownLatch; |
| 48 | import java.util.concurrent.TimeUnit; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 49 | |
Thomas Vachuska | f8cac48 | 2015-04-08 19:40:12 -0700 | [diff] [blame] | 50 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 51 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 52 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
| 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 55 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 56 | * Query, submit and withdraw network intents. |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 57 | */ |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 58 | @Path("intents") |
| 59 | public class IntentsWebResource extends AbstractWebResource { |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 60 | @Context |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 61 | private UriInfo uriInfo; |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 62 | |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 63 | private static final Logger log = getLogger(IntentsWebResource.class); |
| 64 | private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5; |
| 65 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 66 | private static final String INTENT_NOT_FOUND = "Intent is not found"; |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 69 | * Gets all intents. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 70 | * Returns array containing all the intents in the system. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 71 | * |
| 72 | * @return 200 OK with array of all the intents in the system |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 73 | * @onos.rsModel Intents |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 74 | */ |
| 75 | @GET |
| 76 | @Produces(MediaType.APPLICATION_JSON) |
| 77 | public Response getIntents() { |
| 78 | final Iterable<Intent> intents = get(IntentService.class).getIntents(); |
| 79 | final ObjectNode root = encodeArray(Intent.class, "intents", intents); |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 80 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 84 | * Gets intent by application and key. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 85 | * Returns details of the specified intent. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 86 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 87 | * @param appId application identifier |
| 88 | * @param key intent key |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 89 | * @return 200 OK with intent data |
| 90 | * @onos.rsModel Intents |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 91 | */ |
| 92 | @GET |
| 93 | @Produces(MediaType.APPLICATION_JSON) |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 94 | @Path("{appId}/{key}") |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 95 | public Response getIntentById(@PathParam("appId") String appId, |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 96 | @PathParam("key") String key) { |
| 97 | final ApplicationId app = get(CoreService.class).getAppId(appId); |
| 98 | |
| 99 | Intent intent = get(IntentService.class).getIntent(Key.of(key, app)); |
| 100 | if (intent == null) { |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 101 | long numericalKey = Long.decode(key); |
| 102 | intent = get(IntentService.class).getIntent(Key.of(numericalKey, app)); |
Ayaka Koshibe | c06c89b | 2015-02-10 19:25:41 -0800 | [diff] [blame] | 103 | } |
| 104 | nullIsNotFound(intent, INTENT_NOT_FOUND); |
| 105 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 106 | final ObjectNode root; |
| 107 | if (intent instanceof HostToHostIntent) { |
| 108 | root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this); |
| 109 | } else if (intent instanceof PointToPointIntent) { |
| 110 | root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this); |
| 111 | } else { |
| 112 | root = codec(Intent.class).encode(intent, this); |
| 113 | } |
Ray Milkey | 3f02569 | 2015-01-26 11:15:41 -0800 | [diff] [blame] | 114 | return ok(root).build(); |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 115 | } |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 116 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Internal listener for tracking the intent deletion events. |
| 119 | */ |
| 120 | private class DeleteListener implements IntentListener { |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 121 | final Key key; |
| 122 | final CountDownLatch latch; |
| 123 | |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Default constructor. |
| 126 | * |
| 127 | * @param key key |
| 128 | * @param latch count down latch |
| 129 | */ |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 130 | DeleteListener(Key key, CountDownLatch latch) { |
| 131 | this.key = key; |
| 132 | this.latch = latch; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void event(IntentEvent event) { |
| 137 | if (Objects.equals(event.subject().key(), key) && |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 138 | (event.type() == IntentEvent.Type.WITHDRAWN || |
| 139 | event.type() == IntentEvent.Type.FAILED)) { |
Ray Milkey | 67c2272 | 2015-03-09 15:48:57 -0700 | [diff] [blame] | 140 | latch.countDown(); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 146 | * Submits a new intent. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 147 | * Creates and submits intent from the JSON request. |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 148 | * |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 149 | * @param stream input JSON |
| 150 | * @return status of the request - CREATED if the JSON is correct, |
| 151 | * BAD_REQUEST if the JSON is invalid |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 152 | * @onos.rsModel IntentHost |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 153 | */ |
| 154 | @POST |
| 155 | @Consumes(MediaType.APPLICATION_JSON) |
| 156 | @Produces(MediaType.APPLICATION_JSON) |
| 157 | public Response createIntent(InputStream stream) { |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 158 | try { |
| 159 | IntentService service = get(IntentService.class); |
| 160 | ObjectNode root = (ObjectNode) mapper().readTree(stream); |
| 161 | Intent intent = codec(Intent.class).decode(root, this); |
| 162 | service.submit(intent); |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 163 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 164 | .path("intents") |
Ray Milkey | 8d07640 | 2015-08-31 15:43:18 -0700 | [diff] [blame] | 165 | .path(intent.appId().name()) |
Ray Milkey | 303e671 | 2015-07-17 14:06:21 -0700 | [diff] [blame] | 166 | .path(Long.toString(intent.id().fingerprint())); |
| 167 | return Response |
| 168 | .created(locationBuilder.build()) |
| 169 | .build(); |
| 170 | } catch (IOException ioe) { |
| 171 | throw new IllegalArgumentException(ioe); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 172 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 175 | /** |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 176 | * Withdraws intent. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 177 | * Withdraws the specified intent from the system. |
| 178 | * |
| 179 | * @param appId application identifier |
| 180 | * @param key intent key |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 181 | * @return 204 NO CONTENT |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 182 | */ |
| 183 | @DELETE |
| 184 | @Path("{appId}/{key}") |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 185 | public Response deleteIntentById(@PathParam("appId") String appId, |
Jian Li | cc730a6 | 2016-05-10 16:36:16 -0700 | [diff] [blame] | 186 | @PathParam("key") String key) { |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 187 | final ApplicationId app = get(CoreService.class).getAppId(appId); |
| 188 | |
| 189 | Intent intent = get(IntentService.class).getIntent(Key.of(key, app)); |
| 190 | IntentService service = get(IntentService.class); |
| 191 | |
| 192 | if (intent == null) { |
| 193 | intent = service |
| 194 | .getIntent(Key.of(Long.decode(key), app)); |
| 195 | } |
| 196 | if (intent == null) { |
| 197 | // No such intent. REST standards recommend a positive status code |
| 198 | // in this case. |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 199 | return Response.noContent().build(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 202 | Key k = intent.key(); |
| 203 | |
| 204 | // set up latch and listener to track uninstall progress |
| 205 | CountDownLatch latch = new CountDownLatch(1); |
| 206 | |
| 207 | IntentListener listener = new DeleteListener(k, latch); |
| 208 | service.addListener(listener); |
| 209 | |
| 210 | try { |
| 211 | // request the withdraw |
| 212 | service.withdraw(intent); |
| 213 | |
| 214 | try { |
| 215 | latch.await(WITHDRAW_EVENT_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 216 | } catch (InterruptedException e) { |
| 217 | log.info("REST Delete operation timed out waiting for intent {}", k); |
| 218 | } |
| 219 | // double check the state |
| 220 | IntentState state = service.getIntentState(k); |
| 221 | if (state == WITHDRAWN || state == FAILED) { |
| 222 | service.purge(intent); |
| 223 | } |
| 224 | |
| 225 | } finally { |
| 226 | // clean up the listener |
| 227 | service.removeListener(listener); |
| 228 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 229 | return Response.noContent().build(); |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 230 | } |
Ray Milkey | 2b21714 | 2014-12-15 09:24:24 -0800 | [diff] [blame] | 231 | } |