Merge "Migrade JUnitMatchers references"
[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             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(final Feature feature) {
85         for(int retries=0;retries<MAX_RETRIES;retries++) {
86             try {
87                 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
88                 if(installedFeatures.contains(feature)) {
89                     return true;
90                 } else {
91                     LOG.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}",retries,feature.getName(),feature.getVersion());
92                 }
93             } catch (Exception e) {
94                 if(retries < MAX_RETRIES) {
95                     LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
96                 } else {
97                     LOG.error("Giving up on Karaf featuresService.listInstalledFeatures() which has thrown an exception, retry {}", retries, e);
98                     throw e;
99                 }
100             }
101             try {
102                 Thread.sleep(RETRY_PAUSE_MILLIS);
103             } catch (InterruptedException e1) {
104                 throw new IllegalStateException(e1);
105             }
106         }
107         LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",MAX_RETRIES,feature.getName(),feature.getVersion());
108         return false;
109     }
110
111     private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(final LinkedHashSet<FeatureConfigSnapshotHolder> configs) throws InterruptedException {
112         LinkedHashSet<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<FeatureConfigSnapshotHolder>(configs);
113         configsToPush.removeAll(pushedConfigs);
114         if(!configsToPush.isEmpty()) {
115             pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
116             pushedConfigs.addAll(configsToPush);
117         }
118         LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
119         configsPushed.retainAll(configs);
120         return configsPushed;
121     }
122 }