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