efec85bab2e0722bd725e40318e1357d6cf4a138
[nepi.git] / examples / netns / local_switch_ping.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 #
21 #
22 # This example must be executed as root: 
23 # $ sudo PYTHONPATH=$PYTHONPATH:src python examples/netns/local_switch_ping.py
24
25 from nepi.execution.ec import ExperimentController 
26
27 ec = ExperimentController(exp_id = "netns-local-p2p-ping")
28
29 # Simulation will executed in the local machine
30 node = ec.register_resource("linux::Node")
31 ec.set(node, "hostname", "localhost")
32
33 # Add an emulation resource
34 emu = ec.register_resource("linux::netns::Emulation")
35 ec.set(emu, "verbose", True)
36 ec.register_connection(emu, node)
37
38 # Add two emulated nodes
39 netnsnode1 = ec.register_resource("netns::Node")
40 ec.register_connection(netnsnode1, emu)
41
42 iface1 = ec.register_resource("netns::NodeInterface")
43 ec.register_connection(iface1, netnsnode1)
44
45 ip1 = ec.register_resource("netns::IPv4Address")
46 ec.set(ip1, "ip", "10.0.0.1")
47 ec.set(ip1, "prefix", "30")
48 ec.register_connection(ip1, iface1)
49
50 netnsnode2 = ec.register_resource("netns::Node")
51 ec.register_connection(netnsnode2, emu)
52
53 iface2 = ec.register_resource("netns::NodeInterface")
54 ec.register_connection(iface2, netnsnode2)
55
56 ip2 = ec.register_resource("netns::IPv4Address")
57 ec.set(ip2, "ip", "10.0.0.2")
58 ec.set(ip2, "prefix", "30")
59 ec.register_connection(ip2, iface2)
60
61 switch = ec.register_resource("netns::Switch")
62 ec.register_connection(iface1, switch)
63 ec.register_connection(iface2, switch)
64
65 ping = ec.register_resource("netns::Application")
66 ec.set(ping, "command", "ping -c20 10.0.0.2")
67 ec.register_connection(ping, netnsnode1)
68
69 ec.deploy()
70
71 ec.wait_finished([ping])
72
73 stdout = ec.trace(ping, "stdout") 
74
75 ec.shutdown()
76
77 print "PING OUTPUT", stdout