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.FeatureListProvider;
20 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.ServiceReference;
23 import org.osgi.framework.ServiceRegistration;
24 import org.osgi.util.tracker.ServiceTrackerCustomizer;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class FeatureServiceCustomizer implements ServiceTrackerCustomizer<FeaturesService, FeaturesService>, AutoCloseable {
29 private static final Logger LOG = LoggerFactory.getLogger(FeatureServiceCustomizer.class);
30 private ConfigPusher configPusher = null;
31 private ServiceRegistration<?> registration;
33 FeatureServiceCustomizer(ConfigPusher c) {
38 public FeaturesService addingService(ServiceReference<FeaturesService> reference) {
39 BundleContext bc = reference.getBundle().getBundleContext();
40 final FeaturesService featureService = bc.getService(reference);
41 final Optional<XmlFileStorageAdapter> currentPersister = XmlFileStorageAdapter.getInstance();
43 if (XmlFileStorageAdapter.getInstance().isPresent()) {
44 final Set<String> installedFeatureIds = Sets.newHashSet();
46 for (final Feature installedFeature : featureService.listInstalledFeatures()) {
47 installedFeatureIds.add(installedFeature.getId());
49 } catch (Exception e) {
50 LOG.error("Error listing installed features", e);
53 currentPersister.get().setFeaturesService(new FeatureListProvider() {
55 public Set<String> listFeatures() {
56 return installedFeatureIds;
60 ConfigFeaturesListener configFeaturesListener = new ConfigFeaturesListener(configPusher, featureService);
61 registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
62 return featureService;
66 public void modifiedService(ServiceReference<FeaturesService> reference,
67 FeaturesService service) {
68 // we don't care if the properties change
72 public void removedService(ServiceReference<FeaturesService> reference,
73 FeaturesService service) {
79 if (registration != null) {
80 registration.unregister();