blob: 989417402ebf1cfe06585f21fc6b371bf6ec077d [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);
96
Thomas Vachuska02aeb032015-01-06 22:36:30 -080097 return store.getApplications();
98 }
99
100 @Override
101 public ApplicationId getId(String name) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900102 checkPermission(Permission.APP_READ);
103
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 Yoon541ef712015-05-23 17:18:34 +0900110 checkPermission(Permission.APP_READ);
111
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800112 checkNotNull(appId, APP_ID_NULL);
113 return store.getApplication(appId);
114 }
115
116 @Override
117 public ApplicationState getState(ApplicationId appId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900118 checkPermission(Permission.APP_READ);
119
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800120 checkNotNull(appId, APP_ID_NULL);
121 return store.getState(appId);
122 }
123
124 @Override
125 public Set<Permission> getPermissions(ApplicationId appId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900126 checkPermission(Permission.APP_READ);
127
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800128 checkNotNull(appId, APP_ID_NULL);
129 return store.getPermissions(appId);
130 }
131
132 @Override
133 public Application install(InputStream appDescStream) {
134 checkNotNull(appDescStream, "Application archive stream cannot be null");
135 return store.create(appDescStream);
136 }
137
138 @Override
139 public void uninstall(ApplicationId appId) {
140 checkNotNull(appId, APP_ID_NULL);
141 try {
142 store.remove(appId);
143 } catch (Exception e) {
144 log.warn("Unable to purge application directory for {}", appId.name());
145 }
146 }
147
148 @Override
149 public void activate(ApplicationId appId) {
150 checkNotNull(appId, APP_ID_NULL);
151 store.activate(appId);
152 }
153
154 @Override
155 public void deactivate(ApplicationId appId) {
156 checkNotNull(appId, APP_ID_NULL);
157 store.deactivate(appId);
158 }
159
160 @Override
161 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
162 checkNotNull(appId, APP_ID_NULL);
163 checkNotNull(permissions, "Permissions cannot be null");
164 store.setPermissions(appId, permissions);
165 }
166
167 @Override
168 public void addListener(ApplicationListener listener) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900169 checkPermission(Permission.APP_EVENT);
170
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800171 listenerRegistry.addListener(listener);
172 }
173
174 @Override
175 public void removeListener(ApplicationListener listener) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900176 checkPermission(Permission.APP_EVENT);
177
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800178 listenerRegistry.removeListener(listener);
179 }
180
181 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 }
208 eventDispatcher.post(event);
209
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 Vachuska9ff88a92015-05-13 13:57:18 -0700219 private synchronized boolean installAppArtifacts(Application app) throws Exception {
220 if (app.featuresRepo().isPresent() &&
221 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800222 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700223 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800224 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700225 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800226 }
227
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700228 private synchronized boolean uninstallAppArtifacts(Application app) throws Exception {
229 if (app.featuresRepo().isPresent() &&
230 featuresService.getRepository(app.featuresRepo().get()) != null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800231 featuresService.removeRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700232 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800233 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700234 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800235 }
236
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700237 private synchronized boolean installAppFeatures(Application app) throws Exception {
238 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800239 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800240 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700241 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800242 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700243 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700244 } else if (feature == null && !initializing) {
245 // Suppress feature-not-found reporting during startup since these
246 // can arise naturally from the staggered cluster install.
247 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800248 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800249 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700250 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800251 }
252
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700253 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
254 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800255 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800256 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700257 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800258 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700259 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700260 } else if (feature == null) {
261 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800262 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800263 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700264 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800265 }
266
267}