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.XmlFileStorageAdapter;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceReference;
20 import org.osgi.framework.ServiceRegistration;
21 import org.osgi.util.tracker.ServiceTrackerCustomizer;
23 public class FeatureServiceCustomizer implements ServiceTrackerCustomizer<FeaturesService, FeaturesService>, AutoCloseable {
24 private ConfigPusher configPusher = null;
25 private ServiceRegistration<?> registration;
27 FeatureServiceCustomizer(final ConfigPusher c) {
32 public FeaturesService addingService(final ServiceReference<FeaturesService> reference) {
33 BundleContext bc = reference.getBundle().getBundleContext();
34 final FeaturesService featureService = bc.getService(reference);
35 final Optional<XmlFileStorageAdapter> currentPersister = XmlFileStorageAdapter.getInstance();
37 if(XmlFileStorageAdapter.getInstance().isPresent()) {
38 final Set<String> installedFeatureIds = Sets.newHashSet();
39 for (final Feature installedFeature : featureService.listInstalledFeatures()) {
40 installedFeatureIds.add(installedFeature.getId());
43 currentPersister.get().setFeaturesService(() -> installedFeatureIds);
45 ConfigFeaturesListener configFeaturesListener = new ConfigFeaturesListener(configPusher, featureService);
46 registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
47 return featureService;
51 public void modifiedService(final ServiceReference<FeaturesService> reference,
52 final FeaturesService service) {
53 // we don't care if the properties change
57 public void removedService(final ServiceReference<FeaturesService> reference,
58 final FeaturesService service) {
64 if(registration != null) {
65 registration.unregister();