Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.cluster; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkState; |
| 19 | |
| 20 | import java.util.Map; |
| 21 | import java.util.Set; |
| 22 | import java.util.stream.Collectors; |
| 23 | |
| 24 | import com.google.common.collect.ImmutableSet; |
| 25 | import com.google.common.collect.Maps; |
| 26 | import com.google.common.collect.Sets; |
| 27 | |
| 28 | /** |
| 29 | * Utility for examining differences between two {@link ClusterMetadata metadata} values. |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 30 | * |
| 31 | * @deprecated since 1.14 |
Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 32 | */ |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 33 | @Deprecated |
Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 34 | public 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 Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 40 | private final boolean secretChanged; |
Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 41 | |
| 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 Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 55 | |
| 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 Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 69 | } |
| 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 Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 88 | * 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 Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 96 | * 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 | } |