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.collect.LinkedHashMultimap;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.LinkedHashSet;
14 import java.util.List;
15 import org.apache.karaf.features.Feature;
16 import org.apache.karaf.features.FeaturesService;
17 import org.opendaylight.controller.config.persist.api.ConfigPusher;
18 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Simple class to push configs to the config subsystem from Feature's configfiles
25 public class FeatureConfigPusher {
26 private static final Logger LOG = LoggerFactory.getLogger(FeatureConfigPusher.class);
27 private static final int MAX_RETRIES=100;
28 private static final int RETRY_PAUSE_MILLIS=1;
29 private FeaturesService featuresService = null;
30 private ConfigPusher pusher = null;
32 * A LinkedHashSet (to preserve order and insure uniqueness) of the pushedConfigs
33 * This is used to prevent pushing duplicate configs if a Feature is in multiple dependency
34 * chains. Also, preserves the *original* Feature chain for which we pushed the config.
35 * (which is handy for logging).
37 LinkedHashSet<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
39 * LinkedHashMultimap to track which configs we pushed for each Feature installation
42 LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap.create();
45 * @param p - ConfigPusher to push ConfigSnapshotHolders
47 public FeatureConfigPusher(final ConfigPusher p, final FeaturesService f) {
52 * Push config files from Features to config subsystem
53 * @param features - list of Features to extract config files from recursively and push
54 * to the config subsystem
56 * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
57 * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
58 * (Ususally because it was not yet installed)
60 public LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> pushConfigs(final List<Feature> features) throws Exception, InterruptedException {
61 LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
62 for(Feature feature: features) {
63 LinkedHashSet<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
64 if(!configSnapShots.isEmpty()) {
65 pushedFeatures.putAll(feature,configSnapShots);
68 return pushedFeatures;
71 private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception, InterruptedException {
72 LinkedHashSet<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
73 if(isInstalled(feature)) {
74 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature,featuresService);
75 configs = wrappedFeature.getFeatureConfigSnapshotHolders();
76 if(!configs.isEmpty()) {
77 configs = pushConfig(configs);
78 feature2configs.putAll(feature, configs);
84 private boolean isInstalled(final Feature feature) {
85 for(int retries=0;retries<MAX_RETRIES;retries++) {
87 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
88 if(installedFeatures.contains(feature)) {
91 LOG.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}",retries,feature.getName(),feature.getVersion());
93 } catch (Exception e) {
94 if(retries < MAX_RETRIES) {
95 LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
97 LOG.error("Giving up on Karaf featuresService.listInstalledFeatures() which has thrown an exception, retry {}", retries, e);
102 Thread.sleep(RETRY_PAUSE_MILLIS);
103 } catch (InterruptedException e1) {
104 throw new IllegalStateException(e1);
107 LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",MAX_RETRIES,feature.getName(),feature.getVersion());
111 private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(final LinkedHashSet<FeatureConfigSnapshotHolder> configs) throws InterruptedException {
112 LinkedHashSet<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<FeatureConfigSnapshotHolder>(configs);
113 configsToPush.removeAll(pushedConfigs);
114 if(!configsToPush.isEmpty()) {
115 pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
116 pushedConfigs.addAll(configsToPush);
118 LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
119 configsPushed.retainAll(configs);
120 return configsPushed;