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;
15 import org.apache.karaf.features.Feature;
16 import org.apache.karaf.features.FeaturesListener;
17 import org.apache.karaf.features.FeaturesService;
18 import org.opendaylight.controller.config.persist.api.ConfigPusher;
19 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceReference;
22 import org.osgi.framework.ServiceRegistration;
23 import org.osgi.util.tracker.ServiceTrackerCustomizer;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class FeatureServiceCustomizer implements ServiceTrackerCustomizer<FeaturesService, FeaturesService>,
29 private static final Logger LOG = LoggerFactory.getLogger(FeatureServiceCustomizer.class);
30 private ConfigPusher configPusher = null;
31 private ServiceRegistration<?> registration;
33 FeatureServiceCustomizer(final ConfigPusher configPusher) {
34 this.configPusher = configPusher;
38 @SuppressWarnings("IllegalCatch")
39 public FeaturesService addingService(final ServiceReference<FeaturesService> reference) {
40 BundleContext bc = reference.getBundle().getBundleContext();
41 final FeaturesService featureService = bc.getService(reference);
42 final Optional<XmlFileStorageAdapter> currentPersister = XmlFileStorageAdapter.getInstance();
44 if (XmlFileStorageAdapter.getInstance().isPresent()) {
45 final Set<String> installedFeatureIds = Sets.newHashSet();
47 for (final Feature installedFeature : featureService.listInstalledFeatures()) {
48 installedFeatureIds.add(installedFeature.getId());
50 } catch (final Exception e) {
51 LOG.error("Error listing installed features", e);
54 currentPersister.get().setFeaturesService(() -> installedFeatureIds);
56 ConfigFeaturesListener configFeaturesListener = new ConfigFeaturesListener(configPusher, featureService);
57 registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
58 return featureService;
62 public void modifiedService(final ServiceReference<FeaturesService> reference, final FeaturesService service) {
63 // we don't care if the properties change
67 public void removedService(final ServiceReference<FeaturesService> reference, final FeaturesService service) {
73 if (registration != null) {
74 registration.unregister();