BUG-2836 Workaround
[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 com.google.common.collect.LinkedHashMultimap;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.LinkedHashSet;
14 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;
21
22 /*
23  * Simple class to push configs to the config subsystem from Feature's configfiles
24  */
25 public class FeatureConfigPusher {
26     private static final Logger LOG = LoggerFactory.getLogger(FeatureConfigPusher.class);
27     private static final int MAX_RETRIES=100;
28     private static final int RETRY_PAUSE_MILLIS=1;
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(final ConfigPusher p, final 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(final 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(final Feature feature) throws Exception, InterruptedException {
72         LinkedHashSet<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
73         if(isInstalled(feature)) {
74             // FIXME Workaround for BUG-2836, features service returns null for feature: standard-condition-webconsole_0_0_0, 3.0.1
75             if(featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
76                 LOG.warn("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature.getVersion());
77             } else {
78                 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
79                 configs = wrappedFeature.getFeatureConfigSnapshotHolders();
80                 if (!configs.isEmpty()) {
81                     configs = pushConfig(configs);
82                     feature2configs.putAll(feature, configs);
83                 }
84             }
85         }
86         return configs;
87     }
88
89     private boolean isInstalled(final Feature feature) {
90         for(int retries=0;retries<MAX_RETRIES;retries++) {
91             try {
92                 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
93                 if(installedFeatures.contains(feature)) {
94                     return true;
95                 } else {
96                     LOG.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}",retries,feature.getName(),feature.getVersion());
97                 }
98             } catch (Exception e) {
99                 if(retries < MAX_RETRIES) {
100                     LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
101                 } else {
102                     LOG.error("Giving up on Karaf featuresService.listInstalledFeatures() which has thrown an exception, retry {}", retries, e);
103                     throw e;
104                 }
105             }
106             try {
107                 Thread.sleep(RETRY_PAUSE_MILLIS);
108             } catch (InterruptedException e1) {
109                 throw new IllegalStateException(e1);
110             }
111         }
112         LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",MAX_RETRIES,feature.getName(),feature.getVersion());
113         return false;
114     }
115
116     private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(final LinkedHashSet<FeatureConfigSnapshotHolder> configs) throws InterruptedException {
117         LinkedHashSet<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<FeatureConfigSnapshotHolder>(configs);
118         configsToPush.removeAll(pushedConfigs);
119         if(!configsToPush.isEmpty()) {
120             pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
121             pushedConfigs.addAll(configsToPush);
122         }
123         LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
124         configsPushed.retainAll(configs);
125         return configsPushed;
126     }
127 }