use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / omf / iminds_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 : iMinds
29 #
30 #     Node
31 #     node0ZZ 
32 #     0
33 #     |
34 #     |
35 #     0
36 #     Node
37 #     node0ZZ 
38 #     PING
39 #   
40 #      - Experiment:
41 #        - t0 : Deployment
42 #        - t1 : Ping Start
43 #        - t2 (t1 + 10s) : Ping stop
44 #        - t3 (t2 + 2s) : Kill the application
45 #
46
47 from __future__ import print_function
48
49 from nepi.execution.resource import ResourceAction, ResourceState
50 from nepi.execution.ec import ExperimentController
51
52 from optparse import OptionParser
53 import os
54
55 usage = ("usage: %prog -x <nodex> -z <nodez> -s <slice-name> -c <channel>")
56
57 parser = OptionParser(usage = usage)
58 parser.add_option("-x", "--nodex", dest="nodex", 
59         help="w-iLab.t first reserved node "
60             "(must be of form:  "
61             " nodex.<experiment_id>.<project_id>.wilab2.ilabt.iminds.be"
62             " all letters in lowercase )", 
63         type="str")
64 parser.add_option("-z", "--nodez", dest="nodez", 
65         help="w-iLab.t first reserved node "
66              "(must be of form:  "
67             " nodex.<experiment_id>.<project_id>.wilab2.ilabt.iminds.be"
68             " all letters in lowercase )", 
69         type="str")
70 parser.add_option("-s", "--slice-name", dest="slicename", 
71         help="Nitos slice name", type="str")
72 (options, args) = parser.parse_args()
73
74 nodex = options.nodex
75 nodez = options.nodez
76 slicename = options.slicename
77
78 # Create the EC
79 ec = ExperimentController(exp_id="iminds_omf6_ping")
80
81 # Create and Configure the Nodes
82
83 node1 = ec.register_resource("omf::Node")
84 ec.set(node1, "hostname", nodex)
85 ec.set(node1, "xmppUser", slicename)
86 ec.set(node1, "xmppServer", "xmpp.ilabt.iminds.be")
87 ec.set(node1, "xmppPort", "5222")
88 ec.set(node1, "xmppPassword", "1234")
89
90 iface1 = ec.register_resource("omf::WifiInterface")
91 ec.set(iface1, "name", "wlan0")
92 ec.set(iface1, "mode", "adhoc")
93 ec.set(iface1, "hw_mode", "g")
94 ec.set(iface1, "essid", "ping")
95 ec.set(iface1, "ip", "192.168.0.1/24")
96 ec.register_connection(iface1, node1)
97
98 node2 = ec.register_resource("omf::Node")
99 ec.set(node2, "hostname", nodez)
100 ec.set(node2, "xmppUser", slicename)
101 ec.set(node2, "xmppServer", "xmpp.ilabt.iminds.be")
102 ec.set(node2, "xmppPort", "5222")
103 ec.set(node2, "xmppPassword", "1234")
104
105 iface2 = ec.register_resource("omf::WifiInterface")
106 ec.set(iface2, "name", "wlan0")
107 ec.set(iface2, "mode", "adhoc")
108 ec.set(iface2, "hw_mode", "g")
109 ec.set(iface2, "essid", "ping")
110 ec.set(iface2, "ip", "192.168.0.2/24")
111 ec.register_connection(iface2, node2)
112
113 channel = ec.register_resource("omf::Channel")
114 ec.set(channel, "channel", "6")
115 ec.register_connection(iface1, channel)
116 ec.register_connection(iface2, channel)
117
118 # Create and Configure the Application
119 app1 = ec.register_resource("omf::Application")
120 ec.set(app1, "command", "ping -c3 192.168.0.2") 
121 ec.register_connection(app1, node1)
122
123 ## Make sure the ping stops after 30 seconds
124 ec.register_condition(app1, ResourceAction.STOP, app1, 
125         ResourceState.STARTED , "30s")
126
127 # Deploy
128 ec.deploy()
129
130 # Wait until the VLC client is finished
131 ec.wait_finished([app1])
132
133 # Retrieve the output of the ping command
134 ping_output = ec.trace(app1, "stdout")
135 print("\n PING OUTPUT\n", ping_output, "\n")
136
137 # Stop Experiment
138 ec.shutdown()
139