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 FeaturesService featuresService = null;
31 private ConfigPusher pusher = null;
33 * A LinkedHashSet (to preserve order and insure uniqueness) of the pushedConfigs
34 * This is used to prevent pushing duplicate configs if a Feature is in multiple dependency
35 * chains. Also, preserves the *original* Feature chain for which we pushed the config.
36 * (which is handy for logging).
38 LinkedHashSet<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
40 * LinkedHashMultimap to track which configs we pushed for each Feature installation
43 LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap.create();
46 * @param p - ConfigPusher to push ConfigSnapshotHolders
48 public FeatureConfigPusher(ConfigPusher p, FeaturesService f) {
53 * Push config files from Features to config subsystem
54 * @param features - list of Features to extract config files from recursively and push
55 * to the config subsystem
57 * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
58 * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
59 * (Ususally because it was not yet installed)
61 public LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> pushConfigs(List<Feature> features) throws Exception, InterruptedException {
62 LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
63 for(Feature feature: features) {
64 LinkedHashSet<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
65 if(!configSnapShots.isEmpty()) {
66 pushedFeatures.putAll(feature,configSnapShots);
69 return pushedFeatures;
72 private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(Feature feature) throws Exception, InterruptedException {
73 LinkedHashSet<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
74 if(isInstalled(feature)) {
75 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature,featuresService);
76 configs = wrappedFeature.getFeatureConfigSnapshotHolders();
77 if(!configs.isEmpty()) {
78 configs = pushConfig(configs);
79 feature2configs.putAll(feature, configs);
85 private boolean isInstalled(Feature feature) {
86 List<Feature> installedFeatures= null;
91 installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
93 } catch (Exception e) {
94 if(retries < MAX_RETRIES) {
95 logger.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}, Exception {}", retries,e);
98 } catch (InterruptedException e1) {
99 throw new IllegalStateException(e1);
104 logger.error("Giving up on Karaf featuresService.listInstalledFeatures() which has thrown an exception, retry {}, Exception {}", retries,e);
109 return installedFeatures.contains(feature);
112 private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(LinkedHashSet<FeatureConfigSnapshotHolder> configs) throws InterruptedException {
113 LinkedHashSet<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<FeatureConfigSnapshotHolder>(configs);
114 configsToPush.removeAll(pushedConfigs);
115 if(!configsToPush.isEmpty()) {
116 pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
117 pushedConfigs.addAll(configsToPush);
119 LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
120 configsPushed.retainAll(configs);
121 return configsPushed;