Reduce missing feature warning to debug
[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.base.Optional;
11 import com.google.common.collect.LinkedHashMultimap;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.LinkedHashSet;
15 import java.util.List;
16 import org.apache.karaf.features.Feature;
17 import org.apache.karaf.features.FeaturesService;
18 import org.opendaylight.controller.config.persist.api.ConfigPusher;
19 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
20 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
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 LOG = 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(final ConfigPusher p, final 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(final List<Feature> features) throws Exception {
63         LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
64         for (Feature feature : features) {
65
66
67             LinkedHashSet<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
68             if (!configSnapShots.isEmpty()) {
69                 pushedFeatures.putAll(feature, configSnapShots);
70             }
71         }
72         return pushedFeatures;
73     }
74
75     private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
76         LinkedHashSet<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<>();
77         if(isInstalled(feature)) {
78             // FIXME Workaround for BUG-2836, features service returns null for feature: standard-condition-webconsole_0_0_0, 3.0.1
79             if(featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
80                 LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature.getVersion());
81             } else {
82                 ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
83                 configs = wrappedFeature.getFeatureConfigSnapshotHolders();
84                 if (!configs.isEmpty()) {
85                     configs = pushConfig(configs, feature);
86                     feature2configs.putAll(feature, configs);
87                 }
88             }
89         }
90         return configs;
91     }
92
93     private boolean isInstalled(final Feature feature) {
94         for(int retries=0;retries<MAX_RETRIES;retries++) {
95             try {
96                 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
97                 if(installedFeatures.contains(feature)) {
98                     return true;
99                 } else {
100                     LOG.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}",retries,feature.getName(),feature.getVersion());
101                 }
102             } catch (Exception e) {
103                 if(retries < MAX_RETRIES) {
104                     LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
105                 } else {
106                     LOG.error("Giving up on Karaf featuresService.listInstalledFeatures() which has thrown an exception, retry {}", retries, e);
107                     throw e;
108                 }
109             }
110             try {
111                 Thread.sleep(RETRY_PAUSE_MILLIS);
112             } catch (InterruptedException e1) {
113                 throw new IllegalStateException(e1);
114             }
115         }
116         LOG.error("Giving up (after {} retries) on Karaf featuresService.listInstalledFeatures() which has not yet finished installing feature {} {}",MAX_RETRIES,feature.getName(),feature.getVersion());
117         return false;
118     }
119
120     private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(final LinkedHashSet<FeatureConfigSnapshotHolder> configs, final Feature feature) throws InterruptedException {
121         LinkedHashSet<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<FeatureConfigSnapshotHolder>(configs);
122         configsToPush.removeAll(pushedConfigs);
123         if (!configsToPush.isEmpty()) {
124
125             // Ignore features that are present in persisted current config
126             final Optional<XmlFileStorageAdapter> currentCfgPusher = XmlFileStorageAdapter.getInstance();
127             if (currentCfgPusher.isPresent() &&
128                     currentCfgPusher.get().getPersistedFeatures().contains(feature.getId())) {
129                 LOG.warn("Ignoring default configuration {} for feature {}, the configuration is present in {}",
130                         configsToPush, feature.getId(), currentCfgPusher.get());
131             } else {
132                 pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
133             }
134
135             pushedConfigs.addAll(configsToPush);
136         }
137         LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
138         configsPushed.retainAll(configs);
139         return configsPushed;
140     }
141 }