Merge "Fix bug of incorrect size when storing into RAMCloud Allow ruby script to run without having to type ruby." into dev/ramcloud-new-datamodel
diff --git a/src/main/java/net/onrc/onos/datastore/topology/RCDevice.java b/src/main/java/net/onrc/onos/datastore/topology/RCDevice.java
index 8d696ed..bb8a1f2 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCDevice.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCDevice.java
@@ -54,7 +54,7 @@
 
     // Assuming mac is unique cluster-wide
     public static byte[] getDeviceID(final byte[] mac) {
-        return DeviceEvent.getDeviceID(mac);
+        return DeviceEvent.getDeviceID(mac).array();
     }
 
     public static StringBuilder keysToSB(Collection<byte[]> keys) {
diff --git a/src/main/java/net/onrc/onos/datastore/topology/RCLink.java b/src/main/java/net/onrc/onos/datastore/topology/RCLink.java
index 79daa66..e3edff0 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCLink.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCLink.java
@@ -22,7 +22,6 @@
 import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
 
 public class RCLink extends RCObject {
-    @SuppressWarnings("unused")
     private static final Logger log = LoggerFactory.getLogger(RCLink.class);
 
     private static final ThreadLocal<Kryo> linkKryo = new ThreadLocal<Kryo>() {
@@ -66,13 +65,6 @@
 
     public static final String GLOBAL_LINK_TABLE_NAME = "G:Link";
 
-    // FIXME these should be Enum or some number, not String
-    private static final String PROP_STATUS = "status";
-    private static final String PROP_SRC_SW_ID = "src-sw-id";
-    private static final String PROP_SRC_PORT_ID = "src-port-id";
-    private static final String PROP_DST_SW_ID = "dst-sw-id";
-    private static final String PROP_DST_PORT_ID = "dst-port-id";
-
     // must not re-order enum members, ordinal will be sent over wire
     public enum STATUS {
 	INACTIVE, ACTIVE;
@@ -85,7 +77,7 @@
     public static byte[] getLinkID(Long src_dpid, Long src_port_no,
 	    Long dst_dpid, Long dst_port_no) {
 	return LinkEvent.getLinkID(src_dpid, src_port_no, dst_dpid,
-		dst_port_no);
+		dst_port_no).array();
     }
 
     public static StringBuilder keysToSB(Collection<byte[]> keys) {
@@ -213,12 +205,12 @@
 	link.setDstSwId(ByteString.copyFrom(dst.getSwitchID()));
 	link.setDstPortId(ByteString.copyFrom(dst.getPortID()));
 	link.setStatus(status.ordinal());
-	
+
 	if (!map.isEmpty()) {
 	    serializeAndSetValue(linkKryo.get(), map);
 	    link.setValue(ByteString.copyFrom(this.getSerializedValue()));
 	}
-	
+
 	this.value = link.build().toByteArray();
     }
 
@@ -239,7 +231,7 @@
 	} catch (InvalidProtocolBufferException e) {
 	    log.error("{" + toString() + "}: Read Link: ", e);
 	    return null;
-	}	
+	}
     }
 
     @Override
diff --git a/src/main/java/net/onrc/onos/datastore/topology/RCPort.java b/src/main/java/net/onrc/onos/datastore/topology/RCPort.java
index 5fa5d42..0b88b60 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCPort.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCPort.java
@@ -1,15 +1,10 @@
 package net.onrc.onos.datastore.topology;
 
 import java.nio.ByteBuffer;
-import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
-
 import org.openflow.util.HexString;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -22,7 +17,6 @@
 import net.onrc.onos.datastore.RCProtos.PortProperty;
 import net.onrc.onos.datastore.RCObject;
 import net.onrc.onos.datastore.RCTable;
-import net.onrc.onos.datastore.utils.ByteArrayComparator;
 import net.onrc.onos.datastore.utils.ByteArrayUtil;
 import net.onrc.onos.ofcontroller.networkgraph.PortEvent;
 
@@ -46,13 +40,6 @@
 
     public static final String GLOBAL_PORT_TABLE_NAME = "G:Port";
 
