blob: a8609acfa7c96d05a1e155a80e7550219668b423 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
Ray Milkey9f87e512016-01-05 10:00:22 -080018import java.io.InputStream;
19import java.util.Map;
20import java.util.Set;
21
Thomas Vachuska02aeb032015-01-06 22:36:30 -080022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.apache.felix.scr.annotations.Service;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080028import org.apache.karaf.features.Feature;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080029import org.apache.karaf.features.FeaturesService;
30import org.onosproject.app.ApplicationAdminService;
31import org.onosproject.app.ApplicationEvent;
32import org.onosproject.app.ApplicationListener;
33import org.onosproject.app.ApplicationService;
34import org.onosproject.app.ApplicationState;
35import org.onosproject.app.ApplicationStore;
36import org.onosproject.app.ApplicationStoreDelegate;
37import org.onosproject.core.Application;
38import org.onosproject.core.ApplicationId;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080039import org.onosproject.event.AbstractListenerManager;
Changhoon Yoonb856b812015-08-10 03:47:19 +090040import org.onosproject.security.Permission;
41import org.onosproject.security.SecurityUtil;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080042import org.slf4j.Logger;
43
Ray Milkey9f87e512016-01-05 10:00:22 -080044import com.google.common.collect.Maps;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080045
46import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkey9f87e512016-01-05 10:00:22 -080047import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED;
48import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED;
49import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED;
50import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED;
Changhoon Yoon541ef712015-05-23 17:18:34 +090051import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080052import static org.onosproject.security.AppPermission.Type.APP_READ;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080053import static org.slf4j.LoggerFactory.getLogger;
54
55/**
56 * Implementation of the application management service.
57 */
58@Component(immediate = true)
59@Service
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070060public class ApplicationManager
61 extends AbstractListenerManager<ApplicationEvent, ApplicationListener>
62 implements ApplicationService, ApplicationAdminService {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080063
64 private final Logger log = getLogger(getClass());
65
66 private static final String APP_ID_NULL = "Application ID cannot be null";
67
Thomas Vachuska02aeb032015-01-06 22:36:30 -080068 private final ApplicationStoreDelegate delegate = new InternalStoreDelegate();
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected ApplicationStore store;
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected FeaturesService featuresService;
75
Thomas Vachuska62f04a42015-04-22 14:38:34 -070076 private boolean initializing;
77
Thomas Vachuskac65dd712015-11-04 17:19:10 -080078 // Application supplied hooks for pre-activation processing.
79 private final Map<String, Runnable> deactivateHooks = Maps.newConcurrentMap();
80
Thomas Vachuska02aeb032015-01-06 22:36:30 -080081 @Activate
82 public void activate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080083 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070084
85 initializing = true;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070086 store.setDelegate(delegate);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070087 initializing = false;
88
Thomas Vachuska02aeb032015-01-06 22:36:30 -080089 log.info("Started");
90 }
91
92 @Deactivate
93 public void deactivate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080094 eventDispatcher.removeSink(ApplicationEvent.class);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070095 store.unsetDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080096 log.info("Stopped");
97 }
98
99 @Override
100 public Set<Application> getApplications() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900101 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800102 return store.getApplications();
103 }
104
105 @Override
106 public ApplicationId getId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900107 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800108 checkNotNull(name, "Name cannot be null");
109 return store.getId(name);
110 }
111
112 @Override
113 public Application getApplication(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900114 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800115 checkNotNull(appId, APP_ID_NULL);
116 return store.getApplication(appId);
117 }
118
119 @Override
120 public ApplicationState getState(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900121 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800122 checkNotNull(appId, APP_ID_NULL);
123 return store.getState(appId);
124 }
125
126 @Override
127 public Set<Permission> getPermissions(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900128 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800129 checkNotNull(appId, APP_ID_NULL);
130 return store.getPermissions(appId);
131 }
132
133 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800134 public void registerDeactivateHook(ApplicationId appId, Runnable hook) {
135 checkPermission(APP_READ);
136 checkNotNull(appId, APP_ID_NULL);
137 checkNotNull(hook, "Hook cannot be null");
138 deactivateHooks.put(appId.name(), hook);
139 }
140
141 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800142 public Application install(InputStream appDescStream) {
143 checkNotNull(appDescStream, "Application archive stream cannot be null");
Changhoon Yoonb856b812015-08-10 03:47:19 +0900144 Application app = store.create(appDescStream);
145 SecurityUtil.register(app.id());
146 return app;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800147 }
148
149 @Override
150 public void uninstall(ApplicationId appId) {
151 checkNotNull(appId, APP_ID_NULL);
152 try {
153 store.remove(appId);
154 } catch (Exception e) {
155 log.warn("Unable to purge application directory for {}", appId.name());
156 }
157 }
158
159 @Override
160 public void activate(ApplicationId appId) {
161 checkNotNull(appId, APP_ID_NULL);
Changhoon Yoonb856b812015-08-10 03:47:19 +0900162 if (!SecurityUtil.isAppSecured(appId)) {
163 return;
164 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800165 store.activate(appId);
166 }
167
168 @Override
169 public void deactivate(ApplicationId appId) {
170 checkNotNull(appId, APP_ID_NULL);
171 store.deactivate(appId);
172 }
173
174 @Override
175 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
176 checkNotNull(appId, APP_ID_NULL);
177 checkNotNull(permissions, "Permissions cannot be null");
178 store.setPermissions(appId, permissions);
179 }
180
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800181 private class InternalStoreDelegate implements ApplicationStoreDelegate {
182 @Override
183 public void notify(ApplicationEvent event) {
184 ApplicationEvent.Type type = event.type();
185 Application app = event.subject();
186 try {
187 if (type == APP_ACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700188 if (installAppFeatures(app)) {
189 log.info("Application {} has been activated", app.id().name());
190 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800191
192 } else if (type == APP_DEACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700193 if (uninstallAppFeatures(app)) {
194 log.info("Application {} has been deactivated", app.id().name());
195 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800196
197 } else if (type == APP_INSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700198 if (installAppArtifacts(app)) {
199 log.info("Application {} has been installed", app.id().name());
200 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800201
202 } else if (type == APP_UNINSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700203 if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) {
204 log.info("Application {} has been uninstalled", app.id().name());
205 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800206
207 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700208 post(event);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800209
210 } catch (Exception e) {
211 log.warn("Unable to perform operation on application " + app.id().name(), e);
212 }
213 }
214 }
215
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700216 // The following methods are fully synchronized to guard against remote vs.
217 // locally induced feature service interactions.
218
Thomas Vachuska761f0042015-11-11 19:10:17 -0800219 // Installs all feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700220 private synchronized boolean installAppArtifacts(Application app) throws Exception {
221 if (app.featuresRepo().isPresent() &&
222 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800223 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700224 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800225 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700226 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800227 }
228
Thomas Vachuska761f0042015-11-11 19:10:17 -0800229 // Uninstalls all the feature repositories required by the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700230 private synchronized boolean uninstallAppArtifacts(Application app) throws Exception {
231 if (app.featuresRepo().isPresent() &&
232 featuresService.getRepository(app.featuresRepo().get()) != null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800233 featuresService.removeRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700234 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800235 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700236 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800237 }
238
Thomas Vachuska761f0042015-11-11 19:10:17 -0800239 // Installs all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700240 private synchronized boolean installAppFeatures(Application app) throws Exception {
241 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800242 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800243 Feature feature = featuresService.getFeature(name);
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800244
245 // If we see an attempt at activation of a non-existent feature
246 // attempt to install the app artifacts first and then retry.
247 // This can be triggered by a race condition between different ONOS
248 // instances "installing" the apps from disk at their own pace.
249 // Perhaps there is a more elegant solution to be explored in the
250 // future.
251 if (feature == null) {
252 installAppArtifacts(app);
253 feature = featuresService.getFeature(name);
254 }
255
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700256 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800257 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700258 changed = true;
Thomas Vachuskab56b9172015-11-24 11:24:23 -0800259 } else if (feature == null) {
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700260 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800261 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800262 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700263 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800264 }
265
Thomas Vachuska761f0042015-11-11 19:10:17 -0800266 // Uninstalls all features that define the specified app.
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700267 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
268 boolean changed = false;
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800269 invokeHook(deactivateHooks.get(app.id().name()), app.id());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800270 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800271 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700272 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800273 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700274 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700275 } else if (feature == null) {
276 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800277 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800278 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700279 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800280 }
281
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800282 // Invokes the specified function, if not null.
Ray Milkeyaef45852016-01-11 17:13:19 -0800283 @java.lang.SuppressWarnings("squid:S1217") // We really do mean to call run()
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800284 private void invokeHook(Runnable hook, ApplicationId appId) {
285 if (hook != null) {
286 try {
287 hook.run();
288 } catch (Exception e) {
289 log.warn("Deactivate hook for application {} encountered an error",
290 appId.name(), e);
291 }
292 }
293 }
294
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800295}