blob: 24ad50afa5d10f4fde25cf76216ee5fc00dde293 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska02aeb032015-01-06 22:36:30 -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;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080017
alshabib20a070b2016-06-03 14:44:05 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080019import org.onosproject.app.ApplicationAdminService;
20import org.onosproject.core.Application;
21import org.onosproject.core.ApplicationId;
Jian Lie1c1c8d2016-05-09 16:24:40 -070022import org.onosproject.core.CoreService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070023import org.onosproject.rest.AbstractWebResource;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080024
25import javax.ws.rs.Consumes;
26import javax.ws.rs.DELETE;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080027import javax.ws.rs.DefaultValue;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080028import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080033import javax.ws.rs.QueryParam;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080034import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
alshabib20a070b2016-06-03 14:44:05 -070036import java.io.IOException;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080037import java.io.InputStream;
alshabib20a070b2016-06-03 14:44:05 -070038import java.net.URL;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080039import java.util.Set;
40
Jian Licbf49892016-05-10 14:54:44 -070041import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey86ee5e82018-04-02 15:33:07 -070042import static org.onlab.util.Tools.readTreeFromStream;
Jian Licbf49892016-05-10 14:54:44 -070043
Thomas Vachuska02aeb032015-01-06 22:36:30 -080044/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070045 * Manage inventory of applications.
Thomas Vachuska02aeb032015-01-06 22:36:30 -080046 */
47@Path("applications")
48public class ApplicationsWebResource extends AbstractWebResource {
49
Jian Licbf49892016-05-10 14:54:44 -070050 private static final String APP_ID_NOT_FOUND = "Application ID is not found";
51 private static final String APP_NOT_FOUND = "Application is not found";
52
alshabib20a070b2016-06-03 14:44:05 -070053 private static final String URL = "url";
54 private static final String ACTIVATE = "activate";
55
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070056 /**
57 * Get all installed applications.
58 * Returns array of all installed applications.
59 *
60 * @return 200 OK
Jian Lie1c1c8d2016-05-09 16:24:40 -070061 * @onos.rsModel Applications
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070062 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080063 @GET
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070064 public Response getApps() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080065 ApplicationAdminService service = get(ApplicationAdminService.class);
66 Set<Application> apps = service.getApplications();
67 return ok(encodeArray(Application.class, "applications", apps)).build();
68 }
69
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070070 /**
71 * Get application details.
72 * Returns details of the specified application.
Jian Lie1c1c8d2016-05-09 16:24:40 -070073 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070074 * @param name application name
75 * @return 200 OK; 404; 401
Jian Lie1c1c8d2016-05-09 16:24:40 -070076 * @onos.rsModel Application
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070077 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080078 @GET
79 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070080 public Response getApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080081 ApplicationAdminService service = get(ApplicationAdminService.class);
Jayasree Ghoshe6213cf2016-09-28 07:55:55 +053082 ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080083 return response(service, appId);
84 }
85
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070086 /**
87 * Install a new application.
88 * Uploads application archive stream and optionally activates the
89 * application.
alshabib20a070b2016-06-03 14:44:05 -070090
91 * @param raw json object containing location (url) of application oar
92 * @return 200 OK; 404; 401
93 */
94 @POST
95 @Consumes(MediaType.APPLICATION_JSON)
96 @Produces(MediaType.APPLICATION_JSON)
97 public Response installApp(InputStream raw) {
98 Application app;
99 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700100 ObjectNode jsonTree = readTreeFromStream(mapper(), raw);
alshabib20a070b2016-06-03 14:44:05 -0700101 URL url = new URL(jsonTree.get(URL).asText());
102 boolean activate = false;
103 if (jsonTree.has(ACTIVATE)) {
104 activate = jsonTree.get(ACTIVATE).asBoolean();
105 }
106
107 ApplicationAdminService service = get(ApplicationAdminService.class);
108 app = service.install(url.openStream());
109 if (activate) {
110 service.activate(app.id());
111 }
112 } catch (IOException ex) {
113 throw new IllegalArgumentException(ex);
114 }
115 return ok(codec(Application.class).encode(app, this)).build();
116 }
117
118 /**
119 * Install a new application.
120 * Uploads application archive stream and optionally activates the
121 * application.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700122 *
123 * @param activate true to activate app also
124 * @param stream application archive stream
125 * @return 200 OK; 404; 401
126 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800127 @POST
128 @Consumes(MediaType.APPLICATION_OCTET_STREAM)
129 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700130 public Response installApp(@QueryParam("activate")
131 @DefaultValue("false") boolean activate,
132 InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800133 ApplicationAdminService service = get(ApplicationAdminService.class);
134 Application app = service.install(stream);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800135 if (activate) {
136 service.activate(app.id());
137 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800138 return ok(codec(Application.class).encode(app, this)).build();
139 }
140
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700141 /**
142 * Uninstall application.
143 * Uninstalls the specified application deactivating it first if necessary.
144 *
145 * @param name application name
Jian Lic2a542b2016-05-10 11:48:19 -0700146 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700147 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800148 @DELETE
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800149 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700150 public Response uninstallApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800151 ApplicationAdminService service = get(ApplicationAdminService.class);
152 ApplicationId appId = service.getId(name);
dvaddiree113b652017-08-04 14:58:06 +0530153 if (appId != null) {
154 service.uninstall(appId);
155 }
Jian Lic2a542b2016-05-10 11:48:19 -0700156 return Response.noContent().build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800157 }
158
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700159 /**
160 * Activate application.
161 * Activates the specified application.
162 *
163 * @param name application name
164 * @return 200 OK; 404; 401
165 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800166 @POST
167 @Produces(MediaType.APPLICATION_JSON)
168 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700169 public Response activateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800170 ApplicationAdminService service = get(ApplicationAdminService.class);
Hyunsun Moon14998642016-06-10 12:54:26 -0700171 ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800172 service.activate(appId);
173 return response(service, appId);
174 }
175
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700176 /**
177 * De-activate application.
178 * De-activates the specified application.
179 *
180 * @param name application name
dvaddiree113b652017-08-04 14:58:06 +0530181 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700182 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800183 @DELETE
184 @Produces(MediaType.APPLICATION_JSON)
185 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700186 public Response deactivateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800187 ApplicationAdminService service = get(ApplicationAdminService.class);
188 ApplicationId appId = service.getId(name);
dvaddiree113b652017-08-04 14:58:06 +0530189 if (appId != null) {
190 service.deactivate(appId);
191 }
192 return Response.noContent().build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800193 }
194
Jian Lie1c1c8d2016-05-09 16:24:40 -0700195 /**
196 * Registers an on or off platform application.
197 *
198 * @param name application name
199 * @return 200 OK; 404; 401
200 * @onos.rsModel ApplicationId
201 */
202 @POST
203 @Produces(MediaType.APPLICATION_JSON)
204 @Path("{name}/register")
205 public Response registerAppId(@PathParam("name") String name) {
206 CoreService service = get(CoreService.class);
207 ApplicationId appId = service.registerApplication(name);
208 return response(appId);
209 }
210
211 /**
Thomas Vachuska08b4dec2017-08-31 15:20:17 -0700212 * Get application OAR/JAR file.
213 * Returns the OAR/JAR file used to install the specified application.
214 *
215 * @param name application name
216 * @return 200 OK; 404; 401
217 */
218 @GET
219 @Produces(MediaType.APPLICATION_OCTET_STREAM)
220 @Path("{name}/bits")
221 public Response getAppBits(@PathParam("name") String name) {
222 ApplicationAdminService service = get(ApplicationAdminService.class);
223 ApplicationId appId = nullIsNotFound(service.getId(name), APP_ID_NOT_FOUND);
224 InputStream bits = service.getApplicationArchive(appId);
225 return ok(bits).build();
226 }
227
228 /**
Jian Li847242b2016-05-11 18:58:53 -0700229 * Gets applicationId entry by either id or name.
Jian Lie1c1c8d2016-05-09 16:24:40 -0700230 *
Jian Li847242b2016-05-11 18:58:53 -0700231 * @param id id of application
Jian Lie1c1c8d2016-05-09 16:24:40 -0700232 * @param name name of application
233 * @return 200 OK; 404; 401
234 * @onos.rsModel ApplicationId
235 */
236 @GET
237 @Produces(MediaType.APPLICATION_JSON)
Jian Li847242b2016-05-11 18:58:53 -0700238 @Path("ids/entry")
Jian Lia3e4c7a2016-05-12 13:15:40 -0700239 public Response getAppIdByName(@QueryParam("id") String id,
Jian Li847242b2016-05-11 18:58:53 -0700240 @QueryParam("name") String name) {
Jian Lie1c1c8d2016-05-09 16:24:40 -0700241 CoreService service = get(CoreService.class);
Jian Li847242b2016-05-11 18:58:53 -0700242 ApplicationId appId = null;
243 if (id != null) {
Jian Lia3e4c7a2016-05-12 13:15:40 -0700244 appId = service.getAppId(Short.valueOf(id));
Jian Li847242b2016-05-11 18:58:53 -0700245 } else if (name != null) {
246 appId = service.getAppId(name);
247 }
Jian Lie1c1c8d2016-05-09 16:24:40 -0700248 return response(appId);
249 }
250
251 /**
252 * Gets a collection of application ids.
253 * Returns array of all registered application ids.
254 *
255 * @return 200 OK; 404; 401
256 * @onos.rsModel ApplicationIds
257 */
258 @GET
259 @Produces(MediaType.APPLICATION_JSON)
260 @Path("ids")
261 public Response getAppIds() {
262 CoreService service = get(CoreService.class);
263 Set<ApplicationId> appIds = service.getAppIds();
264 return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build();
265 }
266
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800267 private Response response(ApplicationAdminService service, ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700268 Application app = nullIsNotFound(service.getApplication(appId), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800269 return ok(codec(Application.class).encode(app, this)).build();
270 }
271
Jian Lie1c1c8d2016-05-09 16:24:40 -0700272 private Response response(ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700273 ApplicationId checkedAppId = nullIsNotFound(appId, APP_ID_NOT_FOUND);
274 return ok(codec(ApplicationId.class).encode(checkedAppId, this)).build();
Jian Lie1c1c8d2016-05-09 16:24:40 -0700275 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800276}