68ae3d31cbafd2776cfa17fd645f14e685ac63e4
[controller.git] / opendaylight / config / config-persister-feature-adapter / src / main / java / org / opendaylight / controller / configpusherfeature / internal / FeatureServiceCustomizer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.configpusherfeature.internal;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Sets;
12
13 import java.util.Set;
14
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;
26
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;
31
32     FeatureServiceCustomizer(final ConfigPusher c) {
33         configPusher = c;
34     }
35
36     @Override
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();
41
42         if (XmlFileStorageAdapter.getInstance().isPresent()) {
43             final Set<String> installedFeatureIds = Sets.newHashSet();
44             try {
45                 for (final Feature installedFeature : featureService.listInstalledFeatures()) {
46                     installedFeatureIds.add(installedFeature.getId());
47                 }
48             } catch (final Exception e) {
49                 LOG.error("Error listing installed features", e);
50             }
51
52             currentPersister.get().setFeaturesService(() -> installedFeatureIds);
53         }
54         ConfigFeaturesListener configFeaturesListener = new ConfigFeaturesListener(configPusher, featureService);
55         registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
56         return featureService;
57     }
58
59     @Override
60     public void modifiedService(final ServiceReference<FeaturesService> reference,
61                                 final FeaturesService service) {
62         // we don't care if the properties change
63     }
64
65     @Override
66     public void removedService(final ServiceReference<FeaturesService> reference,
67                                final FeaturesService service) {
68         close();
69     }
70
71     @Override
72     public void close() {
73         if (registration != null) {
74             registration.unregister();
75             registration = null;
76         }
77     }
78 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.