Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.cluster; |
| 17 | |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 18 | import java.util.Arrays; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 19 | import java.util.Collection; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 20 | import java.util.Collections; |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 21 | import java.util.Objects; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 22 | import java.util.Set; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 23 | |
| 24 | import com.google.common.base.MoreObjects; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableSet; |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 26 | import com.google.common.collect.Sets; |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 27 | import com.google.common.hash.Funnel; |
| 28 | import com.google.common.hash.PrimitiveSink; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Provided; |
| 30 | import org.onosproject.net.provider.ProviderId; |
| 31 | |
| 32 | import static com.google.common.base.Charsets.UTF_8; |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Cluster metadata. |
| 37 | * <p> |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 38 | * Metadata specifies how a ONOS cluster is constituted and is made up of the collection |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 39 | * of {@link org.onosproject.cluster.ControllerNode nodes} and the collection of data |
| 40 | * {@link org.onosproject.cluster.Partition partitions}. |
| 41 | */ |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 42 | public final class ClusterMetadata implements Provided { |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 43 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 44 | private final ProviderId providerId; |
| 45 | private final String name; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 46 | private final ControllerNode localNode; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 47 | private final Set<ControllerNode> controllerNodes; |
| 48 | private final Set<Node> storageNodes; |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 49 | private final String clusterSecret; |
| 50 | |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 51 | |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 52 | 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 Jampani | 931e97d | 2016-02-26 12:20:44 -0800 | [diff] [blame] | 59 | @SuppressWarnings("unused") |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 60 | private ClusterMetadata() { |
| 61 | providerId = null; |
| 62 | name = null; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 63 | localNode = null; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 64 | controllerNodes = null; |
| 65 | storageNodes = null; |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 66 | 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 Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 92 | public ClusterMetadata( |
| 93 | ProviderId providerId, |
| 94 | String name, |
| 95 | ControllerNode localNode, |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 96 | Set<ControllerNode> controllerNodes, |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 97 | Set<Node> storageNodes, |
| 98 | String clusterSecret) { |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 99 | this.providerId = checkNotNull(providerId); |
| 100 | this.name = checkNotNull(name); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 101 | this.localNode = localNode; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 102 | this.controllerNodes = ImmutableSet.copyOf(checkNotNull(controllerNodes)); |
| 103 | this.storageNodes = ImmutableSet.copyOf(checkNotNull(storageNodes)); |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 104 | 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 Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 120 | public ClusterMetadata( |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 121 | 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 Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public ProviderId providerId() { |
| 128 | return providerId; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 129 | } |
| 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 Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 141 | * Returns the local controller node. |
| 142 | * @return the local controller node |
| 143 | */ |
| 144 | public ControllerNode getLocalNode() { |
| 145 | return localNode; |
| 146 | } |
| 147 | |
| 148 | /** |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 149 | * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster. |
| 150 | * @return cluster nodes |
| 151 | */ |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 152 | @Deprecated |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 153 | public Collection<ControllerNode> getNodes() { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 154 | 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 Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Returns the collection of storage nodes. |
| 167 | * |
| 168 | * @return the collection of storage nodes |
| 169 | */ |
| 170 | public Collection<Node> getStorageNodes() { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 171 | return storageNodes; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 175 | * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make |
| 176 | * up the cluster. |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 177 | * @return collection of partitions. |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 178 | * @deprecated since 1.14 |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 179 | */ |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 180 | @Deprecated |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 181 | public Collection<Partition> getPartitions() { |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 182 | return Collections.emptySet(); |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 185 | /** |
| 186 | * Returns the cluster's shared secret. |
| 187 | * @return key. |
| 188 | */ |
| 189 | public String getClusterSecret() { |
| 190 | return clusterSecret; |
| 191 | } |
| 192 | |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 193 | @Override |
| 194 | public String toString() { |
| 195 | return MoreObjects.toStringHelper(ClusterMetadata.class) |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 196 | .add("providerId", providerId) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 197 | .add("name", name) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 198 | .add("controllerNodes", controllerNodes) |
| 199 | .add("storageNodes", storageNodes) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 200 | .toString(); |
| 201 | } |
| 202 | |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 203 | @Override |
| 204 | public int hashCode() { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 205 | return Arrays.deepHashCode(new Object[] {providerId, name, controllerNodes, storageNodes}); |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 209 | * Provide a deep equality check of the cluster metadata (non-Javadoc) |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 210 | * |
| 211 | * @see java.lang.Object#equals(java.lang.Object) |
| 212 | */ |
| 213 | @Override |
| 214 | public boolean equals(Object object) { |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 215 | if (object == null) { |
| 216 | return false; |
| 217 | } |
| 218 | |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 219 | if (!ClusterMetadata.class.isInstance(object)) { |
| 220 | return false; |
| 221 | } |
| 222 | ClusterMetadata that = (ClusterMetadata) object; |
| 223 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 224 | return Objects.equals(this.name, that.name) && |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 225 | 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. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 230 | } |
Madan Jampani | f6c6a30 | 2016-01-18 14:33:45 -0800 | [diff] [blame] | 231 | } |