blob: 975baa1af6635d114ad8a28c91cdf2243031c34d [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;
36import org.onosproject.event.AbstractListenerRegistry;
37import 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.*;
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Implementation of the application management service.
49 */
50@Component(immediate = true)
51@Service
52public class ApplicationManager implements ApplicationService, ApplicationAdminService {
53
54 private final Logger log = getLogger(getClass());
55
56 private static final String APP_ID_NULL = "Application ID cannot be null";
57
58 protected final AbstractListenerRegistry<ApplicationEvent, ApplicationListener>
59 listenerRegistry = new AbstractListenerRegistry<>();
60
61 private final ApplicationStoreDelegate delegate = new InternalStoreDelegate();
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected ApplicationStore store;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected FeaturesService featuresService;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected EventDeliveryService eventDispatcher;
71
Thomas Vachuska62f04a42015-04-22 14:38:34 -070072 private boolean initializing;
73
Thomas Vachuska02aeb032015-01-06 22:36:30 -080074 @Activate
75 public void activate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080076 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070077
78 initializing = true;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070079 store.setDelegate(delegate);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070080 initializing = false;
81
Thomas Vachuska02aeb032015-01-06 22:36:30 -080082 log.info("Started");
83 }
84
85 @Deactivate
86 public void deactivate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080087 eventDispatcher.removeSink(ApplicationEvent.class);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070088 store.unsetDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080089 log.info("Stopped");
90 }
91
92 @Override
93 public Set<Application> getApplications() {
94 return store.getApplications();
95 }
96
97 @Override
98 public ApplicationId getId(String name) {
99 checkNotNull(name, "Name cannot be null");
100 return store.getId(name);
101 }
102
103 @Override
104 public Application getApplication(ApplicationId appId) {
105 checkNotNull(appId, APP_ID_NULL);
106 return store.getApplication(appId);
107 }
108
109 @Override
110 public ApplicationState getState(ApplicationId appId) {
111 checkNotNull(appId, APP_ID_NULL);
112 return store.getState(appId);
113 }
114
115 @Override
116 public Set<Permission> getPermissions(ApplicationId appId) {
117 checkNotNull(appId, APP_ID_NULL);
118 return store.getPermissions(appId);
119 }
120
121 @Override
122 public Application install(InputStream appDescStream) {
123 checkNotNull(appDescStream, "Application archive stream cannot be null");
124 return store.create(appDescStream);
125 }
126
127 @Override
128 public void uninstall(ApplicationId appId) {
129 checkNotNull(appId, APP_ID_NULL);
130 try {
131 store.remove(appId);
132 } catch (Exception e) {
133 log.warn("Unable to purge application directory for {}", appId.name());
134 }
135 }
136
137 @Override
138 public void activate(ApplicationId appId) {
139 checkNotNull(appId, APP_ID_NULL);
140 store.activate(appId);
141 }
142
143 @Override
144 public void deactivate(ApplicationId appId) {
145 checkNotNull(appId, APP_ID_NULL);
146 store.deactivate(appId);
147 }
148
149 @Override
150 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
151 checkNotNull(appId, APP_ID_NULL);
152 checkNotNull(permissions, "Permissions cannot be null");
153 store.setPermissions(appId, permissions);
154 }
155
156 @Override
157 public void addListener(ApplicationListener listener) {
158 listenerRegistry.addListener(listener);
159 }
160
161 @Override
162 public void removeListener(ApplicationListener listener) {
163 listenerRegistry.removeListener(listener);
164 }
165
166 private class InternalStoreDelegate implements ApplicationStoreDelegate {
167 @Override
168 public void notify(ApplicationEvent event) {
169 ApplicationEvent.Type type = event.type();
170 Application app = event.subject();
171 try {
172 if (type == APP_ACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700173 if (installAppFeatures(app)) {
174 log.info("Application {} has been activated", app.id().name());
175 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800176
177 } else if (type == APP_DEACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700178 if (uninstallAppFeatures(app)) {
179 log.info("Application {} has been deactivated", app.id().name());
180 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800181
182 } else if (type == APP_INSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700183 if (installAppArtifacts(app)) {
184 log.info("Application {} has been installed", app.id().name());
185 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800186
187 } else if (type == APP_UNINSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700188 if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) {
189 log.info("Application {} has been uninstalled", app.id().name());
190 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800191
192 }
193 eventDispatcher.post(event);
194
195 } catch (Exception e) {
196 log.warn("Unable to perform operation on application " + app.id().name(), e);
197 }
198 }
199 }
200
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700201 // The following methods are fully synchronized to guard against remote vs.
202 // locally induced feature service interactions.
203
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700204 private synchronized boolean installAppArtifacts(Application app) throws Exception {
205 if (app.featuresRepo().isPresent() &&
206 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800207 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700208 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800209 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700210 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800211 }
212
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700213 private synchronized boolean uninstallAppArtifacts(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.removeRepository(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 installAppFeatures(Application app) throws Exception {
223 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800224 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800225 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700226 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800227 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700228 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700229 } else if (feature == null && !initializing) {
230 // Suppress feature-not-found reporting during startup since these
231 // can arise naturally from the staggered cluster install.
232 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800233 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800234 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700235 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800236 }
237
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700238 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
239 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800240 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800241 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700242 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800243 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700244 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700245 } else if (feature == null) {
246 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800247 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800248 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700249 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800250 }
251
252}