blob: 153b0249d1848b28a18bc11e4da1e409da2e7bc2 [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
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080024import org.apache.karaf.features.Feature;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080025import org.apache.karaf.features.FeaturesService;
26import org.onosproject.app.ApplicationAdminService;
27import org.onosproject.app.ApplicationEvent;
28import org.onosproject.app.ApplicationListener;
29import org.onosproject.app.ApplicationService;
30import org.onosproject.app.ApplicationState;
31import org.onosproject.app.ApplicationStore;
32import org.onosproject.app.ApplicationStoreDelegate;
33import org.onosproject.core.Application;
34import org.onosproject.core.ApplicationId;
35import org.onosproject.core.Permission;
Simon Huntff663742015-05-14 13:33:05 -070036import org.onosproject.event.ListenerRegistry;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080037import org.onosproject.event.EventDeliveryService;
38import org.slf4j.Logger;
39
40import java.io.InputStream;
41import java.util.Set;
42
43import static com.google.common.base.Preconditions.checkNotNull;
44import static org.onosproject.app.ApplicationEvent.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090045import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080046import static org.slf4j.LoggerFactory.getLogger;
47
48/**
49 * Implementation of the application management service.
50 */
51@Component(immediate = true)
52@Service
53public class ApplicationManager implements ApplicationService, ApplicationAdminService {
54
55 private final Logger log = getLogger(getClass());
56
57 private static final String APP_ID_NULL = "Application ID cannot be null";
58
Simon Huntff663742015-05-14 13:33:05 -070059 protected final ListenerRegistry<ApplicationEvent, ApplicationListener>
60 listenerRegistry = new ListenerRegistry<>();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080061
62 private final ApplicationStoreDelegate delegate = new InternalStoreDelegate();
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected ApplicationStore store;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected FeaturesService featuresService;
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected EventDeliveryService eventDispatcher;
72
Thomas Vachuska62f04a42015-04-22 14:38:34 -070073 private boolean initializing;
74
Thomas Vachuska02aeb032015-01-06 22:36:30 -080075 @Activate
76 public void activate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080077 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070078
79 initializing = true;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070080 store.setDelegate(delegate);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070081 initializing = false;
82
Thomas Vachuska02aeb032015-01-06 22:36:30 -080083 log.info("Started");
84 }
85
86 @Deactivate
87 public void deactivate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080088 eventDispatcher.removeSink(ApplicationEvent.class);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070089 store.unsetDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080090 log.info("Stopped");
91 }
92
93 @Override
94 public Set<Application> getApplications() {
Changhoon Yoon541ef712015-05-23 17:18:34 +090095 checkPermission(Permission.APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080096 return store.getApplications();
97 }
98
99 @Override
100 public ApplicationId getId(String name) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900101 checkPermission(Permission.APP_READ);
102
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800103 checkNotNull(name, "Name cannot be null");
104 return store.getId(name);
105 }
106
107 @Override
108 public Application getApplication(ApplicationId appId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900109 checkPermission(Permission.APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800110 checkNotNull(appId, APP_ID_NULL);
111 return store.getApplication(appId);
112 }
113
114 @Override
115 public ApplicationState getState(ApplicationId appId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900116 checkPermission(Permission.APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800117 checkNotNull(appId, APP_ID_NULL);
118 return store.getState(appId);
119 }
120
121 @Override
122 public Set<Permission> getPermissions(ApplicationId appId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900123 checkPermission(Permission.APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800124 checkNotNull(appId, APP_ID_NULL);
125 return store.getPermissions(appId);
126 }
127
128 @Override
129 public Application install(InputStream appDescStream) {
130 checkNotNull(appDescStream, "Application archive stream cannot be null");
131 return store.create(appDescStream);
132 }
133
134 @Override
135 public void uninstall(ApplicationId appId) {
136 checkNotNull(appId, APP_ID_NULL);
137 try {
138 store.remove(appId);
139 } catch (Exception e) {
140 log.warn("Unable to purge application directory for {}", appId.name());
141 }
142 }
143
144 @Override
145 public void activate(ApplicationId appId) {
146 checkNotNull(appId, APP_ID_NULL);
147 store.activate(appId);
148 }
149
150 @Override
151 public void deactivate(ApplicationId appId) {
152 checkNotNull(appId, APP_ID_NULL);
153 store.deactivate(appId);
154 }
155
156 @Override
157 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
158 checkNotNull(appId, APP_ID_NULL);
159 checkNotNull(permissions, "Permissions cannot be null");
160 store.setPermissions(appId, permissions);
161 }
162
163 @Override
164 public void addListener(ApplicationListener listener) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900165 checkPermission(Permission.APP_EVENT);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800166 listenerRegistry.addListener(listener);
167 }
168
169 @Override
170 public void removeListener(ApplicationListener listener) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900171 checkPermission(Permission.APP_EVENT);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800172 listenerRegistry.removeListener(listener);
173 }
174
175 private class InternalStoreDelegate implements ApplicationStoreDelegate {
176 @Override
177 public void notify(ApplicationEvent event) {
178 ApplicationEvent.Type type = event.type();
179 Application app = event.subject();
180 try {
181 if (type == APP_ACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700182 if (installAppFeatures(app)) {
183 log.info("Application {} has been activated", app.id().name());
184 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800185
186 } else if (type == APP_DEACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700187 if (uninstallAppFeatures(app)) {
188 log.info("Application {} has been deactivated", app.id().name());
189 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800190
191 } else if (type == APP_INSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700192 if (installAppArtifacts(app)) {
193 log.info("Application {} has been installed", app.id().name());
194 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800195
196 } else if (type == APP_UNINSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700197 if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) {
198 log.info("Application {} has been uninstalled", app.id().name());
199 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800200
201 }
202 eventDispatcher.post(event);
203
204 } catch (Exception e) {
205 log.warn("Unable to perform operation on application " + app.id().name(), e);
206 }
207 }
208 }
209
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700210 // The following methods are fully synchronized to guard against remote vs.
211 // locally induced feature service interactions.
212
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700213 private synchronized boolean installAppArtifacts(Application app) throws Exception {
214 if (app.featuresRepo().isPresent() &&
215 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800216 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700217 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800218 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700219 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800220 }
221
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700222 private synchronized boolean uninstallAppArtifacts(Application app) throws Exception {
223 if (app.featuresRepo().isPresent() &&
224 featuresService.getRepository(app.featuresRepo().get()) != null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800225 featuresService.removeRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700226 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800227 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700228 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800229 }
230
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700231 private synchronized boolean installAppFeatures(Application app) throws Exception {
232 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800233 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800234 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700235 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800236 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700237 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700238 } else if (feature == null && !initializing) {
239 // Suppress feature-not-found reporting during startup since these
240 // can arise naturally from the staggered cluster install.
241 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800242 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800243 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700244 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800245 }
246
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700247 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
248 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800249 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800250 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700251 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800252 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700253 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700254 } else if (feature == null) {
255 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800256 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800257 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700258 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800259 }
260
261}