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 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(ConfigPusher p, 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(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(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(Feature feature) {
85 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
86 return installedFeatures.contains(feature);
89 private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(LinkedHashSet<FeatureConfigSnapshotHolder> configs) throws InterruptedException {
90 LinkedHashSet<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<FeatureConfigSnapshotHolder>(configs);
91 configsToPush.removeAll(pushedConfigs);
92 if(!configsToPush.isEmpty()) {
93 pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
94 pushedConfigs.addAll(configsToPush);
96 LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
97 configsPushed.retainAll(configs);