WIP: adding utility methods
Change-Id: I078a33e67e4b424dd2f1c4eff362550fb3be3424
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 16ff01b..de9d5a4 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
@@ -75,7 +75,10 @@
}
public static long getDpidFromKey(byte[] key) {
- ByteBuffer keyBuf = ByteBuffer.wrap(key);
+ return getDpidFromKey(ByteBuffer.wrap(key));
+ }
+
+ public static long getDpidFromKey(ByteBuffer keyBuf) {
if (keyBuf.getChar() != 'S') {
throw new IllegalArgumentException("Invalid Switch key");
}
@@ -255,10 +258,13 @@
private static void topology_setup() {
log.debug("topology_setup start.");
+ // d1 - s1p1 - s1 - s1p2 - s2p1 - s2 - s2p2
+
RCSwitch sw1 = new RCSwitch(0x1L);
sw1.setStatus(STATUS.ACTIVE);
try {
sw1.create();
+ log.debug("Create {}", sw1);
} catch (ObjectExistsException e) {
log.error("Switch creation failed", e);
}
@@ -269,7 +275,9 @@
sw1p2.setStatus(RCPort.STATUS.ACTIVE);
try {
sw1p1.create();
+ log.debug("Create {}", sw1p1);
sw1p2.create();
+ log.debug("Create {}", sw1p2);
} catch (ObjectExistsException e) {
log.error("Port creation failed", e);
}
@@ -279,14 +287,24 @@
sw1.addPortId(sw1p2.getId());
try {
sw1.update();
+ log.debug("Update {} - {}", sw1, sw1.getAllPortIds());
} 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();
+ log.debug("Create {}", d1);
+ try {
+ sw1p1.update();
+ } catch (ObjectDoesntExistException | WrongVersionException e) {
+ log.error("Link update failed", e);
+ }
+ log.debug("Create {}", sw1p1);
} catch (ObjectExistsException e) {
log.error("Device creation failed", e);
}
@@ -305,31 +323,51 @@
RCDevice d2 = new RCDevice(new byte[] { 6, 5, 4, 3, 2, 1, 0 });
d2.addPortId(sw2p2.getId());
+ sw2p2.addDeviceId(d2.getId());
try {
sw2.create();
+ log.debug("Create {}", sw2);
sw2p1.create();
+ log.debug("Create {}", sw2p1);
sw2p2.create();
+ log.debug("Create {}", sw2p2);
d2.create();
+ log.debug("Create {}", d2);
} catch (ObjectExistsException e) {
log.error("One of Switch/Port/Device creation failed", e);
}
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);
+ try {
+ sw1p2.update();
+ log.debug("Update {}", sw1p2);
+ sw2p1.update();
+ log.debug("Update {}", sw2p1);
+ } catch (ObjectDoesntExistException | WrongVersionException e) {
+ log.error("Port update failed", e);
+ }
} catch (ObjectExistsException e) {
log.error("Link creation failed", e);
}
+
log.debug("topology_setup end.");
}
private static void topology_walk() {
log.debug("topology_walk start.");
+
RCSwitch sw1 = new RCSwitch(0x1L);
try {
sw1.read();
+ log.debug("{}", sw1);
} catch (ObjectDoesntExistException e) {
log.error("Reading switch failed", e);
}
@@ -338,6 +376,84 @@
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("Port 0x1:{} - LinkIDs:{} DeviceIDs:{}",
+ port.getNumber(), port.getAllLinkIds(),
+ port.getAllDeviceIds());
+
+ for (byte[] deviceId : port.getAllDeviceIds()) {
+ RCDevice device = RCDevice.createFromKey(deviceId);
+ try {
+ device.read();
+ log.debug("Device {} - PortIDs:{}", device.getMac(),
+ 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);
+ }
+ }
+
+ RCSwitch sw2 = new RCSwitch(0x1L);
+ try {
+ sw2.read();
+ log.debug("{}", sw2);
+ } catch (ObjectDoesntExistException e) {
+ log.error("Reading switch failed", e);
+ }
+
+ 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() == 0x1L);
+ log.debug("Port 0x2:{} - LinkIDs:{} DeviceIDs:{}",
+ port.getNumber(), port.getAllLinkIds(),
+ port.getAllDeviceIds());
+
+ for (byte[] deviceId : port.getAllDeviceIds()) {
+ RCDevice device = RCDevice.createFromKey(deviceId);
+ try {
+ device.read();
+ log.debug("Device {} - PortIDs:{}", device,
+ 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);
+ }
}
log.debug("topology_walk end.");
@@ -345,6 +461,75 @@
private static void topology_delete() {
log.debug("topology_delete start.");
+
+ // TODO implement get all kind of API
+ RCSwitch sw1 = new RCSwitch(0x1L);
+ RCPort sw1p1 = new RCPort(0x1L, 1L);
+ RCPort sw1p2 = new RCPort(0x1L, 2L);
+ RCDevice d1 = new RCDevice(new byte[] { 0, 1, 2, 3, 4, 5, 6 });
+ RCLink l1 = new RCLink(0x1L, 2L, 0x2L, 1L);
+ RCSwitch sw2 = new RCSwitch(0x2L);
+ RCPort sw2p1 = new RCPort(0x2L, 1L);
+ RCPort sw2p2 = new RCPort(0x2L, 2L);
+ RCDevice d2 = new RCDevice(new byte[] { 6, 5, 4, 3, 2, 1, 0 });
+
+ try {
+ sw1.read();
+ sw1.delete();
+ } catch (ObjectDoesntExistException e) {
+ log.debug("Delete Switch Failed", e);
+ }
+ try {
+ sw1p1.read();
+ sw1p1.delete();
+ } catch (ObjectDoesntExistException e) {
+ log.debug("Delete Port Failed", e);
+ }
+ try {
+ sw1p2.read();
+ sw1p2.delete();
+ } catch (ObjectDoesntExistException e) {
+ log.debug("Delete Port Failed", e);
+ }
+ try {
+ d1.read();
+ d1.delete();
+ } catch (ObjectDoesntExistException e) {
+ log.debug("Delete Device Failed", e);
+ }
+
+ try {
+ l1.read();
+ l1.delete();
+ } catch (ObjectDoesntExistException e) {
+ log.debug("Delete Link Failed", e);
+ }
+
+ try {
+ sw2.read();
+ sw2.delete();
+ } catch (ObjectDoesntExistException e) {
+ log.debug("Delete Switch Failed", e);
+ }
+ try {
+ sw2p1.read();
+ sw2p1.delete();
+ } catch (ObjectDoesntExistException e) {
+ log.debug("Delete Port Failed", e);
+ }
+ try {
+ sw2p2.read();
+ sw2p2.delete();
+ } catch (ObjectDoesntExistException e) {
+ log.debug("Delete Port Failed", e);
+ }
+ try {
+ d2.read();
+ d2.delete();
+ } catch (ObjectDoesntExistException e) {
+ log.debug("Delete Device Failed", e);
+ }
+
log.debug("topology_delete end.");
}