2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.entityownership.selectionstrategy;
10 import com.google.common.base.Preconditions;
12 import java.util.Map.Entry;
13 import org.opendaylight.controller.cluster.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig.Builder;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
18 * Reads the entity owner selection strategy config.
21 public final class EntityOwnerSelectionStrategyConfigReader {
23 private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfigReader.class);
24 private static final String ENTITY_TYPE_PREFIX = "entity.type.";
26 private EntityOwnerSelectionStrategyConfigReader() {
30 @SuppressWarnings("checkstyle:IllegalCatch")
31 public static EntityOwnerSelectionStrategyConfig loadStrategyWithConfig(final Map<Object, Object> props) {
32 final EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder();
34 if (props != null && !props.isEmpty()) {
35 parseConfiguration(builder, props);
38 LOG.debug("Could not read strategy configuration file, will use default configuration.");
40 LOG.debug("Configuration file is empty, will use default configuration.");
43 return builder.build();
46 private static EntityOwnerSelectionStrategyConfig parseConfiguration(final Builder builder,
47 final Map<Object, Object> properties) {
49 for (final Entry<Object, Object> entry : properties.entrySet()) {
50 final String key = (String) entry.getKey();
51 if (!key.startsWith(ENTITY_TYPE_PREFIX)) {
52 LOG.debug("Ignoring non-conforming property key : {}", key);
56 final String[] strategyClassAndDelay = ((String) properties.get(key)).split(",");
57 final Class<? extends EntityOwnerSelectionStrategy> aClass = loadClass(strategyClassAndDelay[0]);
60 if (strategyClassAndDelay.length > 1) {
61 delay = Long.parseLong(strategyClassAndDelay[1]);
66 final String entityType = key.substring(key.lastIndexOf(".") + 1);
67 builder.addStrategy(entityType, aClass, delay);
68 LOG.debug("Entity Type '{}' using strategy {} delay {}", entityType, aClass, delay);
71 return builder.build();
74 @SuppressWarnings("unchecked")
75 private static Class<? extends EntityOwnerSelectionStrategy> loadClass(final String strategyClassAndDelay) {
78 clazz = EntityOwnerSelectionStrategyConfigReader.class.getClassLoader().loadClass(strategyClassAndDelay);
79 } catch (final ClassNotFoundException e) {
80 throw new IllegalArgumentException("Failed to load strategy " + strategyClassAndDelay, e);
83 Preconditions.checkArgument(EntityOwnerSelectionStrategy.class.isAssignableFrom(clazz),
84 "Selected implementation %s must implement EntityOwnerSelectionStrategy, clazz");
86 return (Class<? extends EntityOwnerSelectionStrategy>) clazz;