blob: 180737419601b983e565935dfdd3d953b5e0dc69 [file] [log] [blame]
ashuef3490b2015-02-17 11:01:04 -06001#!/usr/bin/env python
ashu34c3ee02015-03-25 14:41:14 -05002import os
ashuef3490b2015-02-17 11:01:04 -06003
4class 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
ashu34c3ee02015-03-25 14:41:14 -05009 self.isRunning = False
ashuef3490b2015-02-17 11:01:04 -060010
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):
ashu34c3ee02015-03-25 14:41:14 -050022 if self.isRunning is True:
23 try:
24 os.kill(int(self.processId), 0)
25 except OSError:
26 self.isRunning = False
ashuef3490b2015-02-17 11:01:04 -060027
ashu34c3ee02015-03-25 14:41:14 -050028 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
ashuef3490b2015-02-17 11:01:04 -060039
40class NlsrConfigGenerator:
41
42 ROUTING_LINK_STATE = "ls"
43 ROUTING_HYPERBOLIC = "hr"
44
ashu2ad32e22015-05-29 13:37:40 -050045 def __init__(self, node):
46 node.cmd("sudo cp /usr/local/etc/mini-ndn/nlsr.conf nlsr.conf")
ashuef3490b2015-02-17 11:01:04 -060047 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)
Ashlesh Gawandec3ed2b92015-07-01 12:58:08 -050055 self.logLevel = parameters.get("nlsr-log-level", "DEBUG")
ashuef3490b2015-02-17 11:01:04 -060056
57 def createConfigFile(self):
58
59 filePath = "/tmp/%s/nlsr.conf" % self.node.name
60
61 configFile = open(filePath, 'r')
62 oldContent = configFile.read()
63 configFile.close()
64
65 newContent = oldContent.replace("$GENERAL_SECTION", self.__getGeneralSection())
66 newContent = newContent.replace("$NEIGHBORS_SECTION", self.__getNeighborsSection())
67 newContent = newContent.replace("$HYPERBOLIC_SECTION", self.__getHyperbolicSection())
68 newContent = newContent.replace("$FIB_SECTION", self.__getFibSection())
69 newContent = newContent.replace("$ADVERTISING_SECTION", self.__getAdvertisingSection())
70
71 configFile = open(filePath, 'w')
72 configFile.write(newContent)
73 configFile.close()
74
75 def __getConfig(self):
76
77 config = self.__getGeneralSection()
78 config += self.__getNeighborsSection()
79 config += self.__getHyperbolicSection()
80 config += self.__getFibSection()
81 config += self.__getAdvertisingSection()
82 config += self.__getSecuritySection()
83
84 return config
85
86 def __getGeneralSection(self):
87
88 general = "general\n"
89 general += "{\n"
90 general += " network /ndn/\n"
91 general += " site /edu\n"
92 general += " router /%C1.Router/cs/" + self.node.name + "\n"
Ashlesh Gawandec3ed2b92015-07-01 12:58:08 -050093 general += " log-level " + self.logLevel + "\n"
ashuef3490b2015-02-17 11:01:04 -060094 general += " log-dir /tmp/" + self.node.name + "/log\n"
95 general += " seq-dir /tmp/" + self.node.name + "/log\n"
96 general += "}\n"
97
98 return general
99
100 def __getNeighborsSection(self):
101
102 neighbors = "neighbors\n"
103 neighbors += "{\n"
104
105 for intf in self.node.intfList():
106 link = intf.link
107 if link:
108 node1, node2 = link.intf1.node, link.intf2.node
109
110 if node1 == self.node:
111 other = node2
112 ip = other.IP(str(link.intf2))
113 else:
114 other = node1
115 ip = other.IP(str(link.intf1))
116
ashu7b6ba182015-04-17 15:02:37 -0500117 linkCost = intf.params.get("delay", "10ms").replace("ms", "")
ashuef3490b2015-02-17 11:01:04 -0600118
119 neighbors += "neighbor\n"
120 neighbors += "{\n"
121 neighbors += " name /ndn/edu/%C1.Router/cs/" + other.name + "\n"
122 neighbors += " face-uri udp://" + str(ip) + "\n"
123 neighbors += " link-cost " + linkCost + "\n"
124 neighbors += "}\n"
125
126 neighbors += "}\n"
127
128 return neighbors
129
130 def __getHyperbolicSection(self):
131
132 hyper = "hyperbolic\n"
133 hyper += "{\n"
134 hyper += "state %s\n" % self.hyperbolicState
135 hyper += "radius " + str(self.hyperRadius) + "\n"
136 hyper += "angle " + str(self.hyperAngle) + "\n"
137 hyper += "}\n"
138
139 return hyper
140
141 def __getFibSection(self):
142
143 fib = "fib\n"
144 fib += "{\n"
145 fib += " max-faces-per-prefix " + str(self.nFaces) + "\n"
146 fib += "}\n"
147
148 return fib
149
150 def __getAdvertisingSection(self):
151
152 advertising = "advertising\n"
153 advertising += "{\n"
154 advertising += " prefix /ndn/edu/" + self.node.name + "\n"
155 advertising += "}\n"
156
157 return advertising
158
159 def __getSecuritySection(self):
160
161 security = "security\n"
162 security += "{\n"
163 security += " validator\n"
164 security += " {\n"
165 security += " trust-anchor\n"
166 security += " {\n"
167 security += " type any\n"
168 security += " }\n"
169 security += " }\n"
170 security += "}\n"
171
172 return security