Network Graph Refactoring: WIP: extract the events by canceling matching events

 * Extract the events by canceling matching events
 * Don't embed the PortEvent inside the SwitchEvent, because with such
   with such embedding we cannot handle events reordering. E.g.,
   If ADD(SwitchEvent) with embedded ADD(PortEvent) is followed by
   REMOVE(PortEvent), then reordering of those two events is problematic
   and cannot be detected easily.

 * Adjust some of the API to reflect the removal of the embedded
  PortEvent events within the SwitchEvent.

 * Renamed TopologyManage.loadWholeTopologyFromDB() to
   readWholeTopologyFromDB() and change its implementation to
   return a collection of Topology Events.
   Not used for now.

Change-Id: I456514e1ef997c5b50684448193fe47d7a46b141
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
index 61dde3a..dca4eb0 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/NetworkGraphDatastore.java
@@ -2,6 +2,7 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Collection;
 
 import net.onrc.onos.datastore.RCObject;
 import net.onrc.onos.datastore.RCObject.WriteOp;
@@ -36,9 +37,11 @@
 	 * Add a switch to the database.
 	 *
 	 * @param sw the switch to add.
+	 * @param portEvents the corresponding switch ports to add.
 	 * @return true on success, otherwise false.
 	 */
-	public boolean addSwitch(SwitchEvent sw) {
+	public boolean addSwitch(SwitchEvent sw,
+				 Collection<PortEvent> portEvents) {
 		log.debug("Adding switch {}", sw);
 		ArrayList<WriteOp> groupOp = new ArrayList<>();
 
@@ -50,7 +53,7 @@
 		// to assure that DPID is unique cluster-wide, etc.
 		groupOp.add(WriteOp.ForceCreate(rcSwitch));
 
-		for (PortEvent portEvent : sw.getPorts()) {
+		for (PortEvent portEvent : portEvents) {
 			RCPort rcPort = new RCPort(sw.getDpid(), portEvent.getNumber());
 			rcPort.setStatus(RCPort.STATUS.ACTIVE);
 
@@ -75,9 +78,11 @@
 	 * Update a switch as inactive in the database.
 	 *
 	 * @param sw the switch to update.
+	 * @param portEvents the corresponding switch ports to update.
 	 * @return true on success, otherwise false.
 	 */
-	public boolean deactivateSwitch(SwitchEvent sw) {
+	public boolean deactivateSwitch(SwitchEvent sw,
+					Collection<PortEvent> portEvents) {
 		log.debug("Deactivating switch {}", sw);
 		RCSwitch rcSwitch = new RCSwitch(sw.getDpid());
 
@@ -86,7 +91,7 @@
 
 		groupOp.add(WriteOp.ForceCreate(rcSwitch));
 
-		for (PortEvent portEvent : sw.getPorts()) {
+		for (PortEvent portEvent : portEvents) {
 			RCPort rcPort = new RCPort(sw.getDpid(), (long)portEvent.getNumber());
 			rcPort.setStatus(RCPort.STATUS.INACTIVE);
 
@@ -94,7 +99,7 @@
 		}
 
 		boolean failed = RCObject.multiWrite(groupOp);
-		
+
 		return !failed;
 	}