Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
alshabib | 20a070b | 2016-06-03 14:44:05 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 19 | import org.onosproject.app.ApplicationAdminService; |
Ray Milkey | 1657032 | 2018-04-17 10:26:54 -0700 | [diff] [blame] | 20 | import org.onosproject.app.ApplicationException; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 21 | import org.onosproject.core.Application; |
| 22 | import org.onosproject.core.ApplicationId; |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 23 | import org.onosproject.core.CoreService; |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 24 | import org.onosproject.rest.AbstractWebResource; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 25 | |
| 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 28 | import javax.ws.rs.DefaultValue; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 29 | import javax.ws.rs.GET; |
| 30 | import javax.ws.rs.POST; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 34 | import javax.ws.rs.QueryParam; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
alshabib | 20a070b | 2016-06-03 14:44:05 -0700 | [diff] [blame] | 37 | import java.io.IOException; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 38 | import java.io.InputStream; |
alshabib | 20a070b | 2016-06-03 14:44:05 -0700 | [diff] [blame] | 39 | import java.net.URL; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 40 | import java.util.Set; |
| 41 | |
Jian Li | cbf4989 | 2016-05-10 14:54:44 -0700 | [diff] [blame] | 42 | import static org.onlab.util.Tools.nullIsNotFound; |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 43 | import static org.onlab.util.Tools.readTreeFromStream; |
Jian Li | cbf4989 | 2016-05-10 14:54:44 -0700 | [diff] [blame] | 44 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 45 | /** |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 46 | * Manage inventory of applications. |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 47 | */ |
| 48 | @Path("applications") |
| 49 | public class ApplicationsWebResource extends AbstractWebResource { |
| 50 | |
Jian Li | cbf4989 | 2016-05-10 14:54:44 -0700 | [diff] [blame] | 51 | private static final String APP_ID_NOT_FOUND = "Application ID is not found"; |
| 52 | private static final String APP_NOT_FOUND = "Application is not found"; |
| 53 | |
alshabib | 20a070b | 2016-06-03 14:44:05 -0700 | [diff] [blame] | 54 | private static final String URL = "url"; |
| 55 | private static final String ACTIVATE = "activate"; |
| 56 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Get all installed applications. |
| 59 | * Returns array of all installed applications. |
| 60 | * |
| 61 | * @return 200 OK |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 62 | * @onos.rsModel Applications |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 63 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 64 | @GET |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 65 | public Response getApps() { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 66 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 67 | Set<Application> apps = service.getApplications(); |
| 68 | return ok(encodeArray(Application.class, "applications", apps)).build(); |
| 69 | } |
| 70 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Get application details. |
| 73 | * Returns details of the specified application. |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 74 | * |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 75 | * @param name application name |
| 76 | * @return 200 OK; 404; 401 |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 77 | * @onos.rsModel Application |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 78 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 79 | @GET |
| 80 | @Path("{name}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 81 | public Response getApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 82 | ApplicationAdminService service = get(ApplicationAdminService.class); |
Jayasree Ghosh | e6213cf | 2016-09-28 07:55:55 +0530 | [diff] [blame] | 83 | ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 84 | return response(service, appId); |
| 85 | } |
| 86 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Install a new application. |
| 89 | * Uploads application archive stream and optionally activates the |
| 90 | * application. |
alshabib | 20a070b | 2016-06-03 14:44:05 -0700 | [diff] [blame] | 91 | |
| 92 | * @param raw json object containing location (url) of application oar |
| 93 | * @return 200 OK; 404; 401 |
| 94 | */ |
| 95 | @POST |
| 96 | @Consumes(MediaType.APPLICATION_JSON) |
| 97 | @Produces(MediaType.APPLICATION_JSON) |
| 98 | public Response installApp(InputStream raw) { |
| 99 | Application app; |
| 100 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 101 | ObjectNode jsonTree = readTreeFromStream(mapper(), raw); |
alshabib | 20a070b | 2016-06-03 14:44:05 -0700 | [diff] [blame] | 102 | URL url = new URL(jsonTree.get(URL).asText()); |
| 103 | boolean activate = false; |
| 104 | if (jsonTree.has(ACTIVATE)) { |
| 105 | activate = jsonTree.get(ACTIVATE).asBoolean(); |
| 106 | } |
| 107 | |
| 108 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 109 | app = service.install(url.openStream()); |
| 110 | if (activate) { |
| 111 | service.activate(app.id()); |
| 112 | } |
| 113 | } catch (IOException ex) { |
| 114 | throw new IllegalArgumentException(ex); |
| 115 | } |
| 116 | return ok(codec(Application.class).encode(app, this)).build(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Install a new application. |
| 121 | * Uploads application archive stream and optionally activates the |
| 122 | * application. |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 123 | * |
| 124 | * @param activate true to activate app also |
| 125 | * @param stream application archive stream |
| 126 | * @return 200 OK; 404; 401 |
| 127 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 128 | @POST |
| 129 | @Consumes(MediaType.APPLICATION_OCTET_STREAM) |
| 130 | @Produces(MediaType.APPLICATION_JSON) |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 131 | public Response installApp(@QueryParam("activate") |
| 132 | @DefaultValue("false") boolean activate, |
| 133 | InputStream stream) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 134 | ApplicationAdminService service = get(ApplicationAdminService.class); |
Ray Milkey | 1657032 | 2018-04-17 10:26:54 -0700 | [diff] [blame] | 135 | try { |
| 136 | Application app = service.install(stream); |
| 137 | if (activate) { |
| 138 | service.activate(app.id()); |
| 139 | } |
| 140 | return ok(codec(Application.class).encode(app, this)).build(); |
| 141 | } catch (ApplicationException appEx) { |
| 142 | throw new IllegalArgumentException(appEx); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 143 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 146 | /** |
| 147 | * Uninstall application. |
| 148 | * Uninstalls the specified application deactivating it first if necessary. |
| 149 | * |
| 150 | * @param name application name |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 151 | * @return 204 NO CONTENT |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 152 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 153 | @DELETE |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 154 | @Path("{name}") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 155 | public Response uninstallApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 156 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 157 | ApplicationId appId = service.getId(name); |
dvaddire | e113b65 | 2017-08-04 14:58:06 +0530 | [diff] [blame] | 158 | if (appId != null) { |
| 159 | service.uninstall(appId); |
| 160 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 161 | return Response.noContent().build(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Activate application. |
| 166 | * Activates the specified application. |
| 167 | * |
| 168 | * @param name application name |
| 169 | * @return 200 OK; 404; 401 |
| 170 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 171 | @POST |
| 172 | @Produces(MediaType.APPLICATION_JSON) |
| 173 | @Path("{name}/active") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 174 | public Response activateApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 175 | ApplicationAdminService service = get(ApplicationAdminService.class); |
Hyunsun Moon | 1499864 | 2016-06-10 12:54:26 -0700 | [diff] [blame] | 176 | ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 177 | service.activate(appId); |
| 178 | return response(service, appId); |
| 179 | } |
| 180 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 181 | /** |
| 182 | * De-activate application. |
| 183 | * De-activates the specified application. |
| 184 | * |
| 185 | * @param name application name |
dvaddire | e113b65 | 2017-08-04 14:58:06 +0530 | [diff] [blame] | 186 | * @return 204 NO CONTENT |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 187 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 188 | @DELETE |
| 189 | @Produces(MediaType.APPLICATION_JSON) |
| 190 | @Path("{name}/active") |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 191 | public Response deactivateApp(@PathParam("name") String name) { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 192 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 193 | ApplicationId appId = service.getId(name); |
dvaddire | e113b65 | 2017-08-04 14:58:06 +0530 | [diff] [blame] | 194 | if (appId != null) { |
| 195 | service.deactivate(appId); |
| 196 | } |
| 197 | return Response.noContent().build(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Registers an on or off platform application. |
| 202 | * |
| 203 | * @param name application name |
| 204 | * @return 200 OK; 404; 401 |
| 205 | * @onos.rsModel ApplicationId |
| 206 | */ |
| 207 | @POST |
| 208 | @Produces(MediaType.APPLICATION_JSON) |
| 209 | @Path("{name}/register") |
| 210 | public Response registerAppId(@PathParam("name") String name) { |
| 211 | CoreService service = get(CoreService.class); |
| 212 | ApplicationId appId = service.registerApplication(name); |
| 213 | return response(appId); |
| 214 | } |
| 215 | |
| 216 | /** |
Thomas Vachuska | 08b4dec | 2017-08-31 15:20:17 -0700 | [diff] [blame] | 217 | * Get application OAR/JAR file. |
| 218 | * Returns the OAR/JAR file used to install the specified application. |
| 219 | * |
| 220 | * @param name application name |
| 221 | * @return 200 OK; 404; 401 |
| 222 | */ |
| 223 | @GET |
| 224 | @Produces(MediaType.APPLICATION_OCTET_STREAM) |
| 225 | @Path("{name}/bits") |
| 226 | public Response getAppBits(@PathParam("name") String name) { |
| 227 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 228 | ApplicationId appId = nullIsNotFound(service.getId(name), APP_ID_NOT_FOUND); |
| 229 | InputStream bits = service.getApplicationArchive(appId); |
| 230 | return ok(bits).build(); |
| 231 | } |
| 232 | |
| 233 | /** |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 234 | * Gets applicationId entry by either id or name. |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 235 | * |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 236 | * @param id id of application |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 237 | * @param name name of application |
| 238 | * @return 200 OK; 404; 401 |
| 239 | * @onos.rsModel ApplicationId |
| 240 | */ |
| 241 | @GET |
| 242 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 243 | @Path("ids/entry") |
Jian Li | a3e4c7a | 2016-05-12 13:15:40 -0700 | [diff] [blame] | 244 | public Response getAppIdByName(@QueryParam("id") String id, |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 245 | @QueryParam("name") String name) { |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 246 | CoreService service = get(CoreService.class); |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 247 | ApplicationId appId = null; |
| 248 | if (id != null) { |
Jian Li | a3e4c7a | 2016-05-12 13:15:40 -0700 | [diff] [blame] | 249 | appId = service.getAppId(Short.valueOf(id)); |
Jian Li | 847242b | 2016-05-11 18:58:53 -0700 | [diff] [blame] | 250 | } else if (name != null) { |
| 251 | appId = service.getAppId(name); |
| 252 | } |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 253 | return response(appId); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Gets a collection of application ids. |
| 258 | * Returns array of all registered application ids. |
| 259 | * |
| 260 | * @return 200 OK; 404; 401 |
| 261 | * @onos.rsModel ApplicationIds |
| 262 | */ |
| 263 | @GET |
| 264 | @Produces(MediaType.APPLICATION_JSON) |
| 265 | @Path("ids") |
| 266 | public Response getAppIds() { |
| 267 | CoreService service = get(CoreService.class); |
| 268 | Set<ApplicationId> appIds = service.getAppIds(); |
| 269 | return ok(encodeArray(ApplicationId.class, "applicationIds", appIds)).build(); |
| 270 | } |
| 271 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 272 | private Response response(ApplicationAdminService service, ApplicationId appId) { |
Jian Li | cbf4989 | 2016-05-10 14:54:44 -0700 | [diff] [blame] | 273 | Application app = nullIsNotFound(service.getApplication(appId), APP_NOT_FOUND); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 274 | return ok(codec(Application.class).encode(app, this)).build(); |
| 275 | } |
| 276 | |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 277 | private Response response(ApplicationId appId) { |
Jian Li | cbf4989 | 2016-05-10 14:54:44 -0700 | [diff] [blame] | 278 | ApplicationId checkedAppId = nullIsNotFound(appId, APP_ID_NOT_FOUND); |
| 279 | return ok(codec(ApplicationId.class).encode(checkedAppId, this)).build(); |
Jian Li | e1c1c8d | 2016-05-09 16:24:40 -0700 | [diff] [blame] | 280 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 281 | } |