blob: 80a5ca779fde38ecb8f4530f9912b6af161c32d5 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.app.impl;
17
Thomas Vachuskac65dd712015-11-04 17:19:10 -080018import com.google.common.collect.Maps;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080025import org.apache.karaf.features.Feature;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080026import org.apache.karaf.features.FeaturesService;
27import org.onosproject.app.ApplicationAdminService;
28import org.onosproject.app.ApplicationEvent;
29import org.onosproject.app.ApplicationListener;
30import org.onosproject.app.ApplicationService;
31import org.onosproject.app.ApplicationState;
32import org.onosproject.app.ApplicationStore;
33import org.onosproject.app.ApplicationStoreDelegate;
34import org.onosproject.core.Application;
35import org.onosproject.core.ApplicationId;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080036import org.onosproject.event.AbstractListenerManager;
Changhoon Yoonb856b812015-08-10 03:47:19 +090037import org.onosproject.security.Permission;
38import org.onosproject.security.SecurityUtil;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080039import org.slf4j.Logger;
40
41import java.io.InputStream;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080042import java.util.Map;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080043import java.util.Set;
44
45import static com.google.common.base.Preconditions.checkNotNull;
46import static org.onosproject.app.ApplicationEvent.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090047import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080048import static org.onosproject.security.AppPermission.Type.APP_READ;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080049import static org.slf4j.LoggerFactory.getLogger;
50
51/**
52 * Implementation of the application management service.
53 */
54@Component(immediate = true)
55@Service
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070056public class ApplicationManager
57 extends AbstractListenerManager<ApplicationEvent, ApplicationListener>
58 implements ApplicationService, ApplicationAdminService {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080059
60 private final Logger log = getLogger(getClass());
61
62 private static final String APP_ID_NULL = "Application ID cannot be null";
63
Thomas Vachuska02aeb032015-01-06 22:36:30 -080064 private final ApplicationStoreDelegate delegate = new InternalStoreDelegate();
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected ApplicationStore store;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected FeaturesService featuresService;
71
Thomas Vachuska62f04a42015-04-22 14:38:34 -070072 private boolean initializing;
73
Thomas Vachuskac65dd712015-11-04 17:19:10 -080074 // Application supplied hooks for pre-activation processing.
75 private final Map<String, Runnable> deactivateHooks = Maps.newConcurrentMap();
76
Thomas Vachuska02aeb032015-01-06 22:36:30 -080077 @Activate
78 public void activate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080079 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070080
81 initializing = true;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070082 store.setDelegate(delegate);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070083 initializing = false;
84
Thomas Vachuska02aeb032015-01-06 22:36:30 -080085 log.info("Started");
86 }
87
88 @Deactivate
89 public void deactivate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080090 eventDispatcher.removeSink(ApplicationEvent.class);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070091 store.unsetDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080092 log.info("Stopped");
93 }
94
95 @Override
96 public Set<Application> getApplications() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090097 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080098 return store.getApplications();
99 }
100
101 @Override
102 public ApplicationId getId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900103 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800104 checkNotNull(name, "Name cannot be null");
105 return store.getId(name);
106 }
107
108 @Override
109 public Application getApplication(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900110 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800111 checkNotNull(appId, APP_ID_NULL);
112 return store.getApplication(appId);
113 }
114
115 @Override
116 public ApplicationState getState(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900117 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800118 checkNotNull(appId, APP_ID_NULL);
119 return store.getState(appId);
120 }
121
122 @Override
123 public Set<Permission> getPermissions(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900124 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800125 checkNotNull(appId, APP_ID_NULL);
126 return store.getPermissions(appId);
127 }
128
129 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800130 public void registerDeactivateHook(ApplicationId appId, Runnable hook) {
131 checkPermission(APP_READ);
132 checkNotNull(appId, APP_ID_NULL);
133 checkNotNull(hook, "Hook cannot be null");
134 deactivateHooks.put(appId.name(), hook);
135 }
136
137 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800138 public Application install(InputStream appDescStream) {
139 checkNotNull(appDescStream, "Application archive stream cannot be null");
Changhoon Yoonb856b812015-08-10 03:47:19 +0900140 Application app = store.create(appDescStream);
141 SecurityUtil.register(app.id());
142 return app;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800143 }
144
145 @Override
146 public void uninstall(ApplicationId appId) {
147 checkNotNull(appId, APP_ID_NULL);
148 try {
149 store.remove(appId);
150 } catch (Exception e) {
151 log.warn("Unable to purge application directory for {}", appId.name());
152 }
153 }
154
155 @Override
156 public void activate(ApplicationId appId) {
157 checkNotNull(appId, APP_ID_NULL);
Changhoon Yoonb856b812015-08-10 03:47:19 +0900158 if (!SecurityUtil.isAppSecured(appId)) {
159 return;
160 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800161 store.activate(appId);
162 }
163
164 @Override
165 public void deactivate(ApplicationId appId) {
166 checkNotNull(appId, APP_ID_NULL);
167 store.deactivate(appId);
168 }
169
170 @Override
171 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
172 checkNotNull(appId, APP_ID_NULL);
173 checkNotNull(permissions, "Permissions cannot be null");
174 store.setPermissions(appId, permissions);
175 }
176
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800177 private class InternalStoreDelegate implements ApplicationStoreDelegate {
178 @Override
179 public void notify(ApplicationEvent event) {
180 ApplicationEvent.Type type = event.type();
181 Application app = event.subject();
182 try {
183 if (type == APP_ACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700184 if (installAppFeatures(app)) {
185 log.info("Application {} has been activated", app.id().name());
186 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800187
188 } else if (type == APP_DEACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700189 if (uninstallAppFeatures(app)) {
190 log.info("Application {} has been deactivated", app.id().name());
191 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800192
193 } else if (type == APP_INSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700194 if (installAppArtifacts(app)) {
195 log.info("Application {} has been installed", app.id().name());
196 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800197
198 } else if (type == APP_UNINSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700199 if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) {
200 log.info("Application {} has been uninstalled", app.id().name());
201 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800202
203 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700204 post(event);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800205
206 } catch (Exception e) {
207 log.warn("Unable to perform operation on application " + app.id().name(), e);
208 }
209 }
210 }
211
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700212 // The following methods are fully synchronized to guard against remote vs.
213 // locally induced feature service interactions.
214
Thomas Vachuska761f0042015-11-11 19:10:17 -0800215 // Installs all feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700216 private synchronized boolean installAppArtifacts(Application app) throws Exception {
217 if (app.featuresRepo().isPresent() &&
218 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800219 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700220 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800221 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700222 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800223 }
224
Thomas Vachuska761f0042015-11-11 19:10:17 -0800225 // Uninstalls all the feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700226 private synchronized boolean uninstallAppArtifacts(Application app) throws Exception {
227 if (app.featuresRepo().isPresent() &&
228 featuresService.getRepository(app.featuresRepo().get()) != null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800229 featuresService.removeRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700230 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800231 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700232 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800233 }
234
Thomas Vachuska761f0042015-11-11 19:10:17 -0800235 // Installs all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700236 private synchronized boolean installAppFeatures(Application app) throws Exception {
237 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800238 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800239 Feature feature = featuresService.getFeature(name);
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800240
241 // If we see an attempt at activation of a non-existent feature
242 // attempt to install the app artifacts first and then retry.
243 // This can be triggered by a race condition between different ONOS
244 // instances "installing" the apps from disk at their own pace.
245 // Perhaps there is a more elegant solution to be explored in the
246 // future.
247 if (feature == null) {
248 installAppArtifacts(app);
249 feature = featuresService.getFeature(name);
250 }
251
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700252 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800253 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700254 changed = true;
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800255 } else if (feature == null) {
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700256 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800257 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800258 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700259 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800260 }
261
Thomas Vachuska761f0042015-11-11 19:10:17 -0800262 // Uninstalls all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700263 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
264 boolean changed = false;
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800265 invokeHook(deactivateHooks.get(app.id().name()), app.id());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800266 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800267 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700268 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800269 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700270 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700271 } else if (feature == null) {
272 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800273 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800274 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700275 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800276 }
277
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800278 // Invokes the specified function, if not null.
279 private void invokeHook(Runnable hook, ApplicationId appId) {
280 if (hook != null) {
281 try {
282 hook.run();
283 } catch (Exception e) {
284 log.warn("Deactivate hook for application {} encountered an error",
285 appId.name(), e);
286 }
287 }
288 }
289
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800290}