Bug 6787: FeatureConfigPusher confusing WARN log removed
[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.ConcurrentModificationException;
15 import java.util.LinkedHashSet;
16 import java.util.List;
17 import java.util.Set;
18 import org.apache.karaf.features.Feature;
19 import org.apache.karaf.features.FeaturesService;
20 import org.opendaylight.controller.config.persist.api.ConfigPusher;
21 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
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
31     private static final Logger LOG = LoggerFactory.getLogger(FeatureConfigPusher.class);
32
33     private static final int MAX_RETRIES = 10;
34     private static final int RETRY_PAUSE_MILLIS = 100;
35
36     private final FeaturesService featuresService;
37     private final ConfigPusher pusher;
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     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     LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap.create();
52
53     /*
54      * @param p - ConfigPusher to push ConfigSnapshotHolders
55      */
56     public FeatureConfigPusher(final ConfigPusher pusher, final FeaturesService featuresService) {
57         this.pusher = pusher;
58         this.featuresService = featuresService;
59     }
60     /*
61      * Push config files from Features to config subsystem
62      * @param features - list of Features to extract config files from recursively and push
63      * to the config subsystem
64      *
65      * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
66      * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
67      * (Ususally because it was not yet installed)
68      */
69     public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(final List<Feature> features) throws Exception {
70         LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
71         for (Feature feature : features) {
72
73
74             Set<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
75             if (!configSnapShots.isEmpty()) {
76                 pushedFeatures.putAll(feature, configSnapShots);
77             }
78         }
79         return pushedFeatures;
80     }
81
82     private Set<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
83         Set<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<>();
84         if(isInstalled(feature)) {
85             // FIXME Workaround for BUG-2836, features service returns null for feature: standard-condition-webconsole_0_0_0, 3.0.1
86             if(featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
87                 LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature.getVersion());
88             } else {
89                 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
90                 configs = wrappedFeature.getFeatureConfigSnapshotHolders();
91                 if (!configs.isEmpty()) {
92                     configs = pushConfig(configs, feature);
93                     feature2configs.putAll(feature, configs);
94                 }
95             }
96         }
97         return configs;
98     }
99
100     private boolean isInstalled(final Feature feature) {
101         Exception lastException = null;
102         for (int retries = 0; retries < MAX_RETRIES; retries++) {
103             try {
104                 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
105                 if (installedFeatures.contains(feature)) {
106                     return true;
107                 } else {
108                     LOG.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}", retries, feature.getName(), feature.getVersion());
109                 }
110             // TODO This catch of ConcurrentModificationException may be able to simply be removed after
111             // we're fully on Karaf 4 only, as a comment in BUG-6787 indicates that (in Karaf 4) :
112             // "the 'installed' Map of FeaturesServiceImpl .. appears to be correctly synchronized/thread-safe".
113             // (Or, if it's still NOK, then it could be fixed properly upstream in Karaf once we're on recent.)
114             } catch (ConcurrentModificationException e) {
115                 // BUG-6787 experience shows that a LOG.warn (or info) here is very confusing to end-users;
116                 // as we have a retry loop anyway, there is no point informing (and confusing) users of this
117                 // intermediate state of, so ... NOOP, do not log here.
118                 lastException = e;
119             }
120             try {
121                 Thread.sleep(RETRY_PAUSE_MILLIS);
122             } catch (InterruptedException e1) {
123                 throw new IllegalStateException(e1);
124             }
125         }
126         LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() "
127                         + "which has not yet finished installing feature {} {} (stack trace is last exception caught)",
128                 MAX_RETRIES, feature.getName(), feature.getVersion(), lastException);
129         return false;
130     }
131
132     private Set<FeatureConfigSnapshotHolder> pushConfig(final Set<FeatureConfigSnapshotHolder> configs, final Feature feature)
133             throws InterruptedException {
134         Set<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<>(configs);
135         configsToPush.removeAll(pushedConfigs);
136         if (!configsToPush.isEmpty()) {
137
138             // Ignore features that are present in persisted current config
139             final Optional<XmlFileStorageAdapter> currentCfgPusher = XmlFileStorageAdapter.getInstance();
140             if (currentCfgPusher.isPresent() &&
141                     currentCfgPusher.get().getPersistedFeatures().contains(feature.getId())) {
142                 LOG.warn("Ignoring default configuration {} for feature {}, the configuration is present in {}",
143                         configsToPush, feature.getId(), currentCfgPusher.get());
144             } else {
145                 pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
146             }
147
148             pushedConfigs.addAll(configsToPush);
149         }
150         Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
151         configsPushed.retainAll(configs);
152         return configsPushed;
153     }
154 }

©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.