blob: 91148db5f9f4765c1124c17ffbb1b3410ac679cc [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/**
38 * REST resource for interacting with the inventory of applications.
39 */
40@Path("applications")
41public class ApplicationsWebResource extends AbstractWebResource {
42
43 @GET
44 public Response getApplications() {
45 ApplicationAdminService service = get(ApplicationAdminService.class);
46 Set<Application> apps = service.getApplications();
47 return ok(encodeArray(Application.class, "applications", apps)).build();
48 }
49
50 @GET
51 @Path("{name}")
Ray Milkeyf195b022015-02-03 15:13:11 -080052 public Response getApplication(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080053 ApplicationAdminService service = get(ApplicationAdminService.class);
54 ApplicationId appId = service.getId(name);
55 return response(service, appId);
56 }
57
58 @POST
59 @Consumes(MediaType.APPLICATION_OCTET_STREAM)
60 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080061 public Response installApplication(@QueryParam("activate")
62 @DefaultValue("false") boolean activate,
63 InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080064 ApplicationAdminService service = get(ApplicationAdminService.class);
65 Application app = service.install(stream);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080066 if (activate) {
67 service.activate(app.id());
68 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080069 return ok(codec(Application.class).encode(app, this)).build();
70 }
71
72 @DELETE
73 @Produces(MediaType.APPLICATION_JSON)
74 @Path("{name}")
75 public Response uninstallApplication(@PathParam("name") String name) {
76 ApplicationAdminService service = get(ApplicationAdminService.class);
77 ApplicationId appId = service.getId(name);
78 service.uninstall(appId);
79 return Response.ok().build();
80 }
81
82 @POST
83 @Produces(MediaType.APPLICATION_JSON)
84 @Path("{name}/active")
85 public Response activateApplication(@PathParam("name") String name) {
86 ApplicationAdminService service = get(ApplicationAdminService.class);
87 ApplicationId appId = service.getId(name);
88 service.activate(appId);
89 return response(service, appId);
90 }
91
92 @DELETE
93 @Produces(MediaType.APPLICATION_JSON)
94 @Path("{name}/active")
95 public Response deactivateApplication(@PathParam("name") String name) {
96 ApplicationAdminService service = get(ApplicationAdminService.class);
97 ApplicationId appId = service.getId(name);
98 service.deactivate(appId);
99 return response(service, appId);
100 }
101
102 private Response response(ApplicationAdminService service, ApplicationId appId) {
103 Application app = service.getApplication(appId);
104 return ok(codec(Application.class).encode(app, this)).build();
105 }
106
107}