2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
11 import java.lang.reflect.InvocationTargetException;
12 import java.util.HashMap;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
18 * FIXME: this is simple registry service, except it also loads classes.
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<>();
25 private EntityOwnerSelectionStrategyConfig() {
29 public boolean isStrategyConfigured(final String entityType) {
30 return entityTypeToStrategyInfo.get(entityType) != null;
33 public EntityOwnerSelectionStrategy createStrategy(String entityType, Map<String, Long> initialStatistics){
34 final EntityOwnerSelectionStrategy strategy;
35 final EntityOwnerSelectionStrategy existingStrategy = entityTypeToOwnerSelectionStrategy.get(entityType);
36 if(existingStrategy != null){
37 strategy = existingStrategy;
39 EntityOwnerSelectionStrategyConfig.StrategyInfo strategyInfo = entityTypeToStrategyInfo.get(entityType);
40 if(strategyInfo == null){
41 strategy = FirstCandidateSelectionStrategy.INSTANCE;
43 strategy = strategyInfo.createStrategy(initialStatistics);
45 entityTypeToOwnerSelectionStrategy.put(entityType, strategy);
51 * @deprecated FIXME: THIS IS CONFIGURATION FOR A CUSTOM-LOADED CLASS CONSTRUCTOR
53 * This class should not exist. It contains a single long, which is passed to the constructor (via reflection).
54 * We are getting that information from a BundleContext. We are running in OSGi environment, hence this class
55 * needs to be deployed in its own bundle, with its own configuration.
57 * If this is used internally, it needs to be relocated into a separate package along with the implementation
61 public void clearStrategies() {
62 entityTypeToOwnerSelectionStrategy.clear();
65 private static final class StrategyInfo {
66 private final Class<? extends EntityOwnerSelectionStrategy> strategyClass;
67 private final long delay;
69 private StrategyInfo(final Class<? extends EntityOwnerSelectionStrategy> strategyClass, final long delay) {
70 this.strategyClass = strategyClass;
74 public EntityOwnerSelectionStrategy createStrategy(Map<String, Long> initialStatistics){
76 return strategyClass.getDeclaredConstructor(long.class, Map.class).newInstance(delay, initialStatistics);
77 } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
78 LOG.warn("could not create custom strategy", e);
80 return FirstCandidateSelectionStrategy.INSTANCE;
84 public static Builder newBuilder() {
85 return new Builder(new EntityOwnerSelectionStrategyConfig());
88 public static class Builder {
89 private final EntityOwnerSelectionStrategyConfig config;
91 private Builder(final EntityOwnerSelectionStrategyConfig config){
95 public Builder addStrategy(final String entityType, final Class<? extends EntityOwnerSelectionStrategy> strategy, final long delay){
96 config.entityTypeToStrategyInfo.put(entityType, new StrategyInfo(strategy, delay));
100 public EntityOwnerSelectionStrategyConfig build(){