blob: 6163af470dfe2166193743ca4eea76340e2cc8be [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska02aeb032015-01-06 22:36:30 -08003 *
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 Hart9bb32ab2015-05-05 18:17:31 -070016package org.onosproject.rest.resources;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080017
alshabib20a070b2016-06-03 14:44:05 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080019import org.onosproject.app.ApplicationAdminService;
20import org.onosproject.core.Application;
21import org.onosproject.core.ApplicationId;
Jian Lie1c1c8d2016-05-09 16:24:40 -070022import org.onosproject.core.CoreService;
Jonathan Hart9bb32ab2015-05-05 18:17:31 -070023import org.onosproject.rest.AbstractWebResource;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080024
25import javax.ws.rs.Consumes;
26import javax.ws.rs.DELETE;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080027import javax.ws.rs.DefaultValue;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080028import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.PathParam;
32import javax.ws.rs.Produces;
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080033import javax.ws.rs.QueryParam;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080034import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
alshabib20a070b2016-06-03 14:44:05 -070036import java.io.IOException;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080037import java.io.InputStream;
alshabib20a070b2016-06-03 14:44:05 -070038import java.net.URL;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080039import java.util.Set;
40
Jian Licbf49892016-05-10 14:54:44 -070041import static org.onlab.util.Tools.nullIsNotFound;
42
Thomas Vachuska02aeb032015-01-06 22:36:30 -080043/**
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070044 * Manage inventory of applications.
Thomas Vachuska02aeb032015-01-06 22:36:30 -080045 */
46@Path("applications")
47public class ApplicationsWebResource extends AbstractWebResource {
48
Jian Licbf49892016-05-10 14:54:44 -070049 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
alshabib20a070b2016-06-03 14:44:05 -070052 private static final String URL = "url";
53 private static final String ACTIVATE = "activate";
54
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070055 /**
56 * Get all installed applications.
57 * Returns array of all installed applications.
58 *
59 * @return 200 OK
Jian Lie1c1c8d2016-05-09 16:24:40 -070060 * @onos.rsModel Applications
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070061 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080062 @GET
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070063 public Response getApps() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080064 ApplicationAdminService service = get(ApplicationAdminService.class);
65 Set<Application> apps = service.getApplications();
66 return ok(encodeArray(Application.class, "applications", apps)).build();
67 }
68
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070069 /**
70 * Get application details.
71 * Returns details of the specified application.
Jian Lie1c1c8d2016-05-09 16:24:40 -070072 *
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070073 * @param name application name
74 * @return 200 OK; 404; 401
Jian Lie1c1c8d2016-05-09 16:24:40 -070075 * @onos.rsModel Application
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070076 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080077 @GET
78 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070079 public Response getApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080080 ApplicationAdminService service = get(ApplicationAdminService.class);
81 ApplicationId appId = service.getId(name);
82 return response(service, appId);
83 }
84
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070085 /**
86 * Install a new application.
87 * Uploads application archive stream and optionally activates the
88 * application.
alshabib20a070b2016-06-03 14:44:05 -070089
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 Vachuska0fa2aa12015-08-18 12:53:04 -0700121 *
122 * @param activate true to activate app also
123 * @param stream application archive stream
124 * @return 200 OK; 404; 401
125 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800126 @POST
127 @Consumes(MediaType.APPLICATION_OCTET_STREAM)
128 @Produces(MediaType.APPLICATION_JSON)
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700129 public Response installApp(@QueryParam("activate")
130 @DefaultValue("false") boolean activate,
131 InputStream stream) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800132 ApplicationAdminService service = get(ApplicationAdminService.class);
133 Application app = service.install(stream);
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800134 if (activate) {
135 service.activate(app.id());
136 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800137 return ok(codec(Application.class).encode(app, this)).build();
138 }
139
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700140 /**
141 * Uninstall application.
142 * Uninstalls the specified application deactivating it first if necessary.
143 *
144 * @param name application name
Jian Lic2a542b2016-05-10 11:48:19 -0700145 * @return 204 NO CONTENT
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700146 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800147 @DELETE
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800148 @Path("{name}")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700149 public Response uninstallApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800150 ApplicationAdminService service = get(ApplicationAdminService.class);
151 ApplicationId appId = service.getId(name);
152 service.uninstall(appId);
Jian Lic2a542b2016-05-10 11:48:19 -0700153 return Response.noContent().build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800154 }
155
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700156 /**
157 * Activate application.
158 * Activates the specified application.
159 *
160 * @param name application name
161 * @return 200 OK; 404; 401
162 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800163 @POST
164 @Produces(MediaType.APPLICATION_JSON)
165 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700166 public Response activateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800167 ApplicationAdminService service = get(ApplicationAdminService.class);
Hyunsun Moon14998642016-06-10 12:54:26 -0700168 ApplicationId appId = nullIsNotFound(service.getId(name), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800169 service.activate(appId);
170 return response(service, appId);
171 }
172
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700173 /**
174 * De-activate application.
175 * De-activates the specified application.
176 *
177 * @param name application name
178 * @return 200 OK; 404; 401
179 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800180 @DELETE
181 @Produces(MediaType.APPLICATION_JSON)
182 @Path("{name}/active")
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -0700183 public Response deactivateApp(@PathParam("name") String name) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800184 ApplicationAdminService service = get(ApplicationAdminService.class);
185 ApplicationId appId = service.getId(name);
186 service.deactivate(appId);
187 return response(service, appId);
188 }
189
Jian Lie1c1c8d2016-05-09 16:24:40 -0700190 /**
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 Li847242b2016-05-11 18:58:53 -0700207 * Gets applicationId entry by either id or name.
Jian Lie1c1c8d2016-05-09 16:24:40 -0700208 *
Jian Li847242b2016-05-11 18:58:53 -0700209 * @param id id of application
Jian Lie1c1c8d2016-05-09 16:24:40 -0700210 * @param name name of application
211 * @return 200 OK; 404; 401
212 * @onos.rsModel ApplicationId
213 */
214 @GET
215 @Produces(MediaType.APPLICATION_JSON)
Jian Li847242b2016-05-11 18:58:53 -0700216 @Path("ids/entry")
Jian Lia3e4c7a2016-05-12 13:15:40 -0700217 public Response getAppIdByName(@QueryParam("id") String id,
Jian Li847242b2016-05-11 18:58:53 -0700218 @QueryParam("name") String name) {
Jian Lie1c1c8d2016-05-09 16:24:40 -0700219 CoreService service = get(CoreService.class);
Jian Li847242b2016-05-11 18:58:53 -0700220 ApplicationId appId = null;
221 if (id != null) {
Jian Lia3e4c7a2016-05-12 13:15:40 -0700222 appId = service.getAppId(Short.valueOf(id));
Jian Li847242b2016-05-11 18:58:53 -0700223 } else if (name != null) {
224 appId = service.getAppId(name);
225 }
Jian Lie1c1c8d2016-05-09 16:24:40 -0700226 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 Vachuska02aeb032015-01-06 22:36:30 -0800245 private Response response(ApplicationAdminService service, ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700246 Application app = nullIsNotFound(service.getApplication(appId), APP_NOT_FOUND);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800247 return ok(codec(Application.class).encode(app, this)).build();
248 }
249
Jian Lie1c1c8d2016-05-09 16:24:40 -0700250 private Response response(ApplicationId appId) {
Jian Licbf49892016-05-10 14:54:44 -0700251 ApplicationId checkedAppId = nullIsNotFound(appId, APP_ID_NOT_FOUND);
252 return ok(codec(ApplicationId.class).encode(checkedAppId, this)).build();
Jian Lie1c1c8d2016-05-09 16:24:40 -0700253 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800254}