blob: 1b3bd3ac067c62aa84807b71d31632da5ad48309 [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 */
16package org.onosproject.rest;
17
18import org.onosproject.app.ApplicationAdminService;
19import org.onosproject.core.Application;
20import org.onosproject.core.ApplicationId;
21
22import javax.ws.rs.Consumes;
23import javax.ws.rs.DELETE;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080024import javax.ws.rs.DefaultValue;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080025import javax.ws.rs.GET;
26import javax.ws.rs.POST;
27import javax.ws.rs.Path;
28import javax.ws.rs.PathParam;
29import javax.ws.rs.Produces;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080030import javax.ws.rs.QueryParam;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080031import javax.ws.rs.core.MediaType;
32import javax.ws.rs.core.Response;
33import java.io.InputStream;
34import java.util.Set;
35
36/**
37 * REST resource for interacting with the inventory of applications.
38 */
39@Path("applications")
40public class ApplicationsWebResource extends AbstractWebResource {
41
42 @GET
43 public Response getApplications() {
44 ApplicationAdminService service = get(ApplicationAdminService.class);
45 Set<Application> apps = service.getApplications();
46 return ok(encodeArray(Application.class, "applications", apps)).build();
47 }
48
49 @GET
50 @Path("{name}")
Ray Milkeyf195b022015-02-03 15:13:11 -080051 public Response getApplication(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080052 ApplicationAdminService service = get(ApplicationAdminService.class);
53 ApplicationId appId = service.getId(name);
54 return response(service, appId);
55 }
56
57 @POST
58 @Consumes(MediaType.APPLICATION_OCTET_STREAM)
59 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080060 public Response installApplication(@QueryParam("activate")
61 @DefaultValue("false") boolean activate,
62 InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080063 ApplicationAdminService service = get(ApplicationAdminService.class);
64 Application app = service.install(stream);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080065 if (activate) {
66 service.activate(app.id());
67 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080068 return ok(codec(Application.class).encode(app, this)).build();
69 }
70
71 @DELETE
72 @Produces(MediaType.APPLICATION_JSON)
73 @Path("{name}")
74 public Response uninstallApplication(@PathParam("name") String name) {
75 ApplicationAdminService service = get(ApplicationAdminService.class);
76 ApplicationId appId = service.getId(name);
77 service.uninstall(appId);
78 return Response.ok().build();
79 }
80
81 @POST
82 @Produces(MediaType.APPLICATION_JSON)
83 @Path("{name}/active")
84 public Response activateApplication(@PathParam("name") String name) {
85 ApplicationAdminService service = get(ApplicationAdminService.class);
86 ApplicationId appId = service.getId(name);
87 service.activate(appId);
88 return response(service, appId);
89 }
90
91 @DELETE
92 @Produces(MediaType.APPLICATION_JSON)
93 @Path("{name}/active")
94 public Response deactivateApplication(@PathParam("name") String name) {
95 ApplicationAdminService service = get(ApplicationAdminService.class);
96 ApplicationId appId = service.getId(name);
97 service.deactivate(appId);
98 return response(service, appId);
99 }
100
101 private Response response(ApplicationAdminService service, ApplicationId appId) {
102 Application app = service.getApplication(appId);
103 return ok(codec(Application.class).encode(app, this)).build();
104 }
105
106}