Pass in EntityOwnerSelectionStrategyConfig when constructing EntityOwnershipShard
[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 EntityOwnerSelectionStrategy createStrategy(String entityType){
27         final EntityOwnerSelectionStrategy strategy;
28         final EntityOwnerSelectionStrategy existingStrategy = entityTypeToOwnerSelectionStrategy.get(entityType);
29         if(existingStrategy != null){
30             strategy = existingStrategy;
31         } else {
32             EntityOwnerSelectionStrategyConfig.StrategyInfo strategyInfo = entityTypeToStrategyInfo.get(entityType);
33             if(strategyInfo == null){
34                 strategy = FirstCandidateSelectionStrategy.INSTANCE;
35             } else {
36                 strategy = strategyInfo.createStrategy();
37             }
38             entityTypeToOwnerSelectionStrategy.put(entityType, strategy);
39         }
40         return strategy;
41
42     }
43
44     private static final class StrategyInfo {
45         private final Class<? extends EntityOwnerSelectionStrategy> strategyClass;
46         private final long delay;
47
48         private StrategyInfo(Class<? extends EntityOwnerSelectionStrategy> strategyClass, long delay) {
49             this.strategyClass = strategyClass;
50             this.delay = delay;
51         }
52
53         public EntityOwnerSelectionStrategy createStrategy(){
54             try {
55                 return strategyClass.getDeclaredConstructor(long.class).newInstance(delay);
56             } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
57                 LOG.warn("could not create custom strategy", e);
58             }
59             return FirstCandidateSelectionStrategy.INSTANCE;
60         }
61     }
62
63     public static Builder newBuilder(){
64         return new Builder(new EntityOwnerSelectionStrategyConfig());
65     }
66
67     public static class Builder {
68         private final EntityOwnerSelectionStrategyConfig config;
69
70         private Builder(EntityOwnerSelectionStrategyConfig config){
71             this.config = config;
72         }
73
74         public Builder addStrategy(String entityType, Class<? extends EntityOwnerSelectionStrategy> strategy, long delay){
75             config.entityTypeToStrategyInfo.put(entityType, new StrategyInfo(strategy, delay));
76             return this;
77         }
78
79         public EntityOwnerSelectionStrategyConfig build(){
80             return this.config;
81         }
82     }
83
84 }