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