blob: 12db40fd752914b42e0098f227f336f7e1e8bf5c [file] [log] [blame]
Madan Jampaniec1df022015-10-13 21:23:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
28
29import com.google.common.base.MoreObjects;
30import com.google.common.collect.Collections2;
31import com.google.common.collect.ImmutableSet;
David K. Bainbridgee676dab2015-10-23 16:13:07 -070032import com.google.common.collect.Sets;
Madan Jampaniec1df022015-10-13 21:23:03 -070033
34/**
35 * Cluster metadata.
36 * <p>
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080037 * Metadata specifies how a ONOS cluster is constituted and is made up of the collection
Madan Jampaniec1df022015-10-13 21:23:03 -070038 * of {@link org.onosproject.cluster.ControllerNode nodes} and the collection of data
39 * {@link org.onosproject.cluster.Partition partitions}.
40 */
Madan Jampaniad3c5262016-01-20 00:50:17 -080041public final class ClusterMetadata implements Provided {
Madan Jampaniec1df022015-10-13 21:23:03 -070042
Ayaka Koshibe3ddb7b22015-12-10 17:32:59 -080043 // Name to use when the ClusterMetadataService is in transient state
44 public static final String NO_NAME = "";
45
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
Madan Jampani931e97d2016-02-26 12:20:44 -080051 @SuppressWarnings("unused")
Madan Jampaniad3c5262016-01-20 00:50:17 -080052 private ClusterMetadata() {
53 providerId = null;
54 name = null;
55 nodes = null;
56 partitions = null;
57 }
58
59 public ClusterMetadata(ProviderId providerId,
60 String name,
61 Set<ControllerNode> nodes,
62 Set<Partition> partitions) {
63 this.providerId = checkNotNull(providerId);
64 this.name = checkNotNull(name);
65 this.nodes = ImmutableSet.copyOf(checkNotNull(nodes));
66 // verify that partitions are constituted from valid cluster nodes.
67 boolean validPartitions = Collections2.transform(nodes, ControllerNode::id)
68 .containsAll(partitions
69 .stream()
70 .flatMap(r -> r.getMembers().stream())
71 .collect(Collectors.toSet()));
72 verify(validPartitions, "Partition locations must be valid cluster nodes");
73 this.partitions = ImmutableSet.copyOf(checkNotNull(partitions));
74 }
75
76 public ClusterMetadata(String name,
77 Set<ControllerNode> nodes,
78 Set<Partition> partitions) {
79 this(new ProviderId("none", "none"), name, nodes, partitions);
80 }
81
82 @Override
83 public ProviderId providerId() {
84 return providerId;
Madan Jampaniec1df022015-10-13 21:23:03 -070085 }
86
87 /**
88 * Returns the name of the cluster.
89 *
90 * @return cluster name
91 */
92 public String getName() {
93 return this.name;
94 }
95
96 /**
97 * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster.
98 * @return cluster nodes
99 */
100 public Collection<ControllerNode> getNodes() {
101 return this.nodes;
102 }
103
104 /**
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800105 * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make
106 * up the cluster.
Madan Jampaniec1df022015-10-13 21:23:03 -0700107 * @return collection of partitions.
108 */
109 public Collection<Partition> getPartitions() {
110 return this.partitions;
111 }
112
113 @Override
114 public String toString() {
115 return MoreObjects.toStringHelper(ClusterMetadata.class)
Madan Jampaniad3c5262016-01-20 00:50:17 -0800116 .add("providerId", providerId)
Madan Jampaniec1df022015-10-13 21:23:03 -0700117 .add("name", name)
118 .add("nodes", nodes)
119 .add("partitions", partitions)
120 .toString();
121 }
122
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700123 @Override
124 public int hashCode() {
Madan Jampaniad3c5262016-01-20 00:50:17 -0800125 return Arrays.deepHashCode(new Object[] {providerId, name, nodes, partitions});
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700126 }
127
128 /*
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800129 * Provide a deep equality check of the cluster metadata (non-Javadoc)
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700130 *
131 * @see java.lang.Object#equals(java.lang.Object)
132 */
133 @Override
134 public boolean equals(Object object) {
135
136 if (!ClusterMetadata.class.isInstance(object)) {
137 return false;
138 }
139 ClusterMetadata that = (ClusterMetadata) object;
140
141 if (!this.name.equals(that.name) || this.nodes.size() != that.nodes.size()
142 || this.partitions.size() != that.partitions.size()) {
143 return false;
144 }
145
146 return Sets.symmetricDifference(this.nodes, that.nodes).isEmpty()
147 && Sets.symmetricDifference(this.partitions, that.partitions).isEmpty();
148 }
Madan Jampanif6c6a302016-01-18 14:33:45 -0800149}