Merge "Bug 1915: Configuration knob for enabling L3 forwarding in OVSDB"
[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 static final int MAX_RETRIES=100;
30     private FeaturesService featuresService = null;
31     private ConfigPusher pusher = null;
32     /*
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).
37      */
38     LinkedHashSet<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
39     /*
40      * LinkedHashMultimap to track which configs we pushed for each Feature installation
41      * For future use
42      */
43     LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap.create();
44
45     /*
46      * @param p - ConfigPusher to push ConfigSnapshotHolders
47      */
48     public FeatureConfigPusher(ConfigPusher p, FeaturesService f) {
49         pusher = p;
50         featuresService = f;
51     }
52     /*
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
56      *
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)
60      */
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);
67             }
68         }
69         return pushedFeatures;
70     }
71
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);
80             }
81         }
82         return configs;
83     }
84
85     private boolean isInstalled(Feature feature) {
86         List<Feature> installedFeatures= null;
87         boolean cont = true;
88         int retries = 0;
89         while(cont) {
90             try {
91                 installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
92                 break;
93             } catch (Exception e) {
94                 if(retries < MAX_RETRIES) {
95                     logger.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}, Exception {}", retries,e);
96                     try {
97                         Thread.sleep(1);
98                     } catch (InterruptedException e1) {
99                         throw new IllegalStateException(e1);
100                     }
101                     retries++;
102                     continue;
103                 } else {
104                     logger.error("Giving up on Karaf featuresService.listInstalledFeatures() which has thrown an exception, retry {}, Exception {}", retries,e);
105                     throw e;
106                 }
107             }
108         }
109         return installedFeatures.contains(feature);
110     }
111
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);
118         }
119         LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
120         configsPushed.retainAll(configs);
121         return configsPushed;
122     }
123 }