blob: 63a04d1825911bddff33e78bb6464f5160b6556d [file] [log] [blame]
Madan Jampaniec1df022015-10-13 21:23:03 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
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 Jampaniad3c5262016-01-20 00:50:17 -080051 private ClusterMetadata() {
52 providerId = null;
53 name = null;
54 nodes = null;
55 partitions = null;
56 }
57
58 public ClusterMetadata(ProviderId providerId,
59 String name,
60 Set<ControllerNode> nodes,
61 Set<Partition> partitions) {
62 this.providerId = checkNotNull(providerId);
63 this.name = checkNotNull(name);
64 this.nodes = ImmutableSet.copyOf(checkNotNull(nodes));
65 // verify that partitions are constituted from valid cluster nodes.
66 boolean validPartitions = Collections2.transform(nodes, ControllerNode::id)
67 .containsAll(partitions
68 .stream()
69 .flatMap(r -> r.getMembers().stream())
70 .collect(Collectors.toSet()));
71 verify(validPartitions, "Partition locations must be valid cluster nodes");
72 this.partitions = ImmutableSet.copyOf(checkNotNull(partitions));
73 }
74
75 public ClusterMetadata(String name,
76 Set<ControllerNode> nodes,
77 Set<Partition> partitions) {
78 this(new ProviderId("none", "none"), name, nodes, partitions);
79 }
80
81 @Override
82 public ProviderId providerId() {
83 return providerId;
Madan Jampaniec1df022015-10-13 21:23:03 -070084 }
85
86 /**
87 * Returns the name of the cluster.
88 *
89 * @return cluster name
90 */
91 public String getName() {
92 return this.name;
93 }
94
95 /**
96 * Returns the collection of {@link org.onosproject.cluster.ControllerNode nodes} that make up the cluster.
97 * @return cluster nodes
98 */
99 public Collection<ControllerNode> getNodes() {
100 return this.nodes;
101 }
102
103 /**
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800104 * Returns the collection of {@link org.onosproject.cluster.Partition partitions} that make
105 * up the cluster.
Madan Jampaniec1df022015-10-13 21:23:03 -0700106 * @return collection of partitions.
107 */
108 public Collection<Partition> getPartitions() {
109 return this.partitions;
110 }
111
112 @Override
113 public String toString() {
114 return MoreObjects.toStringHelper(ClusterMetadata.class)
Madan Jampaniad3c5262016-01-20 00:50:17 -0800115 .add("providerId", providerId)
Madan Jampaniec1df022015-10-13 21:23:03 -0700116 .add("name", name)
117 .add("nodes", nodes)
118 .add("partitions", partitions)
119 .toString();
120 }
121
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700122 @Override
123 public int hashCode() {
Madan Jampaniad3c5262016-01-20 00:50:17 -0800124 return Arrays.deepHashCode(new Object[] {providerId, name, nodes, partitions});
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700125 }
126
127 /*
Madan Jampaniab7e7cd2016-01-14 14:02:32 -0800128 * Provide a deep equality check of the cluster metadata (non-Javadoc)
David K. Bainbridgee676dab2015-10-23 16:13:07 -0700129 *
130 * @see java.lang.Object#equals(java.lang.Object)
131 */
132 @Override
133 public boolean equals(Object object) {
134
135 if (!ClusterMetadata.class.isInstance(object)) {
136 return false;
137 }
138 ClusterMetadata that = (ClusterMetadata) object;
139
140 if (!this.name.equals(that.name) || this.nodes.size() != that.nodes.size()
141 || this.partitions.size() != that.partitions.size()) {
142 return false;
143 }
144
145 return Sets.symmetricDifference(this.nodes, that.nodes).isEmpty()
146 && Sets.symmetricDifference(this.partitions, that.partitions).isEmpty();
147 }
Madan Jampanif6c6a302016-01-18 14:33:45 -0800148}