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