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 static final String ENTITY_TYPE_PREFIX = "entity.type.";
29 private final BundleContext bundleContext;
30 private final EntityOwnerSelectionStrategyConfig config;
32 public EntityOwnerSelectionStrategyConfigReader(BundleContext bundleContext) {
33 this.bundleContext = Preconditions.checkNotNull(bundleContext);
34 ServiceReference<ConfigurationAdmin> configAdminServiceReference =
35 bundleContext.getServiceReference(ConfigurationAdmin.class);
36 if(configAdminServiceReference == null) {
37 LOG.warn("No ConfigurationAdmin service found");
38 this.config = EntityOwnerSelectionStrategyConfig.newBuilder().build();
40 this.config = readConfiguration(configAdminServiceReference);
44 private EntityOwnerSelectionStrategyConfig readConfiguration(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
45 EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder();
46 ConfigurationAdmin configAdmin = null;
48 configAdmin = bundleContext.getService(configAdminServiceReference);
49 Dictionary<String, Object> properties = getProperties(configAdmin);
50 if(properties != null) {
51 Enumeration<String> keys = properties.keys();
52 while (keys.hasMoreElements()) {
53 String key = keys.nextElement();
54 String strategyProps = (String) properties.get(key);
55 String[] strategyClassAndDelay = strategyProps.split(",");
56 if(key.startsWith(ENTITY_TYPE_PREFIX)) {
57 @SuppressWarnings("unchecked")
58 Class<? extends EntityOwnerSelectionStrategy> aClass
59 = (Class<? extends EntityOwnerSelectionStrategy>) getClass().getClassLoader().loadClass(strategyClassAndDelay[0]);
61 if(strategyClassAndDelay.length > 1){
62 delay = Long.parseLong(strategyClassAndDelay[1]);
64 String entityType = key.substring(key.lastIndexOf(".") + 1);
65 builder.addStrategy(entityType, aClass, delay);
67 LOG.debug("Ignoring non-conforming property key : {}, value : {}", key, strategyProps);
71 LOG.error("Could not read strategy configuration file, will use default configuration");
74 LOG.warn("Failed to read selection strategy configuration file. All configuration will be ignored.", e);
76 if(configAdmin != null) {
78 bundleContext.ungetService(configAdminServiceReference);
79 } catch (Exception e) {
80 LOG.debug("Error from ungetService", e);
85 return builder.build();
89 private static Dictionary<String, Object> getProperties(ConfigurationAdmin configAdmin) throws IOException {
90 Configuration config = configAdmin.getConfiguration(CONFIG_ID);
91 return config != null ? config.getProperties() : null;
94 public EntityOwnerSelectionStrategyConfig getConfig() {