Merge "Optimizations, Monitoring and Logging"
[controller.git] / opendaylight / config / config-persister-feature-adapter / src / main / java / org / opendaylight / controller / configpusherfeature / internal / FeatureConfigPusher.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.configpusherfeature.internal;
9
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.LinkedHashSet;
13 import java.util.List;
14
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;
21
22 import com.google.common.collect.LinkedHashMultimap;
23
24 /*
25  * Simple class to push configs to the config subsystem from Feature's configfiles
26  */
27 public class FeatureConfigPusher {
28     private static final Logger logger = LoggerFactory.getLogger(FeatureConfigPusher.class);
29     private FeaturesService featuresService = null;
30     private ConfigPusher pusher = null;
31     /*
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).
36      */
37     LinkedHashSet<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
38     /*
39      * LinkedHashMultimap to track which configs we pushed for each Feature installation
40      * For future use
41      */
42     LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap.create();
43
44     /*
45      * @param p - ConfigPusher to push ConfigSnapshotHolders
46      */
47     public FeatureConfigPusher(ConfigPusher p, FeaturesService f) {
48         pusher = p;
49         featuresService = f;
50     }
51     /*
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
55      *
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)
59      */
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);
66             }
67         }
68         return pushedFeatures;
69     }
70
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);
79             }
80         }
81         return configs;
82     }
83
84     private boolean isInstalled(Feature feature) {
85         List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
86         return installedFeatures.contains(feature);
87     }
88
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);
95         }
96         LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
97         configsPushed.retainAll(configs);
98         return configsPushed;
99     }
100 }