-    // FIXME these should be Enum or some number, not String
-    private static final String PROP_DPID = "dpid";
-    private static final String PROP_NUMBER = "number";
-    private static final String PROP_STATUS = "status";
-    private static final String PROP_LINK_IDS = "link-ids";
-    private static final String PROP_DEVICE_IDS = "device-ids";
-
     // must not re-order enum members, ordinal will be sent over wire
     public enum STATUS {
 	INACTIVE, ACTIVE;
@@ -62,19 +49,9 @@
     private final Long number;
 
     private STATUS status;
-    // XXX These 2 set of Ids can be removed from DataStore, if In-Memory cache
-    // build the indexing info from Link.
-    @Deprecated
-    private TreeSet<byte[]> linkIds;
-    @Deprecated
-    transient boolean isLinkIdsModified;
-    @Deprecated
-    private TreeSet<byte[]> deviceIds;
-    @Deprecated
-    transient boolean isDeviceIdsModified;
 
     public static byte[] getPortID(Long dpid, Long number) {
-        return PortEvent.getPortID(dpid, number);
+        return PortEvent.getPortID(dpid, number).array();
     }
 
     public static StringBuilder keysToSB(Collection<byte[]> keys) {
@@ -139,10 +116,6 @@
 	this.dpid = dpid;
 	this.number = number;
 	this.status = STATUS.INACTIVE;
-	this.linkIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR);
-	this.isLinkIdsModified = true;
-	this.deviceIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR);
-	this.isDeviceIdsModified = true;
     }
 
     /**
@@ -206,80 +179,20 @@
 	return getKey();
     }
 
-    @Deprecated
-    public void addLinkId(byte[] linkId) {
-	isLinkIdsModified |= linkIds.add(linkId);
-    }
-
-    @Deprecated
-    public void removeLinkId(byte[] linkId) {
-	isLinkIdsModified |= linkIds.remove(linkId);
-    }
-
-    @Deprecated
-    public void emptyLinkIds() {
-	linkIds.clear();
-	isLinkIdsModified = true;
-    }
-
-    @Deprecated
-    public void addAllToLinkIds(Collection<byte[]> linkIds) {
-	isLinkIdsModified |= this.linkIds.addAll(linkIds);
-    }
-
-    /**
-     *
-     * @return Unmodifiable Set view of all the LinkIds;
-     */
-    @Deprecated
-    public Set<byte[]> getAllLinkIds() {
-	return Collections.unmodifiableSet(linkIds);
-    }
-
-    @Deprecated
-    public void addDeviceId(byte[] deviceId) {
-	isDeviceIdsModified |= deviceIds.add(deviceId);
-    }
-
-    @Deprecated
-    public void removeDeviceId(byte[] deviceId) {
-	isDeviceIdsModified |= deviceIds.remove(deviceId);
-    }
-
-    @Deprecated
-    public void emptyDeviceIds() {
-	deviceIds.clear();
-	isDeviceIdsModified = true;
-    }
-
-    @Deprecated
-    public void addAllToDeviceIds(Collection<byte[]> deviceIds) {
-	isDeviceIdsModified |= this.deviceIds.addAll(deviceIds);
-    }
-
-    /**
-     *
-     * @return Unmodifiable Set view of all the LinkIds;
-     */
-    @Deprecated
-    public Set<byte[]> getAllDeviceIds() {
-	return Collections.unmodifiableSet(deviceIds);
-    }
-
     @Override
-    public void serializeAndSetValue() {	
+    public void serializeAndSetValue() {
 	Map<Object, Object> map = getObjectMap();
-	
+
 	PortProperty.Builder port = PortProperty.newBuilder();
 	port.setDpid(dpid);
 	port.setNumber(number);
 	port.setStatus(status.ordinal());
-	
+
 	if (!map.isEmpty()) {
 	    serializeAndSetValue(portKryo.get(), map);
 	    port.setValue(ByteString.copyFrom(this.getSerializedValue()));
 	}
-	
+
 	this.value = port.build().toByteArray();
     }
 
@@ -300,7 +213,7 @@
 	} catch (InvalidProtocolBufferException e) {
 	    log.error("{" + toString() + "}: Read Port: ", e);
 	    return null;
-	}	
+	}
     }
 
     @Override
