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