blob: d1e1d3c29aa928f51ee06df84b6d210c6489e73b [file] [log] [blame]
ashuef3490b2015-02-17 11:01:04 -06001#!/usr/bin/env python
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -05002from ndn.ndn_application import NdnApplication
ashuef3490b2015-02-17 11:01:04 -06003
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -05004class Nlsr(NdnApplication):
ashuef3490b2015-02-17 11:01:04 -06005 def __init__(self, node):
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -05006 NdnApplication.__init__(self, node)
ashuef3490b2015-02-17 11:01:04 -06007 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 Gawande792c6aa2015-07-10 12:18:36 -050021 NdnApplication.start(self, "nlsr -d")
ashuef3490b2015-02-17 11:01:04 -060022
23class NlsrConfigGenerator:
24
25 ROUTING_LINK_STATE = "ls"
26 ROUTING_HYPERBOLIC = "hr"
27
ashu2ad32e22015-05-29 13:37:40 -050028 def __init__(self, node):
29 node.cmd("sudo cp /usr/local/etc/mini-ndn/nlsr.conf nlsr.conf")
ashuef3490b2015-02-17 11:01:04 -060030 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 Gawandec3ed2b92015-07-01 12:58:08 -050038 self.logLevel = parameters.get("nlsr-log-level", "DEBUG")
ashuef3490b2015-02-17 11:01:04 -060039
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 Gawandec3ed2b92015-07-01 12:58:08 -050076 general += " log-level " + self.logLevel + "\n"
ashuef3490b2015-02-17 11:01:04 -060077 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
ashu7b6ba182015-04-17 15:02:37 -0500100 linkCost = intf.params.get("delay", "10ms").replace("ms", "")
ashuef3490b2015-02-17 11:01:04 -0600101
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