Allow passing of delay to the EntityOwnerElectionStrategy
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / selectionstrategy / EntityOwnerSelectionStrategyConfig.java
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfig.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/entityownership/selectionstrategy/EntityOwnerSelectionStrategyConfig.java
new file mode 100644 (file)
index 0000000..db2377d
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.HashMap;
+import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class EntityOwnerSelectionStrategyConfig {
+    private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfig.class);
+    private final Map<String, StrategyInfo> entityTypeToStrategyInfo = new HashMap<>();
+    private final Map<String, EntityOwnerSelectionStrategy> entityTypeToOwnerSelectionStrategy = new HashMap<>();
+
+    private EntityOwnerSelectionStrategyConfig(){
+
+    }
+
+    public EntityOwnerSelectionStrategy createStrategy(String entityType){
+        final EntityOwnerSelectionStrategy strategy;
+        final EntityOwnerSelectionStrategy existingStrategy = entityTypeToOwnerSelectionStrategy.get(entityType);
+        if(existingStrategy != null){
+            strategy = existingStrategy;
+        } else {
+            EntityOwnerSelectionStrategyConfig.StrategyInfo strategyInfo = entityTypeToStrategyInfo.get(entityType);
+            if(strategyInfo == null){
+                strategy = FirstCandidateSelectionStrategy.INSTANCE;
+            } else {
+                strategy = strategyInfo.createStrategy();
+            }
+            entityTypeToOwnerSelectionStrategy.put(entityType, strategy);
+        }
+        return strategy;
+
+    }
+
+    private static final class StrategyInfo {
+        private final Class<? extends EntityOwnerSelectionStrategy> strategyClass;
+        private final long delay;
+
+        private StrategyInfo(Class<? extends EntityOwnerSelectionStrategy> strategyClass, long delay) {
+            this.strategyClass = strategyClass;
+            this.delay = delay;
+        }
+
+        public EntityOwnerSelectionStrategy createStrategy(){
+            try {
+                return strategyClass.getDeclaredConstructor(long.class).newInstance(delay);
+            } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
+                LOG.warn("could not create custom strategy", e);
+            }
+            return FirstCandidateSelectionStrategy.INSTANCE;
+        }
+    }
+
+    public static Builder newBuilder(){
+        return new Builder(new EntityOwnerSelectionStrategyConfig());
+    }
+
+    public static class Builder {
+        private final EntityOwnerSelectionStrategyConfig config;
+
+        private Builder(EntityOwnerSelectionStrategyConfig config){
+            this.config = config;
+        }
+
+        public Builder addStrategy(String entityType, Class<? extends EntityOwnerSelectionStrategy> strategy, long delay){
+            config.entityTypeToStrategyInfo.put(entityType, new StrategyInfo(strategy, delay));
+            return this;
+        }
+
+        public EntityOwnerSelectionStrategyConfig build(){
+            return this.config;
+        }
+    }
+
+}