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 if(isInstalled(feature)) {
79 // FIXME Workaround for BUG-2836, features service returns null for feature: standard-condition-webconsole_0_0_0, 3.0.1
80 if(featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
81 LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature.getVersion());
83 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
84 configs = wrappedFeature.getFeatureConfigSnapshotHolders();
85 if (!configs.isEmpty()) {
86 configs = pushConfig(configs, feature);
87 feature2configs.putAll(feature, configs);
94 private boolean isInstalled(final Feature feature) {
95 for (int retries = 0; retries < MAX_RETRIES; retries++) {
97 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
98 if(installedFeatures.contains(feature)) {
101 LOG.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}",retries,feature.getName(),feature.getVersion());
103 } catch (Exception e) {
104 LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
107 Thread.sleep(RETRY_PAUSE_MILLIS);
108 } catch (InterruptedException e1) {
109 throw new IllegalStateException(e1);
112 LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",MAX_RETRIES,feature.getName(),feature.getVersion());
116 private Set<FeatureConfigSnapshotHolder> pushConfig(final Set<FeatureConfigSnapshotHolder> configs, final Feature feature)
117 throws InterruptedException {
118 Set<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<>(configs);
119 configsToPush.removeAll(pushedConfigs);
120 if (!configsToPush.isEmpty()) {
122 // Ignore features that are present in persisted current config
123 final Optional<XmlFileStorageAdapter> currentCfgPusher = XmlFileStorageAdapter.getInstance();
124 if (currentCfgPusher.isPresent() &&
125 currentCfgPusher.get().getPersistedFeatures().contains(feature.getId())) {
126 LOG.warn("Ignoring default configuration {} for feature {}, the configuration is present in {}",
127 configsToPush, feature.getId(), currentCfgPusher.get());
129 pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
132 pushedConfigs.addAll(configsToPush);
134 Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
135 configsPushed.retainAll(configs);
136 return configsPushed;