Added getAll methods to each Objects
Change-Id: Icfd557b48ed048ad08a15029730c4d6e60a83353
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 8789492..08fe458 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCDevice.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCDevice.java
@@ -5,6 +5,7 @@
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;
@@ -14,6 +15,7 @@
import com.esotericsoftware.kryo.Kryo;
+import edu.stanford.ramcloud.JRamCloud;
import net.onrc.onos.datastore.RCObject;
import net.onrc.onos.datastore.RCTable;
import net.onrc.onos.datastore.topology.RCLink.STATUS;
@@ -73,8 +75,44 @@
this.isPortIdsModified = true;
}
- public static RCDevice createFromKey(byte[] key) {
- return new RCDevice(getMacFromKey(key));
+ /**
+ * Get an instance from Key.
+ *
+ * @note You need to call `read()` to get the DB content.
+ * @param key
+ * @return
+ */
+ public static <D extends RCObject> D createFromKey(byte[] key) {
+ @SuppressWarnings("unchecked")
+ D d = (D) new RCDevice(getMacFromKey(key));
+ return d;
+ }
+
+ public static Iterable<RCDevice> getAllDevices() {
+ return new DeviceEnumerator();
+ }
+
+ public static class DeviceEnumerator implements Iterable<RCDevice> {
+
+ @Override
+ public Iterator<RCDevice> iterator() {
+ return new DeviceIterator();
+ }
+ }
+
+ public static class DeviceIterator extends ObjectIterator<RCDevice> {
+
+ public DeviceIterator() {
+ super(RCTable.getTable(GLOBAL_DEVICE_TABLE_NAME));
+ }
+
+ @Override
+ public RCDevice next() {
+ JRamCloud.Object o = enumerator.next();
+ RCDevice e = RCDevice.createFromKey(o.key);
+ e.setValueAndDeserialize(o.value, o.version);
+ return e;
+ }
}
public byte[] getMac() {
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 dfc4307..eec6a10 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCLink.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCLink.java
@@ -2,6 +2,7 @@
import java.nio.ByteBuffer;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.Map;
import org.slf4j.Logger;
@@ -9,6 +10,7 @@
import com.esotericsoftware.kryo.Kryo;
+import edu.stanford.ramcloud.JRamCloud;
import net.onrc.onos.datastore.RCObject;
import net.onrc.onos.datastore.RCTable;
@@ -45,7 +47,7 @@
}
public byte[] getSwitchID() {
- return RCSwitch.getSwichID(dpid);
+ return RCSwitch.getSwitchID(dpid);
}
@Override
@@ -113,10 +115,46 @@
status = STATUS.INACTIVE;
}
- public static RCLink createFromKey(byte[] key) {
+ /**
+ * Get an instance from Key.
+ *
+ * @note You need to call `read()` to get the DB content.
+ * @param key
+ * @return RCLink instance
+ */
+ public static <L extends RCObject> L createFromKey(byte[] key) {
long linkTuple[] = getLinkTupleFromKey(key);
- return new RCLink(linkTuple[0], linkTuple[1], linkTuple[2],
+ @SuppressWarnings("unchecked")
+ L l = (L) new RCLink(linkTuple[0], linkTuple[1], linkTuple[2],
linkTuple[3]);
+ return l;
+ }
+
+ public static Iterable<RCLink> getAllLinks() {
+ return new LinkEnumerator();
+ }
+
+ public static class LinkEnumerator implements Iterable<RCLink> {
+
+ @Override
+ public Iterator<RCLink> iterator() {
+ return new LinkIterator();
+ }
+ }
+
+ public static class LinkIterator extends ObjectIterator<RCLink> {
+
+ public LinkIterator() {
+ super(RCTable.getTable(GLOBAL_LINK_TABLE_NAME));
+ }
+
+ @Override
+ public RCLink next() {
+ JRamCloud.Object o = enumerator.next();
+ RCLink e = RCLink.createFromKey(o.key);
+ e.setValueAndDeserialize(o.value, o.version);
+ return e;
+ }
}
public STATUS getStatus() {
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 5a13e3e..894e2d2 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCPort.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCPort.java
@@ -5,6 +5,7 @@
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;
@@ -14,6 +15,7 @@
import com.esotericsoftware.kryo.Kryo;
+import edu.stanford.ramcloud.JRamCloud;
import net.onrc.onos.datastore.RCObject;
import net.onrc.onos.datastore.RCTable;
import net.onrc.onos.datastore.utils.ByteArrayComparator;
@@ -122,9 +124,45 @@
this.isDeviceIdsModified = true;
}
- public static RCPort createFromKey(byte[] key) {
+ /**
+ * Get an instance from Key.
+ *
+ * @note You need to call `read()` to get the DB content.
+ * @param key
+ * @return RCPort instance
+ */
+ public static <P extends RCObject> P createFromKey(byte[] key) {
long[] pair = getPortPairFromKey(key);
- return new RCPort(pair[0], pair[1]);
+ @SuppressWarnings("unchecked")
+ P p = (P) new RCPort(pair[0], pair[1]);
+ return p;
+ }
+
+ public static Iterable<RCPort> getAllPorts() {
+ return new PortEnumerator();
+ }
+
+ public static class PortEnumerator implements Iterable<RCPort> {
+
+ @Override
+ public Iterator<RCPort> iterator() {
+ return new PortIterator();
+ }
+ }
+
+ public static class PortIterator extends ObjectIterator<RCPort> {
+
+ public PortIterator() {
+ super(RCTable.getTable(GLOBAL_PORT_TABLE_NAME));
+ }
+
+ @Override
+ public RCPort next() {
+ JRamCloud.Object o = enumerator.next();
+ RCPort e = RCPort.createFromKey(o.key);
+ e.setValueAndDeserialize(o.value, o.version);
+ return e;
+ }
}
public STATUS getStatus() {
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 175209d..aee9c7e 100644
--- a/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
+++ b/src/main/java/net/onrc/onos/datastore/topology/RCSwitch.java
@@ -4,6 +4,7 @@
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;
@@ -18,6 +19,7 @@
import com.esotericsoftware.kryo.Kryo;
+import edu.stanford.ramcloud.JRamCloud;
import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException;
import edu.stanford.ramcloud.JRamCloud.ObjectExistsException;
import edu.stanford.ramcloud.JRamCloud.WrongVersionException;
@@ -66,7 +68,7 @@
public static final int SWITCHID_BYTES = 2 + 8;
- public static byte[] getSwichID(Long dpid) {
+ public static byte[] getSwitchID(Long dpid) {
if (dpid == null) {
throw new IllegalArgumentException("dpid cannot be null");
}
@@ -88,7 +90,7 @@
// FIXME specify DPID here, or Should caller specify the key it self?
// In other words, should layer above have the control of the ID?
public RCSwitch(Long dpid) {
- super(RCTable.getTable(GLOBAL_SWITCH_TABLE_NAME), getSwichID(dpid));
+ super(RCTable.getTable(GLOBAL_SWITCH_TABLE_NAME), getSwitchID(dpid));
this.dpid = dpid;
this.status = STATUS.INACTIVE;
@@ -96,8 +98,44 @@
this.isPortIdsModified = true;
}
- public static RCSwitch createFromKey(byte[] key) {
- return new RCSwitch(getDpidFromKey(key));
+ /**
+ * Get an instance from Key.
+ *
+ * @note You need to call `read()` to get the DB content.
+ * @param key
+ * @return RCSwitch instance
+ */
+ public static <SW extends RCObject> SW createFromKey(byte[] key) {
+ @SuppressWarnings("unchecked")
+ SW sw = (SW) new RCSwitch(getDpidFromKey(key));
+ return sw;
+ }
+
+ public static Iterable<RCSwitch> getAllSwitches() {
+ return new SwitchEnumerator();
+ }
+
+ public static class SwitchEnumerator implements Iterable<RCSwitch> {
+
+ @Override
+ public Iterator<RCSwitch> iterator() {
+ return new SwitchIterator();
+ }
+ }
+
+ public static class SwitchIterator extends ObjectIterator<RCSwitch> {
+
+ public SwitchIterator() {
+ super(RCTable.getTable(GLOBAL_SWITCH_TABLE_NAME));
+ }
+
+ @Override
+ public RCSwitch next() {
+ JRamCloud.Object o = enumerator.next();
+ RCSwitch e = RCSwitch.createFromKey(o.key);
+ e.setValueAndDeserialize(o.value, o.version);
+ return e;
+ }
}
public STATUS getStatus() {
@@ -255,6 +293,7 @@
topology_delete();
}
+ @Deprecated
private static void topology_setup() {
log.debug("topology_setup start.");
@@ -361,9 +400,17 @@
log.debug("topology_setup end.");
}
+ @Deprecated
private static void topology_walk() {
log.debug("topology_walk start.");
+ Iterable<RCSwitch> swIt = RCSwitch.getAllSwitches();
+ log.debug("Enumerating Switches start");
+ for (RCSwitch sw : swIt) {
+ log.debug("{}", sw);
+ }
+ log.debug("Enumerating Switches end");
+
RCSwitch sw1 = new RCSwitch(0x1L);
try {
sw1.read();
@@ -459,6 +506,7 @@
log.debug("topology_walk end.");
}
+ @Deprecated
private static void topology_delete() {
log.debug("topology_delete start.");