Bug 9285 - EntityOwnerSelectionStrategyConfigReader in 07/64607/14
authorJakub Toth <jakub.toth@pantheon.tech>
Wed, 25 Oct 2017 17:27:07 +0000 (19:27 +0200)
committerJakub Toth <jakub.toth@pantheon.tech>
Wed, 25 Oct 2017 21:01:11 +0000 (21:01 +0000)
sal-distributed-datastore uses hardcoded dependencies on OSGI/karaf

 *removed hardcoded dependecies on OSGi
  *removed EntityOwnerSelectionStrategyConfigReader + test
 *set up properties for shards in DistributedEntityOwnershipService
  via blueprint
 *read config from properties via reflection from object type
  org.apache.aries.blueprint.compendium.cm.CmPropertyPlaceholder
 *allowed direct set up properties

Change-Id: Iff429c85e13bf6685e6d515ae044af36b19301bc
Signed-off-by: Jakub Toth <jakub.toth@pantheon.tech>
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/main/resources/org/opendaylight/blueprint/clustered-datastore.xml
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReaderTest.java

index 126324f..0acc296 100644 (file)
@@ -9,27 +9,17 @@
 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
 
 import com.google.common.base.Preconditions;
-import java.io.IOException;
-import java.util.Dictionary;
-import java.util.Enumeration;
+import java.util.Map;
+import java.util.Map.Entry;
 import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig.Builder;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Reads the entity owner selection strategy config.
  *
- * @deprecated FIXME: Service injection class. This class needs to be eliminated in favor of proper service injection,
- *             which can be any of OSGi (which this class uses internally), java.util.ServiceLoader, or config
- *             subsystem.
  */
-@Deprecated
 public final class EntityOwnerSelectionStrategyConfigReader {
-    public static final String CONFIG_ID = "org.opendaylight.controller.cluster.entity.owner.selection.strategies";
 
     private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfigReader.class);
     private static final String ENTITY_TYPE_PREFIX = "entity.type.";
