ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame^] | 2 | from ndn.ndn_application import NdnApplication |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 3 | |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame^] | 4 | class Nlsr(NdnApplication): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 5 | def __init__(self, node): |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame^] | 6 | NdnApplication.__init__(self, node) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 7 | self.routerName = "/%sC1.Router/cs/%s" % ('%', node.name) |
| 8 | self.confFile = "/tmp/%s/nlsr.conf" % node.name |
| 9 | |
| 10 | # Make directory for log file |
| 11 | self.logDir = "/tmp/%s/log" % node.name |
| 12 | node.cmd("mkdir %s" % self.logDir) |
| 13 | |
| 14 | # Configure basic router information in nlsr.conf based on host name |
| 15 | node.cmd("sudo sed -i 's|router .*|router %s|g' %s" % (self.routerName, self.confFile)) |
| 16 | node.cmd("sudo sed -i 's|log-dir .*|log-dir %s|g' %s" % (self.logDir, self.confFile)) |
| 17 | node.cmd("sudo sed -i 's|seq-dir .*|seq-dir %s|g' %s" % (self.logDir, self.confFile)) |
| 18 | node.cmd("sudo sed -i 's|prefix .*netlab|prefix /ndn/edu/%s|g' %s" % (node.name, self.confFile)) |
| 19 | |
| 20 | def start(self): |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame^] | 21 | NdnApplication.start(self, "nlsr -d") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 22 | |
| 23 | class NlsrConfigGenerator: |
| 24 | |
| 25 | ROUTING_LINK_STATE = "ls" |
| 26 | ROUTING_HYPERBOLIC = "hr" |
| 27 | |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 28 | def __init__(self, node): |
| 29 | node.cmd("sudo cp /usr/local/etc/mini-ndn/nlsr.conf nlsr.conf") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 30 | self.node = node |
| 31 | |
| 32 | parameters = node.nlsrParameters |
| 33 | |
| 34 | self.nFaces = parameters.get("max-faces-per-prefix", 3) |
| 35 | self.hyperbolicState = parameters.get("hyperbolic-state", "off") |
| 36 | self.hyperRadius = parameters.get("radius", 0.0) |
| 37 | self.hyperAngle = parameters.get("angle", 0.0) |
Ashlesh Gawande | c3ed2b9 | 2015-07-01 12:58:08 -0500 | [diff] [blame] | 38 | self.logLevel = parameters.get("nlsr-log-level", "DEBUG") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 39 | |
| 40 | def createConfigFile(self): |
| 41 | |
| 42 | filePath = "/tmp/%s/nlsr.conf" % self.node.name |
| 43 | |
| 44 | configFile = open(filePath, 'r') |
| 45 | oldContent = configFile.read() |
| 46 | configFile.close() |
| 47 | |
| 48 | newContent = oldContent.replace("$GENERAL_SECTION", self.__getGeneralSection()) |
| 49 | newContent = newContent.replace("$NEIGHBORS_SECTION", self.__getNeighborsSection()) |
| 50 | newContent = newContent.replace("$HYPERBOLIC_SECTION", self.__getHyperbolicSection()) |
| 51 | newContent = newContent.replace("$FIB_SECTION", self.__getFibSection()) |
| 52 | newContent = newContent.replace("$ADVERTISING_SECTION", self.__getAdvertisingSection()) |
| 53 | |
| 54 | configFile = open(filePath, 'w') |
| 55 | configFile.write(newContent) |
| 56 | configFile.close() |
| 57 | |
| 58 | def __getConfig(self): |
| 59 | |
| 60 | config = self.__getGeneralSection() |
| 61 | config += self.__getNeighborsSection() |
| 62 | config += self.__getHyperbolicSection() |
| 63 | config += self.__getFibSection() |
| 64 | config += self.__getAdvertisingSection() |
| 65 | config += self.__getSecuritySection() |
| 66 | |
| 67 | return config |
| 68 | |
| 69 | def __getGeneralSection(self): |
| 70 | |
| 71 | general = "general\n" |
| 72 | general += "{\n" |
| 73 | general += " network /ndn/\n" |
| 74 | general += " site /edu\n" |
| 75 | general += " router /%C1.Router/cs/" + self.node.name + "\n" |
Ashlesh Gawande | c3ed2b9 | 2015-07-01 12:58:08 -0500 | [diff] [blame] | 76 | general += " log-level " + self.logLevel + "\n" |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 77 | general += " log-dir /tmp/" + self.node.name + "/log\n" |
| 78 | general += " seq-dir /tmp/" + self.node.name + "/log\n" |
| 79 | general += "}\n" |
| 80 | |
| 81 | return general |
| 82 | |
| 83 | def __getNeighborsSection(self): |
| 84 | |
| 85 | neighbors = "neighbors\n" |
| 86 | neighbors += "{\n" |
| 87 | |
| 88 | for intf in self.node.intfList(): |
| 89 | link = intf.link |
| 90 | if link: |
| 91 | node1, node2 = link.intf1.node, link.intf2.node |
| 92 | |
| 93 | if node1 == self.node: |
| 94 | other = node2 |
| 95 | ip = other.IP(str(link.intf2)) |
| 96 | else: |
| 97 | other = node1 |
| 98 | ip = other.IP(str(link.intf1)) |
| 99 | |
ashu | 7b6ba18 | 2015-04-17 15:02:37 -0500 | [diff] [blame] | 100 | linkCost = intf.params.get("delay", "10ms").replace("ms", "") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 101 | |
| 102 | neighbors += "neighbor\n" |
| 103 | neighbors += "{\n" |
| 104 | neighbors += " name /ndn/edu/%C1.Router/cs/" + other.name + "\n" |
| 105 | neighbors += " face-uri udp://" + str(ip) + "\n" |
| 106 | neighbors += " link-cost " + linkCost + "\n" |
| 107 | neighbors += "}\n" |
| 108 | |
| 109 | neighbors += "}\n" |
| 110 | |
| 111 | return neighbors |
| 112 | |
| 113 | def __getHyperbolicSection(self): |
| 114 | |
| 115 | hyper = "hyperbolic\n" |
| 116 | hyper += "{\n" |
| 117 | hyper += "state %s\n" % self.hyperbolicState |
| 118 | hyper += "radius " + str(self.hyperRadius) + "\n" |
| 119 | hyper += "angle " + str(self.hyperAngle) + "\n" |
| 120 | hyper += "}\n" |
| 121 | |
| 122 | return hyper |
| 123 | |
| 124 | def __getFibSection(self): |
| 125 | |
| 126 | fib = "fib\n" |
| 127 | fib += "{\n" |
| 128 | fib += " max-faces-per-prefix " + str(self.nFaces) + "\n" |
| 129 | fib += "}\n" |
| 130 | |
| 131 | return fib |
| 132 | |
| 133 | def __getAdvertisingSection(self): |
| 134 | |
| 135 | advertising = "advertising\n" |
| 136 | advertising += "{\n" |
| 137 | advertising += " prefix /ndn/edu/" + self.node.name + "\n" |
| 138 | advertising += "}\n" |
| 139 | |
| 140 | return advertising |
| 141 | |
| 142 | def __getSecuritySection(self): |
| 143 | |
| 144 | security = "security\n" |
| 145 | security += "{\n" |
| 146 | security += " validator\n" |
| 147 | security += " {\n" |
| 148 | security += " trust-anchor\n" |
| 149 | security += " {\n" |
| 150 | security += " type any\n" |
| 151 | security += " }\n" |
| 152 | security += " }\n" |
| 153 | security += "}\n" |
| 154 | |
| 155 | return security |