blob: bd3e65ef1307a675ccb59bdc9c79a970ff7aa967 [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;
20import java.util.Set;
21import java.util.stream.Collectors;
22
Madan Jampaniad3c5262016-01-20 00:50:17 -080023import org.onosproject.net.Provided;
24import org.onosproject.net.provider.ProviderId;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080025
Madan Jampaniec1df022015-10-13 21:23:03 -070026import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampaniec1df022015-10-13 21:23:03 -070027import static com.google.common.base.Verify.verify;
Ayaka Koshibe48229222016-05-16 18:04:26 -070028import static com.google.common.base.Charsets.UTF_8;
Madan Jampaniec1df022015-10-13 21:23:03 -070029
30import com.google.common.base.MoreObjects;
31import com.google.common.collect.Collections2;
32import com.google.common.collect.ImmutableSet;
David K. Bainbridgee676dab2015-10-23 16:13:07 -070033import com.google.common.collect.Sets;
Ayaka Koshibe48229222016-05-16 18:04:26 -070034import com.google.common.hash.Funnel;
35import com.google.common.hash.PrimitiveSink;
Madan Jampaniec1df022015-10-13 21:23:03 -070036
37/**
38 * Cluster metadata.
39 * <p>
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080040 * Metadata specifies how a ONOS cluster is constituted and is made up of the collection
Madan Jampaniec1df022015-10-13 21:23:03 -070041 * of {@link org.onosproject.cluster.ControllerNode nodes} and the collection of data
42 * {@link org.onosproject.cluster.Partition partitions}.
43 */
Madan Jampaniad3c5262016-01-20 00:50:17 -080044public final class ClusterMetadata implements Provided {
Madan Jampaniec1df022015-10-13 21:23:03 -070045
Madan Jampaniad3c5262016-01-20 00:50:17 -080046 private final ProviderId providerId;
47 private final String name;
48 private final Set<ControllerNode> nodes;
49 private final Set<Partition> partitions;
Madan Jampaniec1df022015-10-13 21:23:03 -070050
Ayaka Koshibe48229222016-05-16 18:04:26 -070051 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 Jampani931e97d2016-02-26 12:20:44 -080058 @SuppressWarnings("unused")
Madan Jampaniad3c5262016-01-20 00:50:17 -080059 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 Jampaniec1df022015-10-13 21:23:03 -070092 }
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 Jampaniab7e7cd2016-01-14 14:02:32 -0800112 * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make
113 * up the cluster.
Madan Jampaniec1df022015-10-13 21:23:03 -0700114 * @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 Jampaniad3c5262016-01-20 00:50:17 -0800123 .add("providerId", providerId)
Madan Jampaniec1df022015-10-13 21:23:03 -0700124 .add("name", name)
125 .add("nodes", nodes)
126 .add("partitions", partitions)
127 .toString();
128 }
129
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700130 @Override
131 public int hashCode() {
Madan Jampaniad3c5262016-01-20 00:50:17 -0800132 return Arrays.deepHashCode(new Object[] {providerId, name, nodes, partitions});
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700133 }
134
135 /*
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800136 * Provide a deep equality check of the cluster metadata (non-Javadoc)
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700137 *
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 Jampanif6c6a302016-01-18 14:33:45 -0800156}