Fix checkstyle issues to enforce it
[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>,
28         AutoCloseable {
29     private static final Logger LOG = LoggerFactory.getLogger(FeatureServiceCustomizer.class);
30     private ConfigPusher configPusher = null;
31     private ServiceRegistration<?> registration;
32
33     FeatureServiceCustomizer(final ConfigPusher configPusher) {
34         this.configPusher = configPusher;
35     }
36
37     @Override
38     @SuppressWarnings("IllegalCatch")
39     public FeaturesService addingService(final ServiceReference<FeaturesService> reference) {
40         BundleContext bc = reference.getBundle().getBundleContext();
41         final FeaturesService featureService = bc.getService(reference);
42         final Optional<XmlFileStorageAdapter> currentPersister = XmlFileStorageAdapter.getInstance();
43
44         if (XmlFileStorageAdapter.getInstance().isPresent()) {
45             final Set<String> installedFeatureIds = Sets.newHashSet();
46             try {
47                 for (final Feature installedFeature : featureService.listInstalledFeatures()) {
48                     installedFeatureIds.add(installedFeature.getId());
49                 }
50             } catch (final Exception e) {
51                 LOG.error("Error listing installed features", e);
52             }
53
54             currentPersister.get().setFeaturesService(() -> installedFeatureIds);
55         }
56         ConfigFeaturesListener configFeaturesListener = new ConfigFeaturesListener(configPusher, featureService);
57         registration = bc.registerService(FeaturesListener.class.getCanonicalName(), configFeaturesListener, null);
58         return featureService;
59     }
60
61     @Override
62     public void modifiedService(final ServiceReference<FeaturesService> reference, final FeaturesService service) {
63         // we don't care if the properties change
64     }
65
66     @Override
67     public void removedService(final ServiceReference<FeaturesService> reference, 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.