Vince Lehman | b8b1806 | 2015-07-14 13:07:22 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
| 3 | # Copyright (C) 2015 The University of Memphis, |
| 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
| 6 | # |
| 7 | # This file is part of Mini-NDN. |
| 8 | # See AUTHORS.md for a complete list of Mini-NDN authors and contributors. |
| 9 | # |
| 10 | # Mini-NDN is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation, either version 3 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # Mini-NDN is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with Mini-NDN, e.g., in COPYING.md file. |
| 22 | # If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame] | 24 | from ndn.ndn_application import NdnApplication |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 25 | |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame] | 26 | class Nlsr(NdnApplication): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 27 | def __init__(self, node): |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame] | 28 | NdnApplication.__init__(self, node) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 29 | self.routerName = "/%sC1.Router/cs/%s" % ('%', node.name) |
| 30 | self.confFile = "/tmp/%s/nlsr.conf" % node.name |
| 31 | |
| 32 | # Make directory for log file |
| 33 | self.logDir = "/tmp/%s/log" % node.name |
| 34 | node.cmd("mkdir %s" % self.logDir) |
| 35 | |
| 36 | # Configure basic router information in nlsr.conf based on host name |
| 37 | node.cmd("sudo sed -i 's|router .*|router %s|g' %s" % (self.routerName, self.confFile)) |
| 38 | node.cmd("sudo sed -i 's|log-dir .*|log-dir %s|g' %s" % (self.logDir, self.confFile)) |
| 39 | node.cmd("sudo sed -i 's|seq-dir .*|seq-dir %s|g' %s" % (self.logDir, self.confFile)) |
| 40 | node.cmd("sudo sed -i 's|prefix .*netlab|prefix /ndn/edu/%s|g' %s" % (node.name, self.confFile)) |
| 41 | |
| 42 | def start(self): |
Ashlesh Gawande | 792c6aa | 2015-07-10 12:18:36 -0500 | [diff] [blame] | 43 | NdnApplication.start(self, "nlsr -d") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 44 | |
| 45 | class NlsrConfigGenerator: |
| 46 | |
| 47 | ROUTING_LINK_STATE = "ls" |
| 48 | ROUTING_HYPERBOLIC = "hr" |
| 49 | |
ashu | 2ad32e2 | 2015-05-29 13:37:40 -0500 | [diff] [blame] | 50 | def __init__(self, node): |
| 51 | node.cmd("sudo cp /usr/local/etc/mini-ndn/nlsr.conf nlsr.conf") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 52 | self.node = node |
| 53 | |
| 54 | parameters = node.nlsrParameters |
| 55 | |
| 56 | self.nFaces = parameters.get("max-faces-per-prefix", 3) |
| 57 | self.hyperbolicState = parameters.get("hyperbolic-state", "off") |
| 58 | self.hyperRadius = parameters.get("radius", 0.0) |
| 59 | self.hyperAngle = parameters.get("angle", 0.0) |
Ashlesh Gawande | c3ed2b9 | 2015-07-01 12:58:08 -0500 | [diff] [blame] | 60 | self.logLevel = parameters.get("nlsr-log-level", "DEBUG") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 61 | |
| 62 | def createConfigFile(self): |
| 63 | |
| 64 | filePath = "/tmp/%s/nlsr.conf" % self.node.name |
| 65 | |
| 66 | configFile = open(filePath, 'r') |
| 67 | oldContent = configFile.read() |
| 68 | configFile.close() |
| 69 | |
| 70 | newContent = oldContent.replace("$GENERAL_SECTION", self.__getGeneralSection()) |
| 71 | newContent = newContent.replace("$NEIGHBORS_SECTION", self.__getNeighborsSection()) |
| 72 | newContent = newContent.replace("$HYPERBOLIC_SECTION", self.__getHyperbolicSection()) |
| 73 | newContent = newContent.replace("$FIB_SECTION", self.__getFibSection()) |
| 74 | newContent = newContent.replace("$ADVERTISING_SECTION", self.__getAdvertisingSection()) |
| 75 | |
| 76 | configFile = open(filePath, 'w') |
| 77 | configFile.write(newContent) |
| 78 | configFile.close() |
| 79 | |
| 80 | def __getConfig(self): |
| 81 | |
| 82 | config = self.__getGeneralSection() |
| 83 | config += self.__getNeighborsSection() |
| 84 | config += self.__getHyperbolicSection() |
| 85 | config += self.__getFibSection() |
| 86 | config += self.__getAdvertisingSection() |
| 87 | config += self.__getSecuritySection() |
| 88 | |
| 89 | return config |
| 90 | |
| 91 | def __getGeneralSection(self): |
| 92 | |
| 93 | general = "general\n" |
| 94 | general += "{\n" |
| 95 | general += " network /ndn/\n" |
| 96 | general += " site /edu\n" |
| 97 | general += " router /%C1.Router/cs/" + self.node.name + "\n" |
Ashlesh Gawande | c3ed2b9 | 2015-07-01 12:58:08 -0500 | [diff] [blame] | 98 | general += " log-level " + self.logLevel + "\n" |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 99 | general += " log-dir /tmp/" + self.node.name + "/log\n" |
| 100 | general += " seq-dir /tmp/" + self.node.name + "/log\n" |
| 101 | general += "}\n" |
| 102 | |
| 103 | return general |
| 104 | |
| 105 | def __getNeighborsSection(self): |
| 106 | |
| 107 | neighbors = "neighbors\n" |
| 108 | neighbors += "{\n" |
| 109 | |
| 110 | for intf in self.node.intfList(): |
| 111 | link = intf.link |
| 112 | if link: |
| 113 | node1, node2 = link.intf1.node, link.intf2.node |
| 114 | |
| 115 | if node1 == self.node: |
| 116 | other = node2 |
| 117 | ip = other.IP(str(link.intf2)) |
| 118 | else: |
| 119 | other = node1 |
| 120 | ip = other.IP(str(link.intf1)) |
| 121 | |
ashu | 7b6ba18 | 2015-04-17 15:02:37 -0500 | [diff] [blame] | 122 | linkCost = intf.params.get("delay", "10ms").replace("ms", "") |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 123 | |
| 124 | neighbors += "neighbor\n" |
| 125 | neighbors += "{\n" |
| 126 | neighbors += " name /ndn/edu/%C1.Router/cs/" + other.name + "\n" |
| 127 | neighbors += " face-uri udp://" + str(ip) + "\n" |
| 128 | neighbors += " link-cost " + linkCost + "\n" |
| 129 | neighbors += "}\n" |
| 130 | |
| 131 | neighbors += "}\n" |
| 132 | |
| 133 | return neighbors |
| 134 | |
| 135 | def __getHyperbolicSection(self): |
| 136 | |
| 137 | hyper = "hyperbolic\n" |
| 138 | hyper += "{\n" |
| 139 | hyper += "state %s\n" % self.hyperbolicState |
| 140 | hyper += "radius " + str(self.hyperRadius) + "\n" |
| 141 | hyper += "angle " + str(self.hyperAngle) + "\n" |
| 142 | hyper += "}\n" |
| 143 | |
| 144 | return hyper |
| 145 | |
| 146 | def __getFibSection(self): |
| 147 | |
| 148 | fib = "fib\n" |
| 149 | fib += "{\n" |
| 150 | fib += " max-faces-per-prefix " + str(self.nFaces) + "\n" |
| 151 | fib += "}\n" |
| 152 | |
| 153 | return fib |
| 154 | |
| 155 | def __getAdvertisingSection(self): |
| 156 | |
| 157 | advertising = "advertising\n" |
| 158 | advertising += "{\n" |
| 159 | advertising += " prefix /ndn/edu/" + self.node.name + "\n" |
| 160 | advertising += "}\n" |
| 161 | |
| 162 | return advertising |
| 163 | |
| 164 | def __getSecuritySection(self): |
| 165 | |
| 166 | security = "security\n" |
| 167 | security += "{\n" |
| 168 | security += " validator\n" |
| 169 | security += " {\n" |
| 170 | security += " trust-anchor\n" |
| 171 | security += " {\n" |
| 172 | security += " type any\n" |
| 173 | security += " }\n" |
| 174 | security += " }\n" |
| 175 | security += "}\n" |
| 176 | |
| 177 | return security |