Removed sonar warnings.
[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 import java.util.Set;
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.FeatureListProvider;
18 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.ServiceReference;
21 import org.osgi.framework.ServiceRegistration;
22 import org.osgi.util.tracker.ServiceTrackerCustomizer;
23
24 public class FeatureServiceCustomizer implements ServiceTrackerCustomizer<FeaturesService, FeaturesService>, AutoCloseable {
25     private ConfigPusher configPusher = null;
26     private ConfigFeaturesListener configFeaturesListener = null;
27     private ServiceRegistration<?> registration;
28
29     FeatureServiceCustomizer(ConfigPusher c) {
30         configPusher = c;
31     }
32
33
34     @Override
35     public FeaturesService addingService(ServiceReference<FeaturesService> reference) {
36         BundleContext bc = reference.getBundle().getBundleContext();
37         final FeaturesService featureService = bc.getService(reference);
38         final Optional<XmlFileStorageAdapter> currentPersister = XmlFileStorageAdapter.getInstance();
39
40         if(XmlFileStorageAdapter.getInstance().isPresent()) {
41             final Set<String> installedFeatureIds = Sets.newHashSet();
42             for (final Feature installedFeature : featureService.listInstalledFeatures()) {
43                 installedFeatureIds.add(installedFeature.getId());
44             }
45
46             currentPersister.get().setFeaturesService(new FeatureListProvider() {
47                 @Override
48                 public Set<String> listFeatures() {
49                     return installedFeatureIds;
50                 }
51             });
52         }
53         configFeaturesListener = new ConfigFeaturesListener(configPusher,featureService);
54         registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
55         return featureService;
56     }
57
58     @Override
59     public void modifiedService(ServiceReference<FeaturesService> reference,
60             FeaturesService service) {
61         // we don't care if the properties change
62
63     }
64
65     @Override
66     public void removedService(ServiceReference<FeaturesService> reference,
67             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
79 }