Bug 4992: Removed old leader's candidates on leader change
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / selectionstrategy / EntityOwnerSelectionStrategyConfigReader.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy;
10
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;
22
23 public class EntityOwnerSelectionStrategyConfigReader {
24     public static final String CONFIG_ID = "org.opendaylight.controller.cluster.entity-owner-selection-strategies";
25
26     private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfigReader.class);
27     private final BundleContext bundleContext;
28     private final EntityOwnerSelectionStrategyConfig config;
29
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();
37         } else {
38             this.config = readConfiguration(configAdminServiceReference);
39         }
40     }
41
42     private EntityOwnerSelectionStrategyConfig readConfiguration(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
43         EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder();
44         ConfigurationAdmin configAdmin = null;
45         try {
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]);
58                         long delay = 0;
59                         if(strategyClassAndDelay.length > 1){
60                             delay = Long.parseLong(strategyClassAndDelay[1]);
61                         }
62                         builder.addStrategy(key, aClass, delay);
63                     }
64                 }
65             }
66         } catch(Exception e){
67             LOG.warn("Failed to read selection strategy configuration file. All configuration will be ignored.", e);
68         } finally {
69             if(configAdmin != null) {
70                 try {
71                     bundleContext.ungetService(configAdminServiceReference);
72                 } catch (Exception e) {
73                     LOG.debug("Error from ungetService", e);
74                 }
75             }
76         }
77
78         return builder.build();
79     }
80
81     @Nullable
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;
85     }
86
87     public EntityOwnerSelectionStrategyConfig getConfig() {
88         return config;
89     }
90 }