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