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