blob: 4a71ffd0647e9363c338a932d5428fbbd1f286dc [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;
42
Thomas Vachuska02aeb032015-01-06 22:36:30 -080043/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070044 * Manage inventory of applications.
Thomas Vachuska02aeb032015-01-06 22:36:30 -080045 */
46@Path("applications")
47public class ApplicationsWebResource extends AbstractWebResource {
48
Jian Licbf49892016-05-10 14:54:44 -070049 private static final String APP_ID_NOT_FOUND = "Application ID is not found";
50 private static final String APP_NOT_FOUND = "Application is not found";
51
alshabib20a070b2016-06-03 14:44:05 -070052 private static final String URL = "url";
53 private static final String ACTIVATE = "activate";
54
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070055 /**
56 * Get all installed applications.
57 * Returns array of all installed applications.
58 *
59 * @return 200 OK
Jian Lie1c1c8d2016-05-09 16:24:40 -070060 * @onos.rsModel Applications
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070061 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080062 @GET
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070063 public Response getApps() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080064 ApplicationAdminService service = get(ApplicationAdminService.class);
65 Set<Application> apps = service.getApplications();
66 return ok(encodeArray(Application.class, "applications", apps)).build();
67 }
68
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070069 /**
70 * Get application details.
71 * Returns details of the specified application.
Jian Lie1c1c8d2016-05-09 16:24:40 -070072 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070073 * @param name application name
74 * @return 200 OK; 404; 401
Jian Lie1c1c8d2016-05-09 16:24:40 -070075 * @onos.rsModel Application
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070076 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080077 @GET
78 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070079 public Response getApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080080 ApplicationAdminService service = get(ApplicationAdminService.class);
Jayasree Ghoshe6213cf2016-09-28 07:55:55 +053081 ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080082 return response(service, appId);
83 }
84
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070085 /**
86 * Install a new application.
87 * Uploads application archive stream and optionally activates the
88 * application.
alshabib20a070b2016-06-03 14:44:05 -070089
90 * @param raw json object containing location (url) of application oar
91 * @return 200 OK; 404; 401
92 */
93 @POST
94 @Consumes(MediaType.APPLICATION_JSON)
95 @Produces(MediaType.APPLICATION_JSON)
96 public Response installApp(InputStream raw) {
97 Application app;
98 try {
99 ObjectNode jsonTree = (ObjectNode) mapper().readTree(raw);
100 URL url = new URL(jsonTree.get(URL).asText());
101 boolean activate = false;
102 if (jsonTree.has(ACTIVATE)) {
103 activate = jsonTree.get(ACTIVATE).asBoolean();
104 }
105
106 ApplicationAdminService service = get(ApplicationAdminService.class);
107 app = service.install(url.openStream());
108 if (activate) {
109 service.activate(app.id());
110 }
111 } catch (IOException ex) {
112 throw new IllegalArgumentException(ex);
113 }
114 return ok(codec(Application.class).encode(app, this)).build();
115 }
116
117 /**
118 * Install a new application.
119 * Uploads application archive stream and optionally activates the
120 * application.
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700121 *
122 * @param activate true to activate app also
123 * @param stream application archive stream
124 * @return 200 OK; 404; 401
125 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800126 @POST
127 @Consumes(MediaType.APPLICATION_OCTET_STREAM)
128 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700129 public Response installApp(@QueryParam("activate")
130 @DefaultValue("false") boolean activate,
131 InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800132 ApplicationAdminService service = get(ApplicationAdminService.class);
133 Application app = service.install(stream);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800134 if (activate) {
135 service.activate(app.id());
136 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800137 return ok(codec(Application.class).encode(app, this)).build();
138 }
139
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700140 /**
141 * Uninstall application.
142 * Uninstalls the specified application deactivating it first if necessary.
143 *
144 * @param name application name
Jian Lic2a542b2016-05-10 11:48:19 -0700145 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700146 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800147 @DELETE
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800148 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700149 public Response uninstallApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800150 ApplicationAdminService service = get(ApplicationAdminService.class);
151 ApplicationId appId = service.getId(name);
dvaddiree113b652017-08-04 14:58:06 +0530152 if (appId != null) {
153 service.uninstall(appId);
154 }
Jian Lic2a542b2016-05-10 11:48:19 -0700155 return Response.noContent().build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800156 }
157
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700158 /**
159 * Activate application.
160 * Activates the specified application.
161 *
162 * @param name application name
163 * @return 200 OK; 404; 401
164 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800165 @POST
166 @Produces(MediaType.APPLICATION_JSON)
167 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700168 public Response activateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800169 ApplicationAdminService service = get(ApplicationAdminService.class);
Hyunsun Moon14998642016-06-10 12:54:26 -0700170 ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800171 service.activate(appId);
172 return response(service, appId);
173 }
174
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700175 /**
176 * De-activate application.
177 * De-activates the specified application.
178 *
179 * @param name application name
dvaddiree113b652017-08-04 14:58:06 +0530180 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700181 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800182 @DELETE
183 @Produces(MediaType.APPLICATION_JSON)
184 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700185 public Response deactivateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800186 ApplicationAdminService service = get(ApplicationAdminService.class);
187 ApplicationId appId = service.getId(name);
dvaddiree113b652017-08-04 14:58:06 +0530188 if (appId != null) {
189 service.deactivate(appId);
190 }
191 return Response.noContent().build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800192 }
193
Jian Lie1c1c8d2016-05-09 16:24:40 -0700194 /**
195 * Registers an on or off platform application.
196 *
197 * @param name application name
198 * @return 200 OK; 404; 401
199 * @onos.rsModel ApplicationId
200 */
201 @POST
202 @Produces(MediaType.APPLICATION_JSON)
203 @Path("{name}/register")
204 public Response registerAppId(@PathParam("name") String name) {
205 CoreService service = get(CoreService.class);
206 ApplicationId appId = service.registerApplication(name);
207 return response(appId);
208 }
209
210 /**
Thomas Vachuska08b4dec2017-08-31 15:20:17 -0700211 * Get application OAR/JAR file.
212 * Returns the OAR/JAR file used to install the specified application.
213 *
214 * @param name application name
215 * @return 200 OK; 404; 401
216 */
217 @GET
218 @Produces(MediaType.APPLICATION_OCTET_STREAM)
219 @Path("{name}/bits")
220 public Response getAppBits(@PathParam("name") String name) {
221 ApplicationAdminService service = get(ApplicationAdminService.class);
222 ApplicationId appId = nullIsNotFound(service.getId(name), APP_ID_NOT_FOUND);
223 InputStream bits = service.getApplicationArchive(appId);
224 return ok(bits).build();
225 }
226
227 /**
Jian Li847242b2016-05-11 18:58:53 -0700228 * Gets applicationId entry by either id or name.
Jian Lie1c1c8d2016-05-09 16:24:40 -0700229 *
Jian Li847242b2016-05-11 18:58:53 -0700230 * @param id id of application
Jian Lie1c1c8d2016-05-09 16:24:40 -0700231 * @param name name of application
232 * @return 200 OK; 404; 401
233 * @onos.rsModel ApplicationId
234 */
235 @GET
236 @Produces(MediaType.APPLICATION_JSON)
Jian Li847242b2016-05-11 18:58:53 -0700237 @Path("ids/entry")
Jian Lia3e4c7a2016-05-12 13:15:40 -0700238 public Response getAppIdByName(@QueryParam("id") String id,
Jian Li847242b2016-05-11 18:58:53 -0700239 @QueryParam("name") String name) {
Jian Lie1c1c8d2016-05-09 16:24:40 -0700240 CoreService service = get(CoreService.class);
Jian Li847242b2016-05-11 18:58:53 -0700241 ApplicationId appId = null;
242 if (id != null) {
Jian Lia3e4c7a2016-05-12 13:15:40 -0700243 appId = service.getAppId(Short.valueOf(id));
Jian Li847242b2016-05-11 18:58:53 -0700244 } else if (name != null) {
245 appId = service.getAppId(name);
246 }
Jian Lie1c1c8d2016-05-09 16:24:40 -0700247 return response(appId);
248 }
249
250 /**
251 * Gets a collection of application ids.
252 * Returns array of all registered application ids.
253 *
254 * @return 200 OK; 404; 401
255 * @onos.rsModel ApplicationIds
256 */
257 @GET
258 @Produces(MediaType.APPLICATION_JSON)
259 @Path("ids")
260 public Response getAppIds() {
261 CoreService service = get(CoreService.class);
262 Set<ApplicationId> appIds = service.getAppIds();
263 return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build();
264 }
265
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800266 private Response response(ApplicationAdminService service, ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700267 Application app = nullIsNotFound(service.getApplication(appId), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800268 return ok(codec(Application.class).encode(app, this)).build();
269 }
270
Jian Lie1c1c8d2016-05-09 16:24:40 -0700271 private Response response(ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700272 ApplicationId checkedAppId = nullIsNotFound(appId, APP_ID_NOT_FOUND);
273 return ok(codec(ApplicationId.class).encode(checkedAppId, this)).build();
Jian Lie1c1c8d2016-05-09 16:24:40 -0700274 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800275}