Charles Chan

Carry previous location information in HostEvent

Change-Id: I06957d368a8a547cc3adb36bce4aaf96c432f4c8
...@@ -15,8 +15,12 @@ ...@@ -15,8 +15,12 @@
15 */ 15 */
16 package org.onosproject.net.host; 16 package org.onosproject.net.host;
17 17
18 +import org.joda.time.LocalDateTime;
18 import org.onosproject.event.AbstractEvent; 19 import org.onosproject.event.AbstractEvent;
19 import org.onosproject.net.Host; 20 import org.onosproject.net.Host;
21 +import org.onosproject.net.HostLocation;
22 +
23 +import static com.google.common.base.MoreObjects.toStringHelper;
20 24
21 /** 25 /**
22 * Describes end-station host event. 26 * Describes end-station host event.
...@@ -48,6 +52,8 @@ public class HostEvent extends AbstractEvent<HostEvent.Type, Host> { ...@@ -48,6 +52,8 @@ public class HostEvent extends AbstractEvent<HostEvent.Type, Host> {
48 HOST_MOVED 52 HOST_MOVED
49 } 53 }
50 54
55 + private HostLocation prevLocation;
56 +
51 /** 57 /**
52 * Creates an event of a given type and for the specified host and the 58 * Creates an event of a given type and for the specified host and the
53 * current time. 59 * current time.
...@@ -70,4 +76,35 @@ public class HostEvent extends AbstractEvent<HostEvent.Type, Host> { ...@@ -70,4 +76,35 @@ public class HostEvent extends AbstractEvent<HostEvent.Type, Host> {
70 super(type, host, time); 76 super(type, host, time);
71 } 77 }
72 78
79 + /**
80 + * Creates an event with HOST_MOVED type along with the previous location
81 + * of the host.
82 + *
83 + * @param host event host subject
84 + * @param prevLocation previous location of the host
85 + */
86 + public HostEvent(Host host, HostLocation prevLocation) {
87 + super(Type.HOST_MOVED, host);
88 + this.prevLocation = prevLocation;
89 + }
90 +
91 + /**
92 + * Gets the previous location information in this host event.
93 + *
94 + * @return the previous location, or null if previous location is not
95 + * specified.
96 + */
97 + public HostLocation prevLocation() {
98 + return this.prevLocation;
99 + }
100 +
101 + @Override
102 + public String toString() {
103 + return toStringHelper(this)
104 + .add("time", new LocalDateTime(time()))
105 + .add("type", type())
106 + .add("subject", subject())
107 + .add("prevLocation", prevLocation())
108 + .toString();
109 + }
73 } 110 }
......
...@@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkState; ...@@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkState;
20 import static org.onosproject.net.DefaultAnnotations.merge; 20 import static org.onosproject.net.DefaultAnnotations.merge;
21 import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; 21 import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
22 import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED; 22 import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED;
23 -import static org.onosproject.net.host.HostEvent.Type.HOST_MOVED;
24 import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED; 23 import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED;
25 import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; 24 import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
26 import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; 25 import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
...@@ -89,7 +88,7 @@ public class ECHostStore ...@@ -89,7 +88,7 @@ public class ECHostStore
89 88
90 private EventuallyConsistentMap<HostId, DefaultHost> hosts; 89 private EventuallyConsistentMap<HostId, DefaultHost> hosts;
91 90
92 - private final ConcurrentHashMap<HostId, ConnectPoint> locations = 91 + private final ConcurrentHashMap<HostId, HostLocation> locations =
93 new ConcurrentHashMap<>(); 92 new ConcurrentHashMap<>();
94 93
95 private EventuallyConsistentMapListener<HostId, DefaultHost> hostLocationTracker = 94 private EventuallyConsistentMapListener<HostId, DefaultHost> hostLocationTracker =
...@@ -254,11 +253,11 @@ public class ECHostStore ...@@ -254,11 +253,11 @@ public class ECHostStore
254 public void event(EventuallyConsistentMapEvent<HostId, DefaultHost> event) { 253 public void event(EventuallyConsistentMapEvent<HostId, DefaultHost> event) {
255 DefaultHost host = checkNotNull(event.value()); 254 DefaultHost host = checkNotNull(event.value());
256 if (event.type() == PUT) { 255 if (event.type() == PUT) {
257 - ConnectPoint prevLocation = locations.put(host.id(), host.location()); 256 + HostLocation prevLocation = locations.put(host.id(), host.location());
258 if (prevLocation == null) { 257 if (prevLocation == null) {
259 notifyDelegate(new HostEvent(HOST_ADDED, host)); 258 notifyDelegate(new HostEvent(HOST_ADDED, host));
260 } else if (!Objects.equals(prevLocation, host.location())) { 259 } else if (!Objects.equals(prevLocation, host.location())) {
261 - notifyDelegate(new HostEvent(HOST_MOVED, host)); 260 + notifyDelegate(new HostEvent(host, prevLocation));
262 } else { 261 } else {
263 notifyDelegate(new HostEvent(HOST_UPDATED, host)); 262 notifyDelegate(new HostEvent(HOST_UPDATED, host));
264 } 263 }
......