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>, AutoCloseable {
28 private static final Logger LOG = LoggerFactory.getLogger(FeatureServiceCustomizer.class);
29 private ConfigPusher configPusher = null;
30 private ServiceRegistration<?> registration;
32 FeatureServiceCustomizer(final ConfigPusher c) {
37 public FeaturesService addingService(final ServiceReference<FeaturesService> reference) {
38 BundleContext bc = reference.getBundle().getBundleContext();
39 final FeaturesService featureService = bc.getService(reference);
40 final Optional<XmlFileStorageAdapter> currentPersister = XmlFileStorageAdapter.getInstance();
42 if (XmlFileStorageAdapter.getInstance().isPresent()) {
43 final Set<String> installedFeatureIds = Sets.newHashSet();
45 for (final Feature installedFeature : featureService.listInstalledFeatures()) {
46 installedFeatureIds.add(installedFeature.getId());
48 } catch (final Exception e) {
49 LOG.error("Error listing installed features", e);
52 currentPersister.get().setFeaturesService(() -> installedFeatureIds);
54 ConfigFeaturesListener configFeaturesListener = new ConfigFeaturesListener(configPusher, featureService);
55 registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
56 return featureService;
60 public void modifiedService(final ServiceReference<FeaturesService> reference,
61 final FeaturesService service) {
62 // we don't care if the properties change
66 public void removedService(final ServiceReference<FeaturesService> reference,
67 final FeaturesService service) {
73 if (registration != null) {
74 registration.unregister();