blob: 2da0894946ea6cd2164f5e90005cc6f3967338e2 [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- 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 Gawande792c6aa2015-07-10 12:18:36 -050024from ndn.ndn_application import NdnApplication
ashuef3490b2015-02-17 11:01:04 -060025
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -050026class Nlsr(NdnApplication):
ashuef3490b2015-02-17 11:01:04 -060027 def __init__(self, node):
Ashlesh Gawande792c6aa2015-07-10 12:18:36 -050028 NdnApplication.__init__(self, node)
ashuef3490b2015-02-17 11:01:04 -060029 self.routerName = "/%sC1.Router/cs/%s" % ('%', node.name)
Ashlesh Gawande1b663692015-10-14 16:38:10 -050030 self.confFile = "%s/nlsr.conf" % node.homeFolder
ashuef3490b2015-02-17 11:01:04 -060031
32 # Make directory for log file
Ashlesh Gawande1b663692015-10-14 16:38:10 -050033 self.logDir = "%s/log" % node.homeFolder
ashuef3490b2015-02-17 11:01:04 -060034 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 Gawande792c6aa2015-07-10 12:18:36 -050043 NdnApplication.start(self, "nlsr -d")
ashuef3490b2015-02-17 11:01:04 -060044
45class NlsrConfigGenerator:
46
47 ROUTING_LINK_STATE = "ls"
48 ROUTING_HYPERBOLIC = "hr"
49
ashu2ad32e22015-05-29 13:37:40 -050050 def __init__(self, node):
51 node.cmd("sudo cp /usr/local/etc/mini-ndn/nlsr.conf nlsr.conf")
ashuef3490b2015-02-17 11:01:04 -060052 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 Gawandec3ed2b92015-07-01 12:58:08 -050060 self.logLevel = parameters.get("nlsr-log-level", "DEBUG")
ashuef3490b2015-02-17 11:01:04 -060061
62 def createConfigFile(self):
63
Ashlesh Gawande1b663692015-10-14 16:38:10 -050064 filePath = "%s/nlsr.conf" % self.node.homeFolder
ashuef3490b2015-02-17 11:01:04 -060065
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 Gawandec3ed2b92015-07-01 12:58:08 -050098 general += " log-level " + self.logLevel + "\n"
Ashlesh Gawande1b663692015-10-14 16:38:10 -050099 general += " log-dir " + self.node.homeFolder + "/log\n"
100 general += " seq-dir " + self.node.homeFolder + "/log\n"
ashuef3490b2015-02-17 11:01:04 -0600101 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
ashu7b6ba182015-04-17 15:02:37 -0500122 linkCost = intf.params.get("delay", "10ms").replace("ms", "")
ashuef3490b2015-02-17 11:01:04 -0600123
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