Ignore event originating from itself

- Hand registryService to NetworkGraphImpl
- Add OriginID to TopologyEvent. (May need to come up with better name)
- Set OriginID on Add/Put event
 - TBD: Cannot add OriginID on remove event

Change-Id: I89b793a48265f2617483651a33ebca1863df7e1f
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
index 176e793..8b6faeb 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphImpl.java
@@ -19,6 +19,7 @@
 import net.onrc.onos.ofcontroller.networkgraph.PortEvent.SwitchPort;
 import net.onrc.onos.ofcontroller.util.EventEntry;
 import net.onrc.onos.ofcontroller.util.Dpid;
+import net.onrc.onos.registry.controller.IControllerRegistryService;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -48,10 +49,12 @@
     private EventHandler eventHandler = new EventHandler();
 
     private final NetworkGraphDatastore datastore;
+    private final IControllerRegistryService registryService;
 
-    public NetworkGraphImpl() {
+    public NetworkGraphImpl(IControllerRegistryService registryService) {
 	super();
 	datastore = new NetworkGraphDatastore(this);
+	this.registryService = registryService;
     }
 
     /**
@@ -119,7 +122,10 @@
 	 */
 	private void processEvents(Collection<EventEntry<TopologyEvent>> events) {
 	    for (EventEntry<TopologyEvent> event : events) {
-		// TODO ignore event triggered by myself
+		if (event.eventData().getOriginID().equals(registryService.getControllerId())) {
+		    // ignore event triggered by myself
+		    continue;
+		}
 		TopologyEvent topologyEvent = event.eventData();
 		switch (event.eventType()) {
 		case ENTRY_ADD:
@@ -227,7 +233,7 @@
 			putSwitch(switchEvent);
 			// Send out notification
 			TopologyEvent topologyEvent =
-			    new TopologyEvent(switchEvent);
+			    new TopologyEvent(switchEvent, registryService.getControllerId());
 			eventChannel.addEntry(topologyEvent.getID(),
 					      topologyEvent);
 		}
@@ -252,7 +258,7 @@
 			putPort(portEvent);
 			// Send out notification
 			TopologyEvent topologyEvent =
-			    new TopologyEvent(portEvent);
+			    new TopologyEvent(portEvent, registryService.getControllerId());
 			eventChannel.addEntry(topologyEvent.getID(),
 					      topologyEvent);
 		}
@@ -277,7 +283,7 @@
 			putLink(linkEvent);
 			// Send out notification
 			TopologyEvent topologyEvent =
-			    new TopologyEvent(linkEvent);
+			    new TopologyEvent(linkEvent, registryService.getControllerId());
 			eventChannel.addEntry(topologyEvent.getID(),
 					      topologyEvent);
 		}
@@ -305,7 +311,7 @@
 //			putDevice(deviceEvent);
 			// Send out notification
 			TopologyEvent topologyEvent =
-			    new TopologyEvent(deviceEvent);
+			    new TopologyEvent(deviceEvent, registryService.getControllerId());
 			eventChannel.addEntry(topologyEvent.getID(),
 					      topologyEvent);
 		}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphModule.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphModule.java
index 3edf5c2..97fa4b5 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphModule.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphModule.java
@@ -13,30 +13,33 @@
 import net.floodlightcontroller.restserver.IRestApiService;
 import net.onrc.onos.datagrid.IDatagridService;
 import net.onrc.onos.ofcontroller.networkgraph.web.NetworkGraphWebRoutable;
