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 com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import java.util.Dictionary;
14 import java.util.Enumeration;
15 import org.osgi.framework.BundleContext;
16 import org.osgi.framework.ServiceReference;
17 import org.osgi.service.cm.Configuration;
18 import org.osgi.service.cm.ConfigurationAdmin;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class EntityOwnerSelectionStrategyConfigReader {
23 public static final String CONFIG_ID = "org.opendaylight.controller.cluster.entity-owner-selection-strategies";
25 private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfigReader.class);
26 private final BundleContext bundleContext;
27 private final EntityOwnerSelectionStrategyConfig config;
29 public EntityOwnerSelectionStrategyConfigReader(BundleContext bundleContext) {
30 this.bundleContext = Preconditions.checkNotNull(bundleContext);
31 ServiceReference<ConfigurationAdmin> configAdminServiceReference =
32 bundleContext.getServiceReference(ConfigurationAdmin.class);
33 if(configAdminServiceReference == null) {
34 LOG.warn("No ConfigurationAdmin service found");
35 this.config = EntityOwnerSelectionStrategyConfig.newBuilder().build();
37 this.config = readConfiguration(configAdminServiceReference);
41 private EntityOwnerSelectionStrategyConfig readConfiguration(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
42 EntityOwnerSelectionStrategyConfig strategyConfig;
43 EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder();
45 ConfigurationAdmin configAdmin = bundleContext.getService(configAdminServiceReference);
46 Configuration config = configAdmin.getConfiguration(CONFIG_ID);
47 Dictionary<String, Object> properties = config.getProperties();
48 Enumeration<String> keys = properties.keys();
49 while (keys.hasMoreElements()) {
50 String key = keys.nextElement();
51 String strategyProps = (String) properties.get(key);
52 String[] strategyClassAndDelay = strategyProps.split(",");
53 if(strategyClassAndDelay.length >= 1) {
54 Class<? extends EntityOwnerSelectionStrategy> aClass
55 = (Class<? extends EntityOwnerSelectionStrategy>) getClass().getClassLoader().loadClass(strategyClassAndDelay[0]);
57 if(strategyClassAndDelay.length > 1){
58 delay = Long.parseLong(strategyClassAndDelay[1]);
60 builder.addStrategy(key, aClass, delay);
63 strategyConfig = builder.build();
64 } catch(IOException | ClassNotFoundException | NumberFormatException e){
65 LOG.warn("Failed to read selection strategy configuration file. All configuration will be ignored.", e);
66 strategyConfig = EntityOwnerSelectionStrategyConfig.newBuilder().build();
68 bundleContext.ungetService(configAdminServiceReference);
70 return strategyConfig;
73 public EntityOwnerSelectionStrategyConfig getConfig() {