blob: 4bb9ddabdf86c4e588cb3eba3e2e6117c4a1b518 [file] [log] [blame]
Yuta HIGUCHId76830b2014-09-30 19:08:40 -07001package org.onlab.onos.store.device.impl;
2
3import java.util.Collection;
4import java.util.HashMap;
5import java.util.HashSet;
6import java.util.Map;
7import java.util.Set;
8
9import org.onlab.onos.cluster.NodeId;
10import org.onlab.onos.net.Device;
11import org.onlab.onos.net.DeviceId;
Madan Jampani195af6e2014-10-09 15:01:17 -070012import org.onlab.onos.net.device.Timestamp;
13import org.onlab.onos.net.device.VersionedValue;
Yuta HIGUCHIc057c632014-10-06 18:38:14 -070014import org.onlab.onos.store.common.impl.AntiEntropyReply;
Yuta HIGUCHId76830b2014-09-30 19:08:40 -070015
16import com.google.common.collect.ImmutableMap;
17import com.google.common.collect.ImmutableSet;
18
19public class DeviceAntiEntropyReply
20 extends AntiEntropyReply<DeviceId, VersionedValue<Device>> {
21
22
23 public DeviceAntiEntropyReply(NodeId sender,
24 Map<DeviceId, VersionedValue<Device>> suggestion,
25 Set<DeviceId> request) {
26 super(sender, suggestion, request);
27 }
28
29 /**
30 * Creates a reply to Anti-Entropy advertisement.
31 *
32 * @param advertisement to respond to
33 * @param self node identifier representing local node
34 * @param localValues local values held on this node
35 * @return reply message
36 */
37 public static DeviceAntiEntropyReply reply(
38 DeviceAntiEntropyAdvertisement advertisement,
39 NodeId self,
40 Collection<VersionedValue<Device>> localValues
41 ) {
42
43 ImmutableMap<DeviceId, Timestamp> ads = advertisement.advertisement();
44
45 ImmutableMap.Builder<DeviceId, VersionedValue<Device>>
46 sug = ImmutableMap.builder();
47
48 Set<DeviceId> req = new HashSet<>(ads.keySet());
49
50 for (VersionedValue<Device> e : localValues) {
51 final DeviceId id = e.entity().id();
52 final Timestamp local = e.timestamp();
53 final Timestamp theirs = ads.get(id);
54 if (theirs == null) {
55 // they don't have it, suggest
56 sug.put(id, e);
57 // don't need theirs
58 req.remove(id);
59 } else if (local.compareTo(theirs) < 0) {
60 // they got older one, suggest
61 sug.put(id, e);
62 // don't need theirs
63 req.remove(id);
64 } else if (local.equals(theirs)) {
65 // same, don't need theirs
66 req.remove(id);
67 }
68 }
69
70 return new DeviceAntiEntropyReply(self, sug.build(), req);
71 }
72
73 /**
74 * Creates a reply to request for values held locally.
75 *
76 * @param requests message containing the request
77 * @param self node identifier representing local node
78 * @param localValues local valeds held on this node
79 * @return reply message
80 */
81 public static DeviceAntiEntropyReply reply(
82 DeviceAntiEntropyReply requests,
83 NodeId self,
84 Map<DeviceId, VersionedValue<Device>> localValues
85 ) {
86
87 Set<DeviceId> reqs = requests.request();
88
89 Map<DeviceId, VersionedValue<Device>> requested = new HashMap<>(reqs.size());
90 for (DeviceId id : reqs) {
91 final VersionedValue<Device> value = localValues.get(id);
92 if (value != null) {
93 requested.put(id, value);
94 }
95 }
96
97 Set<DeviceId> empty = ImmutableSet.of();
98 return new DeviceAntiEntropyReply(self, requested, empty);
99 }
100
101 // For serializer
102 protected DeviceAntiEntropyReply() {}
103}