blob: a9e928e542d8a22edfb3c38ea8484d8d1af272a4 [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
Thomas Vachuskac65dd712015-11-04 17:19:10 -080018import com.google.common.collect.Maps;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080025import org.apache.karaf.features.Feature;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080026import org.apache.karaf.features.FeaturesService;
27import org.onosproject.app.ApplicationAdminService;
28import org.onosproject.app.ApplicationEvent;
29import org.onosproject.app.ApplicationListener;
30import org.onosproject.app.ApplicationService;
31import org.onosproject.app.ApplicationState;
32import org.onosproject.app.ApplicationStore;
33import org.onosproject.app.ApplicationStoreDelegate;
34import org.onosproject.core.Application;
35import org.onosproject.core.ApplicationId;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080036import org.onosproject.event.AbstractListenerManager;
Changhoon Yoonb856b812015-08-10 03:47:19 +090037import org.onosproject.security.Permission;
38import org.onosproject.security.SecurityUtil;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080039import org.slf4j.Logger;
40
41import java.io.InputStream;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080042import java.util.Map;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080043import java.util.Set;
44
45import static com.google.common.base.Preconditions.checkNotNull;
46import static org.onosproject.app.ApplicationEvent.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090047import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080048import static org.onosproject.security.AppPermission.Type.APP_READ;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080049import static org.slf4j.LoggerFactory.getLogger;
50
51/**
52 * Implementation of the application management service.
53 */
54@Component(immediate = true)
55@Service
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070056public class ApplicationManager
57 extends AbstractListenerManager<ApplicationEvent, ApplicationListener>
58 implements ApplicationService, ApplicationAdminService {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080059
60 private final Logger log = getLogger(getClass());
61
62 private static final String APP_ID_NULL = "Application ID cannot be null";
63
Thomas Vachuska02aeb032015-01-06 22:36:30 -080064 private final ApplicationStoreDelegate delegate = new InternalStoreDelegate();
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected ApplicationStore store;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected FeaturesService featuresService;
71
Thomas Vachuska62f04a42015-04-22 14:38:34 -070072 private boolean initializing;
73
Thomas Vachuskac65dd712015-11-04 17:19:10 -080074 // Application supplied hooks for pre-activation processing.
75 private final Map<String, Runnable> deactivateHooks = Maps.newConcurrentMap();
76
Thomas Vachuska02aeb032015-01-06 22:36:30 -080077 @Activate
78 public void activate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080079 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070080
81 initializing = true;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070082 store.setDelegate(delegate);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070083 initializing = false;
84
Thomas Vachuska02aeb032015-01-06 22:36:30 -080085 log.info("Started");
86 }
87
88 @Deactivate
89 public void deactivate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080090 eventDispatcher.removeSink(ApplicationEvent.class);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070091 store.unsetDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080092 log.info("Stopped");
93 }
94
95 @Override
96 public Set<Application> getApplications() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090097 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080098 return store.getApplications();
99 }
100
101 @Override
102 public ApplicationId getId(String name) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900103 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800104 checkNotNull(name, "Name cannot be null");
105 return store.getId(name);
106 }
107
108 @Override
109 public Application getApplication(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900110 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800111 checkNotNull(appId, APP_ID_NULL);
112 return store.getApplication(appId);
113 }
114
115 @Override
116 public ApplicationState getState(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900117 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800118 checkNotNull(appId, APP_ID_NULL);
119 return store.getState(appId);
120 }
121
122 @Override
123 public Set<Permission> getPermissions(ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900124 checkPermission(APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800125 checkNotNull(appId, APP_ID_NULL);
126 return store.getPermissions(appId);
127 }
128
129 @Override
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800130 public void registerDeactivateHook(ApplicationId appId, Runnable hook) {
131 checkPermission(APP_READ);
132 checkNotNull(appId, APP_ID_NULL);
133 checkNotNull(hook, "Hook cannot be null");
134 deactivateHooks.put(appId.name(), hook);
135 }
136
137 @Override
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800138 public Application install(InputStream appDescStream) {
139 checkNotNull(appDescStream, "Application archive stream cannot be null");
Changhoon Yoonb856b812015-08-10 03:47:19 +0900140 Application app = store.create(appDescStream);
141 SecurityUtil.register(app.id());
142 return app;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800143 }
144
145 @Override
146 public void uninstall(ApplicationId appId) {
147 checkNotNull(appId, APP_ID_NULL);
148 try {
149 store.remove(appId);
150 } catch (Exception e) {
151 log.warn("Unable to purge application directory for {}", appId.name());
152 }
153 }
154
155 @Override
156 public void activate(ApplicationId appId) {
157 checkNotNull(appId, APP_ID_NULL);
Changhoon Yoonb856b812015-08-10 03:47:19 +0900158 if (!SecurityUtil.isAppSecured(appId)) {
159 return;
160 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800161 store.activate(appId);
162 }
163
164 @Override
165 public void deactivate(ApplicationId appId) {
166 checkNotNull(appId, APP_ID_NULL);
167 store.deactivate(appId);
168 }
169
170 @Override
171 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
172 checkNotNull(appId, APP_ID_NULL);
173 checkNotNull(permissions, "Permissions cannot be null");
174 store.setPermissions(appId, permissions);
175 }
176
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800177 private class InternalStoreDelegate implements ApplicationStoreDelegate {
178 @Override
179 public void notify(ApplicationEvent event) {
180 ApplicationEvent.Type type = event.type();
181 Application app = event.subject();
182 try {
183 if (type == APP_ACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700184 if (installAppFeatures(app)) {
185 log.info("Application {} has been activated", app.id().name());
186 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800187
188 } else if (type == APP_DEACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700189 if (uninstallAppFeatures(app)) {
190 log.info("Application {} has been deactivated", app.id().name());
191 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800192
193 } else if (type == APP_INSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700194 if (installAppArtifacts(app)) {
195 log.info("Application {} has been installed", app.id().name());
196 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800197
198 } else if (type == APP_UNINSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700199 if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) {
200 log.info("Application {} has been uninstalled", app.id().name());
201 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800202
203 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700204 post(event);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800205
206 } catch (Exception e) {
207 log.warn("Unable to perform operation on application " + app.id().name(), e);
208 }
209 }
210 }
211
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700212 // The following methods are fully synchronized to guard against remote vs.
213 // locally induced feature service interactions.
214
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700215 private synchronized boolean installAppArtifacts(Application app) throws Exception {
216 if (app.featuresRepo().isPresent() &&
217 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800218 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700219 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800220 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700221 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800222 }
223
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700224 private synchronized boolean uninstallAppArtifacts(Application app) throws Exception {
225 if (app.featuresRepo().isPresent() &&
226 featuresService.getRepository(app.featuresRepo().get()) != null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800227 featuresService.removeRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700228 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800229 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700230 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800231 }
232
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700233 private synchronized boolean installAppFeatures(Application app) throws Exception {
234 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800235 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800236 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700237 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800238 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700239 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700240 } else if (feature == null && !initializing) {
241 // Suppress feature-not-found reporting during startup since these
242 // can arise naturally from the staggered cluster install.
243 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800244 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800245 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700246 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800247 }
248
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700249 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
250 boolean changed = false;
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800251 invokeHook(deactivateHooks.get(app.id().name()), app.id());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800252 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800253 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700254 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800255 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700256 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700257 } else if (feature == null) {
258 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800259 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800260 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700261 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800262 }
263
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800264 // Invokes the specified function, if not null.
265 private void invokeHook(Runnable hook, ApplicationId appId) {
266 if (hook != null) {
267 try {
268 hook.run();
269 } catch (Exception e) {
270 log.warn("Deactivate hook for application {} encountered an error",
271 appId.name(), e);
272 }
273 }
274 }
275
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800276}