59acdbdb01f8346232123e5952951a76b073a8c6
[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.AbstractMap.SimpleEntry;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Set;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import org.opendaylight.controller.cluster.access.concepts.MemberName;
27 import org.opendaylight.controller.cluster.datastore.shardstrategy.PrefixShardStrategy;
28 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
29 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
30 import org.opendaylight.controller.cluster.datastore.utils.ClusterUtils;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32
33 // TODO clean this up once we get rid of module based configuration, prefix one should be alot simpler
34 public class ConfigurationImpl implements Configuration {
35     private volatile Map<String, ModuleConfig> moduleConfigMap;
36
37     // TODO should this be initialized with something? on restart we should restore the shards from configuration?
38     private volatile Map<YangInstanceIdentifier, PrefixShardConfiguration> prefixConfigMap = Collections.emptyMap();
39
40     // Look up maps to speed things up
41
42     private volatile Map<String, String> namespaceToModuleName;
43     private volatile Set<String> allShardNames;
44
45     public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
46         this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
47     }
48
49     public ConfigurationImpl(final ModuleShardConfigProvider provider) {
50         ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
51         for (Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
52             mapBuilder.put(e.getKey(), e.getValue().build());
53         }
54
55         this.moduleConfigMap = mapBuilder.build();
56
57         this.allShardNames = createAllShardNames(moduleConfigMap.values());
58         this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
59     }
60
61     private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
62         final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
63         for (ModuleConfig moduleConfig : moduleConfigs) {
64             builder.addAll(moduleConfig.getShardNames());
65         }
66
67         return builder.build();
68     }
69
70     private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
71         final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
72         for (ModuleConfig moduleConfig : moduleConfigs) {
73             if (moduleConfig.getNamespace() != null) {
74                 builder.put(moduleConfig.getNamespace(), moduleConfig.getName());
75             }
76         }
77
78         return builder.build();
79     }
80
81     @Override
82     public Collection<String> getMemberShardNames(final MemberName memberName) {
83         Preconditions.checkNotNull(memberName, "memberName should not be null");
84
85         List<String> shards = new ArrayList<>();
86         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
87             for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
88                 if (shardConfig.getReplicas().contains(memberName)) {
89                     shards.add(shardConfig.getName());
90                 }
91             }
92         }
93
94         return shards;
95     }
96
97     @Override
98     public String getModuleNameFromNameSpace(final String nameSpace) {
99         Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
100
101         return namespaceToModuleName.get(nameSpace);
102     }
103
104     @Override
105     public ShardStrategy getStrategyForModule(String moduleName) {
106         Preconditions.checkNotNull(moduleName, "moduleName should not be null");
107
108         ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
109         return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
110     }
111
112     @Override
113     public String getShardNameForModule(final String moduleName) {
114         Preconditions.checkNotNull(moduleName, "moduleName should not be null");
115
116         ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
117         Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
118             Collections.<ShardConfig>emptySet();
119         return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
120     }
121
122     @Nullable
123     @Override
124     public String getShardNameForPrefix(@Nonnull final YangInstanceIdentifier prefix) {
125         Preconditions.checkNotNull(prefix, "prefix should not be null");
126
127         Entry<YangInstanceIdentifier, PrefixShardConfiguration> bestMatchEntry =
128                 new SimpleEntry<>(YangInstanceIdentifier.EMPTY, null);
129
130         for (Entry<YangInstanceIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
131             if (entry.getKey().contains(prefix) && entry.getKey().getPathArguments().size()
132                     > bestMatchEntry.getKey().getPathArguments().size()) {
133                 bestMatchEntry = entry;
134             }
135         }
136
137         //TODO we really should have mapping based on prefix instead of Strings
138         return ClusterUtils.getCleanShardName(bestMatchEntry.getValue().getPrefix().getRootIdentifier());
139     }
140
141     @Override
142     public Collection<MemberName> getMembersFromShardName(final String shardName) {
143         Preconditions.checkNotNull(shardName, "shardName should not be null");
144
145         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
146             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
147             if (shardConfig != null) {
148                 return shardConfig.getReplicas();
149             }
150         }
151
152         for (final PrefixShardConfiguration prefixConfig : prefixConfigMap.values()) {
153             if (shardName.equals(ClusterUtils.getCleanShardName(prefixConfig.getPrefix().getRootIdentifier()))) {
154                 return prefixConfig.getShardMemberNames();
155             }
156         }
157
158         return Collections.emptyList();
159     }
160
161     @Override
162     public Set<String> getAllShardNames() {
163         return allShardNames;
164     }
165
166     @Override
167     public Collection<MemberName> getUniqueMemberNamesForAllShards() {
168         Set<MemberName> allNames = new HashSet<>();
169         for (String shardName: getAllShardNames()) {
170             allNames.addAll(getMembersFromShardName(shardName));
171         }
172
173         return allNames;
174     }
175
176     @Override
177     public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
178         Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
179
180         ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
181                 .nameSpace(config.getNamespace().toASCIIString())
182                 .shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()))
183                 .shardConfig(config.getShardName(), config.getShardMemberNames()).build();
184
185         updateModuleConfigMap(moduleConfig);
186
187         namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
188                 .put(moduleConfig.getNamespace(), moduleConfig.getName()).build();
189         allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
190     }
191
192     @Override
193     public void addPrefixShardConfiguration(@Nonnull final PrefixShardConfiguration config) {
194         Preconditions.checkNotNull(config, "PrefixShardConfiguration cannot be null");
195         updatePrefixConfigMap(config);
196         allShardNames = ImmutableSet.<String>builder().addAll(allShardNames)
197                 .add(ClusterUtils.getCleanShardName(config.getPrefix().getRootIdentifier())).build();
198     }
199
200     private void updatePrefixConfigMap(final PrefixShardConfiguration config) {
201         final Map<YangInstanceIdentifier, PrefixShardConfiguration> newPrefixConfigMap = new HashMap<>(prefixConfigMap);
202         newPrefixConfigMap.put(config.getPrefix().getRootIdentifier(), config);
203         prefixConfigMap = ImmutableMap.copyOf(newPrefixConfigMap);
204     }
205
206     private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
207         return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
208     }
209
210     @Override
211     public boolean isShardConfigured(String shardName) {
212         Preconditions.checkNotNull(shardName, "shardName should not be null");
213         return allShardNames.contains(shardName);
214     }
215
216     @Override
217     public void addMemberReplicaForShard(String shardName, MemberName newMemberName) {
218         Preconditions.checkNotNull(shardName, "shardName should not be null");
219         Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
220
221         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
222             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
223             if (shardConfig != null) {
224                 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
225                 replicas.add(newMemberName);
226                 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
227                 return;
228             }
229         }
230     }
231
232     @Override
233     public void removeMemberReplicaForShard(String shardName, MemberName newMemberName) {
234         Preconditions.checkNotNull(shardName, "shardName should not be null");
235         Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
236
237         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
238             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
239             if (shardConfig != null) {
240                 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
241                 replicas.remove(newMemberName);
242                 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
243                 return;
244             }
245         }
246     }
247
248     @Override
249     public ShardStrategy getStrategyForPrefix(@Nonnull final YangInstanceIdentifier prefix) {
250         Preconditions.checkNotNull(prefix, "Prefix cannot be null");
251         // FIXME using prefix tables like in mdsal will be better
252         Entry<YangInstanceIdentifier, PrefixShardConfiguration> bestMatchEntry =
253                 new SimpleEntry<>(YangInstanceIdentifier.EMPTY, null);
254
255         for (Entry<YangInstanceIdentifier, PrefixShardConfiguration> entry : prefixConfigMap.entrySet()) {
256             if (entry.getKey().contains(prefix) && entry.getKey().getPathArguments().size()
257                     > bestMatchEntry.getKey().getPathArguments().size()) {
258                 bestMatchEntry = entry;
259             }
260         }
261
262         if (bestMatchEntry.getValue() == null) {
263             return null;
264         }
265         return new PrefixShardStrategy(
266                 ClusterUtils.getCleanShardName(bestMatchEntry.getValue().getPrefix().getRootIdentifier()), this);
267     }
268
269     private void updateModuleConfigMap(final ModuleConfig moduleConfig) {
270         final Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
271         newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
272         moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);
273     }
274 }