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.LinkedHashSet;
15 import java.util.List;
17 import org.apache.karaf.features.Feature;
18 import org.apache.karaf.features.FeaturesService;
19 import org.opendaylight.controller.config.persist.api.ConfigPusher;
20 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
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;
32 private FeaturesService featuresService = null;
33 private ConfigPusher pusher = null;
35 * A LinkedHashSet (to preserve order and insure uniqueness) of the pushedConfigs
36 * This is used to prevent pushing duplicate configs if a Feature is in multiple dependency
37 * chains. Also, preserves the *original* Feature chain for which we pushed the config.
38 * (which is handy for logging).
40 Set<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<>();
42 * LinkedHashMultimap to track which configs we pushed for each Feature installation
45 LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap.create();
48 * @param p - ConfigPusher to push ConfigSnapshotHolders
50 public FeatureConfigPusher(final ConfigPusher p, final FeaturesService f) {
55 * Push config files from Features to config subsystem
56 * @param features - list of Features to extract config files from recursively and push
57 * to the config subsystem
59 * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
60 * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
61 * (Ususally because it was not yet installed)
63 public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(final List<Feature> features) throws Exception {
64 LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
65 for (Feature feature : features) {
68 Set<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
69 if (!configSnapShots.isEmpty()) {
70 pushedFeatures.putAll(feature, configSnapShots);
73 return pushedFeatures;
76 private Set<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
77 Set<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<>();
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 } else if (isInstalled(feature)) {
82 // FIXME Workaround for BUG-2836, features service returns null for feature: standard-condition-webconsole_0_0_0, 3.0.1
83 if (featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
84 LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature.getVersion());
86 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
87 configs = wrappedFeature.getFeatureConfigSnapshotHolders();
88 if (!configs.isEmpty()) {
89 configs = pushConfig(configs, feature);
90 feature2configs.putAll(feature, configs);
97 private boolean isInstalled(final Feature feature) {
98 for (int retries = 0; retries < MAX_RETRIES; retries++) {
100 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
101 if(installedFeatures.contains(feature)) {
104 LOG.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}",retries,feature.getName(),feature.getVersion());
106 } catch (Exception e) {
107 LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
110 Thread.sleep(RETRY_PAUSE_MILLIS);
111 } catch (InterruptedException e1) {
112 throw new IllegalStateException(e1);
115 LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",MAX_RETRIES,feature.getName(),feature.getVersion());
119 private Set<FeatureConfigSnapshotHolder> pushConfig(final Set<FeatureConfigSnapshotHolder> configs, final Feature feature)
120 throws InterruptedException {
121 Set<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<>(configs);
122 configsToPush.removeAll(pushedConfigs);
123 if (!configsToPush.isEmpty()) {
125 // Ignore features that are present in persisted current config
126 final Optional<XmlFileStorageAdapter> currentCfgPusher = XmlFileStorageAdapter.getInstance();
127 if (currentCfgPusher.isPresent() &&
128 currentCfgPusher.get().getPersistedFeatures().contains(feature.getId())) {
129 LOG.warn("Ignoring default configuration {} for feature {}, the configuration is present in {}",
130 configsToPush, feature.getId(), currentCfgPusher.get());
132 pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
135 pushedConfigs.addAll(configsToPush);
137 Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
138 configsPushed.retainAll(configs);
139 return configsPushed;