blob: 1d38215efd3bb95d6619b8f0cba54871989e1dab [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
72 @Activate
73 public void activate() {
74 store.setDelegate(delegate);
75 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
76 log.info("Started");
77 }
78
79 @Deactivate
80 public void deactivate() {
81 store.unsetDelegate(delegate);
82 eventDispatcher.removeSink(ApplicationEvent.class);
83 log.info("Stopped");
84 }
85
86 @Override
87 public Set<Application> getApplications() {
88 return store.getApplications();
89 }
90
91 @Override
92 public ApplicationId getId(String name) {
93 checkNotNull(name, "Name cannot be null");
94 return store.getId(name);
95 }
96
97 @Override
98 public Application getApplication(ApplicationId appId) {
99 checkNotNull(appId, APP_ID_NULL);
100 return store.getApplication(appId);
101 }
102
103 @Override
104 public ApplicationState getState(ApplicationId appId) {
105 checkNotNull(appId, APP_ID_NULL);
106 return store.getState(appId);
107 }
108
109 @Override
110 public Set<Permission> getPermissions(ApplicationId appId) {
111 checkNotNull(appId, APP_ID_NULL);
112 return store.getPermissions(appId);
113 }
114
115 @Override
116 public Application install(InputStream appDescStream) {
117 checkNotNull(appDescStream, "Application archive stream cannot be null");
118 return store.create(appDescStream);
119 }
120
121 @Override
122 public void uninstall(ApplicationId appId) {
123 checkNotNull(appId, APP_ID_NULL);
124 try {
125 store.remove(appId);
126 } catch (Exception e) {
127 log.warn("Unable to purge application directory for {}", appId.name());
128 }
129 }
130
131 @Override
132 public void activate(ApplicationId appId) {
133 checkNotNull(appId, APP_ID_NULL);
134 store.activate(appId);
135 }
136
137 @Override
138 public void deactivate(ApplicationId appId) {
139 checkNotNull(appId, APP_ID_NULL);
140 store.deactivate(appId);
141 }
142
143 @Override
144 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
145 checkNotNull(appId, APP_ID_NULL);
146 checkNotNull(permissions, "Permissions cannot be null");
147 store.setPermissions(appId, permissions);
148 }
149
150 @Override
151 public void addListener(ApplicationListener listener) {
152 listenerRegistry.addListener(listener);
153 }
154
155 @Override
156 public void removeListener(ApplicationListener listener) {
157 listenerRegistry.removeListener(listener);
158 }
159
160 private class InternalStoreDelegate implements ApplicationStoreDelegate {
161 @Override
162 public void notify(ApplicationEvent event) {
163 ApplicationEvent.Type type = event.type();
164 Application app = event.subject();
165 try {
166 if (type == APP_ACTIVATED) {
167 installAppFeatures(app);
168 log.info("Application {} has been activated", app.id().name());
169
170 } else if (type == APP_DEACTIVATED) {
171 uninstallAppFeatures(app);
172 log.info("Application {} has been deactivated", app.id().name());
173
174 } else if (type == APP_INSTALLED) {
175 installAppArtifacts(app);
176 log.info("Application {} has been installed", app.id().name());
177
178 } else if (type == APP_UNINSTALLED) {
179 uninstallAppFeatures(app);
180 uninstallAppArtifacts(app);
181 log.info("Application {} has been uninstalled", app.id().name());
182
183 }
184 eventDispatcher.post(event);
185
186 } catch (Exception e) {
187 log.warn("Unable to perform operation on application " + app.id().name(), e);
188 }
189 }
190 }
191
192 private void installAppArtifacts(Application app) throws Exception {
193 if (app.featuresRepo().isPresent()) {
194 featuresService.addRepository(app.featuresRepo().get());
195 }
196 }
197
198 private void uninstallAppArtifacts(Application app) throws Exception {
199 if (app.featuresRepo().isPresent()) {
200 featuresService.removeRepository(app.featuresRepo().get());
201 }
202 }
203
204 private void installAppFeatures(Application app) throws Exception {
205 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800206 Feature feature = featuresService.getFeature(name);
207 if (!featuresService.isInstalled(feature)) {
208 featuresService.installFeature(name);
209 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800210 }
211 }
212
213 private void uninstallAppFeatures(Application app) throws Exception {
214 for (String name : app.features()) {
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800215 Feature feature = featuresService.getFeature(name);
216 if (featuresService.isInstalled(feature)) {
217 featuresService.uninstallFeature(name);
218 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800219 }
220 }
221
222}