Open Kilda Java Documentation
plan-e.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #
4 # Plan E builds on Plan D, add functions and loops (many mininets) to model test executions
5 
6 
7 from mininet.net import Mininet
8 from mininet.node import OVSSwitch, Controller, RemoteController
9 from mininet.log import setLogLevel, info
10 from mininet.cli import CLI
11 from mininet.clean import cleanup
12 from mininet.util import errRun
13 
14 import subprocess
15 import re
16 
17 # globals
18 net = None # mininet object
19 hosts1 = None
20 hosts2 = None
21 
22 class KildaSwitch( OVSSwitch ):
23  "Add the OpenFlow13 Protocol"
24  def __init__(self,name,**params):
25  params['protocols'] = 'OpenFlow13'
26  OVSSwitch.__init__(self, name, **params)
27 
28  @classmethod
29  def batchStartup( cls, switches, run=errRun ):
30  """
31  Mininet looks for this during stop(). It exists in OVSSwitch. Kilda, at the moment,
32  doesn't like this batch operation (and it shouldn't be done in batch)
33  """
34  info ("\nIGNORE batchStartup()\n")
35  for switch in switches:
36  if switch.batch:
37  info ("\n .... BATCH = TRUE !!!!!!\n")
38  return switches
39 
40  @classmethod
41  def batchShutdown( cls, switches, run=errRun ):
42  """
43  Mininet looks for this during stop(). It exists in OVSSwitch. Kilda, at the moment,
44  doesn't like this batch operation (and it shouldn't be done in batch)
45  """
46  info ("\nIGNORE batchShutdown()\n")
47  for switch in switches:
48  if switch.batch:
49  info ("\n .... BATCH = TRUE !!!!!!\n")
50  return switches
51 
52 
53 
55  gateway = '127.0.0.1'
56  netstat = subprocess.check_output(['netstat', '-rn']).split('\n')
57  for line in netstat:
58  if line.startswith('0.0.0.0'):
59  gateway = re.split('\s+', line)[1]
60  break
61  return gateway
62 
63 
64 def add_rules():
65  subprocess.Popen(["ovs-ofctl","-O","OpenFlow13","add-flow","s1",
66  "idle_timeout=0,priority=1000,in_port=1,actions=output:2"],
67  stdout=subprocess.PIPE).wait()
68  subprocess.Popen(["ovs-ofctl","-O","OpenFlow13","add-flow","s1",
69  "idle_timeout=0,priority=1000,in_port=2,actions=output:1"],
70  stdout=subprocess.PIPE).wait()
71 
72 
73 def build():
74  global net, hosts1, hosts2
75  net = Mininet( controller=RemoteController, switch=KildaSwitch, build=False )
76  gateway = get_gateway()
77 
78  info( "*** Creating (Remote) controllers\n" )
79  c0 = net.addController( 'c0', ip=gateway, port=6653)
80 
81  info( "*** Creating switches\n" )
82  s1 = net.addSwitch( 's1' )
83  s2 = net.addSwitch( 's2' )
84 
85  info( "*** Creating hosts\n" )
86  hosts1 = [ net.addHost( 'h%ds1' % n ) for n in ( 1, 2 ) ]
87  hosts2 = [ net.addHost( 'h%ds2' % n ) for n in ( 1, 2 ) ]
88 
89  info( "*** Creating links\n" )
90  for h in hosts1:
91  net.addLink( h, s1 )
92  for h in hosts2:
93  net.addLink( h, s2 )
94  net.addLink( s1, s2 )
95 
96  info( "*** Starting network\n" )
97  net.configHosts()
98  net.start()
99  add_rules()
100 
101 
102 def ping(host1, host2):
103  result = host1.cmd( 'ping -c1 -w1 %s' % (host2.IP()) )
104  lines = result.split("\n")
105  if "1 packets received" in lines[3]:
106  print "CONNECTION BETWEEN ", host1.IP(), "and", host2.IP()
107  else:
108  print "NO CONNECTION BETWEEN ", host1.IP(), "and", host2.IP()
109 
110 
111 def clean():
112  global hosts1, hosts2, net
113  hosts1 = None
114  hosts2 = None
115  net.stop()
116  net = None
117  setLogLevel( 'warning' ) # mininet cleanup is noisy
118  cleanup()
119  setLogLevel( 'info' )
120 
121 
122 def main():
123  setLogLevel( 'info' )
124 
125  count = 4
126  for i in range(count):
127  info( "*** Running Loop #{} of {}".format(i+1, count) )
128  build()
129  ping(hosts1[0], hosts1[1]) # Connection
130  ping(hosts1[0], hosts2[0]) # No connection
131  info ("--> Cleanup\n")
132  clean()
133  info ("..\n")
134  info ("..\n")
135 
136 
137 # Go for it!
138 main()
def main()
Definition: plan-e.py:122
def batchStartup(cls, switches, run=errRun)
Definition: plan-e.py:29
def clean()
Definition: plan-e.py:111
def get_gateway()
Definition: plan-e.py:54
def add_rules()
Definition: plan-e.py:64
def ping(host1, host2)
Definition: plan-e.py:102
def batchShutdown(cls, switches, run=errRun)
Definition: plan-e.py:41
def __init__(self, name, params)
Definition: plan-e.py:24
def build()
Definition: plan-e.py:73