2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.configpusherfeature.internal;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Sets;
13 import org.apache.karaf.features.Feature;
14 import org.apache.karaf.features.FeaturesListener;
15 import org.apache.karaf.features.FeaturesService;
16 import org.opendaylight.controller.config.persist.api.ConfigPusher;
17 import org.opendaylight.controller.config.persist.storage.file.xml.FeatureListProvider;
18 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.ServiceReference;
21 import org.osgi.framework.ServiceRegistration;
22 import org.osgi.util.tracker.ServiceTrackerCustomizer;
24 public class FeatureServiceCustomizer implements ServiceTrackerCustomizer<FeaturesService, FeaturesService>, AutoCloseable {
25 private ConfigPusher configPusher = null;
26 private ConfigFeaturesListener configFeaturesListener = null;
27 private ServiceRegistration<?> registration;
29 FeatureServiceCustomizer(ConfigPusher c) {
35 public FeaturesService addingService(ServiceReference<FeaturesService> reference) {
36 BundleContext bc = reference.getBundle().getBundleContext();
37 final FeaturesService featureService = bc.getService(reference);
38 final Optional<XmlFileStorageAdapter> currentPersister = XmlFileStorageAdapter.getInstance();
40 if(XmlFileStorageAdapter.getInstance().isPresent()) {
41 final Set<String> installedFeatureIds = Sets.newHashSet();
42 for (final Feature installedFeature : featureService.listInstalledFeatures()) {
43 installedFeatureIds.add(installedFeature.getId());
46 currentPersister.get().setFeaturesService(new FeatureListProvider() {
48 public Set<String> listFeatures() {
49 return installedFeatureIds;
53 configFeaturesListener = new ConfigFeaturesListener(configPusher,featureService);
54 registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
55 return featureService;
59 public void modifiedService(ServiceReference<FeaturesService> reference,
60 FeaturesService service) {
61 // we don't care if the properties change
66 public void removedService(ServiceReference<FeaturesService> reference,
67 FeaturesService service) {
73 if(registration != null) {
74 registration.unregister();