diff --git a/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java b/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
index 7670adf..19b9801 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
@@ -1,19 +1,13 @@
 package net.onrc.onos.datastore.topology;
 
 import java.nio.ByteBuffer;
-import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.HashMap;
-import java.util.Set;
-import java.util.TreeSet;
-
 import net.onrc.onos.datastore.RCObject;
 import net.onrc.onos.datastore.RCTable;
-import net.onrc.onos.datastore.utils.ByteArrayComparator;
 import net.onrc.onos.ofcontroller.networkgraph.SwitchEvent;
 
 import org.openflow.util.HexString;
@@ -57,11 +51,6 @@
 
     public static final String GLOBAL_SWITCH_TABLE_NAME = "G:Switch";
 
-    // FIXME these should be Enum or some number, not String
-    private static final String PROP_DPID = "dpid";
-    private static final String PROP_STATUS = "status";
-    private static final String PROP_PORT_IDS = "port-ids";
-
     // must not re-order enum members, ordinal will be sent over wire
     public enum STATUS {
 	INACTIVE, ACTIVE;
@@ -69,13 +58,9 @@
 
     private final Long dpid;
     private STATUS status;
-    @Deprecated
-    private TreeSet<byte[]> portIds;
-    @Deprecated
-    transient private boolean isPortIdsModified;
 
     public static byte[] getSwitchID(Long dpid) {
-        return SwitchEvent.getSwitchID(dpid);
+        return SwitchEvent.getSwitchID(dpid).array();
     }
 
     public static StringBuilder keysToSB(Collection<byte[]> keys) {
@@ -116,8 +101,6 @@
 
 	this.dpid = dpid;
 	this.status = STATUS.INACTIVE;
-	this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR);
-	this.isPortIdsModified = true;
     }
 
     /**
@@ -176,38 +159,6 @@
 	return getKey();
     }
 
-    @Deprecated
-    public void addPortId(byte[] portId) {
-	// TODO: Should we copy portId, or reference is OK.
-	isPortIdsModified |= portIds.add(portId);
-    }
-
-    @Deprecated
-    public void removePortId(byte[] portId) {
-	isPortIdsModified |= portIds.remove(portId);
-    }
-
-    @Deprecated
-    public void emptyPortIds() {
-	portIds.clear();
-	this.isPortIdsModified = true;
-    }
-
-    @Deprecated
-    public void addAllToPortIds(Collection<byte[]> portIds) {
-	// TODO: Should we copy portId, or reference is OK.
-	isPortIdsModified |= this.portIds.addAll(portIds);
-    }
-
-    /**
-     *
-     * @return Unmodifiable Set view of all the PortIds;
-     */
-    @Deprecated
-    public Set<byte[]> getAllPortIds() {
-	return Collections.unmodifiableSet(portIds);
-    }
-
     @Override
     public void serializeAndSetValue() {
 	Map<Object, Object> map = getObjectMap();
@@ -215,12 +166,12 @@
 	SwitchProperty.Builder sw = SwitchProperty.newBuilder();
 	sw.setDpid(dpid);
 	sw.setStatus(status.ordinal());
-	
+
 	if (!map.isEmpty()) {
 	    serializeAndSetValue(switchKryo.get(), map);
 	    sw.setValue(ByteString.copyFrom(this.getSerializedValue()));
 	}
-	
+
 	this.value = sw.build().toByteArray();
     }
 
@@ -255,8 +206,6 @@
 	// create active switch 0x1 with 2 ports
 	RCSwitch sw = new RCSwitch(0x1L);
 	sw.setStatus(STATUS.ACTIVE);
-	sw.addPortId("SW0x0001P001".getBytes(StandardCharsets.UTF_8));
-	sw.addPortId("SW0x0001P002".getBytes(StandardCharsets.UTF_8));
 
 	try {
 	    sw.create();
@@ -273,15 +222,9 @@
 	    log.debug("Reading Switch Failed", e);
 	}
 	assert (swRead.getStatus() == STATUS.ACTIVE);
-	for (byte[] portId : swRead.getAllPortIds()) {
-	    // XXX bad example code, portId is not expected to be ASCII string
-	    log.debug("PortId: {}", new String(portId, StandardCharsets.UTF_8));
-	}
-	assert (swRead.getAllPortIds().size() == 2);
 
 	// update 0x1
 	swRead.setStatus(STATUS.INACTIVE);
