Move router class to its own file

This commit is contained in:
Wojciech Kozlowski 2019-04-06 13:08:25 +02:00
parent 41b1af10c4
commit a3e6b4f0c0
3 changed files with 52 additions and 42 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
logs/
logs/
*.pyc

27
router.py Normal file
View File

@ -0,0 +1,27 @@
from mininet.node import Switch, Node
class Router(Switch):
"""Defines a new router that is inside a network namespace so that the
individual routing entries don't collide.
"""
ID = 0
def __init__(self, name, **kwargs):
kwargs['inNamespace'] = True
Switch.__init__(self, name, **kwargs)
Router.ID += 1
self.switch_id = Router.ID
def start(self, controllers):
pass
def defaultIntf(self):
if hasattr(self, "controlIntf") and self.controlIntf:
return self.controlIntf
return Node.defaultIntf(self)

View File

@ -1,64 +1,41 @@
#!/usr/bin/env python
import os
import sys
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.node import Switch, Node
import os
class Router(Switch):
"""Defines a new router that is inside a network namespace so that the
individual routing entries don't collide.
"""
ID = 0
def __init__(self, name, **kwargs):
kwargs['inNamespace'] = True
Switch.__init__(self, name, **kwargs)
Router.ID += 1
self.switch_id = Router.ID
@staticmethod
def setup():
return
def defaultIntf( self ):
if hasattr(self, "controlIntf") and self.controlIntf:
return self.controlIntf
else:
return Node.defaultIntf(self)
def start(self, controllers):
pass
def stop(self):
self.deleteIntfs()
sys.path.append(os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'..'))
from router import Router
class NetTopo(Topo):
"""The network topology.
"""
def __init__(self):
# Add default members to class.
super(NetTopo, self ).__init__()
super(NetTopo, self).__init__()
# The topology has one router per AS
routers = [
self.addSwitch('R1'),
self.addSwitch('R2'),
]
hosts = []
r_1 = self.addSwitch('R1')
r_2 = self.addSwitch('R2')
# Setup the links as follows:
#
# R1 --- R2
#
self.addLink('R1', 'R2')
return
self.addLink(r_1, r_2)
def main():
def scenario():
"""Start the network scenario.
"""
os.system("rm -f /tmp/R*.log /tmp/R*.pid logs/*")
os.system("mn -c >/dev/null 2>&1")
os.system("killall -9 zebra > /dev/null 2>&1")
@ -71,7 +48,12 @@ def main():
router.waitOutput()
# Start Zebra (routing table daemon)
router.cmd("/usr/lib/frr/zebra -f conf/zebra-%s.conf -d -i /tmp/zebra-%s.pid > logs/%s-zebra-stdout 2>&1" % (router.name, router.name, router.name))
router.cmd("/usr/lib/frr/zebra"
" -f conf/zebra-%s.conf"
" -d"
" -i /tmp/zebra-%s.pid"
" > logs/%s-zebra-stdout 2>&1"
% (router.name, router.name, router.name))
router.waitOutput()
# Delete spare loopback address for convenience
@ -84,4 +66,4 @@ def main():
if __name__ == "__main__":
main()
scenario()