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