Use ReusableNormalizedNodePruner in PruningDataTreeModification
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / selectionstrategy / EntityOwnerSelectionStrategyConfig.java
index 298589ae0570f585461e0c50ed39d36571c17edb..a5ffc49a0eb6d5341a374a3b1e7b91733c4ed301 100644 (file)
@@ -14,75 +14,94 @@ import java.util.Map;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class EntityOwnerSelectionStrategyConfig {
+/**
+ * FIXME: this is simple registry service, except it also loads classes.
+ */
+public final 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(){
+    private EntityOwnerSelectionStrategyConfig() {
 
     }
 
-    public boolean isStrategyConfigured(String entityType){
+    public boolean isStrategyConfigured(final String entityType) {
         return entityTypeToStrategyInfo.get(entityType) != null;
     }
 
-    public EntityOwnerSelectionStrategy createStrategy(String entityType){
+    public EntityOwnerSelectionStrategy createStrategy(final String entityType,
+            final Map<String, Long> initialStatistics) {
         final EntityOwnerSelectionStrategy strategy;
         final EntityOwnerSelectionStrategy existingStrategy = entityTypeToOwnerSelectionStrategy.get(entityType);
-        if(existingStrategy != null){
+        if (existingStrategy != null) {
             strategy = existingStrategy;
         } else {
             EntityOwnerSelectionStrategyConfig.StrategyInfo strategyInfo = entityTypeToStrategyInfo.get(entityType);
-            if(strategyInfo == null){
+            if (strategyInfo == null) {
                 strategy = FirstCandidateSelectionStrategy.INSTANCE;
             } else {
-                strategy = strategyInfo.createStrategy();
+                strategy = strategyInfo.createStrategy(initialStatistics);
             }
             entityTypeToOwnerSelectionStrategy.put(entityType, strategy);
         }
         return strategy;
+    }
 
+    /**
+     * This class should not exist. It contains a single long, which is passed to the constructor (via reflection).
+     * We are getting that information from a BundleContext. We are running in OSGi environment, hence this class
+     * needs to be deployed in its own bundle, with its own configuration.
+     * If this is used internally, it needs to be relocated into a separate package along with the implementation
+     * using it.
+     *
+     * @deprecated FIXME: THIS IS CONFIGURATION FOR A CUSTOM-LOADED CLASS CONSTRUCTOR
+     */
+    @Deprecated
+    public void clearStrategies() {
+        entityTypeToOwnerSelectionStrategy.clear();
     }
 
     private static final class StrategyInfo {
         private final Class<? extends EntityOwnerSelectionStrategy> strategyClass;
         private final long delay;
 
-        private StrategyInfo(Class<? extends EntityOwnerSelectionStrategy> strategyClass, long delay) {
+        private StrategyInfo(final Class<? extends EntityOwnerSelectionStrategy> strategyClass, final long delay) {
             this.strategyClass = strategyClass;
             this.delay = delay;
         }
 
-        public EntityOwnerSelectionStrategy createStrategy(){
+        public EntityOwnerSelectionStrategy createStrategy(final Map<String, Long> initialStatistics) {
             try {
-                return strategyClass.getDeclaredConstructor(long.class).newInstance(delay);
-            } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
+                return strategyClass.getDeclaredConstructor(long.class, Map.class)
+                        .newInstance(delay, initialStatistics);
+            } catch (InstantiationException | IllegalAccessException | InvocationTargetException
+                    | NoSuchMethodException e) {
                 LOG.warn("could not create custom strategy", e);
             }
             return FirstCandidateSelectionStrategy.INSTANCE;
         }
     }
 
-    public static Builder newBuilder(){
+    public static Builder newBuilder() {
         return new Builder(new EntityOwnerSelectionStrategyConfig());
     }
 
-    public static class Builder {
+    public static final class Builder {
         private final EntityOwnerSelectionStrategyConfig config;
 
-        private Builder(EntityOwnerSelectionStrategyConfig config){
+        Builder(final EntityOwnerSelectionStrategyConfig config) {
             this.config = config;
         }
 
-        public Builder addStrategy(String entityType, Class<? extends EntityOwnerSelectionStrategy> strategy, long delay){
+        public Builder addStrategy(final String entityType,
+                final Class<? extends EntityOwnerSelectionStrategy> strategy, final long delay) {
             config.entityTypeToStrategyInfo.put(entityType, new StrategyInfo(strategy, delay));
             return this;
         }
 
-        public EntityOwnerSelectionStrategyConfig build(){
+        public EntityOwnerSelectionStrategyConfig build() {
             return this.config;
         }
     }
-
 }