blob: 2f46933d19f515aad4928d0846e734170a6efc9e [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;
Deepa Vaddireddyce8a6f72017-09-14 08:57:50 +000020import 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;
43
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 {
100 ObjectNode jsonTree = (ObjectNode) mapper().readTree(raw);
101 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 }
Deepa Vaddireddyce8a6f72017-09-14 08:57:50 +0000112 } catch (IOException | ApplicationException ex) {
alshabib20a070b2016-06-03 14:44:05 -0700113 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);
Deepa Vaddireddyce8a6f72017-09-14 08:57:50 +0000134 Application app;
135 try {
136 app = service.install(stream);
137 if (activate) {
138 service.activate(app.id());
139 }
140 } catch (ApplicationException ex) {
141 throw new IllegalArgumentException(ex);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800142 }
Deepa Vaddireddyce8a6f72017-09-14 08:57:50 +0000143
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800144 return ok(codec(Application.class).encode(app, this)).build();
145 }
146
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700147 /**
148 * Uninstall application.
149 * Uninstalls the specified application deactivating it first if necessary.
150 *
151 * @param name application name
Jian Lic2a542b2016-05-10 11:48:19 -0700152 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700153 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800154 @DELETE
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800155 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700156 public Response uninstallApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800157 ApplicationAdminService service = get(ApplicationAdminService.class);
158 ApplicationId appId = service.getId(name);
dvaddiree113b652017-08-04 14:58:06 +0530159 if (appId != null) {
160 service.uninstall(appId);
161 }
Jian Lic2a542b2016-05-10 11:48:19 -0700162 return Response.noContent().build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800163 }
164
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700165 /**
166 * Activate application.
167 * Activates the specified application.
168 *
169 * @param name application name
170 * @return 200 OK; 404; 401
171 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800172 @POST
173 @Produces(MediaType.APPLICATION_JSON)
174 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700175 public Response activateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800176 ApplicationAdminService service = get(ApplicationAdminService.class);
Hyunsun Moon14998642016-06-10 12:54:26 -0700177 ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800178 service.activate(appId);
179 return response(service, appId);
180 }
181
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700182 /**
183 * De-activate application.
184 * De-activates the specified application.
185 *
186 * @param name application name
dvaddiree113b652017-08-04 14:58:06 +0530187 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700188 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800189 @DELETE
190 @Produces(MediaType.APPLICATION_JSON)
191 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700192 public Response deactivateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800193 ApplicationAdminService service = get(ApplicationAdminService.class);
194 ApplicationId appId = service.getId(name);
dvaddiree113b652017-08-04 14:58:06 +0530195 if (appId != null) {
196 service.deactivate(appId);
197 }
198 return Response.noContent().build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800199 }
200
Jian Lie1c1c8d2016-05-09 16:24:40 -0700201 /**
202 * Registers an on or off platform application.
203 *
204 * @param name application name
205 * @return 200 OK; 404; 401
206 * @onos.rsModel ApplicationId
207 */
208 @POST
209 @Produces(MediaType.APPLICATION_JSON)
210 @Path("{name}/register")
211 public Response registerAppId(@PathParam("name") String name) {
212 CoreService service = get(CoreService.class);
213 ApplicationId appId = service.registerApplication(name);
214 return response(appId);
215 }
216
217 /**
Jian Li847242b2016-05-11 18:58:53 -0700218 * Gets applicationId entry by either id or name.
Jian Lie1c1c8d2016-05-09 16:24:40 -0700219 *
Jian Li847242b2016-05-11 18:58:53 -0700220 * @param id id of application
Jian Lie1c1c8d2016-05-09 16:24:40 -0700221 * @param name name of application
222 * @return 200 OK; 404; 401
223 * @onos.rsModel ApplicationId
224 */
225 @GET
226 @Produces(MediaType.APPLICATION_JSON)
Jian Li847242b2016-05-11 18:58:53 -0700227 @Path("ids/entry")
Jian Lia3e4c7a2016-05-12 13:15:40 -0700228 public Response getAppIdByName(@QueryParam("id") String id,
Jian Li847242b2016-05-11 18:58:53 -0700229 @QueryParam("name") String name) {
Jian Lie1c1c8d2016-05-09 16:24:40 -0700230 CoreService service = get(CoreService.class);
Jian Li847242b2016-05-11 18:58:53 -0700231 ApplicationId appId = null;
232 if (id != null) {
Jian Lia3e4c7a2016-05-12 13:15:40 -0700233 appId = service.getAppId(Short.valueOf(id));
Jian Li847242b2016-05-11 18:58:53 -0700234 } else if (name != null) {
235 appId = service.getAppId(name);
236 }
Jian Lie1c1c8d2016-05-09 16:24:40 -0700237 return response(appId);
238 }
239
240 /**
241 * Gets a collection of application ids.
242 * Returns array of all registered application ids.
243 *
244 * @return 200 OK; 404; 401
245 * @onos.rsModel ApplicationIds
246 */
247 @GET
248 @Produces(MediaType.APPLICATION_JSON)
249 @Path("ids")
250 public Response getAppIds() {
251 CoreService service = get(CoreService.class);
252 Set<ApplicationId> appIds = service.getAppIds();
253 return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build();
254 }
255
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800256 private Response response(ApplicationAdminService service, ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700257 Application app = nullIsNotFound(service.getApplication(appId), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800258 return ok(codec(Application.class).encode(app, this)).build();
259 }
260
Jian Lie1c1c8d2016-05-09 16:24:40 -0700261 private Response response(ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700262 ApplicationId checkedAppId = nullIsNotFound(appId, APP_ID_NOT_FOUND);
263 return ok(codec(ApplicationId.class).encode(checkedAppId, this)).build();
Jian Lie1c1c8d2016-05-09 16:24:40 -0700264 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800265}