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.Collections;
15 import java.util.LinkedHashSet;
16 import java.util.List;
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;
27 * Simple class to push configs to the config subsystem from Feature's configfiles
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;
34 private FeaturesService featuresService = null;
35 private ConfigPusher pusher = null;
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).
43 private final Set<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<>();
46 * LinkedHashMultimap to track which configs we pushed for each Feature installation
49 private final LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap
52 public FeatureConfigPusher(final ConfigPusher configPusher, final FeaturesService featuresService) {
53 pusher = configPusher;
54 this.featuresService = featuresService;
58 * Push config files from Features to config subsystem
59 * @param features - list of Features to extract config files from recursively and push
60 * to the config subsystem
62 * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
63 * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
64 * (Ususally because it was not yet installed)
66 public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(
67 final List<Feature> features) throws Exception {
68 LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
69 for (Feature feature : features) {
70 Set<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
71 if (!configSnapShots.isEmpty()) {
72 pushedFeatures.putAll(feature, configSnapShots);
75 return pushedFeatures;
78 private Set<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
79 // Ignore feature conditions — these encode conditions on other features and shouldn't be processed here
80 if (feature.getName().contains("-condition-")) {
81 LOG.debug("Ignoring conditional feature {}", feature);
82 return Collections.emptySet();
84 // pax-exam's Karaf container generates a wrapper feature holding the test dependencies. Ignore it.
85 if ("test-dependencies".equals(feature.getName())) {
86 LOG.debug("Ignoring pax-exam wrapper feature {}", feature);
87 return Collections.emptySet();
90 if (!isInstalled(feature)) {
91 return Collections.emptySet();
93 // FIXME Workaround for BUG-2836, features service returns null for feature:
94 // standard-condition-webconsole_0_0_0, 3.0.1
95 if (featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
96 LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature
98 return Collections.emptySet();
101 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
102 Set<FeatureConfigSnapshotHolder> configs = wrappedFeature.getFeatureConfigSnapshotHolders();
103 if (!configs.isEmpty()) {
104 configs = pushConfig(configs, feature);
105 feature2configs.putAll(feature, configs);
110 private Set<FeatureConfigSnapshotHolder> pushConfig(final Set<FeatureConfigSnapshotHolder> configs,
111 final Feature feature) throws InterruptedException {
112 Set<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<>(configs);
113 configsToPush.removeAll(pushedConfigs);
114 if (!configsToPush.isEmpty()) {
116 // Ignore features that are present in persisted current config
117 final Optional<XmlFileStorageAdapter> currentCfgPusher = XmlFileStorageAdapter.getInstance();
118 if (currentCfgPusher.isPresent() && currentCfgPusher.get().getPersistedFeatures()
119 .contains(feature.getId())) {
120 LOG.warn("Ignoring default configuration {} for feature {}, the configuration is present in {}",
121 configsToPush, feature.getId(), currentCfgPusher.get());
123 pusher.pushConfigs(new ArrayList<>(configsToPush));
125 pushedConfigs.addAll(configsToPush);
127 Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
128 configsPushed.retainAll(configs);
129 return configsPushed;
132 @SuppressWarnings("IllegalCatch")
133 private boolean isInstalled(final Feature feature) throws InterruptedException {
134 for (int retries = 0; retries < MAX_RETRIES; retries++) {
136 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
137 if (installedFeatures.contains(feature)) {
141 LOG.debug("Karaf Feature Service has not yet finished installing feature {}/{} (retry {})", feature
142 .getName(), feature.getVersion(), retries);
143 } catch (final Exception e) {
144 LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
147 TimeUnit.MILLISECONDS.sleep(RETRY_PAUSE_MILLIS);
150 // In karaf 4.1.3 we see this error logged for 2 system features, "jaas-boot" and "wrap", many times. It's
151 // unclear why listInstalledFeatures doesn't return them but it doesn't really matter since we're only
152 // interested in ODL features that have CSS files. Maybe the common denominator is that they don't have a
153 // version (ie it's 0.0.0) hence the following check to avoid logging the error. This check would not
154 // exclude any ODL feature since all ODL features are versioned (should be anyway).
155 if (!"0.0.0".equals(feature.getVersion())) {
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());