config-persister-feature-adapter: final parameters
[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 ServiceRegistration<?> registration;
27
28     FeatureServiceCustomizer(final ConfigPusher c) {
29         configPusher = c;
30     }
31
32     @Override
33     public FeaturesService addingService(final ServiceReference<FeaturesService> reference) {
34         BundleContext bc = reference.getBundle().getBundleContext();
35         final FeaturesService featureService = bc.getService(reference);
36         final Optional<XmlFileStorageAdapter> currentPersister = XmlFileStorageAdapter.getInstance();
37
38         if(XmlFileStorageAdapter.getInstance().isPresent()) {
39             final Set<String> installedFeatureIds = Sets.newHashSet();
40             for (final Feature installedFeature : featureService.listInstalledFeatures()) {
41                 installedFeatureIds.add(installedFeature.getId());
42             }
43
44             currentPersister.get().setFeaturesService(new FeatureListProvider() {
45                 @Override
46                 public Set<String> listFeatures() {
47                     return installedFeatureIds;
48                 }
49             });
50         }
51         ConfigFeaturesListener configFeaturesListener = new ConfigFeaturesListener(configPusher, featureService);
52         registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
53         return featureService;
54     }
55
56     @Override
57     public void modifiedService(final ServiceReference<FeaturesService> reference,
58             final FeaturesService service) {
59         // we don't care if the properties change
60     }
61
62     @Override
63     public void removedService(final ServiceReference<FeaturesService> reference,
64             final FeaturesService service) {
65         close();
66     }
67
68     @Override
69     public void close() {
70         if(registration != null) {
71             registration.unregister();
72             registration = null;
73         }
74     }
75 }