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