@@ -39,57 +29,26 @@ public final class EntityOwnerSelectionStrategyConfigReader {
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public static EntityOwnerSelectionStrategyConfig loadStrategyWithConfig(final BundleContext bundleContext) {
+    public static EntityOwnerSelectionStrategyConfig loadStrategyWithConfig(final Map<Object, Object> props) {
         final EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder();
 
-        final ServiceReference<ConfigurationAdmin> configAdminServiceReference =
-                bundleContext.getServiceReference(ConfigurationAdmin.class);
-        if (configAdminServiceReference == null) {
-            LOG.warn("No ConfigurationAdmin service found");
-            return builder.build();
-        }
-
-        final ConfigurationAdmin configAdmin = bundleContext.getService(configAdminServiceReference);
-        if (configAdmin == null) {
-            LOG.warn("Failed to get ConfigurationAdmin service");
-            return builder.build();
-        }
-
-        final Configuration config;
-        try {
-            config = configAdmin.getConfiguration(CONFIG_ID);
-            if (config != null) {
-                return parseConfiguration(builder, config);
-            }
-
-            LOG.debug("Could not read strategy configuration file, will use default configuration");
-        } catch (IOException e1) {
-            LOG.warn("Failed to get configuration for {}, starting up empty", CONFIG_ID, e1);
-            return builder.build();
-        } finally {
-            try {
-                bundleContext.ungetService(configAdminServiceReference);
-            } catch (Exception e) {
-                LOG.debug("Error from ungetService", e);
+        if (props != null && !props.isEmpty()) {
+            parseConfiguration(builder, props);
+        } else {
+            if (props == null) {
+                LOG.debug("Could not read strategy configuration file, will use default configuration.");
+            } else {
+                LOG.debug("Configuration file is empty, will use default configuration.");
             }
         }
-
         return builder.build();
     }
 
     private static EntityOwnerSelectionStrategyConfig parseConfiguration(final Builder builder,
-            final Configuration config) {
-        // Historic note: java.util.Dictionary since introduction of java.util.Map in Java 1.2
-        final Dictionary<String, Object> properties = config.getProperties();
-        if (properties == null) {
-            LOG.debug("Empty strategy configuration {}, using defaults", config);
-            return builder.build();
-        }
+            final Map<Object, Object> properties) {
 
-        // No java.util.Iterable: Wheeey, pre-Java 5 world!!!
-        final Enumeration<String> keys = properties.keys();
-        while (keys.hasMoreElements()) {
-            final String key = keys.nextElement();
+        for (final Entry<Object, Object> entry : properties.entrySet()) {
+            final String key = (String) entry.getKey();
             if (!key.startsWith(ENTITY_TYPE_PREFIX)) {
                 LOG.debug("Ignoring non-conforming property key : {}");
                 continue;
@@ -99,7 +58,7 @@ public final class EntityOwnerSelectionStrategyConfigReader {
             final Class<? extends EntityOwnerSelectionStrategy> aClass;
             try {
                 aClass = loadClass(strategyClassAndDelay[0]);
-            } catch (ClassNotFoundException e) {
+            } catch (final ClassNotFoundException e) {
                 LOG.error("Failed to load class {}, ignoring it", strategyClassAndDelay[0], e);
                 continue;
             }
@@ -111,7 +70,7 @@ public final class EntityOwnerSelectionStrategyConfigReader {
                 delay = 0;
             }
 
-            String entityType = key.substring(key.lastIndexOf(".") + 1);
+            final String entityType = key.substring(key.lastIndexOf(".") + 1);
             builder.addStrategy(entityType, aClass, delay);
             LOG.debug("Entity Type '{}' using strategy {} delay {}", entityType, aClass, delay);
         }
@@ -119,14 +78,13 @@ public final class EntityOwnerSelectionStrategyConfigReader {
         return builder.build();
     }
 
-    @Deprecated
     @SuppressWarnings("unchecked")
     private static Class<? extends EntityOwnerSelectionStrategy> loadClass(final String strategyClassAndDelay)
             throws ClassNotFoundException {
         final Class<?> clazz;
         try {
             clazz = EntityOwnerSelectionStrategyConfigReader.class.getClassLoader().loadClass(strategyClassAndDelay);
-        } catch (ClassNotFoundException e) {
+        } catch (final ClassNotFoundException e) {
             throw new IllegalArgumentException("Failed to load strategy " + strategyClassAndDelay, e);
         }
 
index 6f4301f..90e26d9 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
            xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"
-           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0">
+           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0">
 
   <cm:property-placeholder persistent-id="org.opendaylight.controller.cluster.datastore.broker" update-strategy="none">
     <cm:default-properties>
   </bean>
 
   <!-- Distributed EntityOwnershipService -->
+  <cm:cm-properties id="strategiesProps" persistent-id="org.opendaylight.controller.cluster.entity.owner.selection.strategies" />
 
   <bean id="selectionStrategyConfig" class="org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfigReader"
           factory-method="loadStrategyWithConfig">
-    <argument ref="blueprintBundleContext"/>
+    <argument ref="strategiesProps"/>
   </bean>
 
   <bean id="distributedEntityOwnershipService" class="org.opendaylight.controller.cluster.datastore.entityownership.DistributedEntityOwnershipService"
index ca8340a..8e1b4da 100644 (file)
@@ -11,61 +11,36 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.doThrow;
 
 import java.io.IOException;
 import java.util.Collections;
-import java.util.Hashtable;
+import java.util.HashMap;
+import java.util.Map;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
 
 public class EntityOwnerSelectionStrategyConfigReaderTest {
-    @Mock
-    private BundleContext mockBundleContext;
-
-    @Mock
-    private ServiceReference<ConfigurationAdmin> mockConfigAdminServiceRef;
-
-    @Mock
-    private ConfigurationAdmin mockConfigAdmin;
-
-    @Mock
-    private Configuration mockConfig;
 
     @Before
     public void setup() throws IOException {
         MockitoAnnotations.initMocks(this);
-
-        doReturn(mockConfigAdminServiceRef).when(mockBundleContext).getServiceReference(ConfigurationAdmin.class);
-        doReturn(mockConfigAdmin).when(mockBundleContext).getService(mockConfigAdminServiceRef);
-
-        doReturn(mockConfig).when(mockConfigAdmin).getConfiguration(EntityOwnerSelectionStrategyConfigReader.CONFIG_ID);
-    }
-
-    private EntityOwnerSelectionStrategyConfig loadStrategyConfig() {
-        return EntityOwnerSelectionStrategyConfigReader.loadStrategyWithConfig(mockBundleContext);
     }
 
     @Test
     public void testReadStrategies() {
-        Hashtable<String, Object> props = new Hashtable<>();
+        final Map<Object, Object> props = new java.util.HashMap<>();
         props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership."
                 + "selectionstrategy.LastCandidateSelectionStrategy,100");
 
-        doReturn(props).when(mockConfig).getProperties();
 
-        EntityOwnerSelectionStrategyConfig config = loadStrategyConfig();
+        final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
+                .loadStrategyWithConfig(props);
 
         assertTrue(config.isStrategyConfigured("test"));
 
-        EntityOwnerSelectionStrategy strategy = config.createStrategy("test", Collections.<String, Long>emptyMap());
+        final EntityOwnerSelectionStrategy strategy = config.createStrategy("test",
+                Collections.<String, Long>emptyMap());
         assertTrue(strategy.toString(), strategy instanceof LastCandidateSelectionStrategy);
         assertEquals(100L, strategy.getSelectionDelayInMillis());
 
@@ -79,65 +54,48 @@ public class EntityOwnerSelectionStrategyConfigReaderTest {
     }
 
     @Test
-    public void testReadStrategiesWithIOException() throws IOException {
-        doThrow(IOException.class).when(mockConfigAdmin).getConfiguration(
-                EntityOwnerSelectionStrategyConfigReader.CONFIG_ID);
+    public void testReadStrategiesWithEmptyConfiguration() {
 
-        EntityOwnerSelectionStrategyConfig config = loadStrategyConfig();
+        final Map<Object, Object> props = new HashMap<>();
+        final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
+                .loadStrategyWithConfig(props);
 
         assertFalse(config.isStrategyConfigured("test"));
     }
 
     @Test
-    public void testReadStrategiesWithNullConfiguration() throws IOException {
-        doReturn(null).when(mockConfigAdmin).getConfiguration(EntityOwnerSelectionStrategyConfigReader.CONFIG_ID);
-
-        EntityOwnerSelectionStrategyConfig config = loadStrategyConfig();
-
-        assertFalse(config.isStrategyConfigured("test"));
-    }
-
-    @Test
-    public void testReadStrategiesWithNullConfigurationProperties() throws IOException {
-        doReturn(null).when(mockConfig).getProperties();
-
-        EntityOwnerSelectionStrategyConfig config = loadStrategyConfig();
-
+    public void testReadStrategiesWithNullConfiguration() {
+        final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
+                .loadStrategyWithConfig(null);
         assertFalse(config.isStrategyConfigured("test"));
     }
 
     @Test(expected = IllegalArgumentException.class)
     public void testReadStrategiesInvalidDelay() {
-        Hashtable<String, Object> props = new Hashtable<>();
+        final Map<Object, Object> props = new HashMap<>();
         props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership."
                 + "selectionstrategy.LastCandidateSelectionStrategy,foo");
-
-        doReturn(props).when(mockConfig).getProperties();
-
-        loadStrategyConfig();
+        EntityOwnerSelectionStrategyConfigReader.loadStrategyWithConfig(props);
     }
 
     @Test(expected = IllegalArgumentException.class)
     public void testReadStrategiesInvalidClassType() {
-        Hashtable<String, Object> props = new Hashtable<>();
+        final Map<Object, Object> props = new HashMap<>();
         props.put("entity.type.test", "String,100");
-
-        doReturn(props).when(mockConfig).getProperties();
-
-        loadStrategyConfig();
+        EntityOwnerSelectionStrategyConfigReader.loadStrategyWithConfig(props);
     }
 
     @Test
     public void testReadStrategiesMissingDelay() {
-        Hashtable<String, Object> props = new Hashtable<>();
+        final Map<Object, Object> props = new HashMap<>();
         props.put("entity.type.test", "org.opendaylight.controller.cluster.datastore.entityownership."
                 + "selectionstrategy.LastCandidateSelectionStrategy,100");
         props.put("entity.type.test1", "org.opendaylight.controller.cluster.datastore.entityownership."
                 + "selectionstrategy.LastCandidateSelectionStrategy");
 
-        doReturn(props).when(mockConfig).getProperties();
 
-        EntityOwnerSelectionStrategyConfig config = loadStrategyConfig();
+        final EntityOwnerSelectionStrategyConfig config = EntityOwnerSelectionStrategyConfigReader
+                .loadStrategyWithConfig(props);
 
         assertEquals(100, config.createStrategy("test", Collections.emptyMap()).getSelectionDelayInMillis());
         assertEquals(0, config.createStrategy("test2", Collections.emptyMap()).getSelectionDelayInMillis());

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.