blob: 09fdd2fffba1f18282cae6814f9881d22f90a23f [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/**
58 * REST resource for interacting with the inventory of intents.
59 */
60
61@Path("intents")
62public class IntentsWebResource extends AbstractWebResource {
Ray Milkey303e6712015-07-17 14:06:21 -070063 @Context
64 UriInfo uriInfo;
65
Ray Milkey67c22722015-03-09 15:48:57 -070066 private static final Logger log = getLogger(IntentsWebResource.class);
67 private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5;
68
Ray Milkey2b217142014-12-15 09:24:24 -080069 public static final String INTENT_NOT_FOUND = "Intent is not found";
70
71 /**
72 * Gets an array containing all the intents in the system.
73 *
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 /**
85 * Gets a single intent by Id.
86 *
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080087 * @param appId the Application ID
88 * @param key the Intent key value to look up
Ray Milkey2b217142014-12-15 09:24:24 -080089 * @return intent data
90 */
91 @GET
92 @Produces(MediaType.APPLICATION_JSON)
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080093 @Path("{appId}/{key}")
Ray Milkeyf7cb4012015-07-20 13:01:07 -070094 public Response getIntentById(@PathParam("appId") String appId,
Ayaka Koshibec06c89b2015-02-10 19:25:41 -080095 @PathParam("key") String key) {
96 final ApplicationId app = get(CoreService.class).getAppId(appId);
97
98 Intent intent = get(IntentService.class).getIntent(Key.of(key, app));
99 if (intent == null) {
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700100 long numericalKey = Long.decode(key);
101 intent = get(IntentService.class).getIntent(Key.of(numericalKey, app));
Ayaka Koshibec06c89b2015-02-10 19:25:41 -0800102 }
103 nullIsNotFound(intent, INTENT_NOT_FOUND);
104
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800105 final ObjectNode root;
106 if (intent instanceof HostToHostIntent) {
107 root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this);
108 } else if (intent instanceof PointToPointIntent) {
109 root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this);
110 } else {
111 root = codec(Intent.class).encode(intent, this);
112 }
Ray Milkey3f025692015-01-26 11:15:41 -0800113 return ok(root).build();
Ray Milkey2b217142014-12-15 09:24:24 -0800114 }
Ray Milkey67c22722015-03-09 15:48:57 -0700115
116 class DeleteListener implements IntentListener {
117 final Key key;
118 final CountDownLatch latch;
119
120 DeleteListener(Key key, CountDownLatch latch) {
121 this.key = key;
122 this.latch = latch;
123 }
124
125 @Override
126 public void event(IntentEvent event) {
127 if (Objects.equals(event.subject().key(), key) &&
128 (event.type() == IntentEvent.Type.WITHDRAWN ||
129 event.type() == IntentEvent.Type.FAILED)) {
130 latch.countDown();
131 }
132 }
133 }
134
135 /**
136 * Uninstalls a single intent by Id.
137 *
138 * @param appId the Application ID
139 * @param keyString the Intent key value to look up
140 */
141 @DELETE
142 @Path("{appId}/{key}")
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700143 public void deleteIntentById(@PathParam("appId") String appId,
Ray Milkey67c22722015-03-09 15:48:57 -0700144 @PathParam("key") String keyString) {
145 final ApplicationId app = get(CoreService.class).getAppId(appId);
146
147 Intent intent = get(IntentService.class).getIntent(Key.of(keyString, app));
148 IntentService service = get(IntentService.class);
149
150 if (intent == null) {
151 intent = service
Ray Milkey7b158512015-07-21 16:32:43 -0700152 .getIntent(Key.of(Long.decode(keyString), app));
Ray Milkey67c22722015-03-09 15:48:57 -0700153 }
154 if (intent == null) {
155 // No such intent. REST standards recommend a positive status code
156 // in this case.
157 return;
158 }
159
160
161 Key key = intent.key();
162
163 // set up latch and listener to track uninstall progress
164 CountDownLatch latch = new CountDownLatch(1);
165
166 IntentListener listener = new DeleteListener(key, latch);
167 service.addListener(listener);
168
169 try {
170 // request the withdraw
171 service.withdraw(intent);
172
173 try {
174 latch.await(WITHDRAW_EVENT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
175 } catch (InterruptedException e) {
176 log.info("REST Delete operation timed out waiting for intent {}", key);
177 }
178 // double check the state
179 IntentState state = service.getIntentState(key);
180 if (state == WITHDRAWN || state == FAILED) {
181 service.purge(intent);
182 }
183
184 } finally {
185 // clean up the listener
186 service.removeListener(listener);
187 }
188 }
189
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700190 /**
191 * Creates an intent from a POST of a JSON string and attempts to apply it.
192 *
193 * @param stream input JSON
194 * @return status of the request - CREATED if the JSON is correct,
195 * BAD_REQUEST if the JSON is invalid
196 */
197 @POST
198 @Consumes(MediaType.APPLICATION_JSON)
199 @Produces(MediaType.APPLICATION_JSON)
200 public Response createIntent(InputStream stream) {
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700201 try {
202 IntentService service = get(IntentService.class);
203 ObjectNode root = (ObjectNode) mapper().readTree(stream);
204 Intent intent = codec(Intent.class).decode(root, this);
205 service.submit(intent);
Ray Milkey303e6712015-07-17 14:06:21 -0700206 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
207 .path("intents")
208 .path(Short.toString(intent.appId().id()))
209 .path(Long.toString(intent.id().fingerprint()));
210 return Response
211 .created(locationBuilder.build())
212 .build();
213 } catch (IOException ioe) {
214 throw new IllegalArgumentException(ioe);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700215 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700216 }
217
Ray Milkey2b217142014-12-15 09:24:24 -0800218}