Search code examples
pythondictionarymininet

Unable to access values in nested dictionary for adding link in network


I have the following dictionary

d={'s1': {'s2': {}, 's3': {}, 's4': {}}, 's2': {'s5': {'bw': 20, 'delay': '1ms'}}, 's3': {'s6': {'bw': 20, 'delay': '1ms'}}, 's4': {'s7': {'bw': 20, 'delay': '1ms'}}, 's5': {'h1': {'bw': 10, 'delay': '5ms'}, 'h2': {'bw': 10, 'delay': '5ms'}}, 's6': {'h3': {'bw': 10, 'delay': '5ms'}, 'h4': {'bw': 10, 'delay': '5ms'}}, 's7': {'h5': {'bw': 10, 'delay': '5ms'}, 'h6': {'bw': 10, 'delay': '5ms'}}}

I would like to access the values of bw and delay for link. For ex- I want addLink(s7,h6,bw=10,delay=5ms) to work.

I am using following code for it #!/usr/bin/python

import yaml
#from ruamel import yaml
from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink
def myNetwork():

# Create an instance of Mininet class i.e. the network with default values
net = Mininet(autoStaticArp=True, autoSetMacs=True)

a_yaml_file = open("Complex_Topology.yaml")
parsed_yaml_file = yaml.load(a_yaml_file, Loader=yaml.FullLoader)
parsed_yaml_file["Controllers"]
parsed_yaml_file["Hosts"]
print(parsed_yaml_file["Links"])
parsed_yaml_file["Switches"]

for i in parsed_yaml_file["Controllers"]:
    net.addController(i)
for i in parsed_yaml_file["Hosts"]:
    net.addHost(i)
for i in parsed_yaml_file["Switches"]:
    net.addSwitch(i) 
d=parsed_yaml_file["Links"]
def freeze(x):
    if isinstance(x, dict):
        return frozenset(freeze(items) for items in x.items())
    elif isinstance(x, list):
        return tuple(freeze(value) for value in x)
    return x

for i in d.keys():
    for j in d[i].keys():
        k=freeze(d[i].values) 
        net.addLink(i,j,k)
info( '*** Starting network\n')
net.start()
# Start the Mininet CLI to run commands
CLI(net)
# Stop the network
net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()

I am getting following error

Traceback (most recent call last):

File "new.py", line 83, in <module>
myNetwork()
File "new.py", line 73, in myNetwork
net.addLink(i,j,k)
File "/home/sudhanshu/Desktop/SDN/mininet/net.py", line 400, in addLink
link = cls( node1, node2, **options )
File "/home/sudhanshu/Desktop/SDN/mininet/link.py", line 450, in __init__
self.makeIntfPair( intfName1, intfName2, addr1, addr2,
File "/home/sudhanshu/Desktop/SDN/mininet/link.py", line 495, in makeIntfPair
return makeIntfPair( intfname1, intfname2, addr1, addr2, node1, node2,
File "/home/sudhanshu/Desktop/SDN/mininet/util.py", line 266, in makeIntfPair
raise Exception( "Error creating interface pair (%s,%s): %s " %
Exception: Error creating interface pair (s1-eth<built-in method values of dict object at 0x7f1f6b6035c0>,s2-eth1): bash: built-in: No such file or directory

I have tried manipulating my code but failed.

Could anyone please help me with this?


Solution

  • If you want to call the method as addLink(s7, h6, bw=10, delay=5ms), then you could create the links by using:

    for i in d:
        for j in d[i]:
            net.addLink(i, j, **d[i][j])