Fix checkstyle reported by odlparent-3.0.0
[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 /**
18  * FIXME: this is simple registry service, except it also loads classes.
19  */
20 public final class EntityOwnerSelectionStrategyConfig {
21     private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfig.class);
22     private final Map<String, StrategyInfo> entityTypeToStrategyInfo = new HashMap<>();
23     private final Map<String, EntityOwnerSelectionStrategy> entityTypeToOwnerSelectionStrategy = new HashMap<>();
24
25     private EntityOwnerSelectionStrategyConfig() {
26
27     }
28
29     public boolean isStrategyConfigured(final String entityType) {
30         return entityTypeToStrategyInfo.get(entityType) != null;
31     }
32
33     public EntityOwnerSelectionStrategy createStrategy(final String entityType,
34             final Map<String, Long> initialStatistics) {
35         final EntityOwnerSelectionStrategy strategy;
36         final EntityOwnerSelectionStrategy existingStrategy = entityTypeToOwnerSelectionStrategy.get(entityType);
37         if (existingStrategy != null) {
38             strategy = existingStrategy;
39         } else {
40             EntityOwnerSelectionStrategyConfig.StrategyInfo strategyInfo = entityTypeToStrategyInfo.get(entityType);
41             if (strategyInfo == null) {
42                 strategy = FirstCandidateSelectionStrategy.INSTANCE;
43             } else {
44                 strategy = strategyInfo.createStrategy(initialStatistics);
45             }
46             entityTypeToOwnerSelectionStrategy.put(entityType, strategy);
47         }
48         return strategy;
49     }
50
51     /**
52      * This class should not exist. It contains a single long, which is passed to the constructor (via reflection).
53      * We are getting that information from a BundleContext. We are running in OSGi environment, hence this class
54      * needs to be deployed in its own bundle, with its own configuration.
55      * If this is used internally, it needs to be relocated into a separate package along with the implementation
56      * using it.
57      *
58      * @deprecated FIXME: THIS IS CONFIGURATION FOR A CUSTOM-LOADED CLASS CONSTRUCTOR
59      */
60     @Deprecated
61     public void clearStrategies() {
62         entityTypeToOwnerSelectionStrategy.clear();
63     }
64
65     private static final class StrategyInfo {
66         private final Class<? extends EntityOwnerSelectionStrategy> strategyClass;
67         private final long delay;
68
69         private StrategyInfo(final Class<? extends EntityOwnerSelectionStrategy> strategyClass, final long delay) {
70             this.strategyClass = strategyClass;
71             this.delay = delay;
72         }
73
74         public EntityOwnerSelectionStrategy createStrategy(final Map<String, Long> initialStatistics) {
75             try {
76                 return strategyClass.getDeclaredConstructor(long.class, Map.class)
77                         .newInstance(delay, initialStatistics);
78             } catch (InstantiationException | IllegalAccessException | InvocationTargetException
79                     | NoSuchMethodException e) {
80                 LOG.warn("could not create custom strategy", e);
81             }
82             return FirstCandidateSelectionStrategy.INSTANCE;
83         }
84     }
85
86     public static Builder newBuilder() {
87         return new Builder(new EntityOwnerSelectionStrategyConfig());
88     }
89
90     public static final class Builder {
91         private final EntityOwnerSelectionStrategyConfig config;
92
93         Builder(final EntityOwnerSelectionStrategyConfig config) {
94             this.config = config;
95         }
96
97         public Builder addStrategy(final String entityType,
98                 final Class<? extends EntityOwnerSelectionStrategy> strategy, final long delay) {
99             config.entityTypeToStrategyInfo.put(entityType, new StrategyInfo(strategy, delay));
100             return this;
101         }
102
103         public EntityOwnerSelectionStrategyConfig build() {
104             return this.config;
105         }
106     }
107 }