Fix NPE in EntityOwnerSelectionStrategyConfigReader 05/28905/1
authorTom Pantelis <tpanteli@brocade.com>
Wed, 28 Oct 2015 19:37:32 +0000 (15:37 -0400)
committerTom Pantelis <tpanteli@brocade.com>
Wed, 28 Oct 2015 19:37:32 +0000 (15:37 -0400)
Configuration#getProperties can return null if the config doesn't exist
- need to check for that. Also added more checks and unit tests.

Change-Id: If468fb8c3df7ecba664bb00a8f01bdfec7b4ceeb
Signed-off-by: Tom Pantelis <tpanteli@brocade.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReader.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReaderTest.java

index f9d1aea0a1effd93e1aa1e65e23393f35de43ce1..b6092b2e00cc39334ab8475424fc42976ee3c4c2 100644 (file)
@@ -12,6 +12,7 @@ import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.util.Dictionary;
 import java.util.Enumeration;
+import javax.annotation.Nullable;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.cm.Configuration;
@@ -39,35 +40,48 @@ public class EntityOwnerSelectionStrategyConfigReader {
     }
 
     private EntityOwnerSelectionStrategyConfig readConfiguration(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
-        EntityOwnerSelectionStrategyConfig strategyConfig;
         EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder();
+        ConfigurationAdmin configAdmin = null;
         try {
-            ConfigurationAdmin configAdmin = bundleContext.getService(configAdminServiceReference);
-            Configuration config = configAdmin.getConfiguration(CONFIG_ID);
-            Dictionary<String, Object> properties = config.getProperties();
-            Enumeration<String> keys = properties.keys();
-            while (keys.hasMoreElements()) {
-                String key = keys.nextElement();
-                String strategyProps = (String) properties.get(key);
-                String[] strategyClassAndDelay = strategyProps.split(",");
-                if(strategyClassAndDelay.length >= 1) {
-                    Class<? extends EntityOwnerSelectionStrategy> aClass
-                            = (Class<? extends EntityOwnerSelectionStrategy>) getClass().getClassLoader().loadClass(strategyClassAndDelay[0]);
-                    long delay = 0;
-                    if(strategyClassAndDelay.length > 1){
-                        delay = Long.parseLong(strategyClassAndDelay[1]);
+            configAdmin = bundleContext.getService(configAdminServiceReference);
+            Dictionary<String, Object> properties = getProperties(configAdmin);
+            if(properties != null) {
+                Enumeration<String> keys = properties.keys();
+                while (keys.hasMoreElements()) {
+                    String key = keys.nextElement();
+                    String strategyProps = (String) properties.get(key);
+                    String[] strategyClassAndDelay = strategyProps.split(",");
+                    if(strategyClassAndDelay.length >= 1) {
+                        @SuppressWarnings("unchecked")
+                        Class<? extends EntityOwnerSelectionStrategy> aClass
+                        = (Class<? extends EntityOwnerSelectionStrategy>) getClass().getClassLoader().loadClass(strategyClassAndDelay[0]);
+                        long delay = 0;
+                        if(strategyClassAndDelay.length > 1){
+                            delay = Long.parseLong(strategyClassAndDelay[1]);
+                        }
+                        builder.addStrategy(key, aClass, delay);
                     }
-                    builder.addStrategy(key, aClass, delay);
                 }
             }
-            strategyConfig = builder.build();
-        } catch(IOException | ClassNotFoundException | NumberFormatException e){
+        } catch(Exception e){
             LOG.warn("Failed to read selection strategy configuration file. All configuration will be ignored.", e);
-            strategyConfig = EntityOwnerSelectionStrategyConfig.newBuilder().build();
         } finally {
-            bundleContext.ungetService(configAdminServiceReference);
+            if(configAdmin != null) {
+                try {
+                    bundleContext.ungetService(configAdminServiceReference);
+                } catch (Exception e) {
+                    LOG.debug("Error from ungetService", e);
+                }
+            }
         }
-        return strategyConfig;
+
+        return builder.build();
+    }
+
+    @Nullable
+    private static Dictionary<String, Object> getProperties(ConfigurationAdmin configAdmin) throws IOException {
+        Configuration config = configAdmin.getConfiguration(CONFIG_ID);
+        return config != null ? config.getProperties() : null;
     }
 
     public EntityOwnerSelectionStrategyConfig getConfig() {
index feced489a219bd7eac13f37dcd5c0d1d94c1254f..5302283a15439600616deb2c15383a42e3d86fd6 100644 (file)
@@ -37,9 +37,6 @@ public class EntityOwnerSelectionStrategyConfigReaderTest {
         doReturn(mockConfigAdmin).when(mockBundleContext).getService(mockConfigAdminServiceRef);
 
         doReturn(mockConfig).when(mockConfigAdmin).getConfiguration(EntityOwnerSelectionStrategyConfigReader.CONFIG_ID);
-
-
-
     }
 
     @Test
@@ -59,7 +56,7 @@ public class EntityOwnerSelectionStrategyConfigReaderTest {
     }
 
     @Test
-    public void testReadStrategiesForNonExistentFile() throws IOException {
+    public void testReadStrategiesWithIOException() throws IOException {
         doThrow(IOException.class).when(mockConfigAdmin).getConfiguration(EntityOwnerSelectionStrategyConfigReader.CONFIG_ID);
 
         EntityOwnerSelectionStrategyConfig config = new EntityOwnerSelectionStrategyConfigReader(mockBundleContext).getConfig();
@@ -67,6 +64,24 @@ public class EntityOwnerSelectionStrategyConfigReaderTest {
         assertFalse(config.isStrategyConfigured("test"));
     }
 
+    @Test
+    public void testReadStrategiesWithNullConfiguration() throws IOException {
+        doReturn(null).when(mockConfigAdmin).getConfiguration(EntityOwnerSelectionStrategyConfigReader.CONFIG_ID);
+
+        EntityOwnerSelectionStrategyConfig config = new EntityOwnerSelectionStrategyConfigReader(mockBundleContext).getConfig();
+
+        assertFalse(config.isStrategyConfigured("test"));
+    }
+
+    @Test
+    public void testReadStrategiesWithNullConfigurationProperties() throws IOException {
+        doReturn(null).when(mockConfig).getProperties();
+
+        EntityOwnerSelectionStrategyConfig config = new EntityOwnerSelectionStrategyConfigReader(mockBundleContext).getConfig();
+
+        assertFalse(config.isStrategyConfigured("test"));
+    }
+
     @Test
     public void testReadStrategiesInvalidDelay(){
         Hashtable<String, Object> props = new Hashtable<>();