Removed sonar warnings.
[controller.git] / opendaylight / config / config-persister-feature-adapter / src / main / java / org / opendaylight / controller / configpusherfeature / internal / FeatureConfigPusher.java
index 7b90580a660cac0a03a4b90c0c0f8d40a1d2eb68..50757fb7b90961df577d1dc1d7675bbf56ea8bb5 100644 (file)
@@ -7,15 +7,18 @@
  */
 package org.opendaylight.controller.configpusherfeature.internal;
 
+import com.google.common.base.Optional;
 import com.google.common.collect.LinkedHashMultimap;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Set;
 import org.apache.karaf.features.Feature;
 import org.apache.karaf.features.FeaturesService;
 import org.opendaylight.controller.config.persist.api.ConfigPusher;
 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
+import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -34,7 +37,7 @@ public class FeatureConfigPusher {
      * chains.  Also, preserves the *original* Feature chain for which we pushed the config.
      * (which is handy for logging).
      */
-    LinkedHashSet<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
+    Set<FeatureConfigSnapshotHolder> pushedConfigs = new LinkedHashSet<>();
     /*
      * LinkedHashMultimap to track which configs we pushed for each Feature installation
      * For future use
@@ -57,32 +60,39 @@ public class FeatureConfigPusher {
      * If a Feature is not in the returned LinkedHashMultimap then we couldn't push its configs
      * (Ususally because it was not yet installed)
      */
-    public LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> pushConfigs(final List<Feature> features) throws Exception, InterruptedException {
-        LinkedHashMultimap<Feature,FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
-        for(Feature feature: features) {
-            LinkedHashSet<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
-            if(!configSnapShots.isEmpty()) {
-                pushedFeatures.putAll(feature,configSnapShots);
+    public LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushConfigs(final List<Feature> features) throws Exception {
+        LinkedHashMultimap<Feature, FeatureConfigSnapshotHolder> pushedFeatures = LinkedHashMultimap.create();
+        for (Feature feature : features) {
+
+
+            Set<FeatureConfigSnapshotHolder> configSnapShots = pushConfig(feature);
+            if (!configSnapShots.isEmpty()) {
+                pushedFeatures.putAll(feature, configSnapShots);
             }
         }
         return pushedFeatures;
     }
 
-    private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception, InterruptedException {
-        LinkedHashSet<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<FeatureConfigSnapshotHolder>();
+    private Set<FeatureConfigSnapshotHolder> pushConfig(final Feature feature) throws Exception {
+        Set<FeatureConfigSnapshotHolder> configs = new LinkedHashSet<>();
         if(isInstalled(feature)) {
-            ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature,featuresService);
-            configs = wrappedFeature.getFeatureConfigSnapshotHolders();
-            if(!configs.isEmpty()) {
-                configs = pushConfig(configs);
-                feature2configs.putAll(feature, configs);
+            // FIXME Workaround for BUG-2836, features service returns null for feature: standard-condition-webconsole_0_0_0, 3.0.1
+            if(featuresService.getFeature(feature.getName(), feature.getVersion()) == null) {
+                LOG.debug("Feature: {}, {} is missing from features service. Skipping", feature.getName(), feature.getVersion());
+            } else {
+                ChildAwareFeatureWrapper wrappedFeature = new ChildAwareFeatureWrapper(feature, featuresService);
+                configs = wrappedFeature.getFeatureConfigSnapshotHolders();
+                if (!configs.isEmpty()) {
+                    configs = pushConfig(configs, feature);
+                    feature2configs.putAll(feature, configs);
+                }
             }
         }
         return configs;
     }
 
     private boolean isInstalled(final Feature feature) {
-        for(int retries=0;retries<MAX_RETRIES;retries++) {
+        for (int retries = 0; retries < MAX_RETRIES; retries++) {
             try {
                 List<Feature> installedFeatures = Arrays.asList(featuresService.listInstalledFeatures());
                 if(installedFeatures.contains(feature)) {
@@ -91,12 +101,7 @@ public class FeatureConfigPusher {
                     LOG.warn("Karaf featuresService.listInstalledFeatures() has not yet finished installing feature (retry {}) {} {}",retries,feature.getName(),feature.getVersion());
                 }
             } catch (Exception e) {
-                if(retries < MAX_RETRIES) {
-                    LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
-                } else {
-                    LOG.error("Giving up on Karaf featuresService.listInstalledFeatures() which has thrown an exception, retry {}", retries, e);
-                    throw e;
-                }
+                LOG.warn("Karaf featuresService.listInstalledFeatures() has thrown an exception, retry {}", retries, e);
             }
             try {
                 Thread.sleep(RETRY_PAUSE_MILLIS);
@@ -108,14 +113,25 @@ public class FeatureConfigPusher {
         return false;
     }
 
-    private LinkedHashSet<FeatureConfigSnapshotHolder> pushConfig(final LinkedHashSet<FeatureConfigSnapshotHolder> configs) throws InterruptedException {
-        LinkedHashSet<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<FeatureConfigSnapshotHolder>(configs);
+    private Set<FeatureConfigSnapshotHolder> pushConfig(final Set<FeatureConfigSnapshotHolder> configs, final Feature feature)
+            throws InterruptedException {
+        Set<FeatureConfigSnapshotHolder> configsToPush = new LinkedHashSet<>(configs);
         configsToPush.removeAll(pushedConfigs);
-        if(!configsToPush.isEmpty()) {
-            pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
+        if (!configsToPush.isEmpty()) {
+
+            // Ignore features that are present in persisted current config
+            final Optional<XmlFileStorageAdapter> currentCfgPusher = XmlFileStorageAdapter.getInstance();
+            if (currentCfgPusher.isPresent() &&
+                    currentCfgPusher.get().getPersistedFeatures().contains(feature.getId())) {
+                LOG.warn("Ignoring default configuration {} for feature {}, the configuration is present in {}",
+                        configsToPush, feature.getId(), currentCfgPusher.get());
+            } else {
+                pusher.pushConfigs(new ArrayList<ConfigSnapshotHolder>(configsToPush));
+            }
+
             pushedConfigs.addAll(configsToPush);
         }
-        LinkedHashSet<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<FeatureConfigSnapshotHolder>(pushedConfigs);
+        Set<FeatureConfigSnapshotHolder> configsPushed = new LinkedHashSet<>(pushedConfigs);
         configsPushed.retainAll(configs);
         return configsPushed;
     }