Bug 4105: Add dynamic module/shard config for entity-owners shard
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / config / ConfigurationImpl.java
1 /*
2  * Copyright (c) 2014 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.config;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
22 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
23
24 public class ConfigurationImpl implements Configuration {
25     private volatile Map<String, ModuleConfig> moduleConfigMap;
26
27     // Look up maps to speed things up
28
29     private volatile Map<String, String> namespaceToModuleName;
30     private volatile Set<String> allShardNames;
31
32     public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
33         this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
34     }
35
36     public ConfigurationImpl(final ModuleShardConfigProvider provider) {
37         this.moduleConfigMap = ImmutableMap.copyOf(provider.retrieveModuleConfigs(this));
38
39         this.allShardNames = createAllShardNames(moduleConfigMap.values());
40         this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
41     }
42
43     private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
44         final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
45         for(ModuleConfig moduleConfig : moduleConfigs) {
46             builder.addAll(moduleConfig.getShardNames());
47         }
48
49         return builder.build();
50     }
51
52     private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
53         final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
54         for(ModuleConfig moduleConfig : moduleConfigs) {
55             if(moduleConfig.getNameSpace() != null) {
56                 builder.put(moduleConfig.getNameSpace(), moduleConfig.getName());
57             }
58         }
59
60         return builder.build();
61     }
62
63     @Override
64     public Collection<String> getMemberShardNames(final String memberName){
65         Preconditions.checkNotNull(memberName, "memberName should not be null");
66
67         List<String> shards = new ArrayList<>();
68         for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
69             for(ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
70                 if(shardConfig.getReplicas().contains(memberName)) {
71                     shards.add(shardConfig.getName());
72                 }
73             }
74         }
75
76         return shards;
77     }
78
79     @Override
80     public String getModuleNameFromNameSpace(final String nameSpace) {
81         Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
82
83         return namespaceToModuleName.get(nameSpace);
84     }
85
86     @Override
87     public ShardStrategy getStrategyForModule(String moduleName) {
88         Preconditions.checkNotNull(moduleName, "moduleName should not be null");
89
90         ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
91         return moduleConfig != null ? moduleConfig.getShardStrategy(): null;
92     }
93
94     @Override
95     public String getShardNameForModule(final String moduleName) {
96         Preconditions.checkNotNull(moduleName, "moduleName should not be null");
97
98         ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
99         Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
100             Collections.<ShardConfig>emptySet();
101         return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName(): null;
102     }
103
104     @Override
105     public Collection<String> getMembersFromShardName(final String shardName) {
106         Preconditions.checkNotNull(shardName, "shardName should not be null");
107
108         for(ModuleConfig moduleConfig: moduleConfigMap.values()) {
109             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
110             if(shardConfig != null) {
111                 return shardConfig.getReplicas();
112             }
113         }
114
115         return Collections.emptyList();
116     }
117
118     @Override
119     public Set<String> getAllShardNames() {
120         return allShardNames;
121     }
122
123     @Override
124     public Collection<String> getUniqueMemberNamesForAllShards() {
125         Set<String> allNames = new HashSet<>();
126         for(String shardName: getAllShardNames()) {
127             allNames.addAll(getMembersFromShardName(shardName));
128         }
129
130         return allNames;
131     }
132
133     @Override
134     public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
135         Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
136
137         ModuleConfig moduleConfig = new ModuleConfig(config.getModuleName());
138         moduleConfig.setNameSpace(config.getNamespace().toASCIIString());
139         moduleConfig.setShardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()));
140
141         moduleConfig.addShardConfig(config.getShardName(), ImmutableSet.copyOf(config.getShardMemberNames()));
142
143         moduleConfigMap = ImmutableMap.<String, ModuleConfig>builder().putAll(moduleConfigMap).
144                 put(config.getModuleName(), moduleConfig).build();
145
146         namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName).
147                 put(moduleConfig.getNameSpace(), moduleConfig.getName()).build();
148         allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
149     }
150
151     private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
152         return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
153     }
154 }