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