Fix warnings/javadocs in sal-distributed-datastore
[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.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import org.opendaylight.controller.cluster.access.concepts.MemberName;
23 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
24 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
25
26 public class ConfigurationImpl implements Configuration {
27     private volatile Map<String, ModuleConfig> moduleConfigMap;
28
29     // Look up maps to speed things up
30
31     private volatile Map<String, String> namespaceToModuleName;
32     private volatile Set<String> allShardNames;
33
34     public ConfigurationImpl(final String moduleShardsConfigPath, final String modulesConfigPath) {
35         this(new FileModuleShardConfigProvider(moduleShardsConfigPath, modulesConfigPath));
36     }
37
38     public ConfigurationImpl(final ModuleShardConfigProvider provider) {
39         ImmutableMap.Builder<String, ModuleConfig> mapBuilder = ImmutableMap.builder();
40         for (Map.Entry<String, ModuleConfig.Builder> e: provider.retrieveModuleConfigs(this).entrySet()) {
41             mapBuilder.put(e.getKey(), e.getValue().build());
42         }
43
44         this.moduleConfigMap = mapBuilder.build();
45
46         this.allShardNames = createAllShardNames(moduleConfigMap.values());
47         this.namespaceToModuleName = createNamespaceToModuleName(moduleConfigMap.values());
48     }
49
50     private static Set<String> createAllShardNames(Iterable<ModuleConfig> moduleConfigs) {
51         final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
52         for (ModuleConfig moduleConfig : moduleConfigs) {
53             builder.addAll(moduleConfig.getShardNames());
54         }
55
56         return builder.build();
57     }
58
59     private static Map<String, String> createNamespaceToModuleName(Iterable<ModuleConfig> moduleConfigs) {
60         final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
61         for (ModuleConfig moduleConfig : moduleConfigs) {
62             if (moduleConfig.getNameSpace() != null) {
63                 builder.put(moduleConfig.getNameSpace(), moduleConfig.getName());
64             }
65         }
66
67         return builder.build();
68     }
69
70     @Override
71     public Collection<String> getMemberShardNames(final MemberName memberName) {
72         Preconditions.checkNotNull(memberName, "memberName should not be null");
73
74         List<String> shards = new ArrayList<>();
75         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
76             for (ShardConfig shardConfig: moduleConfig.getShardConfigs()) {
77                 if (shardConfig.getReplicas().contains(memberName)) {
78                     shards.add(shardConfig.getName());
79                 }
80             }
81         }
82
83         return shards;
84     }
85
86     @Override
87     public String getModuleNameFromNameSpace(final String nameSpace) {
88         Preconditions.checkNotNull(nameSpace, "nameSpace should not be null");
89
90         return namespaceToModuleName.get(nameSpace);
91     }
92
93     @Override
94     public ShardStrategy getStrategyForModule(String moduleName) {
95         Preconditions.checkNotNull(moduleName, "moduleName should not be null");
96
97         ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
98         return moduleConfig != null ? moduleConfig.getShardStrategy() : null;
99     }
100
101     @Override
102     public String getShardNameForModule(final String moduleName) {
103         Preconditions.checkNotNull(moduleName, "moduleName should not be null");
104
105         ModuleConfig moduleConfig = moduleConfigMap.get(moduleName);
106         Collection<ShardConfig> shardConfigs = moduleConfig != null ? moduleConfig.getShardConfigs() :
107             Collections.<ShardConfig>emptySet();
108         return !shardConfigs.isEmpty() ? shardConfigs.iterator().next().getName() : null;
109     }
110
111     @Override
112     public Collection<MemberName> getMembersFromShardName(final String shardName) {
113         Preconditions.checkNotNull(shardName, "shardName should not be null");
114
115         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
116             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
117             if (shardConfig != null) {
118                 return shardConfig.getReplicas();
119             }
120         }
121
122         return Collections.emptyList();
123     }
124
125     @Override
126     public Set<String> getAllShardNames() {
127         return allShardNames;
128     }
129
130     @Override
131     public Collection<MemberName> getUniqueMemberNamesForAllShards() {
132         Set<MemberName> allNames = new HashSet<>();
133         for (String shardName: getAllShardNames()) {
134             allNames.addAll(getMembersFromShardName(shardName));
135         }
136
137         return allNames;
138     }
139
140     @Override
141     public synchronized void addModuleShardConfiguration(ModuleShardConfiguration config) {
142         Preconditions.checkNotNull(config, "ModuleShardConfiguration should not be null");
143
144         ModuleConfig moduleConfig = ModuleConfig.builder(config.getModuleName())
145                 .nameSpace(config.getNamespace().toASCIIString())
146                 .shardStrategy(createShardStrategy(config.getModuleName(), config.getShardStrategyName()))
147                 .shardConfig(config.getShardName(), config.getShardMemberNames()).build();
148
149         updateModuleConfigMap(moduleConfig);
150
151         namespaceToModuleName = ImmutableMap.<String, String>builder().putAll(namespaceToModuleName)
152                 .put(moduleConfig.getNameSpace(), moduleConfig.getName()).build();
153         allShardNames = ImmutableSet.<String>builder().addAll(allShardNames).add(config.getShardName()).build();
154     }
155
156     private ShardStrategy createShardStrategy(String moduleName, String shardStrategyName) {
157         return ShardStrategyFactory.newShardStrategyInstance(moduleName, shardStrategyName, this);
158     }
159
160     @Override
161     public boolean isShardConfigured(String shardName) {
162         Preconditions.checkNotNull(shardName, "shardName should not be null");
163         return allShardNames.contains(shardName);
164     }
165
166     @Override
167     public void addMemberReplicaForShard(String shardName, MemberName newMemberName) {
168         Preconditions.checkNotNull(shardName, "shardName should not be null");
169         Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
170
171         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
172             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
173             if (shardConfig != null) {
174                 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
175                 replicas.add(newMemberName);
176                 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
177                 return;
178             }
179         }
180     }
181
182     @Override
183     public void removeMemberReplicaForShard(String shardName, MemberName newMemberName) {
184         Preconditions.checkNotNull(shardName, "shardName should not be null");
185         Preconditions.checkNotNull(newMemberName, "MemberName should not be null");
186
187         for (ModuleConfig moduleConfig: moduleConfigMap.values()) {
188             ShardConfig shardConfig = moduleConfig.getShardConfig(shardName);
189             if (shardConfig != null) {
190                 Set<MemberName> replicas = new HashSet<>(shardConfig.getReplicas());
191                 replicas.remove(newMemberName);
192                 updateModuleConfigMap(ModuleConfig.builder(moduleConfig).shardConfig(shardName, replicas).build());
193                 return;
194             }
195         }
196     }
197
198     private void updateModuleConfigMap(ModuleConfig moduleConfig) {
199         Map<String, ModuleConfig> newModuleConfigMap = new HashMap<>(moduleConfigMap);
200         newModuleConfigMap.put(moduleConfig.getName(), moduleConfig);
201         moduleConfigMap = ImmutableMap.copyOf(newModuleConfigMap);
202     }
203 }