blob: a96d80522416ff31c079717f07198fc4ccf9ebf8 [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;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070033import org.onosproject.event.AbstractListenerManager;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080034import org.onosproject.core.Application;
35import org.onosproject.core.ApplicationId;
36import org.onosproject.core.Permission;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080037import org.slf4j.Logger;
38
39import java.io.InputStream;
40import java.util.Set;
41
42import static com.google.common.base.Preconditions.checkNotNull;
43import static org.onosproject.app.ApplicationEvent.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090044import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080045import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Implementation of the application management service.
49 */
50@Component(immediate = true)
51@Service
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070052public class ApplicationManager
53 extends AbstractListenerManager<ApplicationEvent, ApplicationListener>
54 implements ApplicationService, ApplicationAdminService {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080055
56 private final Logger log = getLogger(getClass());
57
58 private static final String APP_ID_NULL = "Application ID cannot be null";
59
Thomas Vachuska02aeb032015-01-06 22:36:30 -080060 private final ApplicationStoreDelegate delegate = new InternalStoreDelegate();
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected ApplicationStore store;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected FeaturesService featuresService;
67
Thomas Vachuska62f04a42015-04-22 14:38:34 -070068 private boolean initializing;
69
Thomas Vachuska02aeb032015-01-06 22:36:30 -080070 @Activate
71 public void activate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080072 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070073
74 initializing = true;
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070075 store.setDelegate(delegate);
Thomas Vachuska62f04a42015-04-22 14:38:34 -070076 initializing = false;
77
Thomas Vachuska02aeb032015-01-06 22:36:30 -080078 log.info("Started");
79 }
80
81 @Deactivate
82 public void deactivate() {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080083 eventDispatcher.removeSink(ApplicationEvent.class);
Thomas Vachuska8dc1a692015-03-31 01:01:37 -070084 store.unsetDelegate(delegate);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080085 log.info("Stopped");
86 }
87
88 @Override
89 public Set<Application> getApplications() {
Changhoon Yoon541ef712015-05-23 17:18:34 +090090 checkPermission(Permission.APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080091 return store.getApplications();
92 }
93
94 @Override
95 public ApplicationId getId(String name) {
Changhoon Yoon541ef712015-05-23 17:18:34 +090096 checkPermission(Permission.APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080097 checkNotNull(name, "Name cannot be null");
98 return store.getId(name);
99 }
100
101 @Override
102 public Application getApplication(ApplicationId appId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900103 checkPermission(Permission.APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800104 checkNotNull(appId, APP_ID_NULL);
105 return store.getApplication(appId);
106 }
107
108 @Override
109 public ApplicationState getState(ApplicationId appId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900110 checkPermission(Permission.APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800111 checkNotNull(appId, APP_ID_NULL);
112 return store.getState(appId);
113 }
114
115 @Override
116 public Set<Permission> getPermissions(ApplicationId appId) {
Changhoon Yoon541ef712015-05-23 17:18:34 +0900117 checkPermission(Permission.APP_READ);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800118 checkNotNull(appId, APP_ID_NULL);
119 return store.getPermissions(appId);
120 }
121
122 @Override
123 public Application install(InputStream appDescStream) {
124 checkNotNull(appDescStream, "Application archive stream cannot be null");
125 return store.create(appDescStream);
126 }
127
128 @Override
129 public void uninstall(ApplicationId appId) {
130 checkNotNull(appId, APP_ID_NULL);
131 try {
132 store.remove(appId);
133 } catch (Exception e) {
134 log.warn("Unable to purge application directory for {}", appId.name());
135 }
136 }
137
138 @Override
139 public void activate(ApplicationId appId) {
140 checkNotNull(appId, APP_ID_NULL);
141 store.activate(appId);
142 }
143
144 @Override
145 public void deactivate(ApplicationId appId) {
146 checkNotNull(appId, APP_ID_NULL);
147 store.deactivate(appId);
148 }
149
150 @Override
151 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
152 checkNotNull(appId, APP_ID_NULL);
153 checkNotNull(permissions, "Permissions cannot be null");
154 store.setPermissions(appId, permissions);
155 }
156
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800157 private class InternalStoreDelegate implements ApplicationStoreDelegate {
158 @Override
159 public void notify(ApplicationEvent event) {
160 ApplicationEvent.Type type = event.type();
161 Application app = event.subject();
162 try {
163 if (type == APP_ACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700164 if (installAppFeatures(app)) {
165 log.info("Application {} has been activated", app.id().name());
166 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800167
168 } else if (type == APP_DEACTIVATED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700169 if (uninstallAppFeatures(app)) {
170 log.info("Application {} has been deactivated", app.id().name());
171 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800172
173 } else if (type == APP_INSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700174 if (installAppArtifacts(app)) {
175 log.info("Application {} has been installed", app.id().name());
176 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800177
178 } else if (type == APP_UNINSTALLED) {
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700179 if (uninstallAppFeatures(app) || uninstallAppArtifacts(app)) {
180 log.info("Application {} has been uninstalled", app.id().name());
181 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800182
183 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700184 post(event);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800185
186 } catch (Exception e) {
187 log.warn("Unable to perform operation on application " + app.id().name(), e);
188 }
189 }
190 }
191
Thomas Vachuska0d6af8d2015-03-23 15:54:47 -0700192 // The following methods are fully synchronized to guard against remote vs.
193 // locally induced feature service interactions.
194
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700195 private synchronized boolean installAppArtifacts(Application app) throws Exception {
196 if (app.featuresRepo().isPresent() &&
197 featuresService.getRepository(app.featuresRepo().get()) == null) {
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800198 featuresService.addRepository(app.featuresRepo().get());
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700199 return true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800200 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700201 return false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800202 }
203
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700204 private synchronized boolean uninstallAppArtifacts(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.removeRepository(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 installAppFeatures(Application app) throws Exception {
214 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800215 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800216 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700217 if (feature != null && !featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800218 featuresService.installFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700219 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700220 } else if (feature == null && !initializing) {
221 // Suppress feature-not-found reporting during startup since these
222 // can arise naturally from the staggered cluster install.
223 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800224 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800225 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700226 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800227 }
228
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700229 private synchronized boolean uninstallAppFeatures(Application app) throws Exception {
230 boolean changed = false;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800231 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800232 Feature feature = featuresService.getFeature(name);
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700233 if (feature != null && featuresService.isInstalled(feature)) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800234 featuresService.uninstallFeature(name);
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700235 changed = true;
Thomas Vachuska62f04a42015-04-22 14:38:34 -0700236 } else if (feature == null) {
237 log.warn("Feature {} not found", name);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800238 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800239 }
Thomas Vachuska9ff88a92015-05-13 13:57:18 -0700240 return changed;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800241 }
242
243}