blob: 8f7d9603d4bf999781d6b2d48c53c2a6c5fad039 [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;
Jordan Halterman00e92da2018-05-22 23:05:52 -070020import java.util.Collections;
Ray Milkey74e59132018-01-17 15:24:52 -080021import java.util.Objects;
Madan Jampaniec1df022015-10-13 21:23:03 -070022import java.util.Set;
Madan Jampaniec1df022015-10-13 21:23:03 -070023
24import com.google.common.base.MoreObjects;
Madan Jampaniec1df022015-10-13 21:23:03 -070025import com.google.common.collect.ImmutableSet;
David K. Bainbridgee676dab2015-10-23 16:13:07 -070026import com.google.common.collect.Sets;
Ayaka Koshibe48229222016-05-16 18:04:26 -070027import com.google.common.hash.Funnel;
28import com.google.common.hash.PrimitiveSink;
Jordan Halterman00e92da2018-05-22 23:05:52 -070029import org.onosproject.net.Provided;
30import org.onosproject.net.provider.ProviderId;
31
32import static com.google.common.base.Charsets.UTF_8;
33import static com.google.common.base.Preconditions.checkNotNull;
Madan Jampaniec1df022015-10-13 21:23:03 -070034
35/**
36 * Cluster metadata.
37 * <p>
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080038 * Metadata specifies how a ONOS cluster is constituted and is made up of the collection
Madan Jampaniec1df022015-10-13 21:23:03 -070039 * of {@link org.onosproject.cluster.ControllerNode nodes} and the collection of data
40 * {@link org.onosproject.cluster.Partition partitions}.
41 */
Madan Jampaniad3c5262016-01-20 00:50:17 -080042public final class ClusterMetadata implements Provided {
Madan Jampaniec1df022015-10-13 21:23:03 -070043
Madan Jampaniad3c5262016-01-20 00:50:17 -080044 private final ProviderId providerId;
45 private final String name;
Jordan Halterman00e92da2018-05-22 23:05:52 -070046 private final ControllerNode localNode;
47 private final Set<Node> nodes;
Madan Jampaniec1df022015-10-13 21:23:03 -070048
Ayaka Koshibe48229222016-05-16 18:04:26 -070049 public static final Funnel<ClusterMetadata> HASH_FUNNEL = new Funnel<ClusterMetadata>() {
50 @Override
51 public void funnel(ClusterMetadata cm, PrimitiveSink into) {
52 into.putString(cm.name, UTF_8);
53 }
54 };
55
Madan Jampani931e97d2016-02-26 12:20:44 -080056 @SuppressWarnings("unused")
Madan Jampaniad3c5262016-01-20 00:50:17 -080057 private ClusterMetadata() {
58 providerId = null;
59 name = null;
Jordan Halterman00e92da2018-05-22 23:05:52 -070060 localNode = null;
Madan Jampaniad3c5262016-01-20 00:50:17 -080061 nodes = null;
Madan Jampaniad3c5262016-01-20 00:50:17 -080062 }
63
Jordan Halterman00e92da2018-05-22 23:05:52 -070064 public ClusterMetadata(
65 ProviderId providerId,
66 String name,
67 ControllerNode localNode,
68 Set<Node> nodes) {
Madan Jampaniad3c5262016-01-20 00:50:17 -080069 this.providerId = checkNotNull(providerId);
70 this.name = checkNotNull(name);
Jordan Halterman00e92da2018-05-22 23:05:52 -070071 this.localNode = localNode;
Madan Jampaniad3c5262016-01-20 00:50:17 -080072 this.nodes = ImmutableSet.copyOf(checkNotNull(nodes));
Madan Jampaniad3c5262016-01-20 00:50:17 -080073 }
74
Jordan Halterman00e92da2018-05-22 23:05:52 -070075 public ClusterMetadata(String name, ControllerNode localNode, Set<Node> nodes) {
76 this(new ProviderId("none", "none"), name, localNode, nodes);
Madan Jampaniad3c5262016-01-20 00:50:17 -080077 }
78
79 @Override
80 public ProviderId providerId() {
81 return providerId;
Madan Jampaniec1df022015-10-13 21:23:03 -070082 }
83
84 /**
85 * Returns the name of the cluster.
86 *
87 * @return cluster name
88 */
89 public String getName() {
90 return this.name;
91 }
92
93 /**
Jordan Halterman00e92da2018-05-22 23:05:52 -070094 * Returns the local controller node.
95 * @return the local controller node
96 */
97 public ControllerNode getLocalNode() {
98 return localNode;
99 }
100
101 /**
Madan Jampaniec1df022015-10-13 21:23:03 -0700102 * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster.
103 * @return cluster nodes
104 */
105 public Collection<ControllerNode> getNodes() {
Jordan Halterman00e92da2018-05-22 23:05:52 -0700106 return (Collection) nodes;
107 }
108
109 /**
110 * Returns the collection of storage nodes.
111 *
112 * @return the collection of storage nodes
113 */
114 public Collection<Node> getStorageNodes() {
115 return nodes;
Madan Jampaniec1df022015-10-13 21:23:03 -0700116 }
117
118 /**
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800119 * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make
120 * up the cluster.
Madan Jampaniec1df022015-10-13 21:23:03 -0700121 * @return collection of partitions.
Jordan Halterman00e92da2018-05-22 23:05:52 -0700122 * @deprecated since 1.14
Madan Jampaniec1df022015-10-13 21:23:03 -0700123 */
Jordan Halterman00e92da2018-05-22 23:05:52 -0700124 @Deprecated
Madan Jampaniec1df022015-10-13 21:23:03 -0700125 public Collection<Partition> getPartitions() {
Jordan Halterman00e92da2018-05-22 23:05:52 -0700126 return Collections.emptySet();
Madan Jampaniec1df022015-10-13 21:23:03 -0700127 }
128
129 @Override
130 public String toString() {
131 return MoreObjects.toStringHelper(ClusterMetadata.class)
Madan Jampaniad3c5262016-01-20 00:50:17 -0800132 .add("providerId", providerId)
Madan Jampaniec1df022015-10-13 21:23:03 -0700133 .add("name", name)
134 .add("nodes", nodes)
Madan Jampaniec1df022015-10-13 21:23:03 -0700135 .toString();
136 }
137
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700138 @Override
139 public int hashCode() {
Jordan Halterman00e92da2018-05-22 23:05:52 -0700140 return Arrays.deepHashCode(new Object[] {providerId, name, nodes});
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700141 }
142
143 /*
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800144 * Provide a deep equality check of the cluster metadata (non-Javadoc)
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700145 *
146 * @see java.lang.Object#equals(java.lang.Object)
147 */
148 @Override
149 public boolean equals(Object object) {
Ray Milkey74e59132018-01-17 15:24:52 -0800150 if (object == null) {
151 return false;
152 }
153
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700154 if (!ClusterMetadata.class.isInstance(object)) {
155 return false;
156 }
157 ClusterMetadata that = (ClusterMetadata) object;
158
Ray Milkey74e59132018-01-17 15:24:52 -0800159 return Objects.equals(this.name, that.name) &&
Jordan Halterman00e92da2018-05-22 23:05:52 -0700160 this.localNode.equals(that.localNode) &&
Ray Milkey74e59132018-01-17 15:24:52 -0800161 Objects.equals(this.nodes.size(), that.nodes.size()) &&
Jordan Halterman00e92da2018-05-22 23:05:52 -0700162 Sets.symmetricDifference(this.nodes, that.nodes).isEmpty();
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700163 }
Madan Jampanif6c6a302016-01-18 14:33:45 -0800164}