Merge "Add get-config commit edit-config to testtool"
[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 static final int RETRY_PAUSE_MILLIS=1;
31     private FeaturesService featuresService = null;
32     private ConfigPusher pusher = null;
33     /*
34      * A LinkedHashSet (to preserve order and insure uniqueness) of the pushedConfigs
35      * This is used to prevent pushing duplicate configs if a Feature is in multiple dependency
36      * chains.  Also, preserves the *original* Feature chain for which we pushed the config.
37      * (which is handy for logging).
38      */
39     LinkedHashSet<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
40     /*
41      * LinkedHashMultimap to track which configs we pushed for each Feature installation
42      * For future use
43      */
44     LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> feature2configs = LinkedHashMultimap.create();
45
46     /*
47      * @param p - ConfigPusher to push ConfigSnapshotHolders
48      */
49     public FeatureConfigPusher(ConfigPusher p, FeaturesService f) {
50         pusher = p;
51         featuresService = f;
52     }
53     /*
54      * Push config files from Features to config subsystem
55      * @param features - list of Features to extract config files from recursively and push
56      * to the config subsystem
57      *
58      * @return A LinkedHashMultimap of Features to the FeatureConfigSnapshotHolder actually pushed
59      * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
60      * (Ususally because it was not yet installed)
61      */
62     public LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> pushConfigs(List<Feature> features) throws Exception, InterruptedException {
63         LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
64         for(Feature feature: features) {
65             LinkedHashSet<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
66             if(!configSnapShots.isEmpty()) {
67                 pushedFeatures.putAll(feature,configSnapShots);
68             }
69         }
70         return pushedFeatures;
71     }
72
73     private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(Feature feature) throws Exception, InterruptedException {
74         LinkedHashSet<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
75         if(isInstalled(feature)) {
76             ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature,featuresService);
77             configs = wrappedFeature.getFeatureConfigSnapshotHolders();
78             if(!configs.isEmpty()) {
79                 configs = pushConfig(configs);
80                 feature2configs.putAll(feature, configs);
81             }
82         }
83         return configs;
84     }
85
86     private boolean isInstalled(Feature feature) {
87         for(int retries=0;retries<MAX_RETRIES;retries++) {
88             try {
89                 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
90                 if(installedFeatures.contains(feature)) {
91                     return true;
92                 } else {
93                     LOGGER.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}",retries,feature.getName(),feature.getVersion());
94                 }
95             } catch (Exception e) {
96                 if(retries < MAX_RETRIES) {
97                     LOGGER.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}, Exception {}", retries,e);
98                 } else {
99                     LOGGER.error("Giving up on Karaf featuresService.listInstalledFeatures() which has thrown an exception, retry {}, Exception {}", retries,e);
100                     throw e;
101                 }
102             }
103             try {
104                 Thread.sleep(RETRY_PAUSE_MILLIS);
105             } catch (InterruptedException e1) {
106                 throw new IllegalStateException(e1);
107             }
108         }
109         LOGGER.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",MAX_RETRIES,feature.getName(),feature.getVersion());
110         return false;
111     }
112
113     private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(LinkedHashSet<FeatureConfigSnapshotHolder> configs) throws InterruptedException {
114         LinkedHashSet<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<FeatureConfigSnapshotHolder>(configs);
115         configsToPush.removeAll(pushedConfigs);
116         if(!configsToPush.isEmpty()) {
117             pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
118             pushedConfigs.addAll(configsToPush);
119         }
120         LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
121         configsPushed.retainAll(configs);
122         return configsPushed;
123     }
124 }