blob: 6ee5c83ed40da9eab0dbba00e6cdb0c83fbf766b [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
18import org.onosproject.app.ApplicationAdminService;
19import org.onosproject.core.Application;
20import org.onosproject.core.ApplicationId;
Jian Lie1c1c8d2016-05-09 16:24:40 -070021import org.onosproject.core.CoreService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070022import org.onosproject.rest.AbstractWebResource;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080023
24import javax.ws.rs.Consumes;
25import javax.ws.rs.DELETE;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080026import javax.ws.rs.DefaultValue;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080027import javax.ws.rs.GET;
28import javax.ws.rs.POST;
29import javax.ws.rs.Path;
30import javax.ws.rs.PathParam;
31import javax.ws.rs.Produces;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080032import javax.ws.rs.QueryParam;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080033import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import java.io.InputStream;
36import java.util.Set;
37
Jian Licbf49892016-05-10 14:54:44 -070038import static org.onlab.util.Tools.nullIsNotFound;
39
Thomas Vachuska02aeb032015-01-06 22:36:30 -080040/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070041 * Manage inventory of applications.
Thomas Vachuska02aeb032015-01-06 22:36:30 -080042 */
43@Path("applications")
44public class ApplicationsWebResource extends AbstractWebResource {
45
Jian Licbf49892016-05-10 14:54:44 -070046 private static final String APP_ID_NOT_FOUND = "Application ID is not found";
47 private static final String APP_NOT_FOUND = "Application is not found";
48
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070049 /**
50 * Get all installed applications.
51 * Returns array of all installed applications.
52 *
53 * @return 200 OK
Jian Lie1c1c8d2016-05-09 16:24:40 -070054 * @onos.rsModel Applications
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070055 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080056 @GET
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070057 public Response getApps() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080058 ApplicationAdminService service = get(ApplicationAdminService.class);
59 Set<Application> apps = service.getApplications();
60 return ok(encodeArray(Application.class, "applications", apps)).build();
61 }
62
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070063 /**
64 * Get application details.
65 * Returns details of the specified application.
Jian Lie1c1c8d2016-05-09 16:24:40 -070066 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070067 * @param name application name
68 * @return 200 OK; 404; 401
Jian Lie1c1c8d2016-05-09 16:24:40 -070069 * @onos.rsModel Application
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070070 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080071 @GET
72 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070073 public Response getApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080074 ApplicationAdminService service = get(ApplicationAdminService.class);
75 ApplicationId appId = service.getId(name);
76 return response(service, appId);
77 }
78
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070079 /**
80 * Install a new application.
81 * Uploads application archive stream and optionally activates the
82 * application.
83 *
84 * @param activate true to activate app also
85 * @param stream application archive stream
86 * @return 200 OK; 404; 401
87 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080088 @POST
89 @Consumes(MediaType.APPLICATION_OCTET_STREAM)
90 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070091 public Response installApp(@QueryParam("activate")
92 @DefaultValue("false") boolean activate,
93 InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080094 ApplicationAdminService service = get(ApplicationAdminService.class);
95 Application app = service.install(stream);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080096 if (activate) {
97 service.activate(app.id());
98 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080099 return ok(codec(Application.class).encode(app, this)).build();
100 }
101
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700102 /**
103 * Uninstall application.
104 * Uninstalls the specified application deactivating it first if necessary.
105 *
106 * @param name application name
Jian Lic2a542b2016-05-10 11:48:19 -0700107 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700108 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800109 @DELETE
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800110 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700111 public Response uninstallApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800112 ApplicationAdminService service = get(ApplicationAdminService.class);
113 ApplicationId appId = service.getId(name);
114 service.uninstall(appId);
Jian Lic2a542b2016-05-10 11:48:19 -0700115 return Response.noContent().build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800116 }
117
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700118 /**
119 * Activate application.
120 * Activates the specified application.
121 *
122 * @param name application name
123 * @return 200 OK; 404; 401
124 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800125 @POST
126 @Produces(MediaType.APPLICATION_JSON)
127 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700128 public Response activateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800129 ApplicationAdminService service = get(ApplicationAdminService.class);
130 ApplicationId appId = service.getId(name);
131 service.activate(appId);
132 return response(service, appId);
133 }
134
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700135 /**
136 * De-activate application.
137 * De-activates the specified application.
138 *
139 * @param name application name
140 * @return 200 OK; 404; 401
141 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800142 @DELETE
143 @Produces(MediaType.APPLICATION_JSON)
144 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700145 public Response deactivateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800146 ApplicationAdminService service = get(ApplicationAdminService.class);
147 ApplicationId appId = service.getId(name);
148 service.deactivate(appId);
149 return response(service, appId);
150 }
151
Jian Lie1c1c8d2016-05-09 16:24:40 -0700152 /**
153 * Registers an on or off platform application.
154 *
155 * @param name application name
156 * @return 200 OK; 404; 401
157 * @onos.rsModel ApplicationId
158 */
159 @POST
160 @Produces(MediaType.APPLICATION_JSON)
161 @Path("{name}/register")
162 public Response registerAppId(@PathParam("name") String name) {
163 CoreService service = get(CoreService.class);
164 ApplicationId appId = service.registerApplication(name);
165 return response(appId);
166 }
167
168 /**
Jian Li847242b2016-05-11 18:58:53 -0700169 * Gets applicationId entry by either id or name.
Jian Lie1c1c8d2016-05-09 16:24:40 -0700170 *
Jian Li847242b2016-05-11 18:58:53 -0700171 * @param id id of application
Jian Lie1c1c8d2016-05-09 16:24:40 -0700172 * @param name name of application
173 * @return 200 OK; 404; 401
174 * @onos.rsModel ApplicationId
175 */
176 @GET
177 @Produces(MediaType.APPLICATION_JSON)
Jian Li847242b2016-05-11 18:58:53 -0700178 @Path("ids/entry")
Jian Lia3e4c7a2016-05-12 13:15:40 -0700179 public Response getAppIdByName(@QueryParam("id") String id,
Jian Li847242b2016-05-11 18:58:53 -0700180 @QueryParam("name") String name) {
Jian Lie1c1c8d2016-05-09 16:24:40 -0700181 CoreService service = get(CoreService.class);
Jian Li847242b2016-05-11 18:58:53 -0700182 ApplicationId appId = null;
183 if (id != null) {
Jian Lia3e4c7a2016-05-12 13:15:40 -0700184 appId = service.getAppId(Short.valueOf(id));
Jian Li847242b2016-05-11 18:58:53 -0700185 } else if (name != null) {
186 appId = service.getAppId(name);
187 }
Jian Lie1c1c8d2016-05-09 16:24:40 -0700188 return response(appId);
189 }
190
191 /**
192 * Gets a collection of application ids.
193 * Returns array of all registered application ids.
194 *
195 * @return 200 OK; 404; 401
196 * @onos.rsModel ApplicationIds
197 */
198 @GET
199 @Produces(MediaType.APPLICATION_JSON)
200 @Path("ids")
201 public Response getAppIds() {
202 CoreService service = get(CoreService.class);
203 Set<ApplicationId> appIds = service.getAppIds();
204 return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build();
205 }
206
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800207 private Response response(ApplicationAdminService service, ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700208 Application app = nullIsNotFound(service.getApplication(appId), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800209 return ok(codec(Application.class).encode(app, this)).build();
210 }
211
Jian Lie1c1c8d2016-05-09 16:24:40 -0700212 private Response response(ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700213 ApplicationId checkedAppId = nullIsNotFound(appId, APP_ID_NOT_FOUND);
214 return ok(codec(ApplicationId.class).encode(checkedAppId, this)).build();
Jian Lie1c1c8d2016-05-09 16:24:40 -0700215 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800216}