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.Collections;
14 import java.util.LinkedHashSet;
15 import java.util.List;
17 import java.util.concurrent.TimeUnit;
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.storage.file.xml.XmlFileStorageAdapter;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Simple class to push configs to the config subsystem from Feature's configfiles
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;
33 private FeaturesService featuresService = null;
34 private ConfigPusher pusher = null;
37 * A LinkedHashSet (to preserve order and insure uniqueness) of the pushedConfigs
38 * This is used to prevent pushing duplicate configs if a Feature is in multiple dependency
39 * chains. Also, preserves the *original* Feature chain for which we pushed the config.
40 * (which is handy for logging).
42 private final Set<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<>();
45 * LinkedHashMultimap to track which configs we pushed for each Feature installation
48 private final LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap
51 public FeatureConfigPusher(final ConfigPusher configPusher, final FeaturesService featuresService) {
52 pusher = configPusher;
53 this.featuresService = featuresService;
57 * Push config files from Features to config subsystem
58 * @param features - list of Features to extract config files from recursively and push
59 * to the config subsystem
61 * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
62 * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
63 * (Ususally because it was not yet installed)
65 public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(
66 final List<Feature> features) throws Exception {
67 LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
68 for (Feature feature : features) {
69 Set<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
70 if (!configSnapShots.isEmpty()) {
71 pushedFeatures.putAll(feature, configSnapShots);
74 return pushedFeatures;
77 private Set<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
78 // Ignore feature conditions — these encode conditions on other features and shouldn't be processed here
79 if (feature.getName().contains("-condition-")) {
80 LOG.debug("Ignoring conditional feature {}", feature);
81 return Collections.emptySet();
83 // pax-exam's Karaf container generates a wrapper feature holding the test dependencies. Ignore it.
84 if ("test-dependencies".equals(feature.getName())) {
85 LOG.debug("Ignoring pax-exam wrapper feature {}", feature);
86 return Collections.emptySet();
89 if (!isInstalled(feature)) {
90 return Collections.emptySet();
92 // FIXME Workaround for BUG-2836, features service returns null for feature:
93 // standard-condition-webconsole_0_0_0, 3.0.1
94 if (featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
95 LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature
97 return Collections.emptySet();
100 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
101 Set<FeatureConfigSnapshotHolder> configs = wrappedFeature.getFeatureConfigSnapshotHolders();
102 if (!configs.isEmpty()) {
103 configs = pushConfig(configs, feature);
104 feature2configs.putAll(feature, configs);
109 private Set<FeatureConfigSnapshotHolder> pushConfig(final Set<FeatureConfigSnapshotHolder> configs,
110 final Feature feature) throws InterruptedException {
111 Set<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<>(configs);
112 configsToPush.removeAll(pushedConfigs);
113 if (!configsToPush.isEmpty()) {
115 // Ignore features that are present in persisted current config
116 final Optional<XmlFileStorageAdapter> currentCfgPusher = XmlFileStorageAdapter.getInstance();
117 if (currentCfgPusher.isPresent() && currentCfgPusher.get().getPersistedFeatures()
118 .contains(feature.getId())) {
119 LOG.warn("Ignoring default configuration {} for feature {}, the configuration is present in {}",
120 configsToPush, feature.getId(), currentCfgPusher.get());
122 pusher.pushConfigs(new ArrayList<>(configsToPush));
124 pushedConfigs.addAll(configsToPush);
126 Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
127 configsPushed.retainAll(configs);
128 return configsPushed;
131 @SuppressWarnings("IllegalCatch")
132 private boolean isInstalled(final Feature feature) throws InterruptedException {
133 for (int retries = 0; retries < MAX_RETRIES; retries++) {
135 // We process the installed features manually to handle the special "0.0.0" version
136 // which is the representation for null
137 for (Feature installedFeature : featuresService.listInstalledFeatures()) {
138 if (feature.equals(installedFeature)) {
141 if ("0.0.0".equals(feature.getVersion()) && feature.getName().equals(installedFeature.getName())
142 && !installedFeature.hasVersion()) {
147 LOG.debug("Karaf Feature Service has not yet finished installing feature {}/{} (retry {})", feature
148 .getName(), feature.getVersion(), retries);
149 } catch (final Exception e) {
150 LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
153 TimeUnit.MILLISECONDS.sleep(RETRY_PAUSE_MILLIS);
156 LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet "
157 + "finished installing feature {} {}", MAX_RETRIES, feature.getName(), feature.getVersion());