remove all the self.fail() to avoid deadlock
[nepi.git] / examples / openvswitch / ovs_ping_exp.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation, either version 3 of the License, or
9 #    (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15 #
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 #         Alexandros Kouvakas <alexandros.kouvakas@gmail.com>
21 #
22 #         Switch1 ------- Switch2         
23 #            /                \           
24 #           /                  \          
25 #          /                    \         
26 #       Host1                  Host2      
27
28
29
30 from nepi.execution.ec import ExperimentController
31
32 def add_node(ec, host, user):
33     node = ec.register_resource("PlanetlabNode")
34     ec.set(node, "hostname", host)
35     ec.set(node, "username", user)
36     ec.set(node, "cleanHome", True)
37     ec.set(node, "cleanProcesses", True)
38     return node
39
40 def add_ovs(ec, bridge_name, virtual_ip_pref, controller_ip, controller_port, node):
41     ovs = ec.register_resource("OVSWitch")
42     ec.set(ovs, "bridge_name", bridge_name)
43     ec.set(ovs, "virtual_ip_pref", virtual_ip_pref)
44     ec.set(ovs, "controller_ip", controller_ip)
45     ec.set(ovs, "controller_port", controller_port)
46     ec.register_connection(ovs, node)
47     return ovs
48
49 def add_port(ec, port_name, ovs):
50     port = ec.register_resource("OVSPort")
51     ec.set(port, "port_name", port_name)
52     ec.register_connection(port, ovs)
53     return port
54
55 def add_tap(ec, ip4, prefix4, pointopoint, node):
56     tap = ec.register_resource("PlanetlabTap")
57     ec.set(tap, "ip4", ip4)
58     ec.set(tap, "prefix4", prefix4)
59     ec.set(tap, "pointopoint", pointopoint)
60     ec.set(tap, "up", True)
61     ec.register_connection(tap, node)
62     return tap
63
64 def add_tunnel(ec, port0, tap):
65     tunnel = ec.register_resource("Tunnel")
66     ec.register_connection(port0, tunnel)
67     ec.register_connection(tunnel, tap)
68     return tunnel
69
70 def add_app(ec, command, node):
71     app = ec.register_resource("LinuxApplication")
72     ec.set(app, "command", command)
73     ec.register_connection(app, node)
74     return app
75
76 # Create the EC
77 ec = ExperimentController(exp_id = "one")
78
79 switch1 = "planetlab2.virtues.fi"
80 switch2 = "planetlab2.upc.es"
81 host1 = "planetlab2.ionio.gr"
82 host2 = "planetlab2.cs.aueb.gr"
83
84 slicename = "inria_nepi"
85
86 s1_node = add_node(ec, switch1, slicename)
87 s2_node = add_node(ec, switch2, slicename)
88
89 # Add switches 
90 ovs1 = add_ovs(ec, "nepi_bridge", "192.168.3.1/24", "85.23.168.77", "6633", s1_node)
91 ovs2 = add_ovs(ec, "nepi_bridge", "192.168.3.2/24", "85.23.168.77", "6633", s2_node)
92
93 # Add ports on ovs
94 port1 = add_port(ec, "nepi_port1", ovs1)
95 port3 = add_port(ec, "nepi_port3", ovs1)
96 port2 = add_port(ec, "nepi_port2", ovs2)
97 port4 = add_port(ec, "nepi_port4", ovs2)
98
99 h1_node = add_node(ec, host1, slicename)
100 h2_node = add_node(ec, host2, slicename) 
101
102 # Add tap devices
103 tap1 = add_tap(ec, "192.168.3.3", 24, "192.168.3.1", h1_node)
104 tap2 = add_tap(ec, "192.168.3.4", 24, "192.168.3.1", h2_node)
105
106 # Connect the nodes
107 tunnel1 = add_tunnel(ec, port1, tap1)
108 tunnel2 = add_tunnel(ec, port2, tap2)
109 tunnel3 = add_tunnel(ec, port3, port4)
110
111 # Add ping commands
112 app1 = add_app(ec, "ping -c3 192.168.3.3", s1_node)
113 app2 = add_app(ec, "ping -c3 192.168.3.4", s2_node)
114
115 ec.deploy()
116 ec.wait_finished([app2])
117
118 # Retreive ping results and save
119 # them in a file
120 ping1 = ec.trace(app1, 'stdout')
121 ping2 = ec.trace(app2, 'stdout')
122 f = open("examples/openvswitch/ping_res.txt", 'w').close()
123 f = open("examples/openvswitch/ping_res.txt", 'a')
124 f.write(ping1)
125 f.write(ping2)
126 f.close()
127
128 # Delete the overlay network
129 ec.shutdown()
130
131
132
133
134