blob: c82c77569f75b41519aaa23b65f4c74fc189d4f6 [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 Jampania0c4bdb2016-06-17 11:02:38 -0700163 updateStoreAndWaitForNotificationHandling(appId, store::remove);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800164 }
165
166 @Override
167 public void activate(ApplicationId appId) {
168 checkNotNull(appId, APP_ID_NULL);
Changhoon Yoonb856b812015-08-10 03:47:19 +0900169 if (!SecurityUtil.isAppSecured(appId)) {
170 return;
171 }
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700172 updateStoreAndWaitForNotificationHandling(appId, store::activate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800173 }
174
175 @Override
176 public void deactivate(ApplicationId appId) {
177 checkNotNull(appId, APP_ID_NULL);
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700178 updateStoreAndWaitForNotificationHandling(appId, store::deactivate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800179 }
180
181 @Override
182 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
183 checkNotNull(appId, APP_ID_NULL);
184 checkNotNull(permissions, "Permissions cannot be null");
185 store.setPermissions(appId, permissions);
186 }
187
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700188 private void updateStoreAndWaitForNotificationHandling(ApplicationId appId,
189 Consumer<ApplicationId> storeUpdateTask) {
190 CountDownLatch latch = new CountDownLatch(1);
191 try {
192 pendingOperations.put(appId, latch);
193 storeUpdateTask.accept(appId);
194 } catch (Exception e) {
195 pendingOperations.invalidate(appId);
196 latch.countDown();
197 log.warn("Failed to update store for {}", appId.name(), e);
198 }
199 Uninterruptibles.awaitUninterruptibly(latch, DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
200 }
201
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800202 private class InternalStoreDelegate implements ApplicationStoreDelegate {
203 @Override
204 public void notify(ApplicationEvent event) {
205 ApplicationEvent.Type type = event.type();
206 Application app = event.subject();
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700207 CountDownLatch latch = pendingOperations.getIfPresent(app.id());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800208 try {
209 if (type == APP_ACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700210 if (installAppFeatures(app)) {
211 log.info("Application {} has been activated", app.id().name());
212 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800213
214 } else if (type == APP_DEACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700215 if (uninstallAppFeatures(app)) {
216 log.info("Application {} has been deactivated", app.id().name());
217 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800218
219 } else if (type == APP_INSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700220 if (installAppArtifacts(app)) {
221 log.info("Application {} has been installed", app.id().name());
222 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800223
224 } else if (type == APP_UNINSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700225 if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) {
226 log.info("Application {} has been uninstalled", app.id().name());
227 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800228
229 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700230 post(event);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800231 } catch (Exception e) {
232 log.warn("Unable to perform operation on application " + app.id().name(), e);
Madan Jampani015d3a32016-06-16 18:25:11 -0700233 } finally {
234 if (latch != null) {
235 latch.countDown();
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700236 pendingOperations.invalidate(app.id());
Madan Jampani015d3a32016-06-16 18:25:11 -0700237 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800238 }
239 }
240 }
241
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700242 // The following methods are fully synchronized to guard against remote vs.
243 // locally induced feature service interactions.
244
Thomas Vachuska761f0042015-11-11 19:10:17 -0800245 // Installs all feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700246 private synchronized boolean installAppArtifacts(Application app) throws Exception {
247 if (app.featuresRepo().isPresent() &&
248 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800249 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700250 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800251 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700252 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800253 }
254
Thomas Vachuska761f0042015-11-11 19:10:17 -0800255 // Uninstalls all the feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700256 private synchronized boolean uninstallAppArtifacts(Application app) throws Exception {
257 if (app.featuresRepo().isPresent() &&
258 featuresService.getRepository(app.featuresRepo().get()) != null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800259 featuresService.removeRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700260 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800261 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700262 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800263 }
264
Thomas Vachuska761f0042015-11-11 19:10:17 -0800265 // Installs all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700266 private synchronized boolean installAppFeatures(Application app) throws Exception {
267 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800268 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800269 Feature feature = featuresService.getFeature(name);
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800270
271 // If we see an attempt at activation of a non-existent feature
272 // attempt to install the app artifacts first and then retry.
273 // This can be triggered by a race condition between different ONOS
274 // instances "installing" the apps from disk at their own pace.
275 // Perhaps there is a more elegant solution to be explored in the
276 // future.
277 if (feature == null) {
278 installAppArtifacts(app);
279 feature = featuresService.getFeature(name);
280 }
281
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700282 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800283 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700284 changed = true;
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800285 } else if (feature == null) {
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700286 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800287 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800288 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700289 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800290 }
291
Thomas Vachuska761f0042015-11-11 19:10:17 -0800292 // Uninstalls all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700293 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
294 boolean changed = false;
Jonathan Hartc32585f2016-06-16 14:01:27 -0700295 deactivateHooks.removeAll(app.id().name()).forEach(hook -> invokeHook(hook, app.id()));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800296 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800297 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700298 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800299 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700300 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700301 } else if (feature == null) {
302 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800303 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800304 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700305 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800306 }
307
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800308 // Invokes the specified function, if not null.
Ray Milkeyaef45852016-01-11 17:13:19 -0800309 @java.lang.SuppressWarnings("squid:S1217") // We really do mean to call run()
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800310 private void invokeHook(Runnable hook, ApplicationId appId) {
311 if (hook != null) {
312 try {
313 hook.run();
314 } catch (Exception e) {
315 log.warn("Deactivate hook for application {} encountered an error",
316 appId.name(), e);
317 }
318 }
319 }
320
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800321}