blob: c2053456fe104ec1be087ffb0fa12aabe87d1e80 [file] [log] [blame]
Madan Jampaniec1df022015-10-13 21:23:03 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Madan Jampaniec1df022015-10-13 21:23:03 -07003 *
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 */
16package org.onosproject.cluster;
17
David K. Bainbridgee676dab2015-10-23 16:13:07 -070018import java.util.Arrays;
Madan Jampaniec1df022015-10-13 21:23:03 -070019import java.util.Collection;
Ray Milkey74e59132018-01-17 15:24:52 -080020import java.util.Objects;
Madan Jampaniec1df022015-10-13 21:23:03 -070021import java.util.Set;
22import java.util.stream.Collectors;
23
Madan Jampaniad3c5262016-01-20 00:50:17 -080024import org.onosproject.net.Provided;
25import org.onosproject.net.provider.ProviderId;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080026
Madan Jampaniec1df022015-10-13 21:23:03 -070027import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampaniec1df022015-10-13 21:23:03 -070028import static com.google.common.base.Verify.verify;
Ayaka Koshibe48229222016-05-16 18:04:26 -070029import static com.google.common.base.Charsets.UTF_8;
Madan Jampaniec1df022015-10-13 21:23:03 -070030
31import com.google.common.base.MoreObjects;
32import com.google.common.collect.Collections2;
33import com.google.common.collect.ImmutableSet;
David K. Bainbridgee676dab2015-10-23 16:13:07 -070034import com.google.common.collect.Sets;
Ayaka Koshibe48229222016-05-16 18:04:26 -070035import com.google.common.hash.Funnel;
36import com.google.common.hash.PrimitiveSink;
Madan Jampaniec1df022015-10-13 21:23:03 -070037
38/**
39 * Cluster metadata.
40 * <p>
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080041 * Metadata specifies how a ONOS cluster is constituted and is made up of the collection
Madan Jampaniec1df022015-10-13 21:23:03 -070042 * of {@link org.onosproject.cluster.ControllerNode nodes} and the collection of data
43 * {@link org.onosproject.cluster.Partition partitions}.
44 */
Madan Jampaniad3c5262016-01-20 00:50:17 -080045public final class ClusterMetadata implements Provided {
Madan Jampaniec1df022015-10-13 21:23:03 -070046
Madan Jampaniad3c5262016-01-20 00:50:17 -080047 private final ProviderId providerId;
48 private final String name;
49 private final Set<ControllerNode> nodes;
50 private final Set<Partition> partitions;
Madan Jampaniec1df022015-10-13 21:23:03 -070051
Ayaka Koshibe48229222016-05-16 18:04:26 -070052 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 Jampani931e97d2016-02-26 12:20:44 -080059 @SuppressWarnings("unused")
Madan Jampaniad3c5262016-01-20 00:50:17 -080060 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 Jampaniec1df022015-10-13 21:23:03 -070093 }
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 Jampaniab7e7cd2016-01-14 14:02:32 -0800113 * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make
114 * up the cluster.
Madan Jampaniec1df022015-10-13 21:23:03 -0700115 * @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 Jampaniad3c5262016-01-20 00:50:17 -0800124 .add("providerId", providerId)
Madan Jampaniec1df022015-10-13 21:23:03 -0700125 .add("name", name)
126 .add("nodes", nodes)
127 .add("partitions", partitions)
128 .toString();
129 }
130
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700131 @Override
132 public int hashCode() {
Madan Jampaniad3c5262016-01-20 00:50:17 -0800133 return Arrays.deepHashCode(new Object[] {providerId, name, nodes, partitions});
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700134 }
135
136 /*
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800137 * Provide a deep equality check of the cluster metadata (non-Javadoc)
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700138 *
139 * @see java.lang.Object#equals(java.lang.Object)
140 */
141 @Override
142 public boolean equals(Object object) {
143
Ray Milkey74e59132018-01-17 15:24:52 -0800144 if (object == null) {
145 return false;
146 }
147
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700148 if (!ClusterMetadata.class.isInstance(object)) {
149 return false;
150 }
151 ClusterMetadata that = (ClusterMetadata) object;
152
Ray Milkey74e59132018-01-17 15:24:52 -0800153 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. Bainbridgee676dab2015-10-23 16:13:07 -0700158 }
Madan Jampanif6c6a302016-01-18 14:33:45 -0800159}