blob: e1eacfeed74c91c003880997d98ce8a85a43b009 [file] [log] [blame]
Madan Jampaniec1df022015-10-13 21:23:03 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.cluster;
17
David K. Bainbridgee676dab2015-10-23 16:13:07 -070018import java.util.Arrays;
Madan Jampaniec1df022015-10-13 21:23:03 -070019import java.util.Collection;
20import java.util.Set;
21import java.util.stream.Collectors;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24import static com.google.common.base.Verify.verifyNotNull;
25import static com.google.common.base.Verify.verify;
26
27import com.google.common.base.MoreObjects;
28import com.google.common.collect.Collections2;
29import com.google.common.collect.ImmutableSet;
David K. Bainbridgee676dab2015-10-23 16:13:07 -070030import com.google.common.collect.Sets;
Madan Jampaniec1df022015-10-13 21:23:03 -070031
32/**
33 * Cluster metadata.
34 * <p>
35 * Metadata specifies the attributes that define a ONOS cluster and comprises the collection
36 * of {@link org.onosproject.cluster.ControllerNode nodes} and the collection of data
37 * {@link org.onosproject.cluster.Partition partitions}.
38 */
39public final class ClusterMetadata {
40
41 private String name;
42 private Set<ControllerNode> nodes;
43 private Set<Partition> partitions;
44
45 /**
46 * Returns a new cluster metadata builder.
47 * @return The cluster metadata builder.
48 */
49 public static Builder builder() {
50 return new Builder();
51 }
52
53 /**
54 * Returns the name of the cluster.
55 *
56 * @return cluster name
57 */
58 public String getName() {
59 return this.name;
60 }
61
62 /**
63 * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster.
64 * @return cluster nodes
65 */
66 public Collection<ControllerNode> getNodes() {
67 return this.nodes;
68 }
69
70 /**
71 * Returns the collection of data {@link org.onosproject.cluster.Partition partitions} that make up the cluster.
72 * @return collection of partitions.
73 */
74 public Collection<Partition> getPartitions() {
75 return this.partitions;
76 }
77
78 @Override
79 public String toString() {
80 return MoreObjects.toStringHelper(ClusterMetadata.class)
81 .add("name", name)
82 .add("nodes", nodes)
83 .add("partitions", partitions)
84 .toString();
85 }
86
David K. Bainbridgee676dab2015-10-23 16:13:07 -070087 @Override
88 public int hashCode() {
89 return Arrays.deepHashCode(new Object[] {name, nodes, partitions});
90 }
91
92 /*
93 * Provide a deep quality check of the meta data (non-Javadoc)
94 *
95 * @see java.lang.Object#equals(java.lang.Object)
96 */
97 @Override
98 public boolean equals(Object object) {
99
100 if (!ClusterMetadata.class.isInstance(object)) {
101 return false;
102 }
103 ClusterMetadata that = (ClusterMetadata) object;
104
105 if (!this.name.equals(that.name) || this.nodes.size() != that.nodes.size()
106 || this.partitions.size() != that.partitions.size()) {
107 return false;
108 }
109
110 return Sets.symmetricDifference(this.nodes, that.nodes).isEmpty()
111 && Sets.symmetricDifference(this.partitions, that.partitions).isEmpty();
112 }
113
Madan Jampaniec1df022015-10-13 21:23:03 -0700114 /**
115 * Builder for a {@link ClusterMetadata} instance.
116 */
117 public static class Builder {
118
119 private final ClusterMetadata metadata;
120
121 public Builder() {
122 metadata = new ClusterMetadata();
123 }
124
125 /**
126 * Sets the cluster name, returning the cluster metadata builder for method chaining.
127 * @param name cluster name
128 * @return this cluster metadata builder
129 */
130 public Builder withName(String name) {
131 metadata.name = checkNotNull(name);
132 return this;
133 }
134
135 /**
136 * Sets the collection of cluster nodes, returning the cluster metadata builder for method chaining.
137 * @param controllerNodes collection of cluster nodes
138 * @return this cluster metadata builder
139 */
140 public Builder withControllerNodes(Collection<ControllerNode> controllerNodes) {
141 metadata.nodes = ImmutableSet.copyOf(checkNotNull(controllerNodes));
142 return this;
143 }
144
145 /**
146 * Sets the collection of data partitions, returning the cluster metadata builder for method chaining.
147 * @param partitions collection of partitions
148 * @return this cluster metadata builder
149 */
150 public Builder withPartitions(Collection<Partition> partitions) {
151 metadata.partitions = ImmutableSet.copyOf(checkNotNull(partitions));
152 return this;
153 }
154
155 /**
156 * Builds the cluster metadata.
157 * @return cluster metadata
158 * @throws com.google.common.base.VerifyException VerifyException if the metadata is misconfigured
159 */
160 public ClusterMetadata build() {
161 verifyMetadata();
162 return metadata;
163 }
164
165 /**
166 * Validates the constructed metadata for semantic correctness.
167 * @throws VerifyException if the metadata is misconfigured.
168 */
169 private void verifyMetadata() {
170 verifyNotNull(metadata.getName(), "Cluster name must be specified");
171 verifyNotNull(metadata.getNodes(), "Cluster nodes must be specified");
172 verifyNotNull(metadata.getPartitions(), "Cluster partitions must be specified");
173 verify(!metadata.getNodes().isEmpty(), "Cluster nodes must not be empty");
174 verify(!metadata.getPartitions().isEmpty(), "Cluster nodes must not be empty");
175
176 // verify that partitions are constituted from valid cluster nodes.
177 boolean validPartitions = Collections2.transform(metadata.getNodes(), ControllerNode::id)
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700178 .containsAll(metadata.getPartitions()
179 .stream()
180 .flatMap(r -> r.getMembers().stream())
181 .collect(Collectors.toSet()));
Madan Jampaniec1df022015-10-13 21:23:03 -0700182 verify(validPartitions, "Partition locations must be valid cluster nodes");
183 }
184 }
185}