97be5622a423ed307facecc9e21d70abde8f4cca
[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.datastore.shardstrategy.ShardStrategy;
18
19 /**
20  * Encapsulates configuration for a module.
21  *
22  * @author Thomas Pantelis
23  */
24 public class ModuleConfig {
25     private final String name;
26     private final String nameSpace;
27     private final ShardStrategy shardStrategy;
28     private final Map<String, ShardConfig> shardConfigs;
29
30     private ModuleConfig(String name, String nameSpace, ShardStrategy shardStrategy,
31             Map<String, ShardConfig> shardConfigs) {
32         this.name = name;
33         this.nameSpace = nameSpace;
34         this.shardStrategy = shardStrategy;
35         this.shardConfigs = shardConfigs;
36     }
37
38     @Nonnull
39     public String getName() {
40         return name;
41     }
42
43     @Nullable
44     public String getNameSpace() {
45         return nameSpace;
46     }
47
48     @Nullable
49     public ShardStrategy getShardStrategy() {
50         return shardStrategy;
51     }
52
53     @Nullable
54     public ShardConfig getShardConfig(String name) {
55         return shardConfigs.get(name);
56     }
57
58     @Nonnull
59     public Collection<ShardConfig> getShardConfigs() {
60         return shardConfigs.values();
61     }
62
63     @Nonnull
64     public Collection<String> getShardNames() {
65         return shardConfigs.keySet();
66     }
67
68     public static Builder builder(String name) {
69         return new Builder(name);
70     }
71
72     public static Builder builder(ModuleConfig moduleConfig) {
73         return new Builder(moduleConfig);
74     }
75
76     public static class Builder {
77         private String name;
78         private String nameSpace;
79         private ShardStrategy shardStrategy;
80         private final Map<String, ShardConfig> shardConfigs = new HashMap<>();
81
82         private Builder(String name) {
83             this.name = name;
84         }
85
86         private Builder(ModuleConfig moduleConfig) {
87             this.name = moduleConfig.getName();
88             this.nameSpace = moduleConfig.getNameSpace();
89             this.shardStrategy = moduleConfig.getShardStrategy();
90             for (ShardConfig shardConfig : moduleConfig.getShardConfigs()) {
91                 shardConfigs.put(shardConfig.getName(), shardConfig);
92             }
93         }
94
95         public Builder name(String name) {
96             this.name = name;
97             return this;
98         }
99
100         public Builder nameSpace(String nameSpace) {
101             this.nameSpace = nameSpace;
102             return this;
103         }
104
105         public Builder shardStrategy(ShardStrategy shardStrategy) {
106             this.shardStrategy = shardStrategy;
107             return this;
108         }
109
110         public Builder shardConfig(String name, Collection<String> replicas) {
111             shardConfigs.put(name, new ShardConfig(name, replicas));
112             return this;
113         }
114
115         public ModuleConfig build() {
116             return new ModuleConfig(Preconditions.checkNotNull(name), nameSpace, shardStrategy,
117                     ImmutableMap.copyOf(shardConfigs));
118         }
119     }
120 }