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