Modified BgpRoute to use new PATRICIA Trie
diff --git a/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRoute.java b/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRoute.java
index f4ff6b1..9123792 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRoute.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRoute.java
@@ -88,7 +88,8 @@
protected ProxyArpManager proxyArp;
- protected static Ptree ptree;
+ //protected static Ptree ptree;
+ protected IPatriciaTrie ptree;
protected BlockingQueue<RibUpdate> ribUpdates;
protected String bgpdRestIp;
@@ -250,7 +251,8 @@
public void init(FloodlightModuleContext context)
throws FloodlightModuleException {
- ptree = new Ptree(32);
+ //ptree = new Ptree(32);
+ ptree = new PatriciaTrie(32);
ribUpdates = new LinkedBlockingQueue<RibUpdate>();
@@ -307,13 +309,15 @@
//test();
}
- public Ptree getPtree() {
+ //public Ptree getPtree() {
+ public IPatriciaTrie getPtree() {
return ptree;
}
public void clearPtree() {
//ptree = null;
- ptree = new Ptree(32);
+ //ptree = new Ptree(32);
+ ptree = new PatriciaTrie(32);
}
public String getBGPdRestIp() {
@@ -325,6 +329,7 @@
}
// Return nexthop address as byte array.
+ /*
public Rib lookupRib(byte[] dest) {
if (ptree == null) {
log.debug("lookupRib: ptree null");
@@ -346,7 +351,9 @@
return node.rib;
}
+ */
+ /*
//TODO looks like this should be a unit test
@SuppressWarnings("unused")
private void test() throws UnknownHostException {
@@ -403,6 +410,7 @@
}
}
+ */
//TODO once the Ptree is object oriented this can go
private String getPrefixFromPtree(PtreeNode node){
@@ -455,15 +463,19 @@
continue;
}
- PtreeNode node = ptree.acquire(p.getAddress(), p.getPrefixLength());
+ //PtreeNode node = ptree.acquire(p.getAddress(), p.getPrefixLength());
Rib rib = new Rib(router_id, nexthop, p.getPrefixLength());
+ /*
if (node.rib != null) {
node.rib = null;
ptree.delReference(node);
}
node.rib = rib;
+ */
+
+ ptree.put(p, rib);
addPrefixFlows(p, rib);
}
@@ -477,20 +489,22 @@
public void processRibAdd(RibUpdate update) {
Prefix prefix = update.getPrefix();
- PtreeNode node = ptree.acquire(prefix.getAddress(), prefix.getPrefixLength());
+ //PtreeNode node = ptree.acquire(prefix.getAddress(), prefix.getPrefixLength());
+ Rib rib = ptree.put(prefix, update.getRibEntry());
- if (node.rib != null) {
+ //if (node.rib != null) {
+ if (rib != null) {
//There was an existing nexthop for this prefix. This update supersedes that,
//so we need to remove the old flows for this prefix from the switches
deletePrefixFlows(prefix);
//Then remove the old nexthop from the Ptree
- node.rib = null;
- ptree.delReference(node);
+ //node.rib = null;
+ //ptree.delReference(node);
}
//Put the new nexthop in the Ptree
- node.rib = update.getRibEntry();
+ //node.rib = update.getRibEntry();
//Push flows for the new <prefix, nexthop>
addPrefixFlows(prefix, update.getRibEntry());
@@ -499,7 +513,7 @@
public void processRibDelete(RibUpdate update) {
Prefix prefix = update.getPrefix();
- PtreeNode node = ptree.lookup(prefix.getAddress(), prefix.getPrefixLength());
+ //PtreeNode node = ptree.lookup(prefix.getAddress(), prefix.getPrefixLength());
/*
* Remove the flows from the switches before the rib is lost
@@ -510,6 +524,7 @@
* rib is an actual prefix in the Ptree.
*/
+ /*
if (node != null && node.rib != null) {
if (update.getRibEntry().equals(node.rib)) {
node.rib = null;
@@ -518,10 +533,20 @@
deletePrefixFlows(update.getPrefix());
}
}
+ */
+
+ if (ptree.remove(prefix, update.getRibEntry())) {
+ /*
+ * Only delete flows if an entry was actually removed from the trie.
+ * If no entry was removed, the <prefix, nexthop> wasn't there so
+ * it's probably already been removed and we don't need to do anything
+ */
+ deletePrefixFlows(prefix);
+ }
}
//TODO compatibility layer, used by beginRouting()
- public void prefixAdded(PtreeNode node) {
+ /*public void prefixAdded(PtreeNode node) {
Prefix prefix = null;
try {
prefix = new Prefix(node.key, node.rib.masklen);
@@ -530,7 +555,7 @@
}
addPrefixFlows(prefix, node.rib);
- }
+ }*/
private void addPrefixFlows(Prefix prefix, Rib rib) {
if (!topologyReady){
@@ -980,11 +1005,22 @@
setupFullMesh();
//Traverse ptree and create flows for all routes
+ /*
for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)){
if (node.rib != null){
prefixAdded(node);
}
}
+ */
+
+ synchronized (ptree) {
+ Iterator<IPatriciaTrie.Entry> it = ptree.iterator();
+ while (it.hasNext()) {
+ IPatriciaTrie.Entry entry = it.next();
+ addPrefixFlows(entry.getPrefix(), entry.getRib());
+ }
+ }
+
}
private void checkSwitchesConnected(){
diff --git a/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRouteResource.java b/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRouteResource.java
index 39a14cd..2c9d284 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRouteResource.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/bgproute/BgpRouteResource.java
@@ -1,6 +1,6 @@
package net.onrc.onos.ofcontroller.bgproute;
-import java.net.UnknownHostException;
+import java.util.Iterator;
import net.onrc.onos.ofcontroller.bgproute.RibUpdate.Operation;
@@ -59,11 +59,13 @@
return output;
}
else {
- Ptree ptree = bgpRoute.getPtree();
+ //Ptree ptree = bgpRoute.getPtree();
+ IPatriciaTrie ptree = bgpRoute.getPtree();
output += "{\n \"rib\": [\n";
boolean printed = false;
- for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
+ /*
+ for (PtreeNode node = ptree.begin(); node!= null; node = ptree.next(node)) {
if (node.rib == null) {
continue;
}
@@ -73,7 +75,25 @@
output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", ";
output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}";
printed = true;
+ }*/
+
+ synchronized(ptree) {
+ Iterator<IPatriciaTrie.Entry> it = ptree.iterator();
+ while (it.hasNext()) {
+ IPatriciaTrie.Entry entry = it.next();
+
+ if (printed == true) {
+ output += ",\n";
+ }
+
+ output += " {\"prefix\": \"" + entry.getPrefix() +"\", ";
+ output += "\"nexthop\": \"" + entry.getRib().getNextHop().getHostAddress() +"\"}";
+ //output += ",\n";
+
+ printed = true;
+ }
}
+
//output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n";
output += "\n ]\n}\n";
}
diff --git a/src/main/java/net/onrc/onos/ofcontroller/bgproute/IBgpRouteService.java b/src/main/java/net/onrc/onos/ofcontroller/bgproute/IBgpRouteService.java
index d865e6e..b355c4b 100644
--- a/src/main/java/net/onrc/onos/ofcontroller/bgproute/IBgpRouteService.java
+++ b/src/main/java/net/onrc/onos/ofcontroller/bgproute/IBgpRouteService.java
@@ -4,9 +4,10 @@
public interface IBgpRouteService extends IFloodlightService {
- public Rib lookupRib(byte[] dest);
+ //public Rib lookupRib(byte[] dest);
- public Ptree getPtree();
+ //public Ptree getPtree();
+ public IPatriciaTrie getPtree();
public String getBGPdRestIp();