blob: ee6a6b558b93a3519c8d3d1b57adaf5ec43d8fa2 [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
38/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070039 * Manage inventory of applications.
Thomas Vachuska02aeb032015-01-06 22:36:30 -080040 */
41@Path("applications")
42public class ApplicationsWebResource extends AbstractWebResource {
43
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070044 /**
45 * Get all installed applications.
46 * Returns array of all installed applications.
47 *
48 * @return 200 OK
Jian Lie1c1c8d2016-05-09 16:24:40 -070049 * @onos.rsModel Applications
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070050 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080051 @GET
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070052 public Response getApps() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080053 ApplicationAdminService service = get(ApplicationAdminService.class);
54 Set<Application> apps = service.getApplications();
55 return ok(encodeArray(Application.class, "applications", apps)).build();
56 }
57
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070058 /**
59 * Get application details.
60 * Returns details of the specified application.
Jian Lie1c1c8d2016-05-09 16:24:40 -070061 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070062 * @param name application name
63 * @return 200 OK; 404; 401
Jian Lie1c1c8d2016-05-09 16:24:40 -070064 * @onos.rsModel Application
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070065 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080066 @GET
67 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070068 public Response getApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080069 ApplicationAdminService service = get(ApplicationAdminService.class);
70 ApplicationId appId = service.getId(name);
71 return response(service, appId);
72 }
73
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070074 /**
75 * Install a new application.
76 * Uploads application archive stream and optionally activates the
77 * application.
78 *
79 * @param activate true to activate app also
80 * @param stream application archive stream
81 * @return 200 OK; 404; 401
82 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080083 @POST
84 @Consumes(MediaType.APPLICATION_OCTET_STREAM)
85 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070086 public Response installApp(@QueryParam("activate")
87 @DefaultValue("false") boolean activate,
88 InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080089 ApplicationAdminService service = get(ApplicationAdminService.class);
90 Application app = service.install(stream);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080091 if (activate) {
92 service.activate(app.id());
93 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080094 return ok(codec(Application.class).encode(app, this)).build();
95 }
96
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070097 /**
98 * Uninstall application.
99 * Uninstalls the specified application deactivating it first if necessary.
100 *
101 * @param name application name
102 * @return 200 OK; 404; 401
103 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800104 @DELETE
105 @Produces(MediaType.APPLICATION_JSON)
106 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700107 public Response uninstallApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800108 ApplicationAdminService service = get(ApplicationAdminService.class);
109 ApplicationId appId = service.getId(name);
110 service.uninstall(appId);
111 return Response.ok().build();
112 }
113
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700114 /**
115 * Activate application.
116 * Activates the specified application.
117 *
118 * @param name application name
119 * @return 200 OK; 404; 401
120 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800121 @POST
122 @Produces(MediaType.APPLICATION_JSON)
123 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700124 public Response activateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800125 ApplicationAdminService service = get(ApplicationAdminService.class);
126 ApplicationId appId = service.getId(name);
127 service.activate(appId);
128 return response(service, appId);
129 }
130
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700131 /**
132 * De-activate application.
133 * De-activates the specified application.
134 *
135 * @param name application name
136 * @return 200 OK; 404; 401
137 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800138 @DELETE
139 @Produces(MediaType.APPLICATION_JSON)
140 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700141 public Response deactivateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800142 ApplicationAdminService service = get(ApplicationAdminService.class);
143 ApplicationId appId = service.getId(name);
144 service.deactivate(appId);
145 return response(service, appId);
146 }
147
Jian Lie1c1c8d2016-05-09 16:24:40 -0700148 /**
149 * Registers an on or off platform application.
150 *
151 * @param name application name
152 * @return 200 OK; 404; 401
153 * @onos.rsModel ApplicationId
154 */
155 @POST
156 @Produces(MediaType.APPLICATION_JSON)
157 @Path("{name}/register")
158 public Response registerAppId(@PathParam("name") String name) {
159 CoreService service = get(CoreService.class);
160 ApplicationId appId = service.registerApplication(name);
161 return response(appId);
162 }
163
164 /**
165 * Gets application Id entry by short id.
166 *
167 * @param shortId numerical id of application
168 * @return 200 OK; 404; 401
169 * @onos.rsModel ApplicationId
170 */
171 @GET
172 @Produces(MediaType.APPLICATION_JSON)
173 @Path("ids/short")
174 public Response getAppIdByShortId(@QueryParam("id") int shortId) {
175 CoreService service = get(CoreService.class);
176 ApplicationId appId = service.getAppId((short) shortId);
177 return response(appId);
178 }
179
180 /**
181 * Gets application Id entry by name.
182 *
183 * @param name name of application
184 * @return 200 OK; 404; 401
185 * @onos.rsModel ApplicationId
186 */
187 @GET
188 @Produces(MediaType.APPLICATION_JSON)
189 @Path("ids/name")
190 public Response getAppIdByName(@QueryParam("name") String name) {
191 CoreService service = get(CoreService.class);
192 ApplicationId appId = service.getAppId(name);
193 return response(appId);
194 }
195
196 /**
197 * Gets a collection of application ids.
198 * Returns array of all registered application ids.
199 *
200 * @return 200 OK; 404; 401
201 * @onos.rsModel ApplicationIds
202 */
203 @GET
204 @Produces(MediaType.APPLICATION_JSON)
205 @Path("ids")
206 public Response getAppIds() {
207 CoreService service = get(CoreService.class);
208 Set<ApplicationId> appIds = service.getAppIds();
209 return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build();
210 }
211
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800212 private Response response(ApplicationAdminService service, ApplicationId appId) {
213 Application app = service.getApplication(appId);
214 return ok(codec(Application.class).encode(app, this)).build();
215 }
216
Jian Lie1c1c8d2016-05-09 16:24:40 -0700217 private Response response(ApplicationId appId) {
218 return ok(codec(ApplicationId.class).encode(appId, this)).build();
219 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800220}