Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-persister-feature-adapter / src / main / java / org / opendaylight / controller / configpusherfeature / internal / FeatureConfigPusher.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.LinkedHashMultimap;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.LinkedHashSet;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.concurrent.TimeUnit;
20
21 import org.apache.karaf.features.Feature;
22 import org.apache.karaf.features.FeaturesService;
23 import org.opendaylight.controller.config.persist.api.ConfigPusher;
24 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /*
29  * Simple class to push configs to the config subsystem from Feature's configfiles
30  */
31 public class FeatureConfigPusher {
32     private static final Logger LOG = LoggerFactory.getLogger(FeatureConfigPusher.class);
33     private static final int MAX_RETRIES = 100;
34     private static final int RETRY_PAUSE_MILLIS = 1;
35
36     private FeaturesService featuresService = null;
37     private ConfigPusher pusher = null;
38
39     /*
40      * A LinkedHashSet (to preserve order and insure uniqueness) of the pushedConfigs
41      * This is used to prevent pushing duplicate configs if a Feature is in multiple dependency
42      * chains.  Also, preserves the *original* Feature chain for which we pushed the config.
43      * (which is handy for logging).
44      */
45     private final Set<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<>();
46
47     /*
48      * LinkedHashMultimap to track which configs we pushed for each Feature installation
49      * For future use
50      */
51     private final LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap
52             .create();
53
54     public FeatureConfigPusher(final ConfigPusher configPusher, final FeaturesService featuresService) {
55         pusher = configPusher;
56         this.featuresService = featuresService;
57     }
58
59     /*
60      * Push config files from Features to config subsystem
61      * @param features - list of Features to extract config files from recursively and push
62      * to the config subsystem
63      *
64      * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
65      * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
66      * (Ususally because it was not yet installed)
67      */
68     public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(
69             final List<Feature> features) throws Exception {
70         LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
71         for (Feature feature : features) {
72             Set<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
73             if (!configSnapShots.isEmpty()) {
74                 pushedFeatures.putAll(feature, configSnapShots);
75             }
76         }
77         return pushedFeatures;
78     }
79
80     private Set<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
81         // Ignore feature conditions — these encode conditions on other features and shouldn't be processed here
82         if (feature.getName().contains("-condition-")) {
83             LOG.debug("Ignoring conditional feature {}", feature);
84             return Collections.emptySet();
85         }
86         // pax-exam's Karaf container generates a wrapper feature holding the test dependencies. Ignore it.
87         if ("test-dependencies".equals(feature.getName())) {
88             LOG.debug("Ignoring pax-exam wrapper feature {}", feature);
89             return Collections.emptySet();
90         }
91
92         if (!isInstalled(feature)) {
93             return Collections.emptySet();
94         }
95         // FIXME Workaround for BUG-2836, features service returns null for feature:
96         // standard-condition-webconsole_0_0_0, 3.0.1
97         if (featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
98             LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature
99                     .getVersion());
100             return Collections.emptySet();
101         }
102
103         ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
104         Set<FeatureConfigSnapshotHolder> configs = wrappedFeature.getFeatureConfigSnapshotHolders();
105         if (!configs.isEmpty()) {
106             configs = pushConfig(configs, feature);
107             feature2configs.putAll(feature, configs);
108         }
109         return configs;
110     }
111
112     private Set<FeatureConfigSnapshotHolder> pushConfig(final Set<FeatureConfigSnapshotHolder> configs,
113                                                         final Feature feature) throws InterruptedException {
114         Set<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<>(configs);
115         configsToPush.removeAll(pushedConfigs);
116         if (!configsToPush.isEmpty()) {
117
118             // Ignore features that are present in persisted current config
119             final Optional<XmlFileStorageAdapter> currentCfgPusher = XmlFileStorageAdapter.getInstance();
120             if (currentCfgPusher.isPresent() && currentCfgPusher.get().getPersistedFeatures()
121                     .contains(feature.getId())) {
122                 LOG.warn("Ignoring default configuration {} for feature {}, the configuration is present in {}",
123                         configsToPush, feature.getId(), currentCfgPusher.get());
124             } else {
125                 pusher.pushConfigs(new ArrayList<>(configsToPush));
126             }
127             pushedConfigs.addAll(configsToPush);
128         }
129         Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
130         configsPushed.retainAll(configs);
131         return configsPushed;
132     }
133
134     @SuppressWarnings("IllegalCatch")
135     private boolean isInstalled(final Feature feature) throws InterruptedException {
136         for (int retries = 0; retries < MAX_RETRIES; retries++) {
137             try {
138                 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
139                 if (installedFeatures.contains(feature)) {
140                     return true;
141                 }
142
143                 LOG.info("Karaf Feature Service has not yet finished installing feature {}/{} (retry {})", feature
144                         .getName(), feature.getVersion(), retries);
145             } catch (final Exception e) {
146                 LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
147             }
148
149             TimeUnit.MILLISECONDS.sleep(RETRY_PAUSE_MILLIS);
150         }
151         LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet "
152                 + "finished installing feature {} {}", MAX_RETRIES, feature.getName(), feature.getVersion());
153         return false;
154     }
155 }