Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
| 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 Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 17 | |
| 18 | import org.onosproject.app.ApplicationAdminService; |
| 19 | import org.onosproject.core.Application; |
| 20 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 21 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 22 | |
| 23 | import javax.ws.rs.Consumes; |
| 24 | import javax.ws.rs.DELETE; |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 25 | import javax.ws.rs.DefaultValue; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 26 | import javax.ws.rs.GET; |
| 27 | import javax.ws.rs.POST; |
| 28 | import javax.ws.rs.Path; |
| 29 | import javax.ws.rs.PathParam; |
| 30 | import javax.ws.rs.Produces; |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 31 | import javax.ws.rs.QueryParam; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import java.io.InputStream; |
| 35 | import java.util.Set; |
| 36 | |
| 37 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 38 | * Manage inventory of applications. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 39 | */ |
| 40 | @Path("applications") |
| 41 | public class ApplicationsWebResource extends AbstractWebResource { |
| 42 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Get all installed applications. |
| 45 | * Returns array of all installed applications. |
| 46 | * |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 47 | * @onos.rsModel Applications |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 48 | * @return 200 OK |
| 49 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 50 | @GET |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 51 | public Response getApps() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 52 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 53 | Set<Application> apps = service.getApplications(); |
| 54 | return ok(encodeArray(Application.class, "applications", apps)).build(); |
| 55 | } |
| 56 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Get application details. |
| 59 | * Returns details of the specified application. |
Andrea Campanella | 10c4adc | 2015-12-03 15:27:54 -0800 | [diff] [blame] | 60 | * @onos.rsModel Application |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 61 | * @param name application name |
| 62 | * @return 200 OK; 404; 401 |
| 63 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 64 | @GET |
| 65 | @Path("{name}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 66 | public Response getApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 67 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 68 | ApplicationId appId = service.getId(name); |
| 69 | return response(service, appId); |
| 70 | } |
| 71 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Install a new application. |
| 74 | * Uploads application archive stream and optionally activates the |
| 75 | * application. |
| 76 | * |
| 77 | * @param activate true to activate app also |
| 78 | * @param stream application archive stream |
| 79 | * @return 200 OK; 404; 401 |
| 80 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 81 | @POST |
| 82 | @Consumes(MediaType.APPLICATION_OCTET_STREAM) |
| 83 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 84 | public Response installApp(@QueryParam("activate") |
| 85 | @DefaultValue("false") boolean activate, |
| 86 | InputStream stream) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 87 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 88 | Application app = service.install(stream); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 89 | if (activate) { |
| 90 | service.activate(app.id()); |
| 91 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 92 | return ok(codec(Application.class).encode(app, this)).build(); |
| 93 | } |
| 94 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Uninstall application. |
| 97 | * Uninstalls the specified application deactivating it first if necessary. |
| 98 | * |
| 99 | * @param name application name |
| 100 | * @return 200 OK; 404; 401 |
| 101 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 102 | @DELETE |
| 103 | @Produces(MediaType.APPLICATION_JSON) |
| 104 | @Path("{name}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 105 | public Response uninstallApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 106 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 107 | ApplicationId appId = service.getId(name); |
| 108 | service.uninstall(appId); |
| 109 | return Response.ok().build(); |
| 110 | } |
| 111 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Activate application. |
| 114 | * Activates the specified application. |
| 115 | * |
| 116 | * @param name application name |
| 117 | * @return 200 OK; 404; 401 |
| 118 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 119 | @POST |
| 120 | @Produces(MediaType.APPLICATION_JSON) |
| 121 | @Path("{name}/active") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 122 | public Response activateApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 123 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 124 | ApplicationId appId = service.getId(name); |
| 125 | service.activate(appId); |
| 126 | return response(service, appId); |
| 127 | } |
| 128 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 129 | /** |
| 130 | * De-activate application. |
| 131 | * De-activates the specified application. |
| 132 | * |
| 133 | * @param name application name |
| 134 | * @return 200 OK; 404; 401 |
| 135 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 136 | @DELETE |
| 137 | @Produces(MediaType.APPLICATION_JSON) |
| 138 | @Path("{name}/active") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 139 | public Response deactivateApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 140 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 141 | ApplicationId appId = service.getId(name); |
| 142 | service.deactivate(appId); |
| 143 | return response(service, appId); |
| 144 | } |
| 145 | |
| 146 | private Response response(ApplicationAdminService service, ApplicationId appId) { |
| 147 | Application app = service.getApplication(appId); |
| 148 | return ok(codec(Application.class).encode(app, this)).build(); |
| 149 | } |
| 150 | |
| 151 | } |