blob: a6297a65241917af06ed43280846dc0cf31444a4 [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 */
16package org.onosproject.app.impl;
17
Madan Jampani015d3a32016-06-16 18:25:11 -070018import com.google.common.cache.Cache;
19import com.google.common.cache.CacheBuilder;
Jonathan Hartc32585f2016-06-16 14:01:27 -070020import com.google.common.collect.HashMultimap;
21import com.google.common.collect.Multimap;
Madan Jampani015d3a32016-06-16 18:25:11 -070022import com.google.common.util.concurrent.Uninterruptibles;
23
Thomas Vachuska02aeb032015-01-06 22:36:30 -080024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.apache.felix.scr.annotations.Service;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080030import org.apache.karaf.features.Feature;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080031import org.apache.karaf.features.FeaturesService;
32import org.onosproject.app.ApplicationAdminService;
33import org.onosproject.app.ApplicationEvent;
34import org.onosproject.app.ApplicationListener;
35import org.onosproject.app.ApplicationService;
36import org.onosproject.app.ApplicationState;
37import org.onosproject.app.ApplicationStore;
38import org.onosproject.app.ApplicationStoreDelegate;
39import org.onosproject.core.Application;
40import org.onosproject.core.ApplicationId;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080041import org.onosproject.event.AbstractListenerManager;
Changhoon Yoonb856b812015-08-10 03:47:19 +090042import org.onosproject.security.Permission;
43import org.onosproject.security.SecurityUtil;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080044import org.slf4j.Logger;
45
Jonathan Hartc32585f2016-06-16 14:01:27 -070046import java.io.InputStream;
47import java.util.Set;
Madan Jampani015d3a32016-06-16 18:25:11 -070048import java.util.concurrent.CountDownLatch;
49import java.util.concurrent.TimeUnit;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080050
51import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkey9f87e512016-01-05 10:00:22 -080052import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED;
53import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED;
54import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED;
55import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090056import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080057import static org.onosproject.security.AppPermission.Type.APP_READ;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080058import static org.slf4j.LoggerFactory.getLogger;
59
60/**
61 * Implementation of the application management service.
62 */
63@Component(immediate = true)
64@Service
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070065public class ApplicationManager
66 extends AbstractListenerManager<ApplicationEvent, ApplicationListener>
67 implements ApplicationService, ApplicationAdminService {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080068
69 private final Logger log = getLogger(getClass());
70
71 private static final String APP_ID_NULL = "Application ID cannot be null";
Madan Jampani015d3a32016-06-16 18:25:11 -070072 private static final long DEFAULT_OPERATION_TIMEOUT_MILLIS = 2000;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080073
Thomas Vachuska02aeb032015-01-06 22:36:30 -080074 private final ApplicationStoreDelegate delegate = new InternalStoreDelegate();
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected ApplicationStore store;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected FeaturesService featuresService;
81
Thomas Vachuska62f04a42015-04-22 14:38:34 -070082 private boolean initializing;
83
Thomas Vachuskac65dd712015-11-04 17:19:10 -080084 // Application supplied hooks for pre-activation processing.
Jonathan Hartc32585f2016-06-16 14:01:27 -070085 private final Multimap<String, Runnable> deactivateHooks = HashMultimap.create();
Madan Jampani015d3a32016-06-16 18:25:11 -070086 private final Cache<ApplicationId, CountDownLatch> pendingUninstalls =
87 CacheBuilder.newBuilder()
88 .expireAfterWrite(DEFAULT_OPERATION_TIMEOUT_MILLIS * 2, TimeUnit.MILLISECONDS)
89 .build();
Thomas Vachuskac65dd712015-11-04 17:19:10 -080090
Thomas Vachuska02aeb032015-01-06 22:36:30 -080091 @Activate
92 public void activate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080093 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070094
95 initializing = true;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070096 store.setDelegate(delegate);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070097 initializing = false;
98
Thomas Vachuska02aeb032015-01-06 22:36:30 -080099 log.info("Started");
100 }
101
102 @Deactivate
103 public void deactivate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800104 eventDispatcher.removeSink(ApplicationEvent.class);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -0700105 store.unsetDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800106 log.info("Stopped");
107 }
108
109 @Override
110 public Set<Application> getApplications() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900111 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800112 return store.getApplications();
113 }
114
115 @Override
116 public ApplicationId getId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900117 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800118 checkNotNull(name, "Name cannot be null");
119 return store.getId(name);
120 }
121
122 @Override
123 public Application getApplication(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.getApplication(appId);
127 }
128
129 @Override
130 public ApplicationState getState(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900131 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800132 checkNotNull(appId, APP_ID_NULL);
133 return store.getState(appId);
134 }
135
136 @Override
137 public Set<Permission> getPermissions(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900138 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800139 checkNotNull(appId, APP_ID_NULL);
140 return store.getPermissions(appId);
141 }
142
143 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800144 public void registerDeactivateHook(ApplicationId appId, Runnable hook) {
145 checkPermission(APP_READ);
146 checkNotNull(appId, APP_ID_NULL);
147 checkNotNull(hook, "Hook cannot be null");
148 deactivateHooks.put(appId.name(), hook);
149 }
150
151 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800152 public Application install(InputStream appDescStream) {
153 checkNotNull(appDescStream, "Application archive stream cannot be null");
Changhoon Yoonb856b812015-08-10 03:47:19 +0900154 Application app = store.create(appDescStream);
155 SecurityUtil.register(app.id());
156 return app;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800157 }
158
159 @Override
160 public void uninstall(ApplicationId appId) {
161 checkNotNull(appId, APP_ID_NULL);
Madan Jampani015d3a32016-06-16 18:25:11 -0700162 CountDownLatch latch = new CountDownLatch(1);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800163 try {
Madan Jampani015d3a32016-06-16 18:25:11 -0700164 pendingUninstalls.put(appId, latch);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800165 store.remove(appId);
166 } catch (Exception e) {
Madan Jampani015d3a32016-06-16 18:25:11 -0700167 pendingUninstalls.invalidate(appId);
168 latch.countDown();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800169 log.warn("Unable to purge application directory for {}", appId.name());
170 }
Madan Jampani015d3a32016-06-16 18:25:11 -0700171 Uninterruptibles.awaitUninterruptibly(latch, DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800172 }
173
174 @Override
175 public void activate(ApplicationId appId) {
176 checkNotNull(appId, APP_ID_NULL);
Changhoon Yoonb856b812015-08-10 03:47:19 +0900177 if (!SecurityUtil.isAppSecured(appId)) {
178 return;
179 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800180 store.activate(appId);
181 }
182
183 @Override
184 public void deactivate(ApplicationId appId) {
185 checkNotNull(appId, APP_ID_NULL);
186 store.deactivate(appId);
187 }
188
189 @Override
190 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
191 checkNotNull(appId, APP_ID_NULL);
192 checkNotNull(permissions, "Permissions cannot be null");
193 store.setPermissions(appId, permissions);
194 }
195
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800196 private class InternalStoreDelegate implements ApplicationStoreDelegate {
197 @Override
198 public void notify(ApplicationEvent event) {
199 ApplicationEvent.Type type = event.type();
200 Application app = event.subject();
Madan Jampani015d3a32016-06-16 18:25:11 -0700201 CountDownLatch latch = pendingUninstalls.getIfPresent(app.id());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800202 try {
203 if (type == APP_ACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700204 if (installAppFeatures(app)) {
205 log.info("Application {} has been activated", app.id().name());
206 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800207
208 } else if (type == APP_DEACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700209 if (uninstallAppFeatures(app)) {
210 log.info("Application {} has been deactivated", app.id().name());
211 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800212
213 } else if (type == APP_INSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700214 if (installAppArtifacts(app)) {
215 log.info("Application {} has been installed", app.id().name());
216 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800217
218 } else if (type == APP_UNINSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700219 if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) {
220 log.info("Application {} has been uninstalled", app.id().name());
221 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800222
223 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700224 post(event);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800225 } catch (Exception e) {
226 log.warn("Unable to perform operation on application " + app.id().name(), e);
Madan Jampani015d3a32016-06-16 18:25:11 -0700227 } finally {
228 if (latch != null) {
229 latch.countDown();
230 pendingUninstalls.invalidate(app.id());
231 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800232 }
233 }
234 }
235
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700236 // The following methods are fully synchronized to guard against remote vs.
237 // locally induced feature service interactions.
238
Thomas Vachuska761f0042015-11-11 19:10:17 -0800239 // Installs all feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700240 private synchronized boolean installAppArtifacts(Application app) throws Exception {
241 if (app.featuresRepo().isPresent() &&
242 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800243 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700244 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800245 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700246 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800247 }
248
Thomas Vachuska761f0042015-11-11 19:10:17 -0800249 // Uninstalls all the feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700250 private synchronized boolean uninstallAppArtifacts(Application app) throws Exception {
251 if (app.featuresRepo().isPresent() &&
252 featuresService.getRepository(app.featuresRepo().get()) != null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800253 featuresService.removeRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700254 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800255 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700256 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800257 }
258
Thomas Vachuska761f0042015-11-11 19:10:17 -0800259 // Installs all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700260 private synchronized boolean installAppFeatures(Application app) throws Exception {
261 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800262 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800263 Feature feature = featuresService.getFeature(name);
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800264
265 // If we see an attempt at activation of a non-existent feature
266 // attempt to install the app artifacts first and then retry.
267 // This can be triggered by a race condition between different ONOS
268 // instances "installing" the apps from disk at their own pace.
269 // Perhaps there is a more elegant solution to be explored in the
270 // future.
271 if (feature == null) {
272 installAppArtifacts(app);
273 feature = featuresService.getFeature(name);
274 }
275
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700276 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800277 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700278 changed = true;
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800279 } else if (feature == null) {
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700280 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800281 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800282 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700283 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800284 }
285
Thomas Vachuska761f0042015-11-11 19:10:17 -0800286 // Uninstalls all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700287 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
288 boolean changed = false;
Jonathan Hartc32585f2016-06-16 14:01:27 -0700289 deactivateHooks.removeAll(app.id().name()).forEach(hook -> invokeHook(hook, app.id()));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800290 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800291 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700292 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800293 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700294 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700295 } else if (feature == null) {
296 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800297 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800298 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700299 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800300 }
301
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800302 // Invokes the specified function, if not null.
Ray Milkeyaef45852016-01-11 17:13:19 -0800303 @java.lang.SuppressWarnings("squid:S1217") // We really do mean to call run()
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800304 private void invokeHook(Runnable hook, ApplicationId appId) {
305 if (hook != null) {
306 try {
307 hook.run();
308 } catch (Exception e) {
309 log.warn("Deactivate hook for application {} encountered an error",
310 appId.name(), e);
311 }
312 }
313 }
314
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800315}