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