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 | |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 44 | private static final ProviderId NONE_PROVIDER_ID = new ProviderId("none", "none"); |
| 45 | private static final String DEFAULT_CLUSTER_SECRET = "INSECURE!"; |
| 46 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 47 | private final ProviderId providerId; |
| 48 | private final String name; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 49 | private final ControllerNode localNode; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 50 | private final Set<ControllerNode> controllerNodes; |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 51 | private final String storageDnsService; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 52 | private final Set<Node> storageNodes; |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 53 | private final String clusterSecret; |
| 54 | |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 55 | |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 56 | 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 Jampani | 931e97d | 2016-02-26 12:20:44 -0800 | [diff] [blame] | 63 | @SuppressWarnings("unused") |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 64 | private ClusterMetadata() { |
| 65 | providerId = null; |
| 66 | name = null; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 67 | localNode = null; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 68 | controllerNodes = null; |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 69 | storageDnsService = null; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 70 | storageNodes = null; |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 71 | 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 Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 80 | * @param storageDnsService The storage DNS service name |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 81 | * @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 Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 89 | String storageDnsService, |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 90 | Set<Node> storageNodes) { |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 91 | 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 Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 104 | public ClusterMetadata( |
| 105 | ProviderId providerId, |
| 106 | String name, |
| 107 | ControllerNode localNode, |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 108 | Set<ControllerNode> controllerNodes, |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 109 | String storageDnsService, |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 110 | Set<Node> storageNodes, |
| 111 | String clusterSecret) { |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 112 | this.providerId = checkNotNull(providerId); |
| 113 | this.name = checkNotNull(name); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 114 | this.localNode = localNode; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 115 | this.controllerNodes = ImmutableSet.copyOf(checkNotNull(controllerNodes)); |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 116 | this.storageDnsService = storageDnsService; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 117 | this.storageNodes = ImmutableSet.copyOf(checkNotNull(storageNodes)); |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 118 | 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 Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 130 | 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 Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 137 | public ClusterMetadata( |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 138 | String name, |
| 139 | ControllerNode localNode, |
| 140 | Set<ControllerNode> controllerNodes, |
| 141 | Set<Node> storageNodes, |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 142 | String clusterSecret) { |
Jordan Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 143 | this(NONE_PROVIDER_ID, name, localNode, controllerNodes, null, storageNodes, clusterSecret); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public ProviderId providerId() { |
| 148 | return providerId; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 149 | } |
| 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 Halterman | 2ef1cf7 | 2019-01-25 15:07:11 -0800 | [diff] [blame] | 161 | * 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 Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 170 | * Returns the local controller node. |
| 171 | * @return the local controller node |
| 172 | */ |
| 173 | public ControllerNode getLocalNode() { |
| 174 | return localNode; |
| 175 | } |
| 176 | |
| 177 | /** |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 178 | * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster. |
| 179 | * @return cluster nodes |
| 180 | */ |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 181 | @Deprecated |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 182 | public Collection<ControllerNode> getNodes() { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 183 | 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 Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns the collection of storage nodes. |
| 196 | * |
| 197 | * @return the collection of storage nodes |
| 198 | */ |
| 199 | public Collection<Node> getStorageNodes() { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 200 | return storageNodes; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 204 | * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make |
| 205 | * up the cluster. |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 206 | * @return collection of partitions. |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 207 | * @deprecated since 1.14 |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 208 | */ |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 209 | @Deprecated |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 210 | public Collection<Partition> getPartitions() { |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 211 | return Collections.emptySet(); |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 214 | /** |
| 215 | * Returns the cluster's shared secret. |
| 216 | * @return key. |
| 217 | */ |
| 218 | public String getClusterSecret() { |
| 219 | return clusterSecret; |
| 220 | } |
| 221 | |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 222 | @Override |
| 223 | public String toString() { |
| 224 | return MoreObjects.toStringHelper(ClusterMetadata.class) |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 225 | .add("providerId", providerId) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 226 | .add("name", name) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 227 | .add("controllerNodes", controllerNodes) |
| 228 | .add("storageNodes", storageNodes) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 229 | .toString(); |
| 230 | } |
| 231 | |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 232 | @Override |
| 233 | public int hashCode() { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 234 | return Arrays.deepHashCode(new Object[] {providerId, name, controllerNodes, storageNodes}); |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 238 | * Provide a deep equality check of the cluster metadata (non-Javadoc) |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 239 | * |
| 240 | * @see java.lang.Object#equals(java.lang.Object) |
| 241 | */ |
| 242 | @Override |
| 243 | public boolean equals(Object object) { |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 244 | if (object == null) { |
| 245 | return false; |
| 246 | } |
| 247 | |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 248 | if (!ClusterMetadata.class.isInstance(object)) { |
| 249 | return false; |
| 250 | } |
| 251 | ClusterMetadata that = (ClusterMetadata) object; |
| 252 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 253 | return Objects.equals(this.name, that.name) && |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 254 | 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. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 259 | } |
Madan Jampani | f6c6a30 | 2016-01-18 14:33:45 -0800 | [diff] [blame] | 260 | } |