You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.0 KiB
Python

5 months ago
#!/usr/bin/env python
from sys import argv
import xml.etree.ElementTree as ET
from mininet.topo import Topo
from mininet.link import TCLink
# It would be nice if we didn't have to do this:
# pylint: disable=arguments-differ
class GraphmlTopo(Topo):
def build(self, filename='topology.graphml'):
positions = dict()
try:
graph = ET.parse(filename).getroot()
except Exception as error:
print('oops: ', error)
exit(1)
for node in graph.iter('{http://graphml.graphdrawing.org/xmlns}node'):
node_id = int(node.get('id')) + 1
privateDirs = ['/var/log','/var/run']
self.addHost('h%i' % node_id, privateDirs=privateDirs) #,
#cls=LinuxRouter)
x_pos = node.find('.//data[@key="x"]')
y_pos = node.find('.//data[@key="y"]')
positions[node_id] = (x_pos, y_pos)
for link in graph.iter('{http://graphml.graphdrawing.org/xmlns}edge'):
link_id = int(link.get('id')) + 1
source = int(link.get('source')) + 1
target = int(link.get('target')) + 1
intfName_s, addr1, params_s = self.format_intf_params(link_id, source)
intfName_t, addr2, params_t = self.format_intf_params(link_id, target)
linkopts = dict() # dict(bw=10, delay='5ms', loss=20, max_queue_size=1000, use_htb=True)
# to implement a function which from nodes positions return linkopts
self.addLink('h%i' % source, 'h%i' % target, key=link_id, cls=TCLink,
intfName1=intfName_s, addr1=addr1, params1=params_s,
intfName2=intfName_t, addr2=addr2 ,params2=params_t,
**linkopts)
def format_intf_params(self, link_id, node_id):
intf_name = 'h%i-eth%i' % (node_id, link_id)
addr = '00:00:00:00:%02i:%02i' % (link_id, node_id)
ip = '10.0.%i.%i/24' % (link_id, node_id)
params = {'ip': ip}
return intf_name, addr, params