blob: 6177bab5f6eccca2a272b65e6d948c7efec9b4a3 [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;
24import javax.ws.rs.GET;
25import javax.ws.rs.POST;
26import javax.ws.rs.Path;
27import javax.ws.rs.PathParam;
28import javax.ws.rs.Produces;
29import javax.ws.rs.core.MediaType;
30import javax.ws.rs.core.Response;
31import java.io.InputStream;
32import java.util.Set;
33
34/**
35 * REST resource for interacting with the inventory of applications.
36 */
37@Path("applications")
38public class ApplicationsWebResource extends AbstractWebResource {
39
40 @GET
41 public Response getApplications() {
42 ApplicationAdminService service = get(ApplicationAdminService.class);
43 Set<Application> apps = service.getApplications();
44 return ok(encodeArray(Application.class, "applications", apps)).build();
45 }
46
47 @GET
48 @Path("{name}")
49 public Response getApplication(@PathParam("id") String name) {
50 ApplicationAdminService service = get(ApplicationAdminService.class);
51 ApplicationId appId = service.getId(name);
52 return response(service, appId);
53 }
54
55 @POST
56 @Consumes(MediaType.APPLICATION_OCTET_STREAM)
57 @Produces(MediaType.APPLICATION_JSON)
58 public Response installApplication(InputStream stream) {
59 ApplicationAdminService service = get(ApplicationAdminService.class);
60 Application app = service.install(stream);
61 return ok(codec(Application.class).encode(app, this)).build();
62 }
63
64 @DELETE
65 @Produces(MediaType.APPLICATION_JSON)
66 @Path("{name}")
67 public Response uninstallApplication(@PathParam("name") String name) {
68 ApplicationAdminService service = get(ApplicationAdminService.class);
69 ApplicationId appId = service.getId(name);
70 service.uninstall(appId);
71 return Response.ok().build();
72 }
73
74 @POST
75 @Produces(MediaType.APPLICATION_JSON)
76 @Path("{name}/active")
77 public Response activateApplication(@PathParam("name") String name) {
78 ApplicationAdminService service = get(ApplicationAdminService.class);
79 ApplicationId appId = service.getId(name);
80 service.activate(appId);
81 return response(service, appId);
82 }
83
84 @DELETE
85 @Produces(MediaType.APPLICATION_JSON)
86 @Path("{name}/active")
87 public Response deactivateApplication(@PathParam("name") String name) {
88 ApplicationAdminService service = get(ApplicationAdminService.class);
89 ApplicationId appId = service.getId(name);
90 service.deactivate(appId);
91 return response(service, appId);
92 }
93
94 private Response response(ApplicationAdminService service, ApplicationId appId) {
95 Application app = service.getApplication(appId);
96 return ok(codec(Application.class).encode(app, this)).build();
97 }
98
99}