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