blob: 7c46c0bb01539d3764cf1023a0712207e013cb7c [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 Vachuskac65dd712015-11-04 17:19:10 -080083 // Application supplied hooks for pre-activation processing.
Jonathan Hartc32585f2016-06-16 14:01:27 -070084 private final Multimap<String, Runnable> deactivateHooks = HashMultimap.create();
Madan Jampania0c4bdb2016-06-17 11:02:38 -070085 private final Cache<ApplicationId, CountDownLatch> pendingOperations =
Madan Jampani015d3a32016-06-16 18:25:11 -070086 CacheBuilder.newBuilder()
87 .expireAfterWrite(DEFAULT_OPERATION_TIMEOUT_MILLIS * 2, TimeUnit.MILLISECONDS)
88 .build();
Thomas Vachuskac65dd712015-11-04 17:19:10 -080089
Thomas Vachuska02aeb032015-01-06 22:36:30 -080090 @Activate
91 public void activate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080092 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070093 store.setDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080094 log.info("Started");
95 }
96
97 @Deactivate
98 public void deactivate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080099 eventDispatcher.removeSink(ApplicationEvent.class);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -0700100 store.unsetDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800101 log.info("Stopped");
102 }
103
104 @Override
105 public Set<Application> getApplications() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900106 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800107 return store.getApplications();
108 }
109
110 @Override
111 public ApplicationId getId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900112 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800113 checkNotNull(name, "Name cannot be null");
114 return store.getId(name);
115 }
116
117 @Override
118 public Application getApplication(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900119 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800120 checkNotNull(appId, APP_ID_NULL);
121 return store.getApplication(appId);
122 }
123
124 @Override
125 public ApplicationState getState(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900126 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800127 checkNotNull(appId, APP_ID_NULL);
128 return store.getState(appId);
129 }
130
131 @Override
132 public Set<Permission> getPermissions(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900133 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800134 checkNotNull(appId, APP_ID_NULL);
135 return store.getPermissions(appId);
136 }
137
138 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800139 public void registerDeactivateHook(ApplicationId appId, Runnable hook) {
140 checkPermission(APP_READ);
141 checkNotNull(appId, APP_ID_NULL);
142 checkNotNull(hook, "Hook cannot be null");
143 deactivateHooks.put(appId.name(), hook);
144 }
145
146 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800147 public Application install(InputStream appDescStream) {
148 checkNotNull(appDescStream, "Application archive stream cannot be null");
Changhoon Yoonb856b812015-08-10 03:47:19 +0900149 Application app = store.create(appDescStream);
150 SecurityUtil.register(app.id());
151 return app;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800152 }
153
154 @Override
155 public void uninstall(ApplicationId appId) {
156 checkNotNull(appId, APP_ID_NULL);
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700157 updateStoreAndWaitForNotificationHandling(appId, store::remove);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800158 }
159
160 @Override
161 public void activate(ApplicationId appId) {
162 checkNotNull(appId, APP_ID_NULL);
Changhoon Yoonb856b812015-08-10 03:47:19 +0900163 if (!SecurityUtil.isAppSecured(appId)) {
164 return;
165 }
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700166 updateStoreAndWaitForNotificationHandling(appId, store::activate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800167 }
168
169 @Override
170 public void deactivate(ApplicationId appId) {
171 checkNotNull(appId, APP_ID_NULL);
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700172 updateStoreAndWaitForNotificationHandling(appId, store::deactivate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800173 }
174
175 @Override
176 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
177 checkNotNull(appId, APP_ID_NULL);
178 checkNotNull(permissions, "Permissions cannot be null");
179 store.setPermissions(appId, permissions);
180 }
181
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700182 private void updateStoreAndWaitForNotificationHandling(ApplicationId appId,
183 Consumer<ApplicationId> storeUpdateTask) {
184 CountDownLatch latch = new CountDownLatch(1);
185 try {
186 pendingOperations.put(appId, latch);
187 storeUpdateTask.accept(appId);
188 } catch (Exception e) {
189 pendingOperations.invalidate(appId);
190 latch.countDown();
191 log.warn("Failed to update store for {}", appId.name(), e);
192 }
193 Uninterruptibles.awaitUninterruptibly(latch, DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
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 Jampania0c4bdb2016-06-17 11:02:38 -0700201 CountDownLatch latch = pendingOperations.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();
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700230 pendingOperations.invalidate(app.id());
Madan Jampani015d3a32016-06-16 18:25:11 -0700231 }
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}