-	swRead.removePortId("SW0x0001P001".getBytes(StandardCharsets.UTF_8));
 	try {
 	    swRead.update();
 	} catch (ObjectDoesntExistException | WrongVersionException e) {
@@ -296,11 +239,6 @@
 	    log.debug("Reading Switch Again Failed", e);
 	}
 	assert (swRead2.getStatus() == STATUS.INACTIVE);
-	for (byte[] portId : swRead2.getAllPortIds()) {
-	    // XXX bad example code, portId is not expected to be ASCII string
-	    log.debug("PortId: {}", new String(portId, StandardCharsets.UTF_8));
-	}
-	assert (swRead2.getAllPortIds().size() == 1);
 	try {
 	    swRead2.delete();
 	} catch (ObjectDoesntExistException | WrongVersionException e) {
@@ -347,20 +285,15 @@
 	    log.error("Port creation failed", e);
 	}
 
-	sw1.emptyPortIds();
-	sw1.addPortId(sw1p1.getId());
-	sw1.addPortId(sw1p2.getId());
 	try {
 	    sw1.update();
-	    log.debug("Update {} - {}", sw1,
-		    RCPort.keysToSB(sw1.getAllPortIds()));
+	    log.debug("Update {}", sw1);
 	} catch (ObjectDoesntExistException | WrongVersionException e) {
 	    log.error("Switch update failed", e);
 	}
 
 	RCDevice d1 = new RCDevice(new byte[] { 0, 1, 2, 3, 4, 5, 6 });
 	d1.addPortId(sw1p1.getId());
