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;
16 import org.apache.karaf.features.Feature;
17 import org.apache.karaf.features.FeaturesService;
18 import org.opendaylight.controller.config.persist.api.ConfigPusher;
19 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
20 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Simple class to push configs to the config subsystem from Feature's configfiles
27 public class FeatureConfigPusher {
28 private static final Logger LOG = LoggerFactory.getLogger(FeatureConfigPusher.class);
29 private static final int MAX_RETRIES=100;
30 private static final int RETRY_PAUSE_MILLIS=1;
31 private FeaturesService featuresService = null;
32 private ConfigPusher pusher = null;
34 * A LinkedHashSet (to preserve order and insure uniqueness) of the pushedConfigs
35 * This is used to prevent pushing duplicate configs if a Feature is in multiple dependency
36 * chains. Also, preserves the *original* Feature chain for which we pushed the config.
37 * (which is handy for logging).
39 LinkedHashSet<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
41 * LinkedHashMultimap to track which configs we pushed for each Feature installation
44 LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap.create();
47 * @param p - ConfigPusher to push ConfigSnapshotHolders
49 public FeatureConfigPusher(final ConfigPusher p, final FeaturesService f) {
54 * Push config files from Features to config subsystem
55 * @param features - list of Features to extract config files from recursively and push
56 * to the config subsystem
58 * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
59 * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
60 * (Ususally because it was not yet installed)
62 public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(final List<Feature> features) throws Exception {
63 LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
64 for (Feature feature : features) {
67 LinkedHashSet<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
68 if (!configSnapShots.isEmpty()) {
69 pushedFeatures.putAll(feature, configSnapShots);
72 return pushedFeatures;
75 private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
76 LinkedHashSet<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<>();
77 if(isInstalled(feature)) {
78 // FIXME Workaround for BUG-2836, features service returns null for feature: standard-condition-webconsole_0_0_0, 3.0.1
79 if(featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
80 LOG.warn("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature.getVersion());
82 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
83 configs = wrappedFeature.getFeatureConfigSnapshotHolders();
84 if (!configs.isEmpty()) {
85 configs = pushConfig(configs, feature);
86 feature2configs.putAll(feature, configs);
93 private boolean isInstalled(final Feature feature) {
94 for(int retries=0;retries<MAX_RETRIES;retries++) {
96 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
97 if(installedFeatures.contains(feature)) {
100 LOG.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}",retries,feature.getName(),feature.getVersion());
102 } catch (Exception e) {
103 if(retries < MAX_RETRIES) {
104 LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
106 LOG.error("Giving up on Karaf featuresService.listInstalledFeatures() which has thrown an exception, retry {}", retries, e);
111 Thread.sleep(RETRY_PAUSE_MILLIS);
112 } catch (InterruptedException e1) {
113 throw new IllegalStateException(e1);
116 LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",MAX_RETRIES,feature.getName(),feature.getVersion());
120 private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(final LinkedHashSet<FeatureConfigSnapshotHolder> configs, final Feature feature) throws InterruptedException {
121 LinkedHashSet<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<FeatureConfigSnapshotHolder>(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 LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
138 configsPushed.retainAll(configs);
139 return configsPushed;