Add a mechanism to read the entity owner selection strategies from a config file
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / selectionstrategy / EntityOwnerSelectionStrategyConfig.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 java.lang.reflect.InvocationTargetException;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public class EntityOwnerSelectionStrategyConfig {
18     private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfig.class);
19     private final Map<String, StrategyInfo> entityTypeToStrategyInfo = new HashMap<>();
20     private final Map<String, EntityOwnerSelectionStrategy> entityTypeToOwnerSelectionStrategy = new HashMap<>();
21
22     private EntityOwnerSelectionStrategyConfig(){
23
24     }
25
26     public boolean isStrategyConfigured(String entityType){
27         return entityTypeToStrategyInfo.get(entityType) != null;
28     }
29
30     public EntityOwnerSelectionStrategy createStrategy(String entityType){
31         final EntityOwnerSelectionStrategy strategy;
32         final EntityOwnerSelectionStrategy existingStrategy = entityTypeToOwnerSelectionStrategy.get(entityType);
33         if(existingStrategy != null){
34             strategy = existingStrategy;
35         } else {
36             EntityOwnerSelectionStrategyConfig.StrategyInfo strategyInfo = entityTypeToStrategyInfo.get(entityType);
37             if(strategyInfo == null){
38                 strategy = FirstCandidateSelectionStrategy.INSTANCE;
39             } else {
40                 strategy = strategyInfo.createStrategy();
41             }
42             entityTypeToOwnerSelectionStrategy.put(entityType, strategy);
43         }
44         return strategy;
45
46     }
47
48     private static final class StrategyInfo {
49         private final Class<? extends EntityOwnerSelectionStrategy> strategyClass;
50         private final long delay;
51
52         private StrategyInfo(Class<? extends EntityOwnerSelectionStrategy> strategyClass, long delay) {
53             this.strategyClass = strategyClass;
54             this.delay = delay;
55         }
56
57         public EntityOwnerSelectionStrategy createStrategy(){
58             try {
59                 return strategyClass.getDeclaredConstructor(long.class).newInstance(delay);
60             } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
61                 LOG.warn("could not create custom strategy", e);
62             }
63             return FirstCandidateSelectionStrategy.INSTANCE;
64         }
65     }
66
67     public static Builder newBuilder(){
68         return new Builder(new EntityOwnerSelectionStrategyConfig());
69     }
70
71     public static class Builder {
72         private final EntityOwnerSelectionStrategyConfig config;
73
74         private Builder(EntityOwnerSelectionStrategyConfig config){
75             this.config = config;
76         }
77
78         public Builder addStrategy(String entityType, Class<? extends EntityOwnerSelectionStrategy> strategy, long delay){
79             config.entityTypeToStrategyInfo.put(entityType, new StrategyInfo(strategy, delay));
80             return this;
81         }
82
83         public EntityOwnerSelectionStrategyConfig build(){
84             return this.config;
85         }
86     }
87
88 }