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;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.LinkedHashSet;
17 import java.util.List;
19 import java.util.concurrent.TimeUnit;
21 import org.apache.karaf.features.Feature;
22 import org.apache.karaf.features.FeaturesService;
23 import org.opendaylight.controller.config.persist.api.ConfigPusher;
24 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Simple class to push configs to the config subsystem from Feature's configfiles
31 public class FeatureConfigPusher {
32 private static final Logger LOG = LoggerFactory.getLogger(FeatureConfigPusher.class);
33 private static final int MAX_RETRIES = 100;
34 private static final int RETRY_PAUSE_MILLIS = 1;
36 private FeaturesService featuresService = null;
37 private ConfigPusher pusher = null;
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 private final Set<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<>();
48 * LinkedHashMultimap to track which configs we pushed for each Feature installation
51 private final LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap
54 public FeatureConfigPusher(final ConfigPusher configPusher, final FeaturesService featuresService) {
55 pusher = configPusher;
56 this.featuresService = featuresService;
60 * Push config files from Features to config subsystem
61 * @param features - list of Features to extract config files from recursively and push
62 * to the config subsystem
64 * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
65 * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
66 * (Ususally because it was not yet installed)
68 public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(
69 final List<Feature> features) throws Exception {
70 LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
71 for (Feature feature : features) {
72 Set<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
73 if (!configSnapShots.isEmpty()) {
74 pushedFeatures.putAll(feature, configSnapShots);
77 return pushedFeatures;
80 private Set<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
81 // Ignore feature conditions — these encode conditions on other features and shouldn't be processed here
82 if (feature.getName().contains("-condition-")) {
83 LOG.debug("Ignoring conditional feature {}", feature);
84 return Collections.emptySet();
86 // pax-exam's Karaf container generates a wrapper feature holding the test dependencies. Ignore it.
87 if ("test-dependencies".equals(feature.getName())) {
88 LOG.debug("Ignoring pax-exam wrapper feature {}", feature);
89 return Collections.emptySet();
92 if (!isInstalled(feature)) {
93 return Collections.emptySet();
95 // FIXME Workaround for BUG-2836, features service returns null for feature:
96 // standard-condition-webconsole_0_0_0, 3.0.1
97 if (featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
98 LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature
100 return Collections.emptySet();
103 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
104 Set<FeatureConfigSnapshotHolder> configs = wrappedFeature.getFeatureConfigSnapshotHolders();
105 if (!configs.isEmpty()) {
106 configs = pushConfig(configs, feature);
107 feature2configs.putAll(feature, configs);
112 private Set<FeatureConfigSnapshotHolder> pushConfig(final Set<FeatureConfigSnapshotHolder> configs,
113 final Feature feature) throws InterruptedException {
114 Set<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<>(configs);
115 configsToPush.removeAll(pushedConfigs);
116 if (!configsToPush.isEmpty()) {
118 // Ignore features that are present in persisted current config
119 final Optional<XmlFileStorageAdapter> currentCfgPusher = XmlFileStorageAdapter.getInstance();
120 if (currentCfgPusher.isPresent() && currentCfgPusher.get().getPersistedFeatures()
121 .contains(feature.getId())) {
122 LOG.warn("Ignoring default configuration {} for feature {}, the configuration is present in {}",
123 configsToPush, feature.getId(), currentCfgPusher.get());
125 pusher.pushConfigs(new ArrayList<>(configsToPush));
127 pushedConfigs.addAll(configsToPush);
129 Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
130 configsPushed.retainAll(configs);
131 return configsPushed;
134 @SuppressWarnings("IllegalCatch")
135 private boolean isInstalled(final Feature feature) throws InterruptedException {
136 for (int retries = 0; retries < MAX_RETRIES; retries++) {
138 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
139 if (installedFeatures.contains(feature)) {
143 LOG.info("Karaf Feature Service has not yet finished installing feature {}/{} (retry {})", feature
144 .getName(), feature.getVersion(), retries);
145 } catch (final Exception e) {
146 LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
149 TimeUnit.MILLISECONDS.sleep(RETRY_PAUSE_MILLIS);
151 LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet "
152 + "finished installing feature {} {}", MAX_RETRIES, feature.getName(), feature.getVersion());