8bac3dd301e29ee281ea7f688c9a135da7bb6ccf
[controller.git] / opendaylight / md-sal / sal-distributed-eos / src / main / java / org / opendaylight / controller / cluster / entityownership / selectionstrategy / EntityOwnerSelectionStrategyConfigReader.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.entityownership.selectionstrategy;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Map;
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;
16
17 /**
18  * Reads the entity owner selection strategy config.
19  *
20  */
21 public final class EntityOwnerSelectionStrategyConfigReader {
22
23     private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfigReader.class);
24     private static final String ENTITY_TYPE_PREFIX = "entity.type.";
25
26     private EntityOwnerSelectionStrategyConfigReader() {
27         // Hidden on purpose
28     }
29
30     @SuppressWarnings("checkstyle:IllegalCatch")
31     public static EntityOwnerSelectionStrategyConfig loadStrategyWithConfig(final Map<Object, Object> props) {
32         final EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder();
33
34         if (props != null && !props.isEmpty()) {
35             parseConfiguration(builder, props);
36         } else {
37             if (props == null) {
38                 LOG.debug("Could not read strategy configuration file, will use default configuration.");
39             } else {
40                 LOG.debug("Configuration file is empty, will use default configuration.");
41             }
42         }
43         return builder.build();
44     }
45
46     private static EntityOwnerSelectionStrategyConfig parseConfiguration(final Builder builder,
47             final Map<Object, Object> properties) {
48
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);
53                 continue;
54             }
55
56             final String[] strategyClassAndDelay = ((String) properties.get(key)).split(",");
57             final Class<? extends EntityOwnerSelectionStrategy> aClass = loadClass(strategyClassAndDelay[0]);
58
59             final long delay;
60             if (strategyClassAndDelay.length > 1) {
61                 delay = Long.parseLong(strategyClassAndDelay[1]);
62             } else {
63                 delay = 0;
64             }
65
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);
69         }
70
71         return builder.build();
72     }
73
74     @SuppressWarnings("unchecked")
75     private static Class<? extends EntityOwnerSelectionStrategy> loadClass(final String strategyClassAndDelay) {
76         final Class<?> clazz;
77         try {
78             clazz = EntityOwnerSelectionStrategyConfigReader.class.getClassLoader().loadClass(strategyClassAndDelay);
79         } catch (final ClassNotFoundException e) {
80             throw new IllegalArgumentException("Failed to load strategy " + strategyClassAndDelay, e);
81         }
82
83         Preconditions.checkArgument(EntityOwnerSelectionStrategy.class.isAssignableFrom(clazz),
84             "Selected implementation %s must implement EntityOwnerSelectionStrategy, clazz");
85
86         return (Class<? extends EntityOwnerSelectionStrategy>) clazz;
87     }
88 }