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.create();
52 * @param p - ConfigPusher to push ConfigSnapshotHolders
54 public FeatureConfigPusher(final ConfigPusher p, final FeaturesService f) {
59 * Push config files from Features to config subsystem
60 * @param features - list of Features to extract config files from recursively and push
61 * to the config subsystem
63 * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
64 * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
65 * (Ususally because it was not yet installed)
67 public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(final List<Feature> features)
69 LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
70 for (Feature feature : features) {
71 Set<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
72 if (!configSnapShots.isEmpty()) {
73 pushedFeatures.putAll(feature, configSnapShots);
76 return pushedFeatures;
79 private Set<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
80 // Ignore feature conditions — these encode conditions on other features and shouldn't be processed here
81 if (feature.getName().contains("-condition-")) {
82 LOG.debug("Ignoring conditional feature {}", feature);
83 return Collections.emptySet();
85 // pax-exam's Karaf container generates a wrapper feature holding the test dependencies. Ignore it.
86 if ("test-dependencies".equals(feature.getName())) {
87 LOG.debug("Ignoring pax-exam wrapper feature {}", feature);
88 return Collections.emptySet();
91 if (!isInstalled(feature)) {
92 return Collections.emptySet();
94 // FIXME Workaround for BUG-2836, features service returns null for feature:
95 // standard-condition-webconsole_0_0_0, 3.0.1
96 if (featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
97 LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(),
98 feature.getVersion());
99 return Collections.emptySet();
102 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
103 Set<FeatureConfigSnapshotHolder> configs = wrappedFeature.getFeatureConfigSnapshotHolders();
104 if (!configs.isEmpty()) {
105 configs = pushConfig(configs, feature);
106 feature2configs.putAll(feature, configs);
111 private boolean isInstalled(final Feature feature) throws InterruptedException {
112 for (int retries = 0; retries < MAX_RETRIES; retries++) {
114 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
115 if (installedFeatures.contains(feature)) {
119 LOG.info("Karaf Feature Service has not yet finished installing feature {}/{} (retry {})",
120 feature.getName(), feature.getVersion(), retries);
121 } catch (Exception e) {
122 LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
125 TimeUnit.MILLISECONDS.sleep(RETRY_PAUSE_MILLIS);
127 LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",
128 MAX_RETRIES, feature.getName(), feature.getVersion());
132 private Set<FeatureConfigSnapshotHolder> pushConfig(final Set<FeatureConfigSnapshotHolder> configs,
133 final Feature feature) 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<>(configsToPush));
148 pushedConfigs.addAll(configsToPush);
150 Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
151 configsPushed.retainAll(configs);
152 return configsPushed;