Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 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; |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame^] | 21 | import org.onosproject.core.CoreService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 22 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 23 | |
| 24 | import javax.ws.rs.Consumes; |
| 25 | import javax.ws.rs.DELETE; |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 26 | import javax.ws.rs.DefaultValue; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 27 | import javax.ws.rs.GET; |
| 28 | import javax.ws.rs.POST; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.PathParam; |
| 31 | import javax.ws.rs.Produces; |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 32 | import javax.ws.rs.QueryParam; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
| 35 | import java.io.InputStream; |
| 36 | import java.util.Set; |
| 37 | |
| 38 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 39 | * Manage inventory of applications. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 40 | */ |
| 41 | @Path("applications") |
| 42 | public class ApplicationsWebResource extends AbstractWebResource { |
| 43 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Get all installed applications. |
| 46 | * Returns array of all installed applications. |
| 47 | * |
| 48 | * @return 200 OK |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame^] | 49 | * @onos.rsModel Applications |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 50 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 51 | @GET |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 52 | public Response getApps() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 53 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 54 | Set<Application> apps = service.getApplications(); |
| 55 | return ok(encodeArray(Application.class, "applications", apps)).build(); |
| 56 | } |
| 57 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Get application details. |
| 60 | * Returns details of the specified application. |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame^] | 61 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 62 | * @param name application name |
| 63 | * @return 200 OK; 404; 401 |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame^] | 64 | * @onos.rsModel Application |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 65 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 66 | @GET |
| 67 | @Path("{name}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 68 | public Response getApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 69 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 70 | ApplicationId appId = service.getId(name); |
| 71 | return response(service, appId); |
| 72 | } |
| 73 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Install a new application. |
| 76 | * Uploads application archive stream and optionally activates the |
| 77 | * application. |
| 78 | * |
| 79 | * @param activate true to activate app also |
| 80 | * @param stream application archive stream |
| 81 | * @return 200 OK; 404; 401 |
| 82 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 83 | @POST |
| 84 | @Consumes(MediaType.APPLICATION_OCTET_STREAM) |
| 85 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 86 | public Response installApp(@QueryParam("activate") |
| 87 | @DefaultValue("false") boolean activate, |
| 88 | InputStream stream) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 89 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 90 | Application app = service.install(stream); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 91 | if (activate) { |
| 92 | service.activate(app.id()); |
| 93 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 94 | return ok(codec(Application.class).encode(app, this)).build(); |
| 95 | } |
| 96 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Uninstall application. |
| 99 | * Uninstalls the specified application deactivating it first if necessary. |
| 100 | * |
| 101 | * @param name application name |
| 102 | * @return 200 OK; 404; 401 |
| 103 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 104 | @DELETE |
| 105 | @Produces(MediaType.APPLICATION_JSON) |
| 106 | @Path("{name}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 107 | public Response uninstallApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 108 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 109 | ApplicationId appId = service.getId(name); |
| 110 | service.uninstall(appId); |
| 111 | return Response.ok().build(); |
| 112 | } |
| 113 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Activate application. |
| 116 | * Activates the specified application. |
| 117 | * |
| 118 | * @param name application name |
| 119 | * @return 200 OK; 404; 401 |
| 120 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 121 | @POST |
| 122 | @Produces(MediaType.APPLICATION_JSON) |
| 123 | @Path("{name}/active") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 124 | public Response activateApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 125 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 126 | ApplicationId appId = service.getId(name); |
| 127 | service.activate(appId); |
| 128 | return response(service, appId); |
| 129 | } |
| 130 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 131 | /** |
| 132 | * De-activate application. |
| 133 | * De-activates the specified application. |
| 134 | * |
| 135 | * @param name application name |
| 136 | * @return 200 OK; 404; 401 |
| 137 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 138 | @DELETE |
| 139 | @Produces(MediaType.APPLICATION_JSON) |
| 140 | @Path("{name}/active") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 141 | public Response deactivateApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 142 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 143 | ApplicationId appId = service.getId(name); |
| 144 | service.deactivate(appId); |
| 145 | return response(service, appId); |
| 146 | } |
| 147 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame^] | 148 | /** |
| 149 | * Registers an on or off platform application. |
| 150 | * |
| 151 | * @param name application name |
| 152 | * @return 200 OK; 404; 401 |
| 153 | * @onos.rsModel ApplicationId |
| 154 | */ |
| 155 | @POST |
| 156 | @Produces(MediaType.APPLICATION_JSON) |
| 157 | @Path("{name}/register") |
| 158 | public Response registerAppId(@PathParam("name") String name) { |
| 159 | CoreService service = get(CoreService.class); |
| 160 | ApplicationId appId = service.registerApplication(name); |
| 161 | return response(appId); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Gets application Id entry by short id. |
| 166 | * |
| 167 | * @param shortId numerical id of application |
| 168 | * @return 200 OK; 404; 401 |
| 169 | * @onos.rsModel ApplicationId |
| 170 | */ |
| 171 | @GET |
| 172 | @Produces(MediaType.APPLICATION_JSON) |
| 173 | @Path("ids/short") |
| 174 | public Response getAppIdByShortId(@QueryParam("id") int shortId) { |
| 175 | CoreService service = get(CoreService.class); |
| 176 | ApplicationId appId = service.getAppId((short) shortId); |
| 177 | return response(appId); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Gets application Id entry by name. |
| 182 | * |
| 183 | * @param name name of application |
| 184 | * @return 200 OK; 404; 401 |
| 185 | * @onos.rsModel ApplicationId |
| 186 | */ |
| 187 | @GET |
| 188 | @Produces(MediaType.APPLICATION_JSON) |
| 189 | @Path("ids/name") |
| 190 | public Response getAppIdByName(@QueryParam("name") String name) { |
| 191 | CoreService service = get(CoreService.class); |
| 192 | ApplicationId appId = service.getAppId(name); |
| 193 | return response(appId); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Gets a collection of application ids. |
| 198 | * Returns array of all registered application ids. |
| 199 | * |
| 200 | * @return 200 OK; 404; 401 |
| 201 | * @onos.rsModel ApplicationIds |
| 202 | */ |
| 203 | @GET |
| 204 | @Produces(MediaType.APPLICATION_JSON) |
| 205 | @Path("ids") |
| 206 | public Response getAppIds() { |
| 207 | CoreService service = get(CoreService.class); |
| 208 | Set<ApplicationId> appIds = service.getAppIds(); |
| 209 | return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build(); |
| 210 | } |
| 211 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 212 | private Response response(ApplicationAdminService service, ApplicationId appId) { |
| 213 | Application app = service.getApplication(appId); |
| 214 | return ok(codec(Application.class).encode(app, this)).build(); |
| 215 | } |
| 216 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame^] | 217 | private Response response(ApplicationId appId) { |
| 218 | return ok(codec(ApplicationId.class).encode(appId, this)).build(); |
| 219 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 220 | } |