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