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 java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.LinkedHashSet;
13 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;
22 import com.google.common.collect.LinkedHashMultimap;
25 * Simple class to push configs to the config subsystem from Feature's configfiles
27 public class FeatureConfigPusher {
28 private static final Logger LOGGER = 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(ConfigPusher p, 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(List<Feature> features) throws Exception, InterruptedException {
63 LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
64 for(Feature feature: features) {
65 LinkedHashSet<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
66 if(!configSnapShots.isEmpty()) {
67 pushedFeatures.putAll(feature,configSnapShots);
70 return pushedFeatures;
73 private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(Feature feature) throws Exception, InterruptedException {
74 LinkedHashSet<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
75 if(isInstalled(feature)) {
76 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature,featuresService);
77 configs = wrappedFeature.getFeatureConfigSnapshotHolders();
78 if(!configs.isEmpty()) {
79 configs = pushConfig(configs);
80 feature2configs.putAll(feature, configs);
86 private boolean isInstalled(Feature feature) {
87 for(int retries=0;retries<MAX_RETRIES;retries++) {
89 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
90 if(installedFeatures.contains(feature)) {
93 LOGGER.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}",retries,feature.getName(),feature.getVersion());
95 } catch (Exception e) {
96 if(retries < MAX_RETRIES) {
97 LOGGER.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}, Exception {}", retries,e);
99 LOGGER.error("Giving up on Karaf featuresService.listInstalledFeatures() which has thrown an exception, retry {}, Exception {}", retries,e);
104 Thread.sleep(RETRY_PAUSE_MILLIS);
105 } catch (InterruptedException e1) {
106 throw new IllegalStateException(e1);
109 LOGGER.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",MAX_RETRIES,feature.getName(),feature.getVersion());
113 private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(LinkedHashSet<FeatureConfigSnapshotHolder> configs) throws InterruptedException {
114 LinkedHashSet<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<FeatureConfigSnapshotHolder>(configs);
115 configsToPush.removeAll(pushedConfigs);
116 if(!configsToPush.isEmpty()) {
117 pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
118 pushedConfigs.addAll(configsToPush);
120 LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
121 configsPushed.retainAll(configs);
122 return configsPushed;