blob: 521e20a3c7522315d85fcd66dcd3789f9c81740d [file] [log] [blame]
Madan Jampaniec1df022015-10-13 21:23:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Madan Jampaniec1df022015-10-13 21:23:03 -07003 *
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;
Jordan Halterman00e92da2018-05-22 23:05:52 -070020import java.util.Collections;
Ray Milkey74e59132018-01-17 15:24:52 -080021import java.util.Objects;
Madan Jampaniec1df022015-10-13 21:23:03 -070022import java.util.Set;
Madan Jampaniec1df022015-10-13 21:23:03 -070023
24import com.google.common.base.MoreObjects;
Madan Jampaniec1df022015-10-13 21:23:03 -070025import com.google.common.collect.ImmutableSet;
David K. Bainbridgee676dab2015-10-23 16:13:07 -070026import com.google.common.collect.Sets;
Ayaka Koshibe48229222016-05-16 18:04:26 -070027import com.google.common.hash.Funnel;
28import com.google.common.hash.PrimitiveSink;
Jordan Halterman00e92da2018-05-22 23:05:52 -070029import org.onosproject.net.Provided;
30import org.onosproject.net.provider.ProviderId;
31
32import static com.google.common.base.Charsets.UTF_8;
33import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampaniec1df022015-10-13 21:23:03 -070034
35/**
36 * Cluster metadata.
37 * <p>
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080038 * Metadata specifies how a ONOS cluster is constituted and is made up of the collection
Madan Jampaniec1df022015-10-13 21:23:03 -070039 * of {@link org.onosproject.cluster.ControllerNode nodes} and the collection of data
40 * {@link org.onosproject.cluster.Partition partitions}.
41 */
Madan Jampaniad3c5262016-01-20 00:50:17 -080042public final class ClusterMetadata implements Provided {
Madan Jampaniec1df022015-10-13 21:23:03 -070043
Jordan Halterman2ef1cf72019-01-25 15:07:11 -080044 private static final ProviderId NONE_PROVIDER_ID = new ProviderId("none", "none");
45 private static final String DEFAULT_CLUSTER_SECRET = "INSECURE!";
46
Madan Jampaniad3c5262016-01-20 00:50:17 -080047 private final ProviderId providerId;
48 private final String name;
Jordan Halterman00e92da2018-05-22 23:05:52 -070049 private final ControllerNode localNode;
Jordan Halterman19c123a2018-07-30 13:57:19 -070050 private final Set<ControllerNode> controllerNodes;
Jordan Halterman2ef1cf72019-01-25 15:07:11 -080051 private final String storageDnsService;
Jordan Halterman19c123a2018-07-30 13:57:19 -070052 private final Set<Node> storageNodes;
Samuel Jero31e16f52018-09-21 10:34:28 -040053 private final String clusterSecret;
54
Madan Jampaniec1df022015-10-13 21:23:03 -070055
Ayaka Koshibe48229222016-05-16 18:04:26 -070056 public static final Funnel<ClusterMetadata> HASH_FUNNEL = new Funnel<ClusterMetadata>() {
57 @Override
58 public void funnel(ClusterMetadata cm, PrimitiveSink into) {
59 into.putString(cm.name, UTF_8);
60 }
61 };
62
Madan Jampani931e97d2016-02-26 12:20:44 -080063 @SuppressWarnings("unused")
Madan Jampaniad3c5262016-01-20 00:50:17 -080064 private ClusterMetadata() {
65 providerId = null;
66 name = null;
Jordan Halterman00e92da2018-05-22 23:05:52 -070067 localNode = null;
Jordan Halterman19c123a2018-07-30 13:57:19 -070068 controllerNodes = null;
Jordan Halterman2ef1cf72019-01-25 15:07:11 -080069 storageDnsService = null;
Jordan Halterman19c123a2018-07-30 13:57:19 -070070 storageNodes = null;
Samuel Jero31e16f52018-09-21 10:34:28 -040071 clusterSecret = null;
72 }
73
74 /**
75 * @deprecated since 1.15.
76 * @param providerId the provider Id
77 * @param name The cluster Name
78 * @param localNode The local node
79 * @param controllerNodes Set of nodes in cluster
Jordan Halterman2ef1cf72019-01-25 15:07:11 -080080 * @param storageDnsService The storage DNS service name
Samuel Jero31e16f52018-09-21 10:34:28 -040081 * @param storageNodes Set of storage nodes
82 */
83 @Deprecated
84 public ClusterMetadata(
85 ProviderId providerId,
86 String name,
87 ControllerNode localNode,
88 Set<ControllerNode> controllerNodes,
Jordan Halterman2ef1cf72019-01-25 15:07:11 -080089 String storageDnsService,
Samuel Jero31e16f52018-09-21 10:34:28 -040090 Set<Node> storageNodes) {
Jordan Halterman2ef1cf72019-01-25 15:07:11 -080091 this(providerId, name, localNode, controllerNodes, storageDnsService, storageNodes, DEFAULT_CLUSTER_SECRET);
92 }
93
94 public ClusterMetadata(
95 ProviderId providerId,
96 String name,
97 ControllerNode localNode,
98 Set<ControllerNode> controllerNodes,
99 Set<Node> storageNodes,
100 String clusterSecret) {
101 this(providerId, name, localNode, controllerNodes, null, storageNodes, clusterSecret);
Madan Jampaniad3c5262016-01-20 00:50:17 -0800102 }
103
Jordan Halterman00e92da2018-05-22 23:05:52 -0700104 public ClusterMetadata(
105 ProviderId providerId,
106 String name,
107 ControllerNode localNode,
Jordan Halterman19c123a2018-07-30 13:57:19 -0700108 Set<ControllerNode> controllerNodes,
Jordan Halterman2ef1cf72019-01-25 15:07:11 -0800109 String storageDnsService,
Samuel Jero31e16f52018-09-21 10:34:28 -0400110 Set<Node> storageNodes,
111 String clusterSecret) {
Madan Jampaniad3c5262016-01-20 00:50:17 -0800112 this.providerId = checkNotNull(providerId);
113 this.name = checkNotNull(name);
Jordan Halterman00e92da2018-05-22 23:05:52 -0700114 this.localNode = localNode;
Jordan Halterman19c123a2018-07-30 13:57:19 -0700115 this.controllerNodes = ImmutableSet.copyOf(checkNotNull(controllerNodes));
Jordan Halterman2ef1cf72019-01-25 15:07:11 -0800116 this.storageDnsService = storageDnsService;
Jordan Halterman19c123a2018-07-30 13:57:19 -0700117 this.storageNodes = ImmutableSet.copyOf(checkNotNull(storageNodes));
Samuel Jero31e16f52018-09-21 10:34:28 -0400118 this.clusterSecret = clusterSecret;
119 }
120
121 /**
122 * @deprecated since 1.15.
123 * @param name The cluster Name
124 * @param localNode The local node
125 * @param controllerNodes Set of nodes in cluster
126 * @param storageNodes Set of storage nodes
127 */
128 @Deprecated
129 public ClusterMetadata(
Jordan Halterman2ef1cf72019-01-25 15:07:11 -0800130 String name,
131 ControllerNode localNode,
132 Set<ControllerNode> controllerNodes,
133 Set<Node> storageNodes) {
134 this(NONE_PROVIDER_ID, name, localNode, controllerNodes, null, storageNodes, DEFAULT_CLUSTER_SECRET);
Madan Jampaniad3c5262016-01-20 00:50:17 -0800135 }
136
Jordan Halterman19c123a2018-07-30 13:57:19 -0700137 public ClusterMetadata(
Jordan Halterman2ef1cf72019-01-25 15:07:11 -0800138 String name,
139 ControllerNode localNode,
140 Set<ControllerNode> controllerNodes,
141 Set<Node> storageNodes,
Samuel Jero31e16f52018-09-21 10:34:28 -0400142 String clusterSecret) {
Jordan Halterman2ef1cf72019-01-25 15:07:11 -0800143 this(NONE_PROVIDER_ID, name, localNode, controllerNodes, null, storageNodes, clusterSecret);
Madan Jampaniad3c5262016-01-20 00:50:17 -0800144 }
145
146 @Override
147 public ProviderId providerId() {
148 return providerId;
Madan Jampaniec1df022015-10-13 21:23:03 -0700149 }
150
151 /**
152 * Returns the name of the cluster.
153 *
154 * @return cluster name
155 */
156 public String getName() {
157 return this.name;
158 }
159
160 /**
Jordan Halterman2ef1cf72019-01-25 15:07:11 -0800161 * Returns the DNS service through which to locate storage nodes.
162 *
163 * @return the DNS service through which to locate storage nodes
164 */
165 public String getStorageDnsService() {
166 return storageDnsService;
167 }
168
169 /**
Jordan Halterman00e92da2018-05-22 23:05:52 -0700170 * Returns the local controller node.
171 * @return the local controller node
172 */
173 public ControllerNode getLocalNode() {
174 return localNode;
175 }
176
177 /**
Madan Jampaniec1df022015-10-13 21:23:03 -0700178 * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster.
179 * @return cluster nodes
180 */
Jordan Halterman19c123a2018-07-30 13:57:19 -0700181 @Deprecated
Madan Jampaniec1df022015-10-13 21:23:03 -0700182 public Collection<ControllerNode> getNodes() {
Jordan Halterman19c123a2018-07-30 13:57:19 -0700183 return getControllerNodes();
184 }
185
186 /**
187 * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster.
188 * @return controller nodes
189 */
190 public Collection<ControllerNode> getControllerNodes() {
191 return controllerNodes;
Jordan Halterman00e92da2018-05-22 23:05:52 -0700192 }
193
194 /**
195 * Returns the collection of storage nodes.
196 *
197 * @return the collection of storage nodes
198 */
199 public Collection<Node> getStorageNodes() {
Jordan Halterman19c123a2018-07-30 13:57:19 -0700200 return storageNodes;
Madan Jampaniec1df022015-10-13 21:23:03 -0700201 }
202
203 /**
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800204 * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make
205 * up the cluster.
Madan Jampaniec1df022015-10-13 21:23:03 -0700206 * @return collection of partitions.
Jordan Halterman00e92da2018-05-22 23:05:52 -0700207 * @deprecated since 1.14
Madan Jampaniec1df022015-10-13 21:23:03 -0700208 */
Jordan Halterman00e92da2018-05-22 23:05:52 -0700209 @Deprecated
Madan Jampaniec1df022015-10-13 21:23:03 -0700210 public Collection<Partition> getPartitions() {
Jordan Halterman00e92da2018-05-22 23:05:52 -0700211 return Collections.emptySet();
Madan Jampaniec1df022015-10-13 21:23:03 -0700212 }
213
Samuel Jero31e16f52018-09-21 10:34:28 -0400214 /**
215 * Returns the cluster's shared secret.
216 * @return key.
217 */
218 public String getClusterSecret() {
219 return clusterSecret;
220 }
221
Madan Jampaniec1df022015-10-13 21:23:03 -0700222 @Override
223 public String toString() {
224 return MoreObjects.toStringHelper(ClusterMetadata.class)
Madan Jampaniad3c5262016-01-20 00:50:17 -0800225 .add("providerId", providerId)
Madan Jampaniec1df022015-10-13 21:23:03 -0700226 .add("name", name)
Jordan Halterman19c123a2018-07-30 13:57:19 -0700227 .add("controllerNodes", controllerNodes)
228 .add("storageNodes", storageNodes)
Madan Jampaniec1df022015-10-13 21:23:03 -0700229 .toString();
230 }
231
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700232 @Override
233 public int hashCode() {
Jordan Halterman19c123a2018-07-30 13:57:19 -0700234 return Arrays.deepHashCode(new Object[] {providerId, name, controllerNodes, storageNodes});
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700235 }
236
237 /*
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800238 * Provide a deep equality check of the cluster metadata (non-Javadoc)
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700239 *
240 * @see java.lang.Object#equals(java.lang.Object)
241 */
242 @Override
243 public boolean equals(Object object) {
Ray Milkey74e59132018-01-17 15:24:52 -0800244 if (object == null) {
245 return false;
246 }
247
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700248 if (!ClusterMetadata.class.isInstance(object)) {
249 return false;
250 }
251 ClusterMetadata that = (ClusterMetadata) object;
252
Ray Milkey74e59132018-01-17 15:24:52 -0800253 return Objects.equals(this.name, that.name) &&
Jordan Halterman19c123a2018-07-30 13:57:19 -0700254 this.localNode.equals(that.localNode) &&
255 Objects.equals(this.controllerNodes.size(), that.controllerNodes.size()) &&
256 Sets.symmetricDifference(this.controllerNodes, that.controllerNodes).isEmpty() &&
257 Objects.equals(this.storageNodes.size(), that.storageNodes.size()) &&
258 Sets.symmetricDifference(this.storageNodes, that.storageNodes).isEmpty();
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700259 }
Madan Jampanif6c6a302016-01-18 14:33:45 -0800260}