blob: c73c61258da0be4c97796b04a265149d2eb31529 [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;
Ray Milkey16570322018-04-17 10:26:54 -070020import org.onosproject.app.ApplicationException;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080021import org.onosproject.core.Application;
22import org.onosproject.core.ApplicationId;
Jian Lie1c1c8d2016-05-09 16:24:40 -070023import org.onosproject.core.CoreService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070024import org.onosproject.rest.AbstractWebResource;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080025
26import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080028import javax.ws.rs.DefaultValue;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080029import javax.ws.rs.GET;
30import javax.ws.rs.POST;
31import javax.ws.rs.Path;
32import javax.ws.rs.PathParam;
33import javax.ws.rs.Produces;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080034import javax.ws.rs.QueryParam;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080035import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
alshabib20a070b2016-06-03 14:44:05 -070037import java.io.IOException;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080038import java.io.InputStream;
alshabib20a070b2016-06-03 14:44:05 -070039import java.net.URL;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080040import java.util.Set;
41
Jian Licbf49892016-05-10 14:54:44 -070042import static org.onlab.util.Tools.nullIsNotFound;
Ray Milkey86ee5e82018-04-02 15:33:07 -070043import static org.onlab.util.Tools.readTreeFromStream;
Jian Licbf49892016-05-10 14:54:44 -070044
Thomas Vachuska02aeb032015-01-06 22:36:30 -080045/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070046 * Manage inventory of applications.
Thomas Vachuska02aeb032015-01-06 22:36:30 -080047 */
48@Path("applications")
49public class ApplicationsWebResource extends AbstractWebResource {
50
Jian Licbf49892016-05-10 14:54:44 -070051 private static final String APP_ID_NOT_FOUND = "Application ID is not found";
52 private static final String APP_NOT_FOUND = "Application is not found";
53
alshabib20a070b2016-06-03 14:44:05 -070054 private static final String URL = "url";
55 private static final String ACTIVATE = "activate";
56
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070057 /**
58 * Get all installed applications.
59 * Returns array of all installed applications.
60 *
61 * @return 200 OK
Jian Lie1c1c8d2016-05-09 16:24:40 -070062 * @onos.rsModel Applications
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070063 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080064 @GET
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070065 public Response getApps() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080066 ApplicationAdminService service = get(ApplicationAdminService.class);
67 Set<Application> apps = service.getApplications();
68 return ok(encodeArray(Application.class, "applications", apps)).build();
69 }
70
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070071 /**
72 * Get application details.
73 * Returns details of the specified application.
Jian Lie1c1c8d2016-05-09 16:24:40 -070074 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070075 * @param name application name
76 * @return 200 OK; 404; 401
Jian Lie1c1c8d2016-05-09 16:24:40 -070077 * @onos.rsModel Application
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070078 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080079 @GET
80 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070081 public Response getApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080082 ApplicationAdminService service = get(ApplicationAdminService.class);
Jayasree Ghoshe6213cf2016-09-28 07:55:55 +053083 ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080084 return response(service, appId);
85 }
86
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070087 /**
88 * Install a new application.
89 * Uploads application archive stream and optionally activates the
90 * application.
alshabib20a070b2016-06-03 14:44:05 -070091
92 * @param raw json object containing location (url) of application oar
93 * @return 200 OK; 404; 401
94 */
95 @POST
96 @Consumes(MediaType.APPLICATION_JSON)
97 @Produces(MediaType.APPLICATION_JSON)
98 public Response installApp(InputStream raw) {
99 Application app;
100 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700101 ObjectNode jsonTree = readTreeFromStream(mapper(), raw);
alshabib20a070b2016-06-03 14:44:05 -0700102 URL url = new URL(jsonTree.get(URL).asText());
103 boolean activate = false;
104 if (jsonTree.has(ACTIVATE)) {
105 activate = jsonTree.get(ACTIVATE).asBoolean();
106 }
107
108 ApplicationAdminService service = get(ApplicationAdminService.class);
109 app = service.install(url.openStream());
110 if (activate) {
111 service.activate(app.id());
112 }
113 } catch (IOException ex) {
114 throw new IllegalArgumentException(ex);
115 }
116 return ok(codec(Application.class).encode(app, this)).build();
117 }
118
119 /**
120 * Install a new application.
121 * Uploads application archive stream and optionally activates the
122 * application.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700123 *
124 * @param activate true to activate app also
125 * @param stream application archive stream
126 * @return 200 OK; 404; 401
127 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800128 @POST
129 @Consumes(MediaType.APPLICATION_OCTET_STREAM)
130 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700131 public Response installApp(@QueryParam("activate")
132 @DefaultValue("false") boolean activate,
133 InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800134 ApplicationAdminService service = get(ApplicationAdminService.class);
Ray Milkey16570322018-04-17 10:26:54 -0700135 try {
136 Application app = service.install(stream);
137 if (activate) {
138 service.activate(app.id());
139 }
140 return ok(codec(Application.class).encode(app, this)).build();
141 } catch (ApplicationException appEx) {
142 throw new IllegalArgumentException(appEx);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800143 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800144 }
145
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700146 /**
147 * Uninstall application.
148 * Uninstalls the specified application deactivating it first if necessary.
149 *
150 * @param name application name
Jian Lic2a542b2016-05-10 11:48:19 -0700151 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700152 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800153 @DELETE
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800154 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700155 public Response uninstallApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800156 ApplicationAdminService service = get(ApplicationAdminService.class);
157 ApplicationId appId = service.getId(name);
dvaddiree113b652017-08-04 14:58:06 +0530158 if (appId != null) {
159 service.uninstall(appId);
160 }
Jian Lic2a542b2016-05-10 11:48:19 -0700161 return Response.noContent().build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800162 }
163
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700164 /**
165 * Activate application.
166 * Activates the specified application.
167 *
168 * @param name application name
169 * @return 200 OK; 404; 401
170 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800171 @POST
172 @Produces(MediaType.APPLICATION_JSON)
173 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700174 public Response activateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800175 ApplicationAdminService service = get(ApplicationAdminService.class);
Hyunsun Moon14998642016-06-10 12:54:26 -0700176 ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800177 service.activate(appId);
178 return response(service, appId);
179 }
180
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700181 /**
182 * De-activate application.
183 * De-activates the specified application.
184 *
185 * @param name application name
dvaddiree113b652017-08-04 14:58:06 +0530186 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700187 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800188 @DELETE
189 @Produces(MediaType.APPLICATION_JSON)
190 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700191 public Response deactivateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800192 ApplicationAdminService service = get(ApplicationAdminService.class);
193 ApplicationId appId = service.getId(name);
dvaddiree113b652017-08-04 14:58:06 +0530194 if (appId != null) {
195 service.deactivate(appId);
196 }
197 return Response.noContent().build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800198 }
199
Jian Lie1c1c8d2016-05-09 16:24:40 -0700200 /**
201 * Registers an on or off platform application.
202 *
203 * @param name application name
204 * @return 200 OK; 404; 401
205 * @onos.rsModel ApplicationId
206 */
207 @POST
208 @Produces(MediaType.APPLICATION_JSON)
209 @Path("{name}/register")
210 public Response registerAppId(@PathParam("name") String name) {
211 CoreService service = get(CoreService.class);
212 ApplicationId appId = service.registerApplication(name);
213 return response(appId);
214 }
215
216 /**
Thomas Vachuska08b4dec2017-08-31 15:20:17 -0700217 * Get application OAR/JAR file.
218 * Returns the OAR/JAR file used to install the specified application.
219 *
220 * @param name application name
221 * @return 200 OK; 404; 401
222 */
223 @GET
224 @Produces(MediaType.APPLICATION_OCTET_STREAM)
225 @Path("{name}/bits")
226 public Response getAppBits(@PathParam("name") String name) {
227 ApplicationAdminService service = get(ApplicationAdminService.class);
228 ApplicationId appId = nullIsNotFound(service.getId(name), APP_ID_NOT_FOUND);
229 InputStream bits = service.getApplicationArchive(appId);
230 return ok(bits).build();
231 }
232
233 /**
Jian Li847242b2016-05-11 18:58:53 -0700234 * Gets applicationId entry by either id or name.
Jian Lie1c1c8d2016-05-09 16:24:40 -0700235 *
Jian Li847242b2016-05-11 18:58:53 -0700236 * @param id id of application
Jian Lie1c1c8d2016-05-09 16:24:40 -0700237 * @param name name of application
238 * @return 200 OK; 404; 401
239 * @onos.rsModel ApplicationId
240 */
241 @GET
242 @Produces(MediaType.APPLICATION_JSON)
Jian Li847242b2016-05-11 18:58:53 -0700243 @Path("ids/entry")
Jian Lia3e4c7a2016-05-12 13:15:40 -0700244 public Response getAppIdByName(@QueryParam("id") String id,
Jian Li847242b2016-05-11 18:58:53 -0700245 @QueryParam("name") String name) {
Jian Lie1c1c8d2016-05-09 16:24:40 -0700246 CoreService service = get(CoreService.class);
Jian Li847242b2016-05-11 18:58:53 -0700247 ApplicationId appId = null;
248 if (id != null) {
Jian Lia3e4c7a2016-05-12 13:15:40 -0700249 appId = service.getAppId(Short.valueOf(id));
Jian Li847242b2016-05-11 18:58:53 -0700250 } else if (name != null) {
251 appId = service.getAppId(name);
252 }
Jian Lie1c1c8d2016-05-09 16:24:40 -0700253 return response(appId);
254 }
255
256 /**
257 * Gets a collection of application ids.
258 * Returns array of all registered application ids.
259 *
260 * @return 200 OK; 404; 401
261 * @onos.rsModel ApplicationIds
262 */
263 @GET
264 @Produces(MediaType.APPLICATION_JSON)
265 @Path("ids")
266 public Response getAppIds() {
267 CoreService service = get(CoreService.class);
268 Set<ApplicationId> appIds = service.getAppIds();
269 return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build();
270 }
271
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800272 private Response response(ApplicationAdminService service, ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700273 Application app = nullIsNotFound(service.getApplication(appId), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800274 return ok(codec(Application.class).encode(app, this)).build();
275 }
276
Jian Lie1c1c8d2016-05-09 16:24:40 -0700277 private Response response(ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700278 ApplicationId checkedAppId = nullIsNotFound(appId, APP_ID_NOT_FOUND);
279 return ok(codec(ApplicationId.class).encode(checkedAppId, this)).build();
Jian Lie1c1c8d2016-05-09 16:24:40 -0700280 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800281}