2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.configpusherfeature.internal;
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;
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;
27 * Simple class to push configs to the config subsystem from Feature's configfiles
29 public class FeatureConfigPusher {
31 private static final Logger LOG = LoggerFactory.getLogger(FeatureConfigPusher.class);
33 private static final int MAX_RETRIES = 10;
34 private static final int RETRY_PAUSE_MILLIS = 100;
36 private final FeaturesService featuresService;
37 private final ConfigPusher pusher;
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).
45 Set<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<>();
48 * LinkedHashMultimap to track which configs we pushed for each Feature installation
51 LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap.create();
54 * @param p - ConfigPusher to push ConfigSnapshotHolders
56 public FeatureConfigPusher(final ConfigPusher pusher, final FeaturesService featuresService) {
58 this.featuresService = featuresService;
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
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)
69 public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(final List<Feature> features) throws Exception {
70 LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
71 for (Feature feature : features) {
74 Set<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
75 if (!configSnapShots.isEmpty()) {
76 pushedFeatures.putAll(feature, configSnapShots);
79 return pushedFeatures;
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());
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);
100 private boolean isInstalled(final Feature feature) {
101 Exception lastException = null;
102 for (int retries = 0; retries < MAX_RETRIES; retries++) {
104 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
105 if (installedFeatures.contains(feature)) {
108 LOG.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}", retries, feature.getName(), feature.getVersion());
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.
121 Thread.sleep(RETRY_PAUSE_MILLIS);
122 } catch (InterruptedException e1) {
123 throw new IllegalStateException(e1);
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);
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()) {
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());
145 pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
148 pushedConfigs.addAll(configsToPush);
150 Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
151 configsPushed.retainAll(configs);
152 return configsPushed;