From: Jakub Toth Date: Wed, 25 Oct 2017 17:27:07 +0000 (+0200) Subject: Bug 9285 - EntityOwnerSelectionStrategyConfigReader in X-Git-Tag: release/oxygen~64 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=f0968198ac30c729b9aad78eafc0e309bcee6865 Bug 9285 - EntityOwnerSelectionStrategyConfigReader in 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 --- diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReader.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReader.java index 126324fff0..0acc296fc6 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReader.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReader.java @@ -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 props) { final EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder(); - final ServiceReference 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 properties = config.getProperties(); - if (properties == null) { - LOG.debug("Empty strategy configuration {}, using defaults", config); - return builder.build(); - } + final Map properties) { - // No java.util.Iterable: Wheeey, pre-Java 5 world!!! - final Enumeration keys = properties.keys(); - while (keys.hasMoreElements()) { - final String key = keys.nextElement(); + for (final Entry 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 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 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); } diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/resources/org/opendaylight/blueprint/clustered-datastore.xml b/opendaylight/md-sal/sal-distributed-datastore/src/main/resources/org/opendaylight/blueprint/clustered-datastore.xml index 6f4301f992..90e26d9458 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/resources/org/opendaylight/blueprint/clustered-datastore.xml +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/resources/org/opendaylight/blueprint/clustered-datastore.xml @@ -1,7 +1,7 @@ + xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.2.0"> @@ -137,10 +137,11 @@ + - + 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 props = new Hashtable<>(); + final Map 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.emptyMap()); + final EntityOwnerSelectionStrategy strategy = config.createStrategy("test", + Collections.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 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 props = new Hashtable<>(); + final Map 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 props = new Hashtable<>(); + final Map props = new HashMap<>(); props.put("entity.type.test", "String,100"); - - doReturn(props).when(mockConfig).getProperties(); - - loadStrategyConfig(); + EntityOwnerSelectionStrategyConfigReader.loadStrategyWithConfig(props); } @Test public void testReadStrategiesMissingDelay() { - Hashtable props = new Hashtable<>(); + final Map 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());