Add Karaf 4 features
[controller.git] / opendaylight / config / config-persister-feature4-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.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;
27
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;
32
33     FeatureServiceCustomizer(ConfigPusher c) {
34         configPusher = c;
35     }
36
37     @Override
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();
42
43         if (XmlFileStorageAdapter.getInstance().isPresent()) {
44             final Set<String> installedFeatureIds = Sets.newHashSet();
45             try {
46                 for (final Feature installedFeature : featureService.listInstalledFeatures()) {
47                     installedFeatureIds.add(installedFeature.getId());
48                 }
49             } catch (Exception e) {
50                 LOG.error("Error listing installed features", e);
51             }
52
53             currentPersister.get().setFeaturesService(new FeatureListProvider() {
54                 @Override
55                 public Set<String> listFeatures() {
56                     return installedFeatureIds;
57                 }
58             });
59         }
60         ConfigFeaturesListener configFeaturesListener = new ConfigFeaturesListener(configPusher, featureService);
61         registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
62         return featureService;
63     }
64
65     @Override
66     public void modifiedService(ServiceReference<FeaturesService> reference,
67                                 FeaturesService service) {
68         // we don't care if the properties change
69     }
70
71     @Override
72     public void removedService(ServiceReference<FeaturesService> reference,
73                                FeaturesService service) {
74         close();
75     }
76
77     @Override
78     public void close() {
79         if (registration != null) {
80             registration.unregister();
81             registration = null;
82         }
83     }
84 }