BUG-5280: use MemberName instead of String
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / config / ModuleConfig.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.Map;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.cluster.access.concepts.MemberName;
18 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategy;
19
20 /**
21  * Encapsulates configuration for a module.
22  *
23  * @author Thomas Pantelis
24  */
25 public class ModuleConfig {
26     private final String name;
27     private final String nameSpace;
28     private final ShardStrategy shardStrategy;
29     private final Map<String, ShardConfig> shardConfigs;
30
31     private ModuleConfig(String name, String nameSpace, ShardStrategy shardStrategy,
32             Map<String, ShardConfig> shardConfigs) {
33         this.name = name;
34         this.nameSpace = nameSpace;
35         this.shardStrategy = shardStrategy;
36         this.shardConfigs = shardConfigs;
37     }
38
39     @Nonnull
40     public String getName() {
41         return name;
42     }
43
44     @Nullable
45     public String getNameSpace() {
46         return nameSpace;
47     }
48
49     @Nullable
50     public ShardStrategy getShardStrategy() {
51         return shardStrategy;
52     }
53
54     @Nullable
55     public ShardConfig getShardConfig(String name) {
56         return shardConfigs.get(name);
57     }
58
59     @Nonnull
60     public Collection<ShardConfig> getShardConfigs() {
61         return shardConfigs.values();
62     }
63
64     @Nonnull
65     public Collection<String> getShardNames() {
66         return shardConfigs.keySet();
67     }
68
69     public static Builder builder(String name) {
70         return new Builder(name);
71     }
72
73     public static Builder builder(ModuleConfig moduleConfig) {
74         return new Builder(moduleConfig);
75     }
76
77     public static class Builder {
78         private String name;
79         private String nameSpace;
80         private ShardStrategy shardStrategy;
81         private final Map<String, ShardConfig> shardConfigs = new HashMap<>();
82
83         private Builder(String name) {
84             this.name = name;
85         }
86
87         private Builder(ModuleConfig moduleConfig) {
88             this.name = moduleConfig.getName();
89             this.nameSpace = moduleConfig.getNameSpace();
90             this.shardStrategy = moduleConfig.getShardStrategy();
91             for (ShardConfig shardConfig : moduleConfig.getShardConfigs()) {
92                 shardConfigs.put(shardConfig.getName(), shardConfig);
93             }
94         }
95
96         public Builder name(String name) {
97             this.name = name;
98             return this;
99         }
100
101         public Builder nameSpace(String nameSpace) {
102             this.nameSpace = nameSpace;
103             return this;
104         }
105
106         public Builder shardStrategy(ShardStrategy shardStrategy) {
107             this.shardStrategy = shardStrategy;
108             return this;
109         }
110
111         public Builder shardConfig(String name, Collection<MemberName> replicas) {
112             shardConfigs.put(name, new ShardConfig(name, replicas));
113             return this;
114         }
115
116         public ModuleConfig build() {
117             return new ModuleConfig(Preconditions.checkNotNull(name), nameSpace, shardStrategy,
118                     ImmutableMap.copyOf(shardConfigs));
119         }
120     }
121 }