blob: b29e3e8523b44c7d56f26566eb6533ed8e7795e3 [file] [log] [blame]
Ray Milkey2b217142014-12-15 09:24:24 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Ray Milkeyb82c42b2015-06-30 09:42:20 -070018import java.io.IOException;
19import java.io.InputStream;
Ray Milkey67c22722015-03-09 15:48:57 -070020import java.util.Objects;
21import java.util.concurrent.CountDownLatch;
22import java.util.concurrent.TimeUnit;
23
Ray Milkeyb82c42b2015-06-30 09:42:20 -070024import javax.ws.rs.Consumes;
Ray Milkey67c22722015-03-09 15:48:57 -070025import javax.ws.rs.DELETE;
Ray Milkey2b217142014-12-15 09:24:24 -080026import javax.ws.rs.GET;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070027import javax.ws.rs.POST;
Ray Milkey2b217142014-12-15 09:24:24 -080028import javax.ws.rs.Path;
29import javax.ws.rs.PathParam;
30import javax.ws.rs.Produces;
Ray Milkey303e6712015-07-17 14:06:21 -070031import javax.ws.rs.core.Context;
Ray Milkey2b217142014-12-15 09:24:24 -080032import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
Ray Milkey303e6712015-07-17 14:06:21 -070034import javax.ws.rs.core.UriBuilder;
35import javax.ws.rs.core.UriInfo;
Ray Milkey2b217142014-12-15 09:24:24 -080036
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080037import org.onosproject.core.ApplicationId;
38import org.onosproject.core.CoreService;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080039import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080040import org.onosproject.net.intent.Intent;
Ray Milkey67c22722015-03-09 15:48:57 -070041import org.onosproject.net.intent.IntentEvent;
42import org.onosproject.net.intent.IntentListener;
Ray Milkey2b217142014-12-15 09:24:24 -080043import org.onosproject.net.intent.IntentService;
Ray Milkey67c22722015-03-09 15:48:57 -070044import org.onosproject.net.intent.IntentState;
Ray Milkeyf9af43c2015-02-09 16:45:48 -080045import org.onosproject.net.intent.Key;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080046import org.onosproject.net.intent.PointToPointIntent;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070047import org.onosproject.rest.AbstractWebResource;
Ray Milkey67c22722015-03-09 15:48:57 -070048import org.slf4j.Logger;
Ray Milkey2b217142014-12-15 09:24:24 -080049
50import com.fasterxml.jackson.databind.node.ObjectNode;
51
Thomas Vachuskaf8cac482015-04-08 19:40:12 -070052import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey67c22722015-03-09 15:48:57 -070053import static org.onosproject.net.intent.IntentState.FAILED;
54import static org.onosproject.net.intent.IntentState.WITHDRAWN;
55import static org.slf4j.LoggerFactory.getLogger;
56
Ray Milkey2b217142014-12-15 09:24:24 -080057/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070058 * Query, submit and withdraw network intents.
Ray Milkey2b217142014-12-15 09:24:24 -080059 */
Ray Milkey2b217142014-12-15 09:24:24 -080060@Path("intents")
61public class IntentsWebResource extends AbstractWebResource {
Ray Milkey303e6712015-07-17 14:06:21 -070062 @Context
63 UriInfo uriInfo;
64
Ray Milkey67c22722015-03-09 15:48:57 -070065 private static final Logger log = getLogger(IntentsWebResource.class);
66 private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5;
67
Ray Milkey2b217142014-12-15 09:24:24 -080068 public static final String INTENT_NOT_FOUND = "Intent is not found";
69
70 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070071 * Get all intents.
72 * Returns array containing all the intents in the system.
Ray Milkey2b217142014-12-15 09:24:24 -080073 *
74 * @return array of all the intents in the system
75 */
76 @GET
77 @Produces(MediaType.APPLICATION_JSON)
78 public Response getIntents() {
79 final Iterable<Intent> intents = get(IntentService.class).getIntents();
80 final ObjectNode root = encodeArray(Intent.class, "intents", intents);
Ray Milkey3f025692015-01-26 11:15:41 -080081 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -080082 }
83
84 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070085 * Get intent by application and key.
86 * Returns details of the specified intent.
Ray Milkey2b217142014-12-15 09:24:24 -080087 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070088 * @param appId application identifier
89 * @param key intent key
Ray Milkey2b217142014-12-15 09:24:24 -080090 * @return intent data
91 */
92 @GET
93 @Produces(MediaType.APPLICATION_JSON)
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080094 @Path("{appId}/{key}")
Ray Milkeyf7cb4012015-07-20 13:01:07 -070095 public Response getIntentById(@PathParam("appId") String appId,
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080096 @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 Milkeyf7cb4012015-07-20 13:01:07 -0700101 long numericalKey = Long.decode(key);
102 intent = get(IntentService.class).getIntent(Key.of(numericalKey, app));
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800103 }
104 nullIsNotFound(intent, INTENT_NOT_FOUND);
105
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800106 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 Milkey3f025692015-01-26 11:15:41 -0800114 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -0800115 }
Ray Milkey67c22722015-03-09 15:48:57 -0700116
117 class DeleteListener implements IntentListener {
118 final Key key;
119 final CountDownLatch latch;
120
121 DeleteListener(Key key, CountDownLatch latch) {
122 this.key = key;
123 this.latch = latch;
124 }
125
126 @Override
127 public void event(IntentEvent event) {
128 if (Objects.equals(event.subject().key(), key) &&
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700129 (event.type() == IntentEvent.Type.WITHDRAWN ||
130 event.type() == IntentEvent.Type.FAILED)) {
Ray Milkey67c22722015-03-09 15:48:57 -0700131 latch.countDown();
132 }
133 }
134 }
135
136 /**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700137 * Submit a new intent.
138 * Creates and submits intent from the JSON request.
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700139 *
140 * @param stream input JSON
141 * @return status of the request - CREATED if the JSON is correct,
142 * BAD_REQUEST if the JSON is invalid
143 */
144 @POST
145 @Consumes(MediaType.APPLICATION_JSON)
146 @Produces(MediaType.APPLICATION_JSON)
147 public Response createIntent(InputStream stream) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700148 try {
149 IntentService service = get(IntentService.class);
150 ObjectNode root = (ObjectNode) mapper().readTree(stream);
151 Intent intent = codec(Intent.class).decode(root, this);
152 service.submit(intent);
Ray Milkey303e6712015-07-17 14:06:21 -0700153 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
154 .path("intents")
155 .path(Short.toString(intent.appId().id()))
156 .path(Long.toString(intent.id().fingerprint()));
157 return Response
158 .created(locationBuilder.build())
159 .build();
160 } catch (IOException ioe) {
161 throw new IllegalArgumentException(ioe);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700162 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700163 }
164
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700165 /**
166 * Withdraw intent.
167 * Withdraws the specified intent from the system.
168 *
169 * @param appId application identifier
170 * @param key intent key
171 */
172 @DELETE
173 @Path("{appId}/{key}")
174 public void deleteIntentById(@PathParam("appId") String appId,
175 @PathParam("key") String key) {
176 final ApplicationId app = get(CoreService.class).getAppId(appId);
177
178 Intent intent = get(IntentService.class).getIntent(Key.of(key, app));
179 IntentService service = get(IntentService.class);
180
181 if (intent == null) {
182 intent = service
183 .getIntent(Key.of(Long.decode(key), app));
184 }
185 if (intent == null) {
186 // No such intent. REST standards recommend a positive status code
187 // in this case.
188 return;
189 }
190
191
192 Key k = intent.key();
193
194 // set up latch and listener to track uninstall progress
195 CountDownLatch latch = new CountDownLatch(1);
196
197 IntentListener listener = new DeleteListener(k, latch);
198 service.addListener(listener);
199
200 try {
201 // request the withdraw
202 service.withdraw(intent);
203
204 try {
205 latch.await(WITHDRAW_EVENT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
206 } catch (InterruptedException e) {
207 log.info("REST Delete operation timed out waiting for intent {}", k);
208 }
209 // double check the state
210 IntentState state = service.getIntentState(k);
211 if (state == WITHDRAWN || state == FAILED) {
212 service.purge(intent);
213 }
214
215 } finally {
216 // clean up the listener
217 service.removeListener(listener);
218 }
219 }
220
Ray Milkey2b217142014-12-15 09:24:24 -0800221}