Add Vagrantfile for L2Switch tutorial
[integration/packaging.git] / tutorials / l2switch / mininet.py
1 #!/usr/bin/env python
2 """Creates a Mininet topology based on examples in the main Mininet documentation."""
3
4 import time
5
6 from mininet.topo import Topo
7 from mininet.net import Mininet
8 from mininet.log import setLogLevel
9 from mininet.node import RemoteController
10
11
12 class SingleSwitchTopo(Topo):
13     """Single switch connected to n hosts."""
14     def build(self, n=2):
15         switch = self.addSwitch("s1")
16         # Python's range(N) generates 0..N-1
17         for h in range(n):
18             host = self.addHost("h%s" % (h + 1))
19             self.addLink(host, switch)
20
21
22 def simple_test():
23     """Create and test a simple network."""
24     topo = SingleSwitchTopo(n=4)
25     net = Mininet(topo=topo, controller=None)
26     net.addController("c0", controller=RemoteController, ip="127.0.0.1", port=6633)
27     net.start()
28     time.sleep(10)
29     print "Testing network connectivity"
30     net.pingAll()
31
32 if __name__ == "__main__":
33     # Tell mininet to print useful information
34     setLogLevel("info")
35     simple_test()