Using provider pattern for cluster metadata.
Change-Id: I5a572b3df9149be959dde9868a9c594dec26a3e0
diff --git a/core/api/src/main/java/org/onosproject/cluster/ClusterMetadata.java b/core/api/src/main/java/org/onosproject/cluster/ClusterMetadata.java
index f0334aa..63a04d1 100644
--- a/core/api/src/main/java/org/onosproject/cluster/ClusterMetadata.java
+++ b/core/api/src/main/java/org/onosproject/cluster/ClusterMetadata.java
@@ -20,10 +20,10 @@
import java.util.Set;
import java.util.stream.Collectors;
-import org.apache.commons.collections.CollectionUtils;
+import org.onosproject.net.Provided;
+import org.onosproject.net.provider.ProviderId;
import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Verify.verifyNotNull;
import static com.google.common.base.Verify.verify;
import com.google.common.base.MoreObjects;
@@ -38,21 +38,49 @@
* of {@link org.onosproject.cluster.ControllerNode nodes} and the collection of data
* {@link org.onosproject.cluster.Partition partitions}.
*/
-public final class ClusterMetadata {
+public final class ClusterMetadata implements Provided {
// Name to use when the ClusterMetadataService is in transient state
public static final String NO_NAME = "";
- private String name;
- private Set<ControllerNode> nodes;
- private Set<Partition> partitions;
+ private final ProviderId providerId;
+ private final String name;
+ private final Set<ControllerNode> nodes;
+ private final Set<Partition> partitions;
- /**
- * Returns a new cluster metadata builder.
- * @return The cluster metadata builder.
- */
- public static Builder builder() {
- return new Builder();
+ private ClusterMetadata() {
+ providerId = null;
+ name = null;
+ nodes = null;
+ partitions = null;
+ }
+
+ public ClusterMetadata(ProviderId providerId,
+ String name,
+ Set<ControllerNode> nodes,
+ Set<Partition> partitions) {
+ this.providerId = checkNotNull(providerId);
+ this.name = checkNotNull(name);
+ this.nodes = ImmutableSet.copyOf(checkNotNull(nodes));
+ // verify that partitions are constituted from valid cluster nodes.
+ boolean validPartitions = Collections2.transform(nodes, ControllerNode::id)
+ .containsAll(partitions
+ .stream()
+ .flatMap(r -> r.getMembers().stream())
+ .collect(Collectors.toSet()));
+ verify(validPartitions, "Partition locations must be valid cluster nodes");
+ this.partitions = ImmutableSet.copyOf(checkNotNull(partitions));
+ }
+
+ public ClusterMetadata(String name,
+ Set<ControllerNode> nodes,
+ Set<Partition> partitions) {
+ this(new ProviderId("none", "none"), name, nodes, partitions);
+ }
+
+ @Override
+ public ProviderId providerId() {
+ return providerId;
}
/**
@@ -84,6 +112,7 @@
@Override
public String toString() {
return MoreObjects.toStringHelper(ClusterMetadata.class)
+ .add("providerId", providerId)
.add("name", name)
.add("nodes", nodes)
.add("partitions", partitions)
@@ -92,7 +121,7 @@
@Override
public int hashCode() {
- return Arrays.deepHashCode(new Object[] {name, nodes, partitions});
+ return Arrays.deepHashCode(new Object[] {providerId, name, nodes, partitions});
}
/*
@@ -116,74 +145,4 @@
return Sets.symmetricDifference(this.nodes, that.nodes).isEmpty()
&& Sets.symmetricDifference(this.partitions, that.partitions).isEmpty();
}
-
- /**
- * Builder for a {@link ClusterMetadata} instance.
- */
- public static class Builder {
-
- private final ClusterMetadata metadata;
-
- public Builder() {
- metadata = new ClusterMetadata();
- }
-
- /**
- * Sets the cluster name, returning the cluster metadata builder for method chaining.
- * @param name cluster name
- * @return this cluster metadata builder
- */
- public Builder withName(String name) {
- metadata.name = checkNotNull(name);
- return this;
- }
-
- /**
- * Sets the collection of cluster nodes, returning the cluster metadata builder for method chaining.
- * @param controllerNodes collection of cluster nodes
- * @return this cluster metadata builder
- */
- public Builder withControllerNodes(Collection<ControllerNode> controllerNodes) {
- metadata.nodes = ImmutableSet.copyOf(checkNotNull(controllerNodes));
- return this;
- }
-
- /**
- * Sets the partitions, returning the cluster metadata builder for method chaining.
- * @param partitions collection of partitions
- * @return this cluster metadata builder
- */
- public Builder withPartitions(Collection<Partition> partitions) {
- metadata.partitions = ImmutableSet.copyOf(checkNotNull(partitions));
- return this;
- }
-
- /**
- * Builds the cluster metadata.
- * @return cluster metadata
- * @throws com.google.common.base.VerifyException VerifyException if the metadata is misconfigured
- */
- public ClusterMetadata build() {
- verifyMetadata();
- return metadata;
- }
-
- /**
- * Validates the constructed metadata for semantic correctness.
- * @throws VerifyException if the metadata is misconfigured.
- */
- private void verifyMetadata() {
- verifyNotNull(metadata.getName(), "Cluster name must be specified");
- verify(CollectionUtils.isNotEmpty(metadata.getNodes()), "Cluster nodes must be specified");
- verify(CollectionUtils.isNotEmpty(metadata.getPartitions()), "Cluster partitions must be specified");
-
- // verify that partitions are constituted from valid cluster nodes.
- boolean validPartitions = Collections2.transform(metadata.getNodes(), ControllerNode::id)
- .containsAll(metadata.getPartitions()
- .stream()
- .flatMap(r -> r.getMembers().stream())
- .collect(Collectors.toSet()));
- verify(validPartitions, "Partition locations must be valid cluster nodes");
- }
- }
}