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 javax.annotation.Nullable;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.ServiceReference;
18 import org.osgi.service.cm.Configuration;
19 import org.osgi.service.cm.ConfigurationAdmin;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class EntityOwnerSelectionStrategyConfigReader {
24 public static final String CONFIG_ID = "org.opendaylight.controller.cluster.entity-owner-selection-strategies";
26 private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfigReader.class);
27 private final BundleContext bundleContext;
28 private final EntityOwnerSelectionStrategyConfig config;
30 public EntityOwnerSelectionStrategyConfigReader(BundleContext bundleContext) {
31 this.bundleContext = Preconditions.checkNotNull(bundleContext);
32 ServiceReference<ConfigurationAdmin> configAdminServiceReference =
33 bundleContext.getServiceReference(ConfigurationAdmin.class);
34 if(configAdminServiceReference == null) {
35 LOG.warn("No ConfigurationAdmin service found");
36 this.config = EntityOwnerSelectionStrategyConfig.newBuilder().build();
38 this.config = readConfiguration(configAdminServiceReference);
42 private EntityOwnerSelectionStrategyConfig readConfiguration(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
43 EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder();
44 ConfigurationAdmin configAdmin = null;
46 configAdmin = bundleContext.getService(configAdminServiceReference);
47 Dictionary<String, Object> properties = getProperties(configAdmin);
48 if(properties != null) {
49 Enumeration<String> keys = properties.keys();
50 while (keys.hasMoreElements()) {
51 String key = keys.nextElement();
52 String strategyProps = (String) properties.get(key);
53 String[] strategyClassAndDelay = strategyProps.split(",");
54 if(strategyClassAndDelay.length >= 1) {
55 @SuppressWarnings("unchecked")
56 Class<? extends EntityOwnerSelectionStrategy> aClass
57 = (Class<? extends EntityOwnerSelectionStrategy>) getClass().getClassLoader().loadClass(strategyClassAndDelay[0]);
59 if(strategyClassAndDelay.length > 1){
60 delay = Long.parseLong(strategyClassAndDelay[1]);
62 builder.addStrategy(key, aClass, delay);
67 LOG.warn("Failed to read selection strategy configuration file. All configuration will be ignored.", e);
69 if(configAdmin != null) {
71 bundleContext.ungetService(configAdminServiceReference);
72 } catch (Exception e) {
73 LOG.debug("Error from ungetService", e);
78 return builder.build();
82 private static Dictionary<String, Object> getProperties(ConfigurationAdmin configAdmin) throws IOException {
83 Configuration config = configAdmin.getConfiguration(CONFIG_ID);
84 return config != null ? config.getProperties() : null;
87 public EntityOwnerSelectionStrategyConfig getConfig() {