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