+import net.onrc.onos.registry.controller.IControllerRegistryService;
 
 public class NetworkGraphModule implements IFloodlightModule, INetworkGraphService {
 
 	// This is initialized as a module for now
 	// private RCNetworkGraphPublisher eventListener;
-	
+
 	private NetworkGraphImpl networkGraph;
 	//private NetworkGraphDatastore southboundNetworkGraph;
 	private IDatagridService datagridService;
+	private IControllerRegistryService registryService;
+
 
 	private IRestApiService restApi;
 
 	@Override
 	public Collection<Class<? extends IFloodlightService>> getModuleServices() {
-		List<Class<? extends IFloodlightService>> services = 
+		List<Class<? extends IFloodlightService>> services =
 				new ArrayList<Class<? extends IFloodlightService>>();
 		services.add(INetworkGraphService.class);
 		return services;
 	}
 
 	@Override
-	public Map<Class<? extends IFloodlightService>, IFloodlightService> 
+	public Map<Class<? extends IFloodlightService>, IFloodlightService>
 			getServiceImpls() {
-		Map<Class<? extends IFloodlightService>, IFloodlightService> impls = 
+		Map<Class<? extends IFloodlightService>, IFloodlightService> impls =
 				new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
 		impls.put(INetworkGraphService.class, this);
 		return impls;
@@ -44,20 +47,21 @@
 
 	@Override
 	public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
-		List<Class<? extends IFloodlightService>> dependencies = 
+		List<Class<? extends IFloodlightService>> dependencies =
 				new ArrayList<Class<? extends IFloodlightService>>();
 		dependencies.add(IDatagridService.class);
 		dependencies.add(IRestApiService.class);
 		return dependencies;
 	}
-	
+
 	@Override
 	public void init(FloodlightModuleContext context)
 			throws FloodlightModuleException {
 		restApi = context.getServiceImpl(IRestApiService.class);
 		datagridService = context.getServiceImpl(IDatagridService.class);
-		
-		networkGraph = new NetworkGraphImpl();
+		registryService = context.getServiceImpl(IControllerRegistryService.class);
+
+		networkGraph = new NetworkGraphImpl(registryService);
 		//southboundNetworkGraph = new NetworkGraphDatastore(networkGraph);
 	}
 
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyEvent.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyEvent.java
index 927d266..397fac5 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyEvent.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyEvent.java
@@ -8,6 +8,8 @@
  * in a single transaction.
  */
 public class TopologyEvent {
+    public static final String NOBODY = "";
+    String originID = NOBODY;
     SwitchEvent switchEvent = null;		// Set for Switch event
     PortEvent portEvent = null;			// Set for Port event
     LinkEvent linkEvent = null;			// Set for Link event
@@ -24,8 +26,9 @@
      *
      * @param switchEvent the Switch event to use.
      */
-    TopologyEvent(SwitchEvent switchEvent) {
+    TopologyEvent(SwitchEvent switchEvent, String originID) {
 	this.switchEvent = switchEvent;
+	setOriginID(originID);
     }
 
     /**
@@ -33,8 +36,9 @@
      *
      * @param portEvent the Port event to use.
      */
-    TopologyEvent(PortEvent portEvent) {
+    TopologyEvent(PortEvent portEvent, String originID) {
 	this.portEvent = portEvent;
+	setOriginID(originID);
     }
 
     /**
@@ -42,8 +46,9 @@
      *
      * @param linkEvent the Link event to use.
      */
-    TopologyEvent(LinkEvent linkEvent) {
+    TopologyEvent(LinkEvent linkEvent, String originID) {
 	this.linkEvent = linkEvent;
+	setOriginID(originID);
     }
 
     /**
@@ -51,8 +56,9 @@
      *
      * @param deviceEvent the Device event to use.
      */
-    TopologyEvent(DeviceEvent deviceEvent) {
+    TopologyEvent(DeviceEvent deviceEvent, String originID) {
 	this.deviceEvent = deviceEvent;
+	setOriginID(originID);
     }
 
     /**
@@ -89,4 +95,16 @@
 	    return deviceEvent.getID();
 	return null;
     }
+
+    public String getOriginID() {
+	return originID;
+    }
+
+    void setOriginID(String originID) {
+	if (originID != null) {
+	    this.originID = originID;
+	} else {
+	    this.originID = NOBODY;
+	}
+    }
 }