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