#222
diff --git a/src/main/java/net/floodlightcontroller/bgproute/BgpRoute.java b/src/main/java/net/floodlightcontroller/bgproute/BgpRoute.java
new file mode 100644
index 0000000..2819253
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/bgproute/BgpRoute.java
@@ -0,0 +1,181 @@
+package net.floodlightcontroller.bgproute;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import net.floodlightcontroller.core.module.FloodlightModuleContext;
+import net.floodlightcontroller.core.module.FloodlightModuleException;
+import net.floodlightcontroller.core.module.IFloodlightModule;
+import net.floodlightcontroller.core.module.IFloodlightService;
+import net.floodlightcontroller.core.IFloodlightProviderService;
+
+import net.floodlightcontroller.linkdiscovery.ILinkDiscovery.LDUpdate;
+import net.floodlightcontroller.restserver.IRestApiService;
+import net.floodlightcontroller.topology.ITopologyListener;
+import net.floodlightcontroller.topology.ITopologyService;
+import net.floodlightcontroller.restclient.RestClient;
+
+import net.floodlightcontroller.linkdiscovery.ILinkDiscovery;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BgpRoute implements IFloodlightModule, IBgpRouteService, ITopologyListener {
+
+ protected static Logger log = LoggerFactory.getLogger(BgpRoute.class);
+
+ protected IFloodlightProviderService floodlightProvider;
+ protected ITopologyService topology;
+
+ protected static Ptree ptree;
+
+ @Override
+ public Collection<Class<? extends IFloodlightService>> getModuleServices() {
+ Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();
+ l.add(IBgpRouteService.class);
+ return l;
+ }
+
+ @Override
+ public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
+ Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
+ m.put(IBgpRouteService.class, this);
+ return m;
+ }
+
+ protected IRestApiService restApi;
+
+ @Override
+ public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
+ Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();
+ l.add(IFloodlightProviderService.class);
+ l.add(ITopologyService.class);
+ l.add(IBgpRouteService.class);
+ return l;
+ }
+
+ @Override
+ public void init(FloodlightModuleContext context)
+ throws FloodlightModuleException {
+
+ ptree = new Ptree(32);
+
+ // Register floodlight provider and REST handler.
+ floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
+ restApi = context.getServiceImpl(IRestApiService.class);
+ topology = context.getServiceImpl(ITopologyService.class);
+
+ // Test.
+ //test();
+ }
+
+ public Ptree getPtree() {
+ return ptree;
+ }
+
+ // Return nexthop address as byte array.
+ public Rib lookupRib(byte[] dest) {
+ if (ptree == null) {
+ log.debug("lookupRib: ptree null");
+ return null;
+ }
+
+ PtreeNode node = ptree.match(dest, 32);
+ if (node == null) {
+ log.debug("lookupRib: ptree node null");
+ return null;
+ }
+ if (node.rib == null) {
+ log.debug("lookupRib: ptree rib null");
+ return null;
+ }
+ ptree.delReference(node);
+
+ return node.rib;
+ }
+
+ @SuppressWarnings("unused")
+ private void test() {
+ System.out.println("Here it is");
+ Prefix p = new Prefix("128.0.0.0", 8);
+ Prefix q = new Prefix("8.0.0.0", 8);
+ Prefix r = new Prefix("10.0.0.0", 24);
+ Prefix a = new Prefix("10.0.0.1", 32);
+
+ ptree.acquire(p.getAddress(), p.masklen);
+ ptree.acquire(q.getAddress(), q.masklen);
+ ptree.acquire(r.getAddress(), r.masklen);
+
+ System.out.println("Traverse start");
+ for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
+ Prefix p_result = new Prefix(node.key, node.keyBits);
+ }
+
+ PtreeNode n = ptree.match(a.getAddress(), a.masklen);
+ if (n != null) {
+ System.out.println("Matched prefix for 10.0.0.1:");
+ Prefix x = new Prefix(n.key, n.keyBits);
+ ptree.delReference(n);
+ }
+
+ n = ptree.lookup(p.getAddress(), p.masklen);
+ if (n != null) {
+ ptree.delReference(n);
+ ptree.delReference(n);
+ }
+ System.out.println("Traverse start");
+ for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
+ Prefix p_result = new Prefix(node.key, node.keyBits);
+ }
+
+ n = ptree.lookup(q.getAddress(), q.masklen);
+ if (n != null) {
+ ptree.delReference(n);
+ ptree.delReference(n);
+ }
+ System.out.println("Traverse start");
+ for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
+ Prefix p_result = new Prefix(node.key, node.keyBits);
+ }
+
+ n = ptree.lookup(r.getAddress(), r.masklen);
+ if (n != null) {
+ ptree.delReference(n);
+ ptree.delReference(n);
+ }
+ System.out.println("Traverse start");
+ for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
+ Prefix p_result = new Prefix(node.key, node.keyBits);
+ }
+
+ }
+
+ @Override
+ public void startUp(FloodlightModuleContext context) {
+ restApi.addRestletRoutable(new BgpRouteWebRoutable());
+ topology.addListener((ITopologyListener) this);
+ }
+
+ @Override
+ public void topologyChanged() {
+ boolean change = false;
+ String changelog = "";
+
+ for (LDUpdate ldu : topology.getLastLinkUpdates()) {
+ if (ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.PORT_DOWN)) {
+ change = true;
+ changelog = changelog + " down ";
+ } else if (ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.PORT_UP)) {
+ change = true;
+ changelog = changelog + " up ";
+ }
+ }
+ log.info ("received topo change" + changelog);
+
+ if (change) {
+ RestClient.get ("http://localhost:5000/topo_change");
+ }
+ }
+}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResource.java b/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResource.java
new file mode 100644
index 0000000..d5abb5a
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResource.java
@@ -0,0 +1,139 @@
+package net.floodlightcontroller.bgproute;
+
+import org.restlet.resource.Get;
+import org.restlet.resource.Post;
+import org.restlet.resource.Delete;
+import org.restlet.resource.ServerResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import net.floodlightcontroller.restclient.RestClient;
+
+public class BgpRouteResource extends ServerResource {
+
+ protected static Logger log = LoggerFactory
+ .getLogger(BgpRouteResource.class);
+
+ private String addrToString(byte [] addr) {
+ String str = "";
+
+ for (int i = 0; i < 4; i++) {
+ int val = (addr[i] & 0xff);
+ str += val;
+ if (i != 3)
+ str += ".";
+ }
+
+ return str;
+ }
+
+ @SuppressWarnings("unused")
+ @Get
+ public String get(String fmJson) {
+ String dest = (String) getRequestAttributes().get("dest");
+ String output = "";
+ IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
+ get(IBgpRouteService.class.getCanonicalName());
+
+ if (dest != null) {
+ Prefix p = new Prefix(dest, 32);
+ if (p == null) {
+ return "[GET]: dest address format is wrong";
+ }
+ byte [] nexthop = bgpRoute.lookupRib(p.getAddress()).nextHop.getAddress();
+ if (nexthop != null) {
+ output += "{\"result\": \"" + addrToString(nexthop) + "\"}\n";
+ } else {
+ output += "{\"result\": \"Nexthop does not exist\"}\n";
+ }
+ } else {
+ Ptree ptree = bgpRoute.getPtree();
+ output += "{\n \"rib\": [\n";
+ boolean printed = false;
+ for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
+ if (node.rib == null) {
+ continue;
+ }
+ if (printed == true) {
+ output += ",\n";
+ }
+ output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", ";
+ output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}";
+ printed = true;
+ }
+ //output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n";
+ output += "\n ]\n}\n";
+ }
+
+ return output;
+ }
+ @Post
+ public String store(String fmJson) {
+ IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
+ get(IBgpRouteService.class.getCanonicalName());
+
+ Ptree ptree = bgpRoute.getPtree();
+
+ String router_id = (String) getRequestAttributes().get("routerid");
+ String prefix = (String) getRequestAttributes().get("prefix");
+ String mask = (String) getRequestAttributes().get("mask");
+ String nexthop = (String) getRequestAttributes().get("nexthop");
+ String capability = (String) getRequestAttributes().get("capability");
+ String reply = null;
+
+ if (capability == null) {
+ // this is a prefix add
+ Prefix p = new Prefix(prefix, Integer.valueOf(mask));
+ PtreeNode node = ptree.acquire(p.getAddress(), p.masklen);
+ Rib rib = new Rib(router_id, nexthop, p.masklen);
+
+ if (node.rib != null) {
+ node.rib = null;
+ ptree.delReference(node);
+ }
+ node.rib = rib;
+
+ reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
+ log.info(reply);
+
+ RestClient.get("http://localhost:5000/bgp_update");
+ }
+
+ return reply + "\n";
+ }
+
+ @Delete
+ public String delete(String fmJson) {
+ IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
+ get(IBgpRouteService.class.getCanonicalName());
+
+ Ptree ptree = bgpRoute.getPtree();
+
+ String routerId = (String) getRequestAttributes().get("routerid");
+ String prefix = (String) getRequestAttributes().get("prefix");
+ String mask = (String) getRequestAttributes().get("mask");
+ String nextHop = (String) getRequestAttributes().get("nexthop");
+ String capability = (String) getRequestAttributes().get("capability");
+ String reply = null;
+
+ if (capability == null) {
+ // this is a prefix delete
+ Prefix p = new Prefix(prefix, Integer.valueOf(mask));
+ PtreeNode node = ptree.lookup(p.getAddress(), p.masklen);
+ Rib r = new Rib(routerId, nextHop, p.masklen);
+
+ if (node != null && node.rib != null) {
+ if (r.equals(node.rib)) {
+ node.rib = null;
+ ptree.delReference(node);
+ }
+ }
+
+ reply = "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
+ log.info(reply);
+
+ RestClient.get("http://localhost:5000/bgp_update");
+ }
+
+ return reply + "\n";
+ }
+}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/BgpRouteWebRoutable.java b/src/main/java/net/floodlightcontroller/bgproute/BgpRouteWebRoutable.java
new file mode 100644
index 0000000..37d5696
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/bgproute/BgpRouteWebRoutable.java
@@ -0,0 +1,24 @@
+package net.floodlightcontroller.bgproute;
+
+import org.restlet.Context;
+import org.restlet.Restlet;
+import org.restlet.routing.Router;
+
+import net.floodlightcontroller.restserver.RestletRoutable;
+
+public class BgpRouteWebRoutable implements RestletRoutable {
+ @Override
+ public Restlet getRestlet(Context context) {
+ Router router = new Router(context);
+ router.attach("/json", BgpRouteResource.class);
+ router.attach("/rib/{dest}", BgpRouteResource.class);
+ router.attach("/{routerid}/{prefix}/{mask}/{nexthop}", BgpRouteResource.class);
+ router.attach("/{routerid}/{capability}", BgpRouteResource.class);
+ return router;
+ }
+
+ @Override
+ public String basePath() {
+ return "/wm/bgp";
+ }
+}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/IBgpRouteService.java b/src/main/java/net/floodlightcontroller/bgproute/IBgpRouteService.java
new file mode 100644
index 0000000..62bdf5e
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/bgproute/IBgpRouteService.java
@@ -0,0 +1,11 @@
+package net.floodlightcontroller.bgproute;
+
+import net.floodlightcontroller.core.module.IFloodlightService;
+
+public interface IBgpRouteService extends IFloodlightService {
+
+ public Rib lookupRib(byte[] dest);
+
+ public Ptree getPtree();
+
+}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/Prefix.java b/src/main/java/net/floodlightcontroller/bgproute/Prefix.java
new file mode 100644
index 0000000..7ba014b
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/bgproute/Prefix.java
@@ -0,0 +1,35 @@
+package net.floodlightcontroller.bgproute;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+public class Prefix {
+ public int masklen;
+ protected InetAddress address;
+
+ Prefix(byte[] addr, int masklen) {
+ try {
+ address = InetAddress.getByAddress(addr);
+ } catch (UnknownHostException e) {
+ System.out.println("InetAddress exception");
+ return;
+ }
+ this.masklen = masklen;
+ System.out.println(address.toString() + "/" + masklen);
+ }
+
+ Prefix(String str, int masklen) {
+ try {
+ address = InetAddress.getByName(str);
+ //System.out.println(address.toString());
+ } catch (UnknownHostException e) {
+ System.out.println("InetAddress exception");
+ return;
+ }
+ this.masklen = masklen;
+ }
+
+ public byte [] getAddress() {
+ return address.getAddress();
+ }
+}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/Ptree.java b/src/main/java/net/floodlightcontroller/bgproute/Ptree.java
new file mode 100644
index 0000000..d53789e
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/bgproute/Ptree.java
@@ -0,0 +1,316 @@
+package net.floodlightcontroller.bgproute;
+
+public class Ptree {
+ int maxKeyBits;
+ int maxKeyOctets;
+ int refCount;
+ PtreeNode top;
+ byte maskBits[] = { (byte)0x00, (byte)0x80, (byte)0xc0, (byte)0xe0, (byte)0xf0, (byte)0xf8, (byte)0xfc, (byte)0xfe, (byte)0xff };
+
+ // Constructor.
+ Ptree(int max_key_bits) {
+ maxKeyBits = max_key_bits;
+ maxKeyOctets = bit_to_octet(max_key_bits);
+ refCount = 0;
+ }
+
+ public PtreeNode acquire(byte [] key) {
+ return acquire(key, maxKeyBits);
+ }
+
+ public PtreeNode acquire(byte [] key, int key_bits) {
+ if (key_bits > maxKeyBits) {
+ return null;
+ }
+
+ PtreeNode node = top;
+ PtreeNode match = null;
+
+ while (node != null
+ && node.keyBits <= key_bits
+ && key_match(node.key, node.keyBits, key, key_bits) == true) {
+ if (node.keyBits == key_bits) {
+ return addReference(node);
+ }
+
+ match = node;
+
+ if (bit_check(key, node.keyBits) == true) {
+ node = node.right;
+ } else {
+ node = node.left;
+ }
+ }
+
+ PtreeNode add = null;
+
+ if (node == null) {
+ add = new PtreeNode(key, key_bits, maxKeyOctets);
+
+ if (match != null) {
+ node_link(match, add);
+ } else {
+ top = add;
+ }
+ } else {
+ add = node_common(node, key, key_bits);
+ if (add == null) {
+ return null;
+ }
+
+ if (match != null) {
+ node_link(match, add);
+ } else {
+ top = add;
+ }
+ node_link(add, node);
+
+ if (add.keyBits != key_bits) {
+ match = add;
+
+ add = new PtreeNode(key, key_bits, maxKeyOctets);
+ node_link(match, add);
+ }
+ }
+
+ return addReference(add);
+ }
+
+ public PtreeNode lookup(byte [] key, int key_bits) {
+ if (key_bits > maxKeyBits) {
+ return null;
+ }
+
+ PtreeNode node = top;
+
+ while (node != null
+ && node.keyBits <= key_bits
+ && key_match(node.key, node.keyBits, key, key_bits) == true) {
+ if (node.keyBits == key_bits) {
+ return addReference(node);
+ }
+
+ if (bit_check(key, node.keyBits) == true) {
+ node = node.right;
+ } else {
+ node = node.left;
+ }
+ }
+ return null;
+ }
+
+ public PtreeNode match(byte [] key, int key_bits) {
+ if (key_bits > maxKeyBits) {
+ return null;
+ }
+ PtreeNode node = top;
+ PtreeNode matched = null;
+
+ if(node!=null)
+
+ while (node != null
+ && node.keyBits <= key_bits
+ && key_match(node.key, node.keyBits, key, key_bits) == true) {
+ matched = node;
+
+ if (bit_check(key, node.keyBits) == true) {
+ node = node.right;
+ } else {
+ node = node.left;
+ }
+ }
+
+ if (matched != null) {
+ return addReference(matched);
+ }
+
+ return null;
+ }
+
+ public PtreeNode begin() {
+ if (top == null) {
+ return null;
+ }
+ return addReference(top);
+ }
+
+ public PtreeNode next(PtreeNode node) {
+ PtreeNode next;
+
+ if (node.left != null) {
+ next = node.left;
+ addReference(next);
+ delReference(node);
+ return next;
+ }
+ if (node.right != null) {
+ next = node.right;
+ addReference(next);
+ delReference(node);
+ return next;
+ }
+
+ PtreeNode start = node;
+ while (node.parent != null) {
+ if (node.parent.left == node && node.parent.right != null) {
+ next = node.parent.right;
+ addReference(next);
+ delReference(start);
+ return next;
+ }
+ node = node.parent;
+ }
+
+ delReference(start);
+
+ return null;
+ }
+
+ static public int bit_to_octet(int key_bits) {
+ return Math.max((key_bits + 7) / 8, 1);
+ }
+
+ private PtreeNode addReference(PtreeNode node) {
+ node.refCount++;
+ return node;
+ }
+
+ public void delReference(PtreeNode node) {
+ if (node.refCount > 0) {
+ node.refCount--;
+ }
+ if (node.refCount == 0) {
+ node_remove(node);
+ }
+ }
+
+ private boolean key_match(byte [] key1, int key1_len, byte [] key2, int key2_len) {
+ int offset;
+ int shift;
+
+ if (key1_len > key2_len) {
+ return false;
+ }
+
+ offset = (Math.min(key1_len, key2_len)) / 8;
+ shift = (Math.min(key1_len, key2_len)) % 8;
+
+ if (shift != 0) {
+ if ((maskBits[shift] & (key1[offset] ^ key2[offset])) != 0) {
+ return false;
+ }
+ }
+
+ while (offset != 0) {
+ offset--;
+ if (key1[offset] != key2[offset]) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private boolean bit_check(byte [] key, int key_bits) {
+ int offset = key_bits / 8;
+ int shift = 7 - (key_bits % 8);
+ int bit = key[offset] & 0xff;
+
+ bit >>= shift;
+
+ if ((bit & 1) == 1) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ private void node_link(PtreeNode node, PtreeNode add) {
+ boolean bit = bit_check(add.key, node.keyBits);
+
+ if (bit == true) {
+ node.right = add;
+ } else {
+ node.left = add;
+ }
+ add.parent = node;
+ }
+
+ @SuppressWarnings("unused")
+ private PtreeNode node_common(PtreeNode node, byte [] key, int key_bits) {
+ int i;
+ int limit = Math.min(node.keyBits, key_bits) / 8;
+
+ for (i = 0; i < limit; i++) {
+ if (node.key[i] != key[i]) {
+ break;
+ }
+ }
+
+ int common_len = i * 8;
+ int boundary = 0;
+
+ if (common_len != key_bits) {
+ byte diff = (byte)(node.key[i] ^ key[i]);
+ byte mask = (byte)0x80;
+ int shift_mask = 0;
+
+ while (common_len < key_bits && ((mask & diff) == 0)) {
+ boundary = 1;
+
+ shift_mask = (mask & 0xff);
+ shift_mask >>= 1;
+ mask = (byte)shift_mask;
+
+ common_len++;
+ }
+ }
+
+ PtreeNode add = new PtreeNode(null, common_len, maxKeyOctets);
+ if (add == null)
+ return null;
+
+ int j;
+ for (j = 0; j < i; j++)
+ add.key[j] = node.key[j];
+
+ if (boundary != 0)
+ add.key[j] = (byte)(node.key[j] & maskBits[add.keyBits % 8]);
+
+ return add;
+ }
+
+ private void node_remove(PtreeNode node) {
+ PtreeNode child;
+ PtreeNode parent;
+
+ if (node.left != null && node.right != null) {
+ return;
+ }
+
+ if (node.left != null) {
+ child = node.left;
+ } else {
+ child = node.right;
+ }
+
+ parent = node.parent;
+
+ if (child != null) {
+ child.parent = parent;
+ }
+
+ if (parent != null) {
+ if (parent.left == node) {
+ parent.left = child;
+ } else {
+ parent.right = child;
+ }
+ } else {
+ top = child;
+ }
+
+ if (parent != null && parent.refCount == 0) {
+ node_remove(parent);
+ }
+ }
+}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/PtreeNode.java b/src/main/java/net/floodlightcontroller/bgproute/PtreeNode.java
new file mode 100644
index 0000000..4d2c265
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/bgproute/PtreeNode.java
@@ -0,0 +1,44 @@
+package net.floodlightcontroller.bgproute;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class PtreeNode {
+ public PtreeNode parent;
+ public PtreeNode left;
+ public PtreeNode right;
+
+ public byte key[];
+ public int keyBits;
+
+ public int refCount;
+
+ public Rib rib;
+ protected static Logger log = LoggerFactory.getLogger(BgpRoute.class);
+
+ PtreeNode(byte [] key, int key_bits, int max_key_octet) {
+ parent = null;
+ left = null;
+ right = null;
+ refCount = 0;
+ rib = null;
+ this.key = new byte[max_key_octet];
+ this.keyBits = key_bits;
+ log.debug("inside Ptreenode constructor key {} bits {}", key, key_bits);
+
+ int octet = Ptree.bit_to_octet(key_bits);
+ for (int i = 0; i < max_key_octet; i++) {
+ if (i < octet) {
+ if (key != null) {
+ log.debug(octet + ": filling key[{}] {}", i, key[i]);
+ this.key[i] = key[i];
+ } else {
+ log.debug("no filling, null key", i);
+ }
+ } else {
+ log.debug("filling key {} as 0", i);
+ this.key[i] = 0;
+ }
+ }
+ }
+}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/Rib.java b/src/main/java/net/floodlightcontroller/bgproute/Rib.java
new file mode 100644
index 0000000..71868ff
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/bgproute/Rib.java
@@ -0,0 +1,45 @@
+package net.floodlightcontroller.bgproute;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+public class Rib {
+ protected InetAddress routerId;
+ protected InetAddress nextHop;
+ protected int masklen;
+// protected int distance;
+
+ Rib(InetAddress router_id, InetAddress nexthop, int masklen) {
+ this.routerId = router_id;
+ this.nextHop = nexthop;
+ this.masklen = masklen;
+// this.distance = distance;
+ }
+
+ Rib(String router_id, String nexthop, int masklen) {
+ try {
+ this.routerId = InetAddress.getByName(router_id);
+ } catch (UnknownHostException e) {
+ System.out.println("InetAddress exception");
+ }
+ try {
+ this.nextHop = InetAddress.getByName(nexthop);
+ } catch (UnknownHostException e) {
+ System.out.println("InetAddress exception");
+ }
+ this.masklen = masklen;
+ }
+
+ public InetAddress getNextHop() {
+ return nextHop;
+ }
+
+ public int getMasklen(){
+ return masklen;
+ }
+
+ public boolean equals(Rib r) {
+
+ return this.routerId == r.routerId && this.nextHop == r.nextHop && this.masklen == r.masklen;
+ }
+}
diff --git a/src/main/java/net/floodlightcontroller/restclient/RestClient.java b/src/main/java/net/floodlightcontroller/restclient/RestClient.java
new file mode 100644
index 0000000..07eab45
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/restclient/RestClient.java
@@ -0,0 +1,51 @@
+package net.floodlightcontroller.restclient;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class RestClient {
+
+ public static void get (String str) {
+
+ if (str == null)
+ return;
+
+ try {
+
+ URL url = new URL(str);
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setRequestMethod("GET");
+ conn.setRequestProperty("Accept", "application/json");
+
+ if (conn.getResponseCode() != 200) {
+ throw new RuntimeException("Failed : HTTP error code : "
+ + conn.getResponseCode());
+ }
+
+ /* Disable reading the output from the server for now
+ *
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ (conn.getInputStream())));
+
+ String output;
+ System.out.println("Output from Server .... \n");
+ while ((output = br.readLine()) != null) {
+ System.out.println(output);
+ }
+ */
+
+ conn.disconnect();
+
+ } catch (MalformedURLException e) {
+
+ e.printStackTrace();
+
+ } catch (IOException e) {
+
+ e.printStackTrace();
+
+ }
+ }
+}