blob: 6c39b6aa4c719a1110c0bdb0ec7dd151f83f0f99 [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;
Madan Jampania0c4bdb2016-06-17 11:02:38 -070050import java.util.function.Consumer;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080051
52import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkey9f87e512016-01-05 10:00:22 -080053import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED;
54import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED;
55import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED;
56import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090057import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080058import static org.onosproject.security.AppPermission.Type.APP_READ;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080059import static org.slf4j.LoggerFactory.getLogger;
60
61/**
62 * Implementation of the application management service.
63 */
64@Component(immediate = true)
65@Service
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070066public class ApplicationManager
67 extends AbstractListenerManager<ApplicationEvent, ApplicationListener>
68 implements ApplicationService, ApplicationAdminService {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080069
70 private final Logger log = getLogger(getClass());
71
72 private static final String APP_ID_NULL = "Application ID cannot be null";
Madan Jampani015d3a32016-06-16 18:25:11 -070073 private static final long DEFAULT_OPERATION_TIMEOUT_MILLIS = 2000;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080074
Thomas Vachuska02aeb032015-01-06 22:36:30 -080075 private final ApplicationStoreDelegate delegate = new InternalStoreDelegate();
76
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected ApplicationStore store;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected FeaturesService featuresService;
82
Thomas Vachuska62f04a42015-04-22 14:38:34 -070083 private boolean initializing;
84
Thomas Vachuskac65dd712015-11-04 17:19:10 -080085 // Application supplied hooks for pre-activation processing.
Jonathan Hartc32585f2016-06-16 14:01:27 -070086 private final Multimap<String, Runnable> deactivateHooks = HashMultimap.create();
Madan Jampania0c4bdb2016-06-17 11:02:38 -070087 private final Cache<ApplicationId, CountDownLatch> pendingOperations =
Madan Jampani015d3a32016-06-16 18:25:11 -070088 CacheBuilder.newBuilder()
89 .expireAfterWrite(DEFAULT_OPERATION_TIMEOUT_MILLIS * 2, TimeUnit.MILLISECONDS)
90 .build();
Thomas Vachuskac65dd712015-11-04 17:19:10 -080091
Thomas Vachuska02aeb032015-01-06 22:36:30 -080092 @Activate
93 public void activate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080094 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070095
96 initializing = true;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070097 store.setDelegate(delegate);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070098 initializing = false;
99
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800100 log.info("Started");
101 }
102
103 @Deactivate
104 public void deactivate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800105 eventDispatcher.removeSink(ApplicationEvent.class);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -0700106 store.unsetDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800107 log.info("Stopped");
108 }
109
110 @Override
111 public Set<Application> getApplications() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900112 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800113 return store.getApplications();
114 }
115
116 @Override
117 public ApplicationId getId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900118 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800119 checkNotNull(name, "Name cannot be null");
120 return store.getId(name);
121 }
122
123 @Override
124 public Application getApplication(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900125 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800126 checkNotNull(appId, APP_ID_NULL);
127 return store.getApplication(appId);
128 }
129
130 @Override
131 public ApplicationState getState(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900132 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800133 checkNotNull(appId, APP_ID_NULL);
134 return store.getState(appId);
135 }
136
137 @Override
138 public Set<Permission> getPermissions(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900139 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800140 checkNotNull(appId, APP_ID_NULL);
141 return store.getPermissions(appId);
142 }
143
144 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800145 public void registerDeactivateHook(ApplicationId appId, Runnable hook) {
146 checkPermission(APP_READ);
147 checkNotNull(appId, APP_ID_NULL);
148 checkNotNull(hook, "Hook cannot be null");
149 deactivateHooks.put(appId.name(), hook);
150 }
151
152 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800153 public Application install(InputStream appDescStream) {
154 checkNotNull(appDescStream, "Application archive stream cannot be null");
Changhoon Yoonb856b812015-08-10 03:47:19 +0900155 Application app = store.create(appDescStream);
156 SecurityUtil.register(app.id());
157 return app;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800158 }
159
160 @Override
161 public void uninstall(ApplicationId appId) {
162 checkNotNull(appId, APP_ID_NULL);
Madan Jampani015d3a32016-06-16 18:25:11 -0700163 CountDownLatch latch = new CountDownLatch(1);
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700164 updateStoreAndWaitForNotificationHandling(appId, store::remove);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800165 }
166
167 @Override
168 public void activate(ApplicationId appId) {
169 checkNotNull(appId, APP_ID_NULL);
Changhoon Yoonb856b812015-08-10 03:47:19 +0900170 if (!SecurityUtil.isAppSecured(appId)) {
171 return;
172 }
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700173 updateStoreAndWaitForNotificationHandling(appId, store::activate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800174 }
175
176 @Override
177 public void deactivate(ApplicationId appId) {
178 checkNotNull(appId, APP_ID_NULL);
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700179 updateStoreAndWaitForNotificationHandling(appId, store::deactivate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800180 }
181
182 @Override
183 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
184 checkNotNull(appId, APP_ID_NULL);
185 checkNotNull(permissions, "Permissions cannot be null");
186 store.setPermissions(appId, permissions);
187 }
188
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700189 private void updateStoreAndWaitForNotificationHandling(ApplicationId appId,
190 Consumer<ApplicationId> storeUpdateTask) {
191 CountDownLatch latch = new CountDownLatch(1);
192 try {
193 pendingOperations.put(appId, latch);
194 storeUpdateTask.accept(appId);
195 } catch (Exception e) {
196 pendingOperations.invalidate(appId);
197 latch.countDown();
198 log.warn("Failed to update store for {}", appId.name(), e);
199 }
200 Uninterruptibles.awaitUninterruptibly(latch, DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
201 }
202
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800203 private class InternalStoreDelegate implements ApplicationStoreDelegate {
204 @Override
205 public void notify(ApplicationEvent event) {
206 ApplicationEvent.Type type = event.type();
207 Application app = event.subject();
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700208 CountDownLatch latch = pendingOperations.getIfPresent(app.id());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800209 try {
210 if (type == APP_ACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700211 if (installAppFeatures(app)) {
212 log.info("Application {} has been activated", app.id().name());
213 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800214
215 } else if (type == APP_DEACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700216 if (uninstallAppFeatures(app)) {
217 log.info("Application {} has been deactivated", app.id().name());
218 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800219
220 } else if (type == APP_INSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700221 if (installAppArtifacts(app)) {
222 log.info("Application {} has been installed", app.id().name());
223 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800224
225 } else if (type == APP_UNINSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700226 if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) {
227 log.info("Application {} has been uninstalled", app.id().name());
228 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800229
230 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700231 post(event);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800232 } catch (Exception e) {
233 log.warn("Unable to perform operation on application " + app.id().name(), e);
Madan Jampani015d3a32016-06-16 18:25:11 -0700234 } finally {
235 if (latch != null) {
236 latch.countDown();
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700237 pendingOperations.invalidate(app.id());
Madan Jampani015d3a32016-06-16 18:25:11 -0700238 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800239 }
240 }
241 }
242
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700243 // The following methods are fully synchronized to guard against remote vs.
244 // locally induced feature service interactions.
245
Thomas Vachuska761f0042015-11-11 19:10:17 -0800246 // Installs all feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700247 private synchronized boolean installAppArtifacts(Application app) throws Exception {
248 if (app.featuresRepo().isPresent() &&
249 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800250 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700251 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800252 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700253 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800254 }
255
Thomas Vachuska761f0042015-11-11 19:10:17 -0800256 // Uninstalls all the feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700257 private synchronized boolean uninstallAppArtifacts(Application app) throws Exception {
258 if (app.featuresRepo().isPresent() &&
259 featuresService.getRepository(app.featuresRepo().get()) != null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800260 featuresService.removeRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700261 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800262 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700263 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800264 }
265
Thomas Vachuska761f0042015-11-11 19:10:17 -0800266 // Installs all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700267 private synchronized boolean installAppFeatures(Application app) throws Exception {
268 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800269 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800270 Feature feature = featuresService.getFeature(name);
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800271
272 // If we see an attempt at activation of a non-existent feature
273 // attempt to install the app artifacts first and then retry.
274 // This can be triggered by a race condition between different ONOS
275 // instances "installing" the apps from disk at their own pace.
276 // Perhaps there is a more elegant solution to be explored in the
277 // future.
278 if (feature == null) {
279 installAppArtifacts(app);
280 feature = featuresService.getFeature(name);
281 }
282
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700283 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800284 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700285 changed = true;
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800286 } else if (feature == null) {
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700287 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800288 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800289 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700290 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800291 }
292
Thomas Vachuska761f0042015-11-11 19:10:17 -0800293 // Uninstalls all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700294 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
295 boolean changed = false;
Jonathan Hartc32585f2016-06-16 14:01:27 -0700296 deactivateHooks.removeAll(app.id().name()).forEach(hook -> invokeHook(hook, app.id()));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800297 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800298 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700299 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800300 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700301 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700302 } else if (feature == null) {
303 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800304 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800305 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700306 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800307 }
308
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800309 // Invokes the specified function, if not null.
Ray Milkeyaef45852016-01-11 17:13:19 -0800310 @java.lang.SuppressWarnings("squid:S1217") // We really do mean to call run()
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800311 private void invokeHook(Runnable hook, ApplicationId appId) {
312 if (hook != null) {
313 try {
314 hook.run();
315 } catch (Exception e) {
316 log.warn("Deactivate hook for application {} encountered an error",
317 appId.name(), e);
318 }
319 }
320 }
321
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800322}