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

©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.