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