blob: bf3e4a0f18b62a95835b0740f589b8283862d9d9 [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
Madan Jampaniad3c5262016-01-20 00:50:17 -080044 private final ProviderId providerId;
45 private final String name;
Jordan Halterman00e92da2018-05-22 23:05:52 -070046 private final ControllerNode localNode;
Jordan Halterman19c123a2018-07-30 13:57:19 -070047 private final Set<ControllerNode> controllerNodes;
48 private final Set<Node> storageNodes;
Samuel Jero31e16f52018-09-21 10:34:28 -040049 private final String clusterSecret;
50
Madan Jampaniec1df022015-10-13 21:23:03 -070051
Ayaka Koshibe48229222016-05-16 18:04:26 -070052 public static final Funnel<ClusterMetadata> HASH_FUNNEL = new Funnel<ClusterMetadata>() {
53 @Override
54 public void funnel(ClusterMetadata cm, PrimitiveSink into) {
55 into.putString(cm.name, UTF_8);
56 }
57 };
58
Madan Jampani931e97d2016-02-26 12:20:44 -080059 @SuppressWarnings("unused")
Madan Jampaniad3c5262016-01-20 00:50:17 -080060 private ClusterMetadata() {
61 providerId = null;
62 name = null;
Jordan Halterman00e92da2018-05-22 23:05:52 -070063 localNode = null;
Jordan Halterman19c123a2018-07-30 13:57:19 -070064 controllerNodes = null;
65 storageNodes = null;
Samuel Jero31e16f52018-09-21 10:34:28 -040066 clusterSecret = null;
67 }
68
69 /**
70 * @deprecated since 1.15.
71 * @param providerId the provider Id
72 * @param name The cluster Name
73 * @param localNode The local node
74 * @param controllerNodes Set of nodes in cluster
75 * @param storageNodes Set of storage nodes
76 */
77 @Deprecated
78 public ClusterMetadata(
79 ProviderId providerId,
80 String name,
81 ControllerNode localNode,
82 Set<ControllerNode> controllerNodes,
83 Set<Node> storageNodes) {
84 this.providerId = checkNotNull(providerId);
85 this.name = checkNotNull(name);
86 this.localNode = localNode;
87 this.controllerNodes = ImmutableSet.copyOf(checkNotNull(controllerNodes));
88 this.storageNodes = ImmutableSet.copyOf(checkNotNull(storageNodes));
89 this.clusterSecret = "INSECURE!";
Madan Jampaniad3c5262016-01-20 00:50:17 -080090 }
91
Jordan Halterman00e92da2018-05-22 23:05:52 -070092 public ClusterMetadata(
93 ProviderId providerId,
94 String name,
95 ControllerNode localNode,
Jordan Halterman19c123a2018-07-30 13:57:19 -070096 Set<ControllerNode> controllerNodes,
Samuel Jero31e16f52018-09-21 10:34:28 -040097 Set<Node> storageNodes,
98 String clusterSecret) {
Madan Jampaniad3c5262016-01-20 00:50:17 -080099 this.providerId = checkNotNull(providerId);
100 this.name = checkNotNull(name);
Jordan Halterman00e92da2018-05-22 23:05:52 -0700101 this.localNode = localNode;
Jordan Halterman19c123a2018-07-30 13:57:19 -0700102 this.controllerNodes = ImmutableSet.copyOf(checkNotNull(controllerNodes));
103 this.storageNodes = ImmutableSet.copyOf(checkNotNull(storageNodes));
Samuel Jero31e16f52018-09-21 10:34:28 -0400104 this.clusterSecret = clusterSecret;
105 }
106
107 /**
108 * @deprecated since 1.15.
109 * @param name The cluster Name
110 * @param localNode The local node
111 * @param controllerNodes Set of nodes in cluster
112 * @param storageNodes Set of storage nodes
113 */
114 @Deprecated
115 public ClusterMetadata(
116 String name, ControllerNode localNode, Set<ControllerNode> controllerNodes, Set<Node> storageNodes) {
117 this(new ProviderId("none", "none"), name, localNode, controllerNodes, storageNodes, "INSECURE!");
Madan Jampaniad3c5262016-01-20 00:50:17 -0800118 }
119
Jordan Halterman19c123a2018-07-30 13:57:19 -0700120 public ClusterMetadata(
Samuel Jero31e16f52018-09-21 10:34:28 -0400121 String name, ControllerNode localNode, Set<ControllerNode> controllerNodes, Set<Node> storageNodes,
122 String clusterSecret) {
123 this(new ProviderId("none", "none"), name, localNode, controllerNodes, storageNodes, clusterSecret);
Madan Jampaniad3c5262016-01-20 00:50:17 -0800124 }
125
126 @Override
127 public ProviderId providerId() {
128 return providerId;
Madan Jampaniec1df022015-10-13 21:23:03 -0700129 }
130
131 /**
132 * Returns the name of the cluster.
133 *
134 * @return cluster name
135 */
136 public String getName() {
137 return this.name;
138 }
139
140 /**
Jordan Halterman00e92da2018-05-22 23:05:52 -0700141 * Returns the local controller node.
142 * @return the local controller node
143 */
144 public ControllerNode getLocalNode() {
145 return localNode;
146 }
147
148 /**
Madan Jampaniec1df022015-10-13 21:23:03 -0700149 * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster.
150 * @return cluster nodes
151 */
Jordan Halterman19c123a2018-07-30 13:57:19 -0700152 @Deprecated
Madan Jampaniec1df022015-10-13 21:23:03 -0700153 public Collection<ControllerNode> getNodes() {
Jordan Halterman19c123a2018-07-30 13:57:19 -0700154 return getControllerNodes();
155 }
156
157 /**
158 * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster.
159 * @return controller nodes
160 */
161 public Collection<ControllerNode> getControllerNodes() {
162 return controllerNodes;
Jordan Halterman00e92da2018-05-22 23:05:52 -0700163 }
164
165 /**
166 * Returns the collection of storage nodes.
167 *
168 * @return the collection of storage nodes
169 */
170 public Collection<Node> getStorageNodes() {
Jordan Halterman19c123a2018-07-30 13:57:19 -0700171 return storageNodes;
Madan Jampaniec1df022015-10-13 21:23:03 -0700172 }
173
174 /**
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800175 * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make
176 * up the cluster.
Madan Jampaniec1df022015-10-13 21:23:03 -0700177 * @return collection of partitions.
Jordan Halterman00e92da2018-05-22 23:05:52 -0700178 * @deprecated since 1.14
Madan Jampaniec1df022015-10-13 21:23:03 -0700179 */
Jordan Halterman00e92da2018-05-22 23:05:52 -0700180 @Deprecated
Madan Jampaniec1df022015-10-13 21:23:03 -0700181 public Collection<Partition> getPartitions() {
Jordan Halterman00e92da2018-05-22 23:05:52 -0700182 return Collections.emptySet();
Madan Jampaniec1df022015-10-13 21:23:03 -0700183 }
184
Samuel Jero31e16f52018-09-21 10:34:28 -0400185 /**
186 * Returns the cluster's shared secret.
187 * @return key.
188 */
189 public String getClusterSecret() {
190 return clusterSecret;
191 }
192
Madan Jampaniec1df022015-10-13 21:23:03 -0700193 @Override
194 public String toString() {
195 return MoreObjects.toStringHelper(ClusterMetadata.class)
Madan Jampaniad3c5262016-01-20 00:50:17 -0800196 .add("providerId", providerId)
Madan Jampaniec1df022015-10-13 21:23:03 -0700197 .add("name", name)
Jordan Halterman19c123a2018-07-30 13:57:19 -0700198 .add("controllerNodes", controllerNodes)
199 .add("storageNodes", storageNodes)
Madan Jampaniec1df022015-10-13 21:23:03 -0700200 .toString();
201 }
202
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700203 @Override
204 public int hashCode() {
Jordan Halterman19c123a2018-07-30 13:57:19 -0700205 return Arrays.deepHashCode(new Object[] {providerId, name, controllerNodes, storageNodes});
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700206 }
207
208 /*
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800209 * Provide a deep equality check of the cluster metadata (non-Javadoc)
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700210 *
211 * @see java.lang.Object#equals(java.lang.Object)
212 */
213 @Override
214 public boolean equals(Object object) {
Ray Milkey74e59132018-01-17 15:24:52 -0800215 if (object == null) {
216 return false;
217 }
218
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700219 if (!ClusterMetadata.class.isInstance(object)) {
220 return false;
221 }
222 ClusterMetadata that = (ClusterMetadata) object;
223
Ray Milkey74e59132018-01-17 15:24:52 -0800224 return Objects.equals(this.name, that.name) &&
Jordan Halterman19c123a2018-07-30 13:57:19 -0700225 this.localNode.equals(that.localNode) &&
226 Objects.equals(this.controllerNodes.size(), that.controllerNodes.size()) &&
227 Sets.symmetricDifference(this.controllerNodes, that.controllerNodes).isEmpty() &&
228 Objects.equals(this.storageNodes.size(), that.storageNodes.size()) &&
229 Sets.symmetricDifference(this.storageNodes, that.storageNodes).isEmpty();
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700230 }
Madan Jampanif6c6a302016-01-18 14:33:45 -0800231}