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; |
Ray Milkey | ae4e5ed | 2018-01-17 15:24:52 -0800 | [diff] [blame^] | 20 | import java.util.Objects; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 21 | import java.util.Set; |
| 22 | import java.util.stream.Collectors; |
| 23 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 24 | import org.onosproject.net.Provided; |
| 25 | import org.onosproject.net.provider.ProviderId; |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 26 | |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 28 | import static com.google.common.base.Verify.verify; |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 29 | import static com.google.common.base.Charsets.UTF_8; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 30 | |
| 31 | import com.google.common.base.MoreObjects; |
| 32 | import com.google.common.collect.Collections2; |
| 33 | import com.google.common.collect.ImmutableSet; |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 34 | import com.google.common.collect.Sets; |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 35 | import com.google.common.hash.Funnel; |
| 36 | import com.google.common.hash.PrimitiveSink; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Cluster metadata. |
| 40 | * <p> |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 41 | * 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] | 42 | * of {@link org.onosproject.cluster.ControllerNode nodes} and the collection of data |
| 43 | * {@link org.onosproject.cluster.Partition partitions}. |
| 44 | */ |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 45 | public final class ClusterMetadata implements Provided { |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 46 | |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 47 | private final ProviderId providerId; |
| 48 | private final String name; |
| 49 | private final Set<ControllerNode> nodes; |
| 50 | private final Set<Partition> partitions; |
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; |
| 63 | nodes = null; |
| 64 | partitions = null; |
| 65 | } |
| 66 | |
| 67 | public ClusterMetadata(ProviderId providerId, |
| 68 | String name, |
| 69 | Set<ControllerNode> nodes, |
| 70 | Set<Partition> partitions) { |
| 71 | this.providerId = checkNotNull(providerId); |
| 72 | this.name = checkNotNull(name); |
| 73 | this.nodes = ImmutableSet.copyOf(checkNotNull(nodes)); |
| 74 | // verify that partitions are constituted from valid cluster nodes. |
| 75 | boolean validPartitions = Collections2.transform(nodes, ControllerNode::id) |
| 76 | .containsAll(partitions |
| 77 | .stream() |
| 78 | .flatMap(r -> r.getMembers().stream()) |
| 79 | .collect(Collectors.toSet())); |
| 80 | verify(validPartitions, "Partition locations must be valid cluster nodes"); |
| 81 | this.partitions = ImmutableSet.copyOf(checkNotNull(partitions)); |
| 82 | } |
| 83 | |
| 84 | public ClusterMetadata(String name, |
| 85 | Set<ControllerNode> nodes, |
| 86 | Set<Partition> partitions) { |
| 87 | this(new ProviderId("none", "none"), name, nodes, partitions); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public ProviderId providerId() { |
| 92 | return providerId; |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the name of the cluster. |
| 97 | * |
| 98 | * @return cluster name |
| 99 | */ |
| 100 | public String getName() { |
| 101 | return this.name; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster. |
| 106 | * @return cluster nodes |
| 107 | */ |
| 108 | public Collection<ControllerNode> getNodes() { |
| 109 | return this.nodes; |
| 110 | } |
| 111 | |
| 112 | /** |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 113 | * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make |
| 114 | * up the cluster. |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 115 | * @return collection of partitions. |
| 116 | */ |
| 117 | public Collection<Partition> getPartitions() { |
| 118 | return this.partitions; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public String toString() { |
| 123 | return MoreObjects.toStringHelper(ClusterMetadata.class) |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 124 | .add("providerId", providerId) |
Madan Jampani | ec1df02 | 2015-10-13 21:23:03 -0700 | [diff] [blame] | 125 | .add("name", name) |
| 126 | .add("nodes", nodes) |
| 127 | .add("partitions", partitions) |
| 128 | .toString(); |
| 129 | } |
| 130 | |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 131 | @Override |
| 132 | public int hashCode() { |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 133 | return Arrays.deepHashCode(new Object[] {providerId, name, nodes, partitions}); |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 137 | * Provide a deep equality check of the cluster metadata (non-Javadoc) |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 138 | * |
| 139 | * @see java.lang.Object#equals(java.lang.Object) |
| 140 | */ |
| 141 | @Override |
| 142 | public boolean equals(Object object) { |
| 143 | |
Ray Milkey | ae4e5ed | 2018-01-17 15:24:52 -0800 | [diff] [blame^] | 144 | if (object == null) { |
| 145 | return false; |
| 146 | } |
| 147 | |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 148 | if (!ClusterMetadata.class.isInstance(object)) { |
| 149 | return false; |
| 150 | } |
| 151 | ClusterMetadata that = (ClusterMetadata) object; |
| 152 | |
Ray Milkey | ae4e5ed | 2018-01-17 15:24:52 -0800 | [diff] [blame^] | 153 | return Objects.equals(this.name, that.name) && |
| 154 | Objects.equals(this.nodes.size(), that.nodes.size()) && |
| 155 | Objects.equals(this.partitions.size(), that.partitions.size()) && |
| 156 | Sets.symmetricDifference(this.nodes, that.nodes).isEmpty() && |
| 157 | Sets.symmetricDifference(this.partitions, that.partitions).isEmpty(); |
David K. Bainbridge | e676dab | 2015-10-23 16:13:07 -0700 | [diff] [blame] | 158 | } |
Madan Jampani | f6c6a30 | 2016-01-18 14:33:45 -0800 | [diff] [blame] | 159 | } |