ndn: Add NFD and NLSR class abstractions
refs: #2528
Change-Id: I3241af0e85f4eec4eb640aca274ff9ca4f812656
diff --git a/ndn/nlsr.py b/ndn/nlsr.py
new file mode 100644
index 0000000..de9ec86
--- /dev/null
+++ b/ndn/nlsr.py
@@ -0,0 +1,154 @@
+#!/usr/bin/env python
+
+class Nlsr:
+ def __init__(self, node):
+ self.node = node
+ self.routerName = "/%sC1.Router/cs/%s" % ('%', node.name)
+ self.confFile = "/tmp/%s/nlsr.conf" % node.name
+
+ # Make directory for log file
+ self.logDir = "/tmp/%s/log" % node.name
+ node.cmd("mkdir %s" % self.logDir)
+
+ # Configure basic router information in nlsr.conf based on host name
+ node.cmd("sudo sed -i 's|router .*|router %s|g' %s" % (self.routerName, self.confFile))
+ node.cmd("sudo sed -i 's|log-dir .*|log-dir %s|g' %s" % (self.logDir, self.confFile))
+ node.cmd("sudo sed -i 's|seq-dir .*|seq-dir %s|g' %s" % (self.logDir, self.confFile))
+ node.cmd("sudo sed -i 's|prefix .*netlab|prefix /ndn/edu/%s|g' %s" % (node.name, self.confFile))
+
+ def start(self):
+ self.node.cmd("nlsr -d")
+
+
+class NlsrConfigGenerator:
+
+ ROUTING_LINK_STATE = "ls"
+ ROUTING_HYPERBOLIC = "hr"
+
+ def __init__(self, node, home):
+ node.cmd("sudo cp %s/mn-ndn/ndn_utils/nlsr.conf nlsr.conf" % home)
+ self.node = node
+
+ parameters = node.nlsrParameters
+
+ self.nFaces = parameters.get("max-faces-per-prefix", 3)
+ self.hyperbolicState = parameters.get("hyperbolic-state", "off")
+ self.hyperRadius = parameters.get("radius", 0.0)
+ self.hyperAngle = parameters.get("angle", 0.0)
+
+ def createConfigFile(self):
+
+ filePath = "/tmp/%s/nlsr.conf" % self.node.name
+
+ configFile = open(filePath, 'r')
+ oldContent = configFile.read()
+ configFile.close()
+
+ newContent = oldContent.replace("$GENERAL_SECTION", self.__getGeneralSection())
+ newContent = newContent.replace("$NEIGHBORS_SECTION", self.__getNeighborsSection())
+ newContent = newContent.replace("$HYPERBOLIC_SECTION", self.__getHyperbolicSection())
+ newContent = newContent.replace("$FIB_SECTION", self.__getFibSection())
+ newContent = newContent.replace("$ADVERTISING_SECTION", self.__getAdvertisingSection())
+
+ configFile = open(filePath, 'w')
+ configFile.write(newContent)
+ configFile.close()
+
+ def __getConfig(self):
+
+ config = self.__getGeneralSection()
+ config += self.__getNeighborsSection()
+ config += self.__getHyperbolicSection()
+ config += self.__getFibSection()
+ config += self.__getAdvertisingSection()
+ config += self.__getSecuritySection()
+
+ return config
+
+ def __getGeneralSection(self):
+
+ general = "general\n"
+ general += "{\n"
+ general += " network /ndn/\n"
+ general += " site /edu\n"
+ general += " router /%C1.Router/cs/" + self.node.name + "\n"
+ general += " log-level DEBUG\n"
+ general += " log-dir /tmp/" + self.node.name + "/log\n"
+ general += " seq-dir /tmp/" + self.node.name + "/log\n"
+ general += "}\n"
+
+ return general
+
+ def __getNeighborsSection(self):
+
+ neighbors = "neighbors\n"
+ neighbors += "{\n"
+
+ for intf in self.node.intfList():
+ link = intf.link
+ if link:
+ node1, node2 = link.intf1.node, link.intf2.node
+
+ if node1 == self.node:
+ other = node2
+ ip = other.IP(str(link.intf2))
+ else:
+ other = node1
+ ip = other.IP(str(link.intf1))
+
+ linkCost = intf.params.get("delay", "0ms").replace("ms", "")
+
+ neighbors += "neighbor\n"
+ neighbors += "{\n"
+ neighbors += " name /ndn/edu/%C1.Router/cs/" + other.name + "\n"
+ neighbors += " face-uri udp://" + str(ip) + "\n"
+ neighbors += " link-cost " + linkCost + "\n"
+ neighbors += "}\n"
+
+ neighbors += "}\n"
+
+ return neighbors
+
+ def __getHyperbolicSection(self):
+
+ hyper = "hyperbolic\n"
+ hyper += "{\n"
+ hyper += "state %s\n" % self.hyperbolicState
+ hyper += "radius " + str(self.hyperRadius) + "\n"
+ hyper += "angle " + str(self.hyperAngle) + "\n"
+ hyper += "}\n"
+
+ return hyper
+
+ def __getFibSection(self):
+
+ fib = "fib\n"
+ fib += "{\n"
+ fib += " max-faces-per-prefix " + str(self.nFaces) + "\n"
+ fib += "}\n"
+
+ return fib
+
+ def __getAdvertisingSection(self):
+
+ advertising = "advertising\n"
+ advertising += "{\n"
+ advertising += " prefix /ndn/edu/" + self.node.name + "\n"
+ advertising += "}\n"
+
+ return advertising
+
+ def __getSecuritySection(self):
+
+ security = "security\n"
+ security += "{\n"
+ security += " validator\n"
+ security += " {\n"
+ security += " trust-anchor\n"
+ security += " {\n"
+ security += " type any\n"
+ security += " }\n"
+ security += " }\n"
+ security += "}\n"
+
+ return security