blob: 08c91a4132a54b81bde83ca8bd602f093714e987 [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;
24import org.apache.karaf.features.FeaturesService;
25import org.onosproject.app.ApplicationAdminService;
26import org.onosproject.app.ApplicationEvent;
27import org.onosproject.app.ApplicationListener;
28import org.onosproject.app.ApplicationService;
29import org.onosproject.app.ApplicationState;
30import org.onosproject.app.ApplicationStore;
31import org.onosproject.app.ApplicationStoreDelegate;
32import org.onosproject.core.Application;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.Permission;
35import org.onosproject.event.AbstractListenerRegistry;
36import org.onosproject.event.EventDeliveryService;
37import 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.*;
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
47 * Implementation of the application management service.
48 */
49@Component(immediate = true)
50@Service
51public class ApplicationManager implements ApplicationService, ApplicationAdminService {
52
53 private final Logger log = getLogger(getClass());
54
55 private static final String APP_ID_NULL = "Application ID cannot be null";
56
57 protected final AbstractListenerRegistry<ApplicationEvent, ApplicationListener>
58 listenerRegistry = new AbstractListenerRegistry<>();
59
60 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
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected EventDeliveryService eventDispatcher;
70
71 @Activate
72 public void activate() {
73 store.setDelegate(delegate);
74 eventDispatcher.addSink(ApplicationEvent.class, listenerRegistry);
75 log.info("Started");
76 }
77
78 @Deactivate
79 public void deactivate() {
80 store.unsetDelegate(delegate);
81 eventDispatcher.removeSink(ApplicationEvent.class);
82 log.info("Stopped");
83 }
84
85 @Override
86 public Set<Application> getApplications() {
87 return store.getApplications();
88 }
89
90 @Override
91 public ApplicationId getId(String name) {
92 checkNotNull(name, "Name cannot be null");
93 return store.getId(name);
94 }
95
96 @Override
97 public Application getApplication(ApplicationId appId) {
98 checkNotNull(appId, APP_ID_NULL);
99 return store.getApplication(appId);
100 }
101
102 @Override
103 public ApplicationState getState(ApplicationId appId) {
104 checkNotNull(appId, APP_ID_NULL);
105 return store.getState(appId);
106 }
107
108 @Override
109 public Set<Permission> getPermissions(ApplicationId appId) {
110 checkNotNull(appId, APP_ID_NULL);
111 return store.getPermissions(appId);
112 }
113
114 @Override
115 public Application install(InputStream appDescStream) {
116 checkNotNull(appDescStream, "Application archive stream cannot be null");
117 return store.create(appDescStream);
118 }
119
120 @Override
121 public void uninstall(ApplicationId appId) {
122 checkNotNull(appId, APP_ID_NULL);
123 try {
124 store.remove(appId);
125 } catch (Exception e) {
126 log.warn("Unable to purge application directory for {}", appId.name());
127 }
128 }
129
130 @Override
131 public void activate(ApplicationId appId) {
132 checkNotNull(appId, APP_ID_NULL);
133 store.activate(appId);
134 }
135
136 @Override
137 public void deactivate(ApplicationId appId) {
138 checkNotNull(appId, APP_ID_NULL);
139 store.deactivate(appId);
140 }
141
142 @Override
143 public void setPermissions(ApplicationId appId, Set<Permission> permissions) {
144 checkNotNull(appId, APP_ID_NULL);
145 checkNotNull(permissions, "Permissions cannot be null");
146 store.setPermissions(appId, permissions);
147 }
148
149 @Override
150 public void addListener(ApplicationListener listener) {
151 listenerRegistry.addListener(listener);
152 }
153
154 @Override
155 public void removeListener(ApplicationListener listener) {
156 listenerRegistry.removeListener(listener);
157 }
158
159 private class InternalStoreDelegate implements ApplicationStoreDelegate {
160 @Override
161 public void notify(ApplicationEvent event) {
162 ApplicationEvent.Type type = event.type();
163 Application app = event.subject();
164 try {
165 if (type == APP_ACTIVATED) {
166 installAppFeatures(app);
167 log.info("Application {} has been activated", app.id().name());
168
169 } else if (type == APP_DEACTIVATED) {
170 uninstallAppFeatures(app);
171 log.info("Application {} has been deactivated", app.id().name());
172
173 } else if (type == APP_INSTALLED) {
174 installAppArtifacts(app);
175 log.info("Application {} has been installed", app.id().name());
176
177 } else if (type == APP_UNINSTALLED) {
178 uninstallAppFeatures(app);
179 uninstallAppArtifacts(app);
180 log.info("Application {} has been uninstalled", app.id().name());
181
182 }
183 eventDispatcher.post(event);
184
185 } catch (Exception e) {
186 log.warn("Unable to perform operation on application " + app.id().name(), e);
187 }
188 }
189 }
190
191 private void installAppArtifacts(Application app) throws Exception {
192 if (app.featuresRepo().isPresent()) {
193 featuresService.addRepository(app.featuresRepo().get());
194 }
195 }
196
197 private void uninstallAppArtifacts(Application app) throws Exception {
198 if (app.featuresRepo().isPresent()) {
199 featuresService.removeRepository(app.featuresRepo().get());
200 }
201 }
202
203 private void installAppFeatures(Application app) throws Exception {
204 for (String name : app.features()) {
205 featuresService.installFeature(name);
206 }
207 }
208
209 private void uninstallAppFeatures(Application app) throws Exception {
210 for (String name : app.features()) {
211 featuresService.uninstallFeature(name);
212 }
213 }
214
215}