From ec0425c18cbe49d368c600160f033acf9fe344ca Mon Sep 17 00:00:00 2001 From: Charles Chan Date: Wed, 11 Nov 2015 12:09:38 -0800 Subject: [PATCH] Carry previous location information in HostEvent Change-Id: I06957d368a8a547cc3adb36bce4aaf96c432f4c8 --- .../org/onosproject/net/host/HostEvent.java | 37 +++++++++++++++++++ .../store/host/impl/ECHostStore.java | 7 ++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/core/api/src/main/java/org/onosproject/net/host/HostEvent.java b/core/api/src/main/java/org/onosproject/net/host/HostEvent.java index 98329df04c..58ac0bb848 100644 --- a/core/api/src/main/java/org/onosproject/net/host/HostEvent.java +++ b/core/api/src/main/java/org/onosproject/net/host/HostEvent.java @@ -15,8 +15,12 @@ */ package org.onosproject.net.host; +import org.joda.time.LocalDateTime; import org.onosproject.event.AbstractEvent; import org.onosproject.net.Host; +import org.onosproject.net.HostLocation; + +import static com.google.common.base.MoreObjects.toStringHelper; /** * Describes end-station host event. @@ -48,6 +52,8 @@ public class HostEvent extends AbstractEvent { HOST_MOVED } + private HostLocation prevLocation; + /** * Creates an event of a given type and for the specified host and the * current time. @@ -70,4 +76,35 @@ public class HostEvent extends AbstractEvent { super(type, host, time); } + /** + * Creates an event with HOST_MOVED type along with the previous location + * of the host. + * + * @param host event host subject + * @param prevLocation previous location of the host + */ + public HostEvent(Host host, HostLocation prevLocation) { + super(Type.HOST_MOVED, host); + this.prevLocation = prevLocation; + } + + /** + * Gets the previous location information in this host event. + * + * @return the previous location, or null if previous location is not + * specified. + */ + public HostLocation prevLocation() { + return this.prevLocation; + } + + @Override + public String toString() { + return toStringHelper(this) + .add("time", new LocalDateTime(time())) + .add("type", type()) + .add("subject", subject()) + .add("prevLocation", prevLocation()) + .toString(); + } } diff --git a/core/store/dist/src/main/java/org/onosproject/store/host/impl/ECHostStore.java b/core/store/dist/src/main/java/org/onosproject/store/host/impl/ECHostStore.java index 7066b21bb2..391a88f7cd 100644 --- a/core/store/dist/src/main/java/org/onosproject/store/host/impl/ECHostStore.java +++ b/core/store/dist/src/main/java/org/onosproject/store/host/impl/ECHostStore.java @@ -20,7 +20,6 @@ import static com.google.common.base.Preconditions.checkState; import static org.onosproject.net.DefaultAnnotations.merge; import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; import static org.onosproject.net.host.HostEvent.Type.HOST_REMOVED; -import static org.onosproject.net.host.HostEvent.Type.HOST_MOVED; import static org.onosproject.net.host.HostEvent.Type.HOST_UPDATED; import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT; import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE; @@ -89,7 +88,7 @@ public class ECHostStore private EventuallyConsistentMap hosts; - private final ConcurrentHashMap locations = + private final ConcurrentHashMap locations = new ConcurrentHashMap<>(); private EventuallyConsistentMapListener hostLocationTracker = @@ -254,11 +253,11 @@ public class ECHostStore public void event(EventuallyConsistentMapEvent event) { DefaultHost host = checkNotNull(event.value()); if (event.type() == PUT) { - ConnectPoint prevLocation = locations.put(host.id(), host.location()); + HostLocation prevLocation = locations.put(host.id(), host.location()); if (prevLocation == null) { notifyDelegate(new HostEvent(HOST_ADDED, host)); } else if (!Objects.equals(prevLocation, host.location())) { - notifyDelegate(new HostEvent(HOST_MOVED, host)); + notifyDelegate(new HostEvent(host, prevLocation)); } else { notifyDelegate(new HostEvent(HOST_UPDATED, host)); }