blob: 2bb9c117b0369d686182c1d16b19bcb085676c02 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080023import org.apache.karaf.features.Feature;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080024import org.apache.karaf.features.FeaturesService;
25import org.onosproject.app.ApplicationAdminService;
26import org.onosproject.app.ApplicationEvent;
27import org.onosproject.app.ApplicationListener;
28import org.onosproject.app.ApplicationService;
29import org.onosproject.app.ApplicationState;
30import org.onosproject.app.ApplicationStore;
31import org.onosproject.app.ApplicationStoreDelegate;
32import org.onosproject.core.Application;
33import org.onosproject.core.ApplicationId;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080034import org.onosproject.event.AbstractListenerManager;
Changhoon Yoonb856b812015-08-10 03:47:19 +090035import org.onosproject.security.Permission;
36import org.onosproject.security.SecurityUtil;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037import org.osgi.service.component.annotations.Activate;
38import org.osgi.service.component.annotations.Component;
39import org.osgi.service.component.annotations.Deactivate;
40import org.osgi.service.component.annotations.Reference;
41import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080042import org.slf4j.Logger;
43
Jonathan Hartc32585f2016-06-16 14:01:27 -070044import java.io.InputStream;
45import java.util.Set;
Madan Jampani015d3a32016-06-16 18:25:11 -070046import java.util.concurrent.CountDownLatch;
47import java.util.concurrent.TimeUnit;
Madan Jampania0c4bdb2016-06-17 11:02:38 -070048import java.util.function.Consumer;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080049
50import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkey9f87e512016-01-05 10:00:22 -080051import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED;
52import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED;
53import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED;
54import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090055import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080056import static org.onosproject.security.AppPermission.Type.APP_READ;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080057import static org.slf4j.LoggerFactory.getLogger;
58
59/**
60 * Implementation of the application management service.
61 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062@Component(immediate = true, service = {ApplicationService.class, ApplicationAdminService.class})
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070063public class ApplicationManager
64 extends AbstractListenerManager<ApplicationEvent, ApplicationListener>
65 implements ApplicationService, ApplicationAdminService {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080066
67 private final Logger log = getLogger(getClass());
68
69 private static final String APP_ID_NULL = "Application ID cannot be null";
Madan Jampani015d3a32016-06-16 18:25:11 -070070 private static final long DEFAULT_OPERATION_TIMEOUT_MILLIS = 2000;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080071
Thomas Vachuska02aeb032015-01-06 22:36:30 -080072 private final ApplicationStoreDelegate delegate = new InternalStoreDelegate();
73
Ray Milkeyd84f89b2018-08-17 14:54:17 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska02aeb032015-01-06 22:36:30 -080075 protected ApplicationStore store;
76
Ray Milkeyd84f89b2018-08-17 14:54:17 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska02aeb032015-01-06 22:36:30 -080078 protected FeaturesService featuresService;
79
Thomas Vachuskac65dd712015-11-04 17:19:10 -080080 // Application supplied hooks for pre-activation processing.
Jonathan Hartc32585f2016-06-16 14:01:27 -070081 private final Multimap<String, Runnable> deactivateHooks = HashMultimap.create();
Madan Jampania0c4bdb2016-06-17 11:02:38 -070082 private final Cache<ApplicationId, CountDownLatch> pendingOperations =
Madan Jampani015d3a32016-06-16 18:25:11 -070083 CacheBuilder.newBuilder()
84 .expireAfterWrite(DEFAULT_OPERATION_TIMEOUT_MILLIS * 2, TimeUnit.MILLISECONDS)
85 .build();
Thomas Vachuskac65dd712015-11-04 17:19:10 -080086
Thomas Vachuska02aeb032015-01-06 22:36:30 -080087 @Activate
88 public void activate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080089 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070090 store.setDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080091 log.info("Started");
92 }
93
94 @Deactivate
95 public void deactivate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080096 eventDispatcher.removeSink(ApplicationEvent.class);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070097 store.unsetDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080098 log.info("Stopped");
99 }
100
101 @Override
102 public Set<Application> getApplications() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900103 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800104 return store.getApplications();
105 }
106
107 @Override
108 public ApplicationId getId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900109 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800110 checkNotNull(name, "Name cannot be null");
111 return store.getId(name);
112 }
113
114 @Override
115 public Application getApplication(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900116 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800117 checkNotNull(appId, APP_ID_NULL);
118 return store.getApplication(appId);
119 }
120
121 @Override
122 public ApplicationState getState(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900123 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800124 checkNotNull(appId, APP_ID_NULL);
125 return store.getState(appId);
126 }
127
128 @Override
129 public Set<Permission> getPermissions(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900130 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800131 checkNotNull(appId, APP_ID_NULL);
132 return store.getPermissions(appId);
133 }
134
135 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800136 public void registerDeactivateHook(ApplicationId appId, Runnable hook) {
137 checkPermission(APP_READ);
138 checkNotNull(appId, APP_ID_NULL);
139 checkNotNull(hook, "Hook cannot be null");
140 deactivateHooks.put(appId.name(), hook);
141 }
142
143 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800144 public Application install(InputStream appDescStream) {
145 checkNotNull(appDescStream, "Application archive stream cannot be null");
Changhoon Yoonb856b812015-08-10 03:47:19 +0900146 Application app = store.create(appDescStream);
147 SecurityUtil.register(app.id());
148 return app;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800149 }
150
151 @Override
152 public void uninstall(ApplicationId appId) {
153 checkNotNull(appId, APP_ID_NULL);
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700154 updateStoreAndWaitForNotificationHandling(appId, store::remove);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800155 }
156
157 @Override
158 public void activate(ApplicationId appId) {
159 checkNotNull(appId, APP_ID_NULL);
Changhoon Yoonb856b812015-08-10 03:47:19 +0900160 if (!SecurityUtil.isAppSecured(appId)) {
161 return;
162 }
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700163 updateStoreAndWaitForNotificationHandling(appId, store::activate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800164 }
165
166 @Override
167 public void deactivate(ApplicationId appId) {
168 checkNotNull(appId, APP_ID_NULL);
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700169 updateStoreAndWaitForNotificationHandling(appId, store::deactivate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800170 }
171
172 @Override
173 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
174 checkNotNull(appId, APP_ID_NULL);
175 checkNotNull(permissions, "Permissions cannot be null");
176 store.setPermissions(appId, permissions);
177 }
178
Thomas Vachuska08b4dec2017-08-31 15:20:17 -0700179 @Override
180 public InputStream getApplicationArchive(ApplicationId appId) {
181 checkNotNull(appId, APP_ID_NULL);
182 return store.getApplicationArchive(appId);
183 }
184
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700185 private void updateStoreAndWaitForNotificationHandling(ApplicationId appId,
186 Consumer<ApplicationId> storeUpdateTask) {
187 CountDownLatch latch = new CountDownLatch(1);
188 try {
189 pendingOperations.put(appId, latch);
190 storeUpdateTask.accept(appId);
191 } catch (Exception e) {
192 pendingOperations.invalidate(appId);
193 latch.countDown();
194 log.warn("Failed to update store for {}", appId.name(), e);
195 }
196 Uninterruptibles.awaitUninterruptibly(latch, DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
197 }
198
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800199 private class InternalStoreDelegate implements ApplicationStoreDelegate {
200 @Override
201 public void notify(ApplicationEvent event) {
202 ApplicationEvent.Type type = event.type();
203 Application app = event.subject();
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700204 CountDownLatch latch = pendingOperations.getIfPresent(app.id());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800205 try {
206 if (type == APP_ACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700207 if (installAppFeatures(app)) {
208 log.info("Application {} has been activated", app.id().name());
209 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800210
211 } else if (type == APP_DEACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700212 if (uninstallAppFeatures(app)) {
213 log.info("Application {} has been deactivated", app.id().name());
214 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800215
216 } else if (type == APP_INSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700217 if (installAppArtifacts(app)) {
218 log.info("Application {} has been installed", app.id().name());
219 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800220
221 } else if (type == APP_UNINSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700222 if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) {
223 log.info("Application {} has been uninstalled", app.id().name());
224 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800225
226 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700227 post(event);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800228 } catch (Exception e) {
229 log.warn("Unable to perform operation on application " + app.id().name(), e);
Madan Jampani015d3a32016-06-16 18:25:11 -0700230 } finally {
231 if (latch != null) {
232 latch.countDown();
Madan Jampania0c4bdb2016-06-17 11:02:38 -0700233 pendingOperations.invalidate(app.id());
Madan Jampani015d3a32016-06-16 18:25:11 -0700234 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800235 }
236 }
237 }
238
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700239 // The following methods are fully synchronized to guard against remote vs.
240 // locally induced feature service interactions.
241
Thomas Vachuska761f0042015-11-11 19:10:17 -0800242 // Installs all feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700243 private synchronized boolean installAppArtifacts(Application app) throws Exception {
244 if (app.featuresRepo().isPresent() &&
245 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800246 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700247 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800248 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700249 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800250 }
251
Thomas Vachuska761f0042015-11-11 19:10:17 -0800252 // Uninstalls all the feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700253 private synchronized boolean uninstallAppArtifacts(Application app) throws Exception {
254 if (app.featuresRepo().isPresent() &&
255 featuresService.getRepository(app.featuresRepo().get()) != null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800256 featuresService.removeRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700257 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800258 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700259 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800260 }
261
Thomas Vachuska761f0042015-11-11 19:10:17 -0800262 // Installs all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700263 private synchronized boolean installAppFeatures(Application app) throws Exception {
264 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800265 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800266 Feature feature = featuresService.getFeature(name);
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800267
268 // If we see an attempt at activation of a non-existent feature
269 // attempt to install the app artifacts first and then retry.
270 // This can be triggered by a race condition between different ONOS
271 // instances "installing" the apps from disk at their own pace.
272 // Perhaps there is a more elegant solution to be explored in the
273 // future.
274 if (feature == null) {
275 installAppArtifacts(app);
276 feature = featuresService.getFeature(name);
277 }
278
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700279 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800280 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700281 changed = true;
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800282 } else if (feature == null) {
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700283 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800284 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800285 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700286 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800287 }
288
Thomas Vachuska761f0042015-11-11 19:10:17 -0800289 // Uninstalls all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700290 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
291 boolean changed = false;
Jonathan Hartc32585f2016-06-16 14:01:27 -0700292 deactivateHooks.removeAll(app.id().name()).forEach(hook -> invokeHook(hook, app.id()));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800293 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800294 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700295 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800296 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700297 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700298 } else if (feature == null) {
299 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800300 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800301 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700302 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800303 }
304
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800305 // Invokes the specified function, if not null.
Ray Milkeyaef45852016-01-11 17:13:19 -0800306 @java.lang.SuppressWarnings("squid:S1217") // We really do mean to call run()
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800307 private void invokeHook(Runnable hook, ApplicationId appId) {
308 if (hook != null) {
309 try {
310 hook.run();
311 } catch (Exception e) {
312 log.warn("Deactivate hook for application {} encountered an error",
313 appId.name(), e);
314 }
315 }
316 }
317
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800318}