blob: acad25fbe78c272cbb0415bd2d04f15a2ed78d44 [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;
40
41 public ClusterMetadataDiff(ClusterMetadata oldValue, ClusterMetadata newValue) {
42 this.oldValue = oldValue;
43 this.newValue = newValue;
44
45 Set<ControllerNode> currentNodeSet = oldValue == null
46 ? ImmutableSet.of() : ImmutableSet.copyOf(oldValue.getNodes());
47 Set<ControllerNode> newNodeSet = newValue == null
48 ? ImmutableSet.of() : ImmutableSet.copyOf(newValue.getNodes());
49 nodesAdded = Sets.difference(newNodeSet, currentNodeSet);
50 nodesRemoved = Sets.difference(currentNodeSet, newNodeSet)
51 .stream()
52 .map(ControllerNode::id)
53 .collect(Collectors.toSet());
54 }
55
56 /**
57 * Returns the set of {@link ControllerNode nodes} added with this metadata change.
58 * @return set of controller nodes
59 */
60 public Set<ControllerNode> nodesAdded() {
61 return nodesAdded;
62 }
63
64 /**
65 * Returns the set of {@link ControllerNode nodes} removed with this metadata change.
66 * @return set of controller node identifiers
67 */
68 public Set<NodeId> nodesRemoved() {
69 return nodesRemoved;
70 }
71
72 /**
73 * Returns a mapping of all partition diffs.
74 * @return partition diffs.
75 */
76 public Map<PartitionId, PartitionDiff> partitionDiffs() {
77 Map<PartitionId, Partition> oldPartitions = Maps.newHashMap();
78 oldValue.getPartitions()
79 .forEach(p -> oldPartitions.put(p.getId(), p));
80 Map<PartitionId, Partition> newPartitions = Maps.newHashMap();
81 newValue.getPartitions()
82 .forEach(p -> newPartitions.put(p.getId(), p));
83 checkState(Sets.symmetricDifference(oldPartitions.keySet(), newPartitions.keySet()).isEmpty(),
84 "Number of partitions cannot change");
85 Map<PartitionId, PartitionDiff> partitionDiffs = Maps.newHashMap();
86 oldPartitions.forEach((k, v) -> {
87 partitionDiffs.put(k, new PartitionDiff(v, newPartitions.get(k)));
88 });
89 return partitionDiffs;
90 }
91}