f9d1aea0a1effd93e1aa1e65e23393f35de43ce1
[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 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;
21
22 public class EntityOwnerSelectionStrategyConfigReader {
23     public static final String CONFIG_ID = "org.opendaylight.controller.cluster.entity-owner-selection-strategies";
24
25     private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerSelectionStrategyConfigReader.class);
26     private final BundleContext bundleContext;
27     private final EntityOwnerSelectionStrategyConfig config;
28
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();
36         } else {
37             this.config = readConfiguration(configAdminServiceReference);
38         }
39     }
40
41     private EntityOwnerSelectionStrategyConfig readConfiguration(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
42         EntityOwnerSelectionStrategyConfig strategyConfig;
43         EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder();
44         try {
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]);
56                     long delay = 0;
57                     if(strategyClassAndDelay.length > 1){
58                         delay = Long.parseLong(strategyClassAndDelay[1]);
59                     }
60                     builder.addStrategy(key, aClass, delay);
61                 }
62             }
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();
67         } finally {
68             bundleContext.ungetService(configAdminServiceReference);
69         }
70         return strategyConfig;
71     }
72
73     public EntityOwnerSelectionStrategyConfig getConfig() {
74         return config;
75     }
76 }