2015-01-06 14:47:15 +00:00
|
|
|
from mininet.topo import Topo
|
2015-01-07 13:44:44 +00:00
|
|
|
from mininet.net import Mininet
|
|
|
|
from mininet.link import TCLink
|
2020-06-22 15:02:46 +00:00
|
|
|
from mininet.node import OVSBridge
|
2015-01-07 13:44:44 +00:00
|
|
|
from mininet.cli import CLI
|
2015-01-14 14:19:39 +00:00
|
|
|
from subprocess import Popen, PIPE
|
2015-01-06 14:47:15 +00:00
|
|
|
|
2020-06-25 08:53:56 +00:00
|
|
|
import logging
|
|
|
|
|
2020-06-24 09:26:56 +00:00
|
|
|
class MininetBuilder(Topo):
|
2020-06-23 11:20:07 +00:00
|
|
|
def __init__(self):
|
|
|
|
Topo.__init__( self )
|
|
|
|
self.net = None
|
2015-02-26 16:43:45 +00:00
|
|
|
|
2020-06-25 08:53:56 +00:00
|
|
|
def command_to(self, who, cmd):
|
|
|
|
"""
|
|
|
|
Launch command `cmd` to the specific name space of `who`
|
|
|
|
"""
|
2020-06-23 11:20:07 +00:00
|
|
|
return who.cmd(cmd)
|
2015-01-14 14:19:39 +00:00
|
|
|
|
2020-06-25 08:53:56 +00:00
|
|
|
def command_global(self, cmd):
|
|
|
|
"""
|
|
|
|
Launch command `cmd` over the global system, i.e., not specific to a name space
|
|
|
|
"""
|
2020-06-23 11:20:07 +00:00
|
|
|
p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
|
|
|
|
stdout, stderr = p.communicate()
|
|
|
|
if stderr:
|
2020-06-25 08:53:56 +00:00
|
|
|
logging.error("Got error when running cmd: {}".format(cmd))
|
2020-06-23 11:20:07 +00:00
|
|
|
return "Error"
|
|
|
|
return stdout
|
2015-02-26 16:43:45 +00:00
|
|
|
|
2020-06-25 08:53:56 +00:00
|
|
|
def start_network(self):
|
|
|
|
"""
|
|
|
|
Start the network using Mininet
|
|
|
|
|
|
|
|
Note that the use of OVSBridge avoid facing issues with OVS controllers.
|
|
|
|
"""
|
2020-06-23 11:20:07 +00:00
|
|
|
self.net = Mininet(topo=self,link=TCLink,switch=OVSBridge)
|
|
|
|
self.net.start()
|
2015-01-07 13:44:44 +00:00
|
|
|
|
2020-06-25 08:53:56 +00:00
|
|
|
def get_cli(self):
|
|
|
|
"""
|
|
|
|
Get the Mininet command line interface
|
|
|
|
"""
|
2020-06-23 11:20:07 +00:00
|
|
|
if self.net is None:
|
2020-06-25 08:53:56 +00:00
|
|
|
logging.error("Cannot get the CLI")
|
2020-06-23 11:20:07 +00:00
|
|
|
else:
|
|
|
|
CLI(self.net)
|
2015-01-07 13:44:44 +00:00
|
|
|
|
2020-06-25 08:53:56 +00:00
|
|
|
def get_host(self, who):
|
2020-06-23 11:20:07 +00:00
|
|
|
if self.net is None:
|
2020-06-25 08:53:56 +00:00
|
|
|
logging.error("Network not available...")
|
|
|
|
raise Exception("Network not ready")
|
2020-06-23 11:20:07 +00:00
|
|
|
else:
|
|
|
|
return self.net.getNodeByName(who)
|
2015-02-26 16:43:45 +00:00
|
|
|
|
2020-06-25 08:53:56 +00:00
|
|
|
def stop_network(self):
|
2020-06-23 11:20:07 +00:00
|
|
|
if self.net is None:
|
2020-06-25 08:53:56 +00:00
|
|
|
logging.warning("Unable to stop the network: net is None")
|
2020-06-23 11:20:07 +00:00
|
|
|
else:
|
|
|
|
self.net.stop()
|