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; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 49 | |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 50 | public static final Funnel<ClusterMetadata> HASH_FUNNEL = new Funnel<ClusterMetadata>() { |
| 51 | @Override |
| 52 | public void funnel(ClusterMetadata cm, PrimitiveSink into) { |
| 53 | into.putString(cm.name, UTF_8); |
| 54 | } |
| 55 | }; |
| 56 | |
Madan Jampani | 931e97d | 2016-02-26 12:20:44 -0800 | [diff] [blame] | 57 | @SuppressWarnings("unused") |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 58 | private ClusterMetadata() { |
| 59 | providerId = null; |
| 60 | name = null; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 61 | localNode = null; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 62 | controllerNodes = null; |
| 63 | storageNodes = null; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 66 | public ClusterMetadata( |
| 67 | ProviderId providerId, |
| 68 | String name, |
| 69 | ControllerNode localNode, |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 70 | Set<ControllerNode> controllerNodes, |
| 71 | Set<Node> storageNodes) { |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 72 | this.providerId = checkNotNull(providerId); |
| 73 | this.name = checkNotNull(name); |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 74 | this.localNode = localNode; |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 75 | this.controllerNodes = ImmutableSet.copyOf(checkNotNull(controllerNodes)); |
| 76 | this.storageNodes = ImmutableSet.copyOf(checkNotNull(storageNodes)); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 79 | public ClusterMetadata( |
| 80 | String name, ControllerNode localNode, Set<ControllerNode> controllerNodes, Set<Node> storageNodes) { |
| 81 | this(new ProviderId("none", "none"), name, localNode, controllerNodes, storageNodes); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public ProviderId providerId() { |
| 86 | return providerId; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the name of the cluster. |
| 91 | * |
| 92 | * @return cluster name |
| 93 | */ |
| 94 | public String getName() { |
| 95 | return this.name; |
| 96 | } |
| 97 | |
| 98 | /** |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 99 | * Returns the local controller node. |
| 100 | * @return the local controller node |
| 101 | */ |
| 102 | public ControllerNode getLocalNode() { |
| 103 | return localNode; |
| 104 | } |
| 105 | |
| 106 | /** |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 107 | * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster. |
| 108 | * @return cluster nodes |
| 109 | */ |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 110 | @Deprecated |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 111 | public Collection<ControllerNode> getNodes() { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 112 | return getControllerNodes(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster. |
| 117 | * @return controller nodes |
| 118 | */ |
| 119 | public Collection<ControllerNode> getControllerNodes() { |
| 120 | return controllerNodes; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns the collection of storage nodes. |
| 125 | * |
| 126 | * @return the collection of storage nodes |
| 127 | */ |
| 128 | public Collection<Node> getStorageNodes() { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 129 | return storageNodes; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 133 | * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make |
| 134 | * up the cluster. |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 135 | * @return collection of partitions. |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 136 | * @deprecated since 1.14 |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 137 | */ |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 138 | @Deprecated |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 139 | public Collection<Partition> getPartitions() { |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 140 | return Collections.emptySet(); |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public String toString() { |
| 145 | return MoreObjects.toStringHelper(ClusterMetadata.class) |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 146 | .add("providerId", providerId) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 147 | .add("name", name) |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 148 | .add("controllerNodes", controllerNodes) |
| 149 | .add("storageNodes", storageNodes) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 150 | .toString(); |
| 151 | } |
| 152 | |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 153 | @Override |
| 154 | public int hashCode() { |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 155 | return Arrays.deepHashCode(new Object[] {providerId, name, controllerNodes, storageNodes}); |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 159 | * Provide a deep equality check of the cluster metadata (non-Javadoc) |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 160 | * |
| 161 | * @see java.lang.Object#equals(java.lang.Object) |
| 162 | */ |
| 163 | @Override |
| 164 | public boolean equals(Object object) { |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 165 | if (object == null) { |
| 166 | return false; |
| 167 | } |
| 168 | |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 169 | if (!ClusterMetadata.class.isInstance(object)) { |
| 170 | return false; |
| 171 | } |
| 172 | ClusterMetadata that = (ClusterMetadata) object; |
| 173 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 174 | return Objects.equals(this.name, that.name) && |
Jordan Halterman | 19c123a | 2018-07-30 13:57:19 -0700 | [diff] [blame] | 175 | this.localNode.equals(that.localNode) && |
| 176 | Objects.equals(this.controllerNodes.size(), that.controllerNodes.size()) && |
| 177 | Sets.symmetricDifference(this.controllerNodes, that.controllerNodes).isEmpty() && |
| 178 | Objects.equals(this.storageNodes.size(), that.storageNodes.size()) && |
| 179 | Sets.symmetricDifference(this.storageNodes, that.storageNodes).isEmpty(); |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 180 | } |
Madan Jampani | f6c6a30 | 2016-01-18 14:33:45 -0800 | [diff] [blame] | 181 | } |