blob: 7754a12d23d7b0f332c16acb135429d9b0ed267a [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) {
173 installAppFeatures(app);
174 log.info("Application {} has been activated", app.id().name());
175
176 } else if (type == APP_DEACTIVATED) {
177 uninstallAppFeatures(app);
178 log.info("Application {} has been deactivated", app.id().name());
179
180 } else if (type == APP_INSTALLED) {
181 installAppArtifacts(app);
182 log.info("Application {} has been installed", app.id().name());
183
184 } else if (type == APP_UNINSTALLED) {
185 uninstallAppFeatures(app);
186 uninstallAppArtifacts(app);
187 log.info("Application {} has been uninstalled", app.id().name());
188
189 }
190 eventDispatcher.post(event);
191
192 } catch (Exception e) {
193 log.warn("Unable to perform operation on application " + app.id().name(), e);
194 }
195 }
196 }
197
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700198 // The following methods are fully synchronized to guard against remote vs.
199 // locally induced feature service interactions.
200
201 private synchronized void installAppArtifacts(Application app) throws Exception {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800202 if (app.featuresRepo().isPresent()) {
203 featuresService.addRepository(app.featuresRepo().get());
204 }
205 }
206
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700207 private synchronized void uninstallAppArtifacts(Application app) throws Exception {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800208 if (app.featuresRepo().isPresent()) {
209 featuresService.removeRepository(app.featuresRepo().get());
210 }
211 }
212
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700213 private synchronized void installAppFeatures(Application app) throws Exception {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800214 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800215 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700216 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800217 featuresService.installFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700218 } else if (feature == null && !initializing) {
219 // Suppress feature-not-found reporting during startup since these
220 // can arise naturally from the staggered cluster install.
221 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800222 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800223 }
224 }
225
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700226 private synchronized void uninstallAppFeatures(Application app) throws Exception {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800227 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800228 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700229 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800230 featuresService.uninstallFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700231 } else if (feature == null) {
232 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800233 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800234 }
235 }
236
237}