Make ByteBuffer wrapped ID available.
We often need ByteBuffer wrapped byte[] to use as a Map key.
So expose ByteBuffer used to build ID.
Change-Id: Ic4b73a779faacf262b44735d21111b279ac2467e
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 fe4eb94..e3edff0 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCLink.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCLink.java
@@ -77,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) {
@@ -205,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();
}
@@ -231,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 3493d8f..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,7 +1,6 @@
package net.onrc.onos.datastore.topology;
import java.nio.ByteBuffer;
-import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
@@ -52,7 +51,7 @@
private STATUS status;
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) {
@@ -181,19 +180,19 @@
}
@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();
}
@@ -214,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 c993902..19b9801 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
@@ -60,7 +60,7 @@
private STATUS status;
public static byte[] getSwitchID(Long dpid) {
- return SwitchEvent.getSwitchID(dpid);
+ return SwitchEvent.getSwitchID(dpid).array();
}
public static StringBuilder keysToSB(Collection<byte[]> keys) {
@@ -166,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();
}
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..361c298 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/LinkEvent.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/LinkEvent.java
@@ -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..0a25c47 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchEvent.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/networkgraph/SwitchEvent.java
@@ -35,15 +35,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/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;
}