Serious refactoring of TUN/TAP and tunnel code. Linux udp/gre tunnels not yet functional
[nepi.git] / examples / omf / nitos_omf6_ping.py
1 #!/usr/bin/env python
2
3 ###############################################################################
4 #
5 #    NEPI, a framework to manage network experiments
6 #    Copyright (C) 2013 INRIA
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 # Authors: Alina Quereilhac <alina.quereilhac@inria.fr>
22 #         Julien Tribino <julien.tribino@inria.fr>
23 #
24 ###############################################################################
25       
26 # Topology
27 #
28 #
29 #  Testbed : Nitos
30 #
31 #     Node
32 #     node0ZZ 
33 #     0
34 #     |
35 #     |
36 #     0
37 #     Node
38 #     node0ZZ 
39 #     PING
40 #     
41 #   
42 #      - Experiment:
43 #        - t0 : Deployment
44 #        - t1 : Ping Start
45 #        - t2 (t1 + 10s) : Ping stop
46 #        - t3 (t2 + 2s) : Kill the application
47 #
48
49 from nepi.execution.ec import ExperimentController
50 from nepi.execution.resource import ResourceAction, ResourceState
51
52 from optparse import OptionParser
53 import os
54
55 usage = ("usage: %prog -x <nodex> -y <nodey> -c <channel> -u <slicename> -g <nitos-gateway>")
56
57 parser = OptionParser(usage = usage)
58 parser.add_option("-x", "--nodex", dest="nodex", 
59         help="Nitos first reserved node "
60             "(e.g. hostname must be of form: omf.nitos.node0XX)", 
61         type="str")
62 parser.add_option("-y", "--nodey", dest="nodey", 
63         help="Nitos second reserved node "
64             "(e.g. hostname must be of form: omf.nitos.node0YY)", 
65         type="str")
66 parser.add_option("-c", "--channel", dest="channel", 
67         help="Nitos reserved channel",
68         type="str")
69 parser.add_option("-g", "--gateway", dest="gateway", 
70         help="Nitos gateway hostname", 
71         type="str", default="nitlab.inf.uth.gr")
72 parser.add_option("-u", "--slicename", dest="slicename", 
73         help="Nitos gateway username (slicename)", 
74         type="str")
75
76 (options, args) = parser.parse_args()
77
78 if not options.channel or not options.nodex or \
79         not options.nodey or not options.slicename:
80     parser.error("Missing argument channel, nodex, nodey, or slicename!")
81
82 nodex = options.nodex.split(".")[-1]
83 nodey = options.nodey.split(".")[-1]
84 slicename = options.slicename
85 chan = options.channel
86 gateway = options.gateway
87
88 # Create the EC
89 ec = ExperimentController(exp_id="nitos_omf6_ping")
90
91 # Create and Configure the Nodes
92 node1 = ec.register_resource("omf::Node")
93 ec.set(node1, "hostname", nodex)
94 ec.set(node1, "xmppUser", slicename)
95 ec.set(node1, "xmppServer", gateway)
96 ec.set(node1, "xmppPort", "5222")
97 ec.set(node1, "xmppPassword", "1234")
98
99 # Create and Configure the Interfaces
100 iface1 = ec.register_resource("omf::WifiInterface")
101 ec.set(iface1, "name", "wlan0")
102 ec.set(iface1, "mode", "adhoc")
103 ec.set(iface1, "hw_mode", "g")
104 ec.set(iface1, "essid", "ping")
105 ec.set(iface1, "ip", "192.168.0.%s/24" % nodex[-2:]) 
106 ec.register_connection(node1, iface1)
107
108 # Create and Configure the Nodes
109 node2 = ec.register_resource("omf::Node")
110 ec.set(node2, "hostname", nodey)
111 ec.set(node2, "xmppUser", slicename)
112 ec.set(node2, "xmppServer", gateway)
113 ec.set(node2, "xmppPort", "5222")
114 ec.set(node2, "xmppPassword", "1234")
115
116 # Create and Configure the Interfaces
117 iface2 = ec.register_resource("omf::WifiInterface")
118 ec.set(iface2, "name", "wlan0")
119 ec.set(iface2, "mode", "adhoc")
120 ec.set(iface2, "hw_mode", "g")
121 ec.set(iface2, "essid", "ping")
122 ec.set(iface2, "ip", "192.168.0.%s/24" % nodey[-2:]) 
123 ec.register_connection(node2, iface2)
124
125 # Create and Configure the Channel
126 channel = ec.register_resource("omf::Channel")
127 ec.set(channel, "channel", chan)
128 ec.set(channel, "xmppUser", slicename)
129 ec.set(channel, "xmppServer", gateway)
130 ec.set(channel, "xmppPort", "5222")
131 ec.set(channel, "xmppPassword", "1234")
132 ec.register_connection(iface1, channel)
133 ec.register_connection(iface2, channel)
134
135 # Create and Configure the PING Application
136 app1 = ec.register_resource("omf::Application")
137 ec.set(app1, "appid", "Ping#1")
138 ec.set(app1, "command", "/bin/ping -c3 192.168.0.%s" % nodey[-2:])
139 ec.register_connection(app1, node1)
140
141 ## Make sure the ping stops after 30 seconds
142 ec.register_condition(app1, ResourceAction.STOP, app1, 
143         ResourceState.STARTED , "30s")
144
145 # Deploy
146 ec.deploy()
147
148 ec.wait_finished([app1])
149
150 # Retrieve the output of the ping command
151 ping_output = ec.trace(app1, "stdout")
152 print "\n PING OUTPUT\n", ping_output, "\n"
153
154 # Stop Experiment
155 ec.shutdown()
156