-	sw1p1.addDeviceId(d1.getId());
 
 	try {
 	    d1.create();
@@ -382,20 +315,14 @@
 	RCPort sw2p2 = new RCPort(0x2L, 2L);
 	sw2p2.setStatus(RCPort.STATUS.ACTIVE);
 
-	sw2.addPortId(sw2p1.getId());
-	sw2.addPortId(sw2p2.getId());
-	sw2.addAllToPortIds(Arrays.asList(sw2p1.getId(), sw2p2.getId()));
-	assert (sw2.getAllPortIds().size() == 2);
-
 	RCDevice d2 = new RCDevice(new byte[] { 6, 5, 4, 3, 2, 1, 0 });
 	d2.addPortId(sw2p2.getId());
-	sw2p2.addDeviceId(d2.getId());
 
 	// XXX Collection created by Arrays.asList needs to be stored, so that
 	// which operation failed
 	Collection<WriteOp> groupOp = Arrays.asList(
-	        RCObject.WriteOp.Create(sw2), RCObject.WriteOp.Create(sw2p1),
-	        RCObject.WriteOp.Create(sw2p2), RCObject.WriteOp.Create(d2));
+		RCObject.WriteOp.Create(sw2), RCObject.WriteOp.Create(sw2p1),
+		RCObject.WriteOp.Create(sw2p2), RCObject.WriteOp.Create(d2));
 	boolean failed = RCObject.multiWrite(groupOp);
 	if (failed) {
 	    log.error("Some of Switch/Port/Device creation failed");
@@ -412,8 +339,6 @@
 	RCLink l1 = new RCLink(0x1L, 2L, 0x2L, 1L);
 	l1.setStatus(RCLink.STATUS.ACTIVE);
 
-	sw1p2.addLinkId(l1.getId());
-	sw2p1.addLinkId(l1.getId());
 	try {
 	    l1.create();
 	    log.debug("Create {}", l1);
@@ -453,39 +378,25 @@
 
 	assert (sw1.getDpid() == 0x1L);
 	assert (sw1.getStatus() == STATUS.ACTIVE);
-	assert (sw1.getAllPortIds().size() == 2);
-	for (byte[] portId : sw1.getAllPortIds()) {
-	    RCPort port = RCPort.createFromKey(portId);
-	    try {
-		port.read();
-		assert (port.getDpid() == 0x1L);
-		log.debug("{} - LinkIDs:{} DeviceIDs:{}", port,
-		        RCLink.keysToSB(port.getAllLinkIds()),
-		        RCDevice.keysToSB(port.getAllDeviceIds()));
+	for (RCPort port : RCPort.getAllPorts()) {
+	    if (port.getDpid() != 0x1L) {
+		continue;
+	    }
+	    log.debug("{}", port);
 
-		for (byte[] deviceId : port.getAllDeviceIds()) {
-		    RCDevice device = RCDevice.createFromKey(deviceId);
-		    try {
-			device.read();
-			log.debug("{} - PortIDs:{}", device,
-			        RCPort.keysToSB(device.getAllPortIds()));
-		    } catch (ObjectDoesntExistException e) {
-			log.error("Reading Device failed", e);
-		    }
+	    for (RCDevice device : RCDevice.getAllDevices()) {
+		if (!device.getAllPortIds().contains(port.getId())) {
+		    continue;
 		}
+		log.debug("{} - PortIDs:{}", device,
+			RCPort.keysToSB(device.getAllPortIds()));
+	    }
 
-		for (byte[] linkId : port.getAllLinkIds()) {
-		    RCLink link = RCLink.createFromKey(linkId);
-		    try {
-			link.read();
-			log.debug("Link {}", link);
-		    } catch (ObjectDoesntExistException e) {
-			log.error("Reading Link failed", e);
-		    }
+	    for (RCLink link : RCLink.getAllLinks()) {
+		if (!Arrays.equals(link.getSrc().getPortID(), port.getId())) {
+		    continue;
 		}
-
-	    } catch (ObjectDoesntExistException e) {
-		log.error("Reading Port failed", e);
+		log.debug("Link {}", link);
 	    }
 	}
 
@@ -499,40 +410,27 @@
 
 	assert (sw2.getDpid() == 0x2L);
 	assert (sw2.getStatus() == STATUS.ACTIVE);
-	assert (sw2.getAllPortIds().size() == 2);
-	for (byte[] portId : sw2.getAllPortIds()) {
-	    RCPort port = RCPort.createFromKey(portId);
-	    try {
-		port.read();
-		assert (port.getDpid() == 0x2L);
-		log.debug("{} - LinkIDs:{} DeviceIDs:{}", port,
-		        RCLink.keysToSB(port.getAllLinkIds()),
-		        RCDevice.keysToSB(port.getAllDeviceIds()));
-
-		for (byte[] deviceId : port.getAllDeviceIds()) {
-		    RCDevice device = RCDevice.createFromKey(deviceId);
-		    try {
-			device.read();
-			log.debug("{} - PortIDs:{}", device,
-			        RCPort.keysToSB(device.getAllPortIds()));
-		    } catch (ObjectDoesntExistException e) {
-			log.error("Reading Device failed", e);
-		    }
-		}
-
-		for (byte[] linkId : port.getAllLinkIds()) {
-		    RCLink link = RCLink.createFromKey(linkId);
-		    try {
-			link.read();
-			log.debug("Link {}", link);
-		    } catch (ObjectDoesntExistException e) {
-			log.error("Reading Link failed", e);
-		    }
-		}
-
-	    } catch (ObjectDoesntExistException e) {
-		log.error("Reading Port failed", e);
+	for (RCPort port : RCPort.getAllPorts()) {
+	    if (port.getDpid() != 0x2L) {
+		continue;
 	    }
+	    log.debug("{}", port);
+
+	    for (RCDevice device : RCDevice.getAllDevices()) {
+		if (!device.getAllPortIds().contains(port.getId())) {
+		    continue;
+		}
+		log.debug("{} - PortIDs:{}", device,
+			RCPort.keysToSB(device.getAllPortIds()));
+	    }
+
+	    for (RCLink link : RCLink.getAllLinks()) {
+		if (!Arrays.equals(link.getSrc().getPortID(), port.getId())) {
+		    continue;
+		}
+		log.debug("Link {}", link);
+	    }
+
 	}
 
 	log.debug("topology_walk end.");
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceEvent.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceEvent.java
index a4c3450..e9ba12e 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceEvent.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/DeviceEvent.java
@@ -81,12 +81,15 @@
     }
 
     // Assuming mac is unique cluster-wide
-    public static byte[] getDeviceID(final byte[] mac) {
-	return ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac)
-		.array();
+    public static ByteBuffer getDeviceID(final byte[] mac) {
+	return ByteBuffer.allocate(2 + mac.length).putChar('D').put(mac);
     }
 
     public byte[] getID() {
+	return getDeviceID(mac.toBytes()).array();
+    }
+
+    public ByteBuffer getIDasByteBuffer() {
 	return getDeviceID(mac.toBytes());
     }
 }
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/LinkEvent.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/LinkEvent.java
index d12e99c..4d060de 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/LinkEvent.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/LinkEvent.java
@@ -11,8 +11,8 @@
  *
  */
 public class LinkEvent {
-    private final SwitchPort src;
-    private final SwitchPort dst;
+    protected final SwitchPort src;
+    protected final SwitchPort dst;
 
     /**
      * Default constructor.
@@ -50,15 +50,20 @@
 
     public static final int LINKID_BYTES = 2 + PortEvent.PORTID_BYTES * 2;
 
-    public static byte[] getLinkID(Long src_dpid, Long src_port_no,
+    public static ByteBuffer getLinkID(Long src_dpid, Long src_port_no,
 	    Long dst_dpid, Long dst_port_no) {
 	return ByteBuffer.allocate(LinkEvent.LINKID_BYTES).putChar('L')
 		.put(PortEvent.getPortID(src_dpid, src_port_no))
-		.put(PortEvent.getPortID(dst_dpid, dst_port_no)).array();
+		.put(PortEvent.getPortID(dst_dpid, dst_port_no));
     }
 
     public byte[] getID() {
 	return getLinkID(src.getDpid(), src.getNumber(),
+		dst.getDpid(), dst.getNumber()).array();
+    }
+
+    public ByteBuffer getIDasByteBuffer() {
+	return getLinkID(src.getDpid(), src.getNumber(),
 		dst.getDpid(), dst.getNumber());
     }
 
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortEvent.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortEvent.java
index f89cdf8..b926037 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortEvent.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/PortEvent.java
@@ -102,7 +102,7 @@
 
     public static final int PORTID_BYTES = SwitchEvent.SWITCHID_BYTES + 2 + 8;
 
-    public static byte[] getPortID(Long dpid, Long number) {
+    public static ByteBuffer getPortID(Long dpid, Long number) {
 	if (dpid == null) {
 	    throw new IllegalArgumentException("dpid cannot be null");
 	}
@@ -110,10 +110,14 @@
 	    throw new IllegalArgumentException("number cannot be null");
 	}
 	return ByteBuffer.allocate(PortEvent.PORTID_BYTES).putChar('S').putLong(dpid)
-		.putChar('P').putLong(number).array();
+		.putChar('P').putLong(number);
     }
 
     public byte[] getID() {
+	return getPortID(getDpid(), getNumber()).array();
+    }
+
+    public ByteBuffer getIDasByteBuffer() {
 	return getPortID(getDpid(), getNumber());
     }
 }
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchEvent.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchEvent.java
index ea5b66d..5aa7008 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchEvent.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchEvent.java
@@ -1,8 +1,6 @@
 package net.onrc.onos.ofcontroller.networkgraph;
 
 import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.List;
 
 /**
  * Self-contained Switch Object
@@ -11,7 +9,7 @@
  *
  */
 public class SwitchEvent {
-    private final Long dpid;
+    protected final Long dpid;
 
     /**
      * Default constructor.
@@ -35,15 +33,18 @@
 
     public static final int SWITCHID_BYTES = 2 + 8;
 
-    public static byte[] getSwitchID(Long dpid) {
+    public static ByteBuffer getSwitchID(Long dpid) {
 	if (dpid == null) {
 	    throw new IllegalArgumentException("dpid cannot be null");
 	}
-	return ByteBuffer.allocate(SwitchEvent.SWITCHID_BYTES).putChar('S').putLong(dpid)
-		.array();
+	return ByteBuffer.allocate(SwitchEvent.SWITCHID_BYTES).putChar('S').putLong(dpid);
     }
 
     public byte[] getID() {
+	return getSwitchID(dpid).array();
+    }
+
+    public ByteBuffer getIDasByteBuffer() {
 	return getSwitchID(dpid);
     }
 }
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchImpl.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchImpl.java
index 00f1236..500e6d2 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchImpl.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchImpl.java
@@ -10,11 +10,6 @@
 import java.util.Map;
 import java.util.Set;
 
-import net.onrc.onos.datastore.topology.RCPort;
-import net.onrc.onos.datastore.topology.RCSwitch;
-import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException;
-import edu.stanford.ramcloud.JRamCloud.WrongVersionException;
-
 /**
  * Switch Object stored in In-memory Topology.
  *
@@ -101,26 +96,6 @@
 		return port;
 	}
 
-	// XXX Do we still need this method?
-	public void store() {
-		RCSwitch rcSwitch = new RCSwitch(dpid);
-
-		for (Port port : ports.values()) {
-			RCPort rcPort = new RCPort(dpid, port.getNumber());
-			rcSwitch.addPortId(rcPort.getId());
-		}
-
-
-		try {
-			rcSwitch.update();
-
-		} catch (ObjectDoesntExistException | WrongVersionException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-
-	}
-
 	@Override
 	public Iterable<Link> getOutgoingLinks() {
 		LinkedList<Link> links = new LinkedList<Link>();
diff --git a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java
index aa1147e..c18f1cc 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/TopologyManager.java
@@ -213,25 +213,25 @@
 		case ENTRY_ADD:
 		    log.debug("Topology event ENTRY_ADD: {}", topologyEvent);
 		    if (switchEvent != null) {
-			ByteBuffer id = ByteBuffer.wrap(switchEvent.getID());
+			ByteBuffer id = switchEvent.getIDasByteBuffer();
 			addedSwitchEvents.put(id, switchEvent);
 			removedSwitchEvents.remove(id);
 			// Switch Events are not affected by event reordering
 		    }
 		    if (portEvent != null) {
-			ByteBuffer id = ByteBuffer.wrap(portEvent.getID());
+			ByteBuffer id = portEvent.getIDasByteBuffer();
 			addedPortEvents.put(id, portEvent);
 			removedPortEvents.remove(id);
 			reorderedAddedPortEvents.remove(id);
 		    }
 		    if (linkEvent != null) {
-			ByteBuffer id = ByteBuffer.wrap(linkEvent.getID());
+			ByteBuffer id = linkEvent.getIDasByteBuffer();
 			addedLinkEvents.put(id, linkEvent);
 			removedLinkEvents.remove(id);
 			reorderedAddedLinkEvents.remove(id);
 		    }
 		    if (deviceEvent != null) {
-			ByteBuffer id = ByteBuffer.wrap(deviceEvent.getID());
+			ByteBuffer id = deviceEvent.getIDasByteBuffer();
 			addedDeviceEvents.put(id, deviceEvent);
 			removedDeviceEvents.remove(id);
 			reorderedAddedDeviceEvents.remove(id);
@@ -240,25 +240,25 @@
 		case ENTRY_REMOVE:
 		    log.debug("Topology event ENTRY_REMOVE: {}", topologyEvent);
 		    if (switchEvent != null) {
-			ByteBuffer id = ByteBuffer.wrap(switchEvent.getID());
+			ByteBuffer id = switchEvent.getIDasByteBuffer();
 			addedSwitchEvents.remove(id);
 			removedSwitchEvents.put(id, switchEvent);
 			// Switch Events are not affected by event reordering
 		    }
 		    if (portEvent != null) {
-			ByteBuffer id = ByteBuffer.wrap(portEvent.getID());
+			ByteBuffer id = portEvent.getIDasByteBuffer();
 			addedPortEvents.remove(id);
 			removedPortEvents.put(id, portEvent);
 			reorderedAddedPortEvents.remove(id);
 		    }
 		    if (linkEvent != null) {
-			ByteBuffer id = ByteBuffer.wrap(linkEvent.getID());
+			ByteBuffer id = linkEvent.getIDasByteBuffer();
 			addedLinkEvents.remove(id);
 			removedLinkEvents.put(id, linkEvent);
 			reorderedAddedLinkEvents.remove(id);
 		    }
 		    if (deviceEvent != null) {
-			ByteBuffer id = ByteBuffer.wrap(deviceEvent.getID());
+			ByteBuffer id = deviceEvent.getIDasByteBuffer();
 			addedDeviceEvents.remove(id);
 			removedDeviceEvents.put(id, deviceEvent);
 			reorderedAddedDeviceEvents.remove(id);
@@ -510,7 +510,7 @@
 	    // Store the new Port Events in the local cache
 	    Map<ByteBuffer, PortEvent> newPortEvents = new HashMap<>();
 	    for (PortEvent portEvent : portEvents) {
-		ByteBuffer id = ByteBuffer.wrap(portEvent.getID());
+		ByteBuffer id = portEvent.getIDasByteBuffer();
 		newPortEvents.put(id, portEvent);
 	    }
 	    discoveredAddedPortEvents.put(switchEvent.getDpid(),
@@ -585,7 +585,7 @@
 		discoveredAddedPortEvents.put(portEvent.getDpid(),
 					      oldPortEvents);
 	    }
-	    ByteBuffer id = ByteBuffer.wrap(portEvent.getID());
+	    ByteBuffer id = portEvent.getIDasByteBuffer();
 	    oldPortEvents.put(id, portEvent);
 	}
     }
@@ -600,7 +600,7 @@
 	    Map<ByteBuffer, PortEvent> oldPortEvents =
 		discoveredAddedPortEvents.get(portEvent.getDpid());
 	    if (oldPortEvents != null) {
-		ByteBuffer id = ByteBuffer.wrap(portEvent.getID());
+		ByteBuffer id = portEvent.getIDasByteBuffer();
 		oldPortEvents.remove(id);
 	    }
 
@@ -656,7 +656,7 @@
 		discoveredAddedLinkEvents.put(linkEvent.getDst().getDpid(),
 					      oldLinkEvents);
 	    }
-	    ByteBuffer id = ByteBuffer.wrap(linkEvent.getID());
+	    ByteBuffer id = linkEvent.getIDasByteBuffer();
 	    oldLinkEvents.put(id, linkEvent);
 	}
     }
@@ -671,7 +671,7 @@
 	    Map<ByteBuffer, LinkEvent> oldLinkEvents =
 		discoveredAddedLinkEvents.get(linkEvent.getDst().getDpid());
 	    if (oldLinkEvents != null) {
-		ByteBuffer id = ByteBuffer.wrap(linkEvent.getID());
+		ByteBuffer id = linkEvent.getIDasByteBuffer();
 		oldLinkEvents.remove(id);
 	    }
 	}
@@ -694,7 +694,7 @@
 		    discoveredAddedDeviceEvents.put(swp.getDpid(),
 						    oldDeviceEvents);
 		}
-		ByteBuffer id = ByteBuffer.wrap(deviceEvent.getID());
+		ByteBuffer id = deviceEvent.getIDasByteBuffer();
 		oldDeviceEvents.put(id, deviceEvent);
 	    }
 	}
@@ -763,7 +763,7 @@
 	Switch sw = networkGraph.getSwitch(portEvent.getDpid());
 	if (sw == null) {
 	    // Reordered event: delay the event in local cache
-	    ByteBuffer id = ByteBuffer.wrap(portEvent.getID());
+	    ByteBuffer id = portEvent.getIDasByteBuffer();
 	    reorderedAddedPortEvents.put(id, portEvent);
 	    return;
 	}
@@ -842,7 +842,7 @@
 					    linkEvent.getDst().number);
 	if ((srcPort == null) || (dstPort == null)) {
 	    // Reordered event: delay the event in local cache
-	    ByteBuffer id = ByteBuffer.wrap(linkEvent.getID());
+	    ByteBuffer id = linkEvent.getIDasByteBuffer();
 	    reorderedAddedLinkEvents.put(id, linkEvent);
 	    return;
 	}
@@ -938,7 +938,7 @@
 	    Port port = networkGraph.getPort(swp.dpid, swp.number);
 	    if (port == null) {
 		// Reordered event: delay the event in local cache
-		ByteBuffer id = ByteBuffer.wrap(deviceEvent.getID());
+		ByteBuffer id = deviceEvent.getIDasByteBuffer();
 		reorderedAddedDeviceEvents.put(id, deviceEvent);
 		continue;
 	    }