blob: 636fc333fe4b72e393b007cb74b1cd2fafded791 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070021import org.onosproject.rest.AbstractWebResource;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080022
23import javax.ws.rs.Consumes;
24import javax.ws.rs.DELETE;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080025import javax.ws.rs.DefaultValue;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080026import javax.ws.rs.GET;
27import javax.ws.rs.POST;
28import javax.ws.rs.Path;
29import javax.ws.rs.PathParam;
30import javax.ws.rs.Produces;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080031import javax.ws.rs.QueryParam;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080032import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34import java.io.InputStream;
35import java.util.Set;
36
37/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070038 * Manage inventory of applications.
Thomas Vachuska02aeb032015-01-06 22:36:30 -080039 */
40@Path("applications")
41public class ApplicationsWebResource extends AbstractWebResource {
42
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070043 /**
44 * Get all installed applications.
45 * Returns array of all installed applications.
46 *
47 * @return 200 OK
48 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080049 @GET
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070050 public Response getApps() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080051 ApplicationAdminService service = get(ApplicationAdminService.class);
52 Set<Application> apps = service.getApplications();
53 return ok(encodeArray(Application.class, "applications", apps)).build();
54 }
55
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070056 /**
57 * Get application details.
58 * Returns details of the specified application.
59 *
60 * @param name application name
61 * @return 200 OK; 404; 401
62 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080063 @GET
64 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070065 public Response getApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080066 ApplicationAdminService service = get(ApplicationAdminService.class);
67 ApplicationId appId = service.getId(name);
68 return response(service, appId);
69 }
70
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070071 /**
72 * Install a new application.
73 * Uploads application archive stream and optionally activates the
74 * application.
75 *
76 * @param activate true to activate app also
77 * @param stream application archive stream
78 * @return 200 OK; 404; 401
79 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080080 @POST
81 @Consumes(MediaType.APPLICATION_OCTET_STREAM)
82 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070083 public Response installApp(@QueryParam("activate")
84 @DefaultValue("false") boolean activate,
85 InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080086 ApplicationAdminService service = get(ApplicationAdminService.class);
87 Application app = service.install(stream);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080088 if (activate) {
89 service.activate(app.id());
90 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080091 return ok(codec(Application.class).encode(app, this)).build();
92 }
93
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070094 /**
95 * Uninstall application.
96 * Uninstalls the specified application deactivating it first if necessary.
97 *
98 * @param name application name
99 * @return 200 OK; 404; 401
100 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800101 @DELETE
102 @Produces(MediaType.APPLICATION_JSON)
103 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700104 public Response uninstallApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800105 ApplicationAdminService service = get(ApplicationAdminService.class);
106 ApplicationId appId = service.getId(name);
107 service.uninstall(appId);
108 return Response.ok().build();
109 }
110
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700111 /**
112 * Activate application.
113 * Activates the specified application.
114 *
115 * @param name application name
116 * @return 200 OK; 404; 401
117 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800118 @POST
119 @Produces(MediaType.APPLICATION_JSON)
120 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700121 public Response activateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800122 ApplicationAdminService service = get(ApplicationAdminService.class);
123 ApplicationId appId = service.getId(name);
124 service.activate(appId);
125 return response(service, appId);
126 }
127
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700128 /**
129 * De-activate application.
130 * De-activates the specified application.
131 *
132 * @param name application name
133 * @return 200 OK; 404; 401
134 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800135 @DELETE
136 @Produces(MediaType.APPLICATION_JSON)
137 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700138 public Response deactivateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800139 ApplicationAdminService service = get(ApplicationAdminService.class);
140 ApplicationId appId = service.getId(name);
141 service.deactivate(appId);
142 return response(service, appId);
143 }
144
145 private Response response(ApplicationAdminService service, ApplicationId appId) {
146 Application app = service.getApplication(appId);
147 return ok(codec(Application.class).encode(app, this)).build();
148 }
149
150}