cc9ff63aba74063812f664285d2d706285811946
[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 static final String ENTITY_TYPE_PREFIX = "entity.type.";
28
29     private final BundleContext bundleContext;
30     private final EntityOwnerSelectionStrategyConfig config;
31
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();
39         } else {
40             this.config = readConfiguration(configAdminServiceReference);
41         }
42     }
43
44     private EntityOwnerSelectionStrategyConfig readConfiguration(ServiceReference<ConfigurationAdmin> configAdminServiceReference) {
45         EntityOwnerSelectionStrategyConfig.Builder builder = EntityOwnerSelectionStrategyConfig.newBuilder();
46         ConfigurationAdmin configAdmin = null;
47         try {
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]);
60                         long delay = 0;
61                         if(strategyClassAndDelay.length > 1){
62                             delay = Long.parseLong(strategyClassAndDelay[1]);
63                         }
64                         String entityType = key.substring(key.lastIndexOf(".") + 1);
65                         builder.addStrategy(entityType, aClass, delay);
66                     } else {
67                         LOG.debug("Ignoring non-conforming property key : {}, value : {}", key, strategyProps);
68                     }
69                 }
70             } else {
71                 LOG.error("Could not read strategy configuration file, will use default configuration");
72             }
73         } catch(Exception e){
74             LOG.warn("Failed to read selection strategy configuration file. All configuration will be ignored.", e);
75         } finally {
76             if(configAdmin != null) {
77                 try {
78                     bundleContext.ungetService(configAdminServiceReference);
79                 } catch (Exception e) {
80                     LOG.debug("Error from ungetService", e);
81                 }
82             }
83         }
84
85         return builder.build();
86     }
87
88     @Nullable
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;
92     }
93
94     public EntityOwnerSelectionStrategyConfig getConfig() {
95         return config;
96     }
97 }