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