blob: e9f73b40a8d474f8e9384d3f9d418610ee27235e [file] [log] [blame]
Madan Jampani18070572016-02-29 13:54:45 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani18070572016-02-29 13:54:45 -08003 *
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
18import static com.google.common.base.Preconditions.checkState;
19
20import java.util.Map;
21import java.util.Set;
22import java.util.stream.Collectors;
23
24import com.google.common.collect.ImmutableSet;
25import com.google.common.collect.Maps;
26import com.google.common.collect.Sets;
27
28/**
29 * Utility for examining differences between two {@link ClusterMetadata metadata} values.
Jordan Halterman00e92da2018-05-22 23:05:52 -070030 *
31 * @deprecated since 1.14
Madan Jampani18070572016-02-29 13:54:45 -080032 */
Jordan Halterman00e92da2018-05-22 23:05:52 -070033@Deprecated
Madan Jampani18070572016-02-29 13:54:45 -080034public class ClusterMetadataDiff {
35
36 private final ClusterMetadata oldValue;
37 private final ClusterMetadata newValue;
38 private final Set<ControllerNode> nodesAdded;
39 private final Set<NodeId> nodesRemoved;
Samuel Jero31e16f52018-09-21 10:34:28 -040040 private final boolean secretChanged;
Madan Jampani18070572016-02-29 13:54:45 -080041
42 public ClusterMetadataDiff(ClusterMetadata oldValue, ClusterMetadata newValue) {
43 this.oldValue = oldValue;
44 this.newValue = newValue;
45
46 Set<ControllerNode> currentNodeSet = oldValue == null
47 ? ImmutableSet.of() : ImmutableSet.copyOf(oldValue.getNodes());
48 Set<ControllerNode> newNodeSet = newValue == null
49 ? ImmutableSet.of() : ImmutableSet.copyOf(newValue.getNodes());
50 nodesAdded = Sets.difference(newNodeSet, currentNodeSet);
51 nodesRemoved = Sets.difference(currentNodeSet, newNodeSet)
52 .stream()
53 .map(ControllerNode::id)
54 .collect(Collectors.toSet());
Samuel Jero31e16f52018-09-21 10:34:28 -040055
56 boolean haveOldSecret = (oldValue != null && oldValue.getClusterSecret() != null);
57 boolean haveNewSecret = (newValue != null && newValue.getClusterSecret() != null);
58
59 if (!haveOldSecret && haveNewSecret) {
60 secretChanged = true;
61 } else if (haveOldSecret && haveNewSecret &&
62 !oldValue.getClusterSecret().equals(newValue.getClusterSecret())) {
63 secretChanged = true;
64 } else if (haveOldSecret && !haveNewSecret) {
65 secretChanged = true;
66 } else {
67 secretChanged = false;
68 }
Madan Jampani18070572016-02-29 13:54:45 -080069 }
70
71 /**
72 * Returns the set of {@link ControllerNode nodes} added with this metadata change.
73 * @return set of controller nodes
74 */
75 public Set<ControllerNode> nodesAdded() {
76 return nodesAdded;
77 }
78
79 /**
80 * Returns the set of {@link ControllerNode nodes} removed with this metadata change.
81 * @return set of controller node identifiers
82 */
83 public Set<NodeId> nodesRemoved() {
84 return nodesRemoved;
85 }
86
87 /**
Samuel Jero31e16f52018-09-21 10:34:28 -040088 * Returns whether the cluster-wide shared secret changed.
89 * @return whether the cluster secret changed
90 */
91 public boolean clusterSecretChanged() {
92 return secretChanged;
93 }
94
95 /**
Madan Jampani18070572016-02-29 13:54:45 -080096 * Returns a mapping of all partition diffs.
97 * @return partition diffs.
98 */
99 public Map<PartitionId, PartitionDiff> partitionDiffs() {
100 Map<PartitionId, Partition> oldPartitions = Maps.newHashMap();
101 oldValue.getPartitions()
102 .forEach(p -> oldPartitions.put(p.getId(), p));
103 Map<PartitionId, Partition> newPartitions = Maps.newHashMap();
104 newValue.getPartitions()
105 .forEach(p -> newPartitions.put(p.getId(), p));
106 checkState(Sets.symmetricDifference(oldPartitions.keySet(), newPartitions.keySet()).isEmpty(),
107 "Number of partitions cannot change");
108 Map<PartitionId, PartitionDiff> partitionDiffs = Maps.newHashMap();
109 oldPartitions.forEach((k, v) -> {
110 partitionDiffs.put(k, new PartitionDiff(v, newPartitions.get(k)));
111 });
112 return partitionDiffs;
113 }
114}