X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-eos%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fentityownership%2Fselectionstrategy%2FEntityOwnerSelectionStrategyConfigReader.java;fp=opendaylight%2Fmd-sal%2Fsal-distributed-eos%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fentityownership%2Fselectionstrategy%2FEntityOwnerSelectionStrategyConfigReader.java;h=8bac3dd301e29ee281ea7f688c9a135da7bb6ccf;hb=f9ee2cce797cf12402dd55c406f3e270d7d2e20d;hp=0000000000000000000000000000000000000000;hpb=44d274e8a4282ef859a35369c563e4963cf2185a;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-eos/src/main/java/org/opendaylight/controller/cluster/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReader.java b/opendaylight/md-sal/sal-distributed-eos/src/main/java/org/opendaylight/controller/cluster/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReader.java new file mode 100644 index 0000000000..8bac3dd301 --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-eos/src/main/java/org/opendaylight/controller/cluster/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfigReader.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.cluster.entityownership.selectionstrategy; + +import com.google.common.base.Preconditions; +import java.util.Map; +import java.util.Map.Entry; +import org.opendaylight.controller.cluster.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig.Builder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Reads the entity owner selection strategy config. + * + */ +public final class EntityOwnerSelectionStrategyConfigReader { + + private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfigReader.class); + private static final String ENTITY_TYPE_PREFIX = "entity.type."; + + private EntityOwnerSelectionStrategyConfigReader() { + // Hidden on purpose + } + + @SuppressWarnings("checkstyle:IllegalCatch") + public static EntityOwnerSelectionStrategyConfig loadStrategyWithConfig(final Map props) { + final EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder(); + + 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 Map properties) { + + 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 : {}", key); + continue; + } + + final String[] strategyClassAndDelay = ((String) properties.get(key)).split(","); + final Class aClass = loadClass(strategyClassAndDelay[0]); + + final long delay; + if (strategyClassAndDelay.length > 1) { + delay = Long.parseLong(strategyClassAndDelay[1]); + } else { + delay = 0; + } + + final String entityType = key.substring(key.lastIndexOf(".") + 1); + builder.addStrategy(entityType, aClass, delay); + LOG.debug("Entity Type '{}' using strategy {} delay {}", entityType, aClass, delay); + } + + return builder.build(); + } + + @SuppressWarnings("unchecked") + private static Class loadClass(final String strategyClassAndDelay) { + final Class clazz; + try { + clazz = EntityOwnerSelectionStrategyConfigReader.class.getClassLoader().loadClass(strategyClassAndDelay); + } catch (final ClassNotFoundException e) { + throw new IllegalArgumentException("Failed to load strategy " + strategyClassAndDelay, e); + } + + Preconditions.checkArgument(EntityOwnerSelectionStrategy.class.isAssignableFrom(clazz), + "Selected implementation %s must implement EntityOwnerSelectionStrategy, clazz"); + + return (Class) clazz; + } +}