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
8 package org.opendaylight.controller.cluster.entityownership.selectionstrategy;
10 import java.lang.reflect.InvocationTargetException;
11 import java.util.HashMap;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
17 * FIXME: this is simple registry service, except it also loads classes.
19 public final class EntityOwnerSelectionStrategyConfig {
20 private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfig.class);
21 private final Map<String, StrategyInfo> entityTypeToStrategyInfo = new HashMap<>();
22 private final Map<String, EntityOwnerSelectionStrategy> entityTypeToOwnerSelectionStrategy = new HashMap<>();
24 private EntityOwnerSelectionStrategyConfig() {
28 public boolean isStrategyConfigured(final String entityType) {
29 return entityTypeToStrategyInfo.get(entityType) != null;
32 public EntityOwnerSelectionStrategy createStrategy(final String entityType,
33 final 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 * This class should not exist. It contains a single long, which is passed to the constructor (via reflection).
52 * We are getting that information from a BundleContext. We are running in OSGi environment, hence this class
53 * needs to be deployed in its own bundle, with its own configuration.
54 * If this is used internally, it needs to be relocated into a separate package along with the implementation
57 * @deprecated FIXME: THIS IS CONFIGURATION FOR A CUSTOM-LOADED CLASS CONSTRUCTOR
60 public void clearStrategies() {
61 entityTypeToOwnerSelectionStrategy.clear();
64 private static final class StrategyInfo {
65 private final Class<? extends EntityOwnerSelectionStrategy> strategyClass;
66 private final long delay;
68 private StrategyInfo(final Class<? extends EntityOwnerSelectionStrategy> strategyClass, final long delay) {
69 this.strategyClass = strategyClass;
73 public EntityOwnerSelectionStrategy createStrategy(final Map<String, Long> initialStatistics) {
75 return strategyClass.getDeclaredConstructor(long.class, Map.class)
76 .newInstance(delay, initialStatistics);
77 } catch (InstantiationException | IllegalAccessException | InvocationTargetException
78 | NoSuchMethodException e) {
79 LOG.warn("could not create custom strategy", e);
81 return FirstCandidateSelectionStrategy.INSTANCE;
85 public static Builder newBuilder() {
86 return new Builder(new EntityOwnerSelectionStrategyConfig());
89 public static final class Builder {
90 private final EntityOwnerSelectionStrategyConfig config;
92 Builder(final EntityOwnerSelectionStrategyConfig config) {
96 public Builder addStrategy(final String entityType,
97 final Class<? extends EntityOwnerSelectionStrategy> strategy, final long delay) {
98 config.entityTypeToStrategyInfo.put(entityType, new StrategyInfo(strategy, delay));
102 public EntityOwnerSelectionStrategyConfig build() {