blob: 8d0bc3edb8f2db7182f5468f8e11cd0e5c3179b3 [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
107 * @return 200 OK; 404; 401
108 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800109 @DELETE
110 @Produces(MediaType.APPLICATION_JSON)
111 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700112 public Response uninstallApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800113 ApplicationAdminService service = get(ApplicationAdminService.class);
114 ApplicationId appId = service.getId(name);
115 service.uninstall(appId);
116 return Response.ok().build();
117 }
118
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700119 /**
120 * Activate application.
121 * Activates the specified application.
122 *
123 * @param name application name
124 * @return 200 OK; 404; 401
125 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800126 @POST
127 @Produces(MediaType.APPLICATION_JSON)
128 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700129 public Response activateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800130 ApplicationAdminService service = get(ApplicationAdminService.class);
131 ApplicationId appId = service.getId(name);
132 service.activate(appId);
133 return response(service, appId);
134 }
135
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700136 /**
137 * De-activate application.
138 * De-activates the specified application.
139 *
140 * @param name application name
141 * @return 200 OK; 404; 401
142 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800143 @DELETE
144 @Produces(MediaType.APPLICATION_JSON)
145 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700146 public Response deactivateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800147 ApplicationAdminService service = get(ApplicationAdminService.class);
148 ApplicationId appId = service.getId(name);
149 service.deactivate(appId);
150 return response(service, appId);
151 }
152
Jian Lie1c1c8d2016-05-09 16:24:40 -0700153 /**
154 * Registers an on or off platform application.
155 *
156 * @param name application name
157 * @return 200 OK; 404; 401
158 * @onos.rsModel ApplicationId
159 */
160 @POST
161 @Produces(MediaType.APPLICATION_JSON)
162 @Path("{name}/register")
163 public Response registerAppId(@PathParam("name") String name) {
164 CoreService service = get(CoreService.class);
165 ApplicationId appId = service.registerApplication(name);
166 return response(appId);
167 }
168
169 /**
170 * Gets application Id entry by short id.
171 *
172 * @param shortId numerical id of application
173 * @return 200 OK; 404; 401
174 * @onos.rsModel ApplicationId
175 */
176 @GET
177 @Produces(MediaType.APPLICATION_JSON)
178 @Path("ids/short")
179 public Response getAppIdByShortId(@QueryParam("id") int shortId) {
180 CoreService service = get(CoreService.class);
181 ApplicationId appId = service.getAppId((short) shortId);
182 return response(appId);
183 }
184
185 /**
186 * Gets application Id entry by name.
187 *
188 * @param name name of application
189 * @return 200 OK; 404; 401
190 * @onos.rsModel ApplicationId
191 */
192 @GET
193 @Produces(MediaType.APPLICATION_JSON)
194 @Path("ids/name")
195 public Response getAppIdByName(@QueryParam("name") String name) {
196 CoreService service = get(CoreService.class);
197 ApplicationId appId = service.getAppId(name);
198 return response(appId);
199 }
200
201 /**
202 * Gets a collection of application ids.
203 * Returns array of all registered application ids.
204 *
205 * @return 200 OK; 404; 401
206 * @onos.rsModel ApplicationIds
207 */
208 @GET
209 @Produces(MediaType.APPLICATION_JSON)
210 @Path("ids")
211 public Response getAppIds() {
212 CoreService service = get(CoreService.class);
213 Set<ApplicationId> appIds = service.getAppIds();
214 return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build();
215 }
216
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800217 private Response response(ApplicationAdminService service, ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700218 Application app = nullIsNotFound(service.getApplication(appId), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800219 return ok(codec(Application.class).encode(app, this)).build();
220 }
221
Jian Lie1c1c8d2016-05-09 16:24:40 -0700222 private Response response(ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700223 ApplicationId checkedAppId = nullIsNotFound(appId, APP_ID_NOT_FOUND);
224 return ok(codec(ApplicationId.class).encode(checkedAppId, this)).build();
Jian Lie1c1c8d2016-05-09 16:24:40 -0700225 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800226}