use print() - import print_function - should be fine for both py2 and py3
[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 __future__ import print_function
49
50 from nepi.execution.ec import ExperimentController
51 from nepi.execution.resource import ResourceAction, ResourceState
52
53 from optparse import OptionParser
54 import os
55
56 usage = ("usage: %prog -x <nodex> -y <nodey> -c <channel> -u <slicename> -g <nitos-gateway>")
57
58 parser = OptionParser(usage = usage)
59 parser.add_option("-x", "--nodex", dest="nodex", 
60         help="Nitos first reserved node "
61             "(e.g. hostname must be of form: omf.nitos.node0XX)", 
62         type="str")
63 parser.add_option("-y", "--nodey", dest="nodey", 
64         help="Nitos second reserved node "
65             "(e.g. hostname must be of form: omf.nitos.node0YY)", 
66         type="str")
67 parser.add_option("-c", "--channel", dest="channel", 
68         help="Nitos reserved channel",
69         type="str")
70 parser.add_option("-g", "--gateway", dest="gateway", 
71         help="Nitos gateway hostname", 
72         type="str", default="nitlab.inf.uth.gr")
73 parser.add_option("-u", "--slicename", dest="slicename", 
74         help="Nitos gateway username (slicename)", 
75         type="str")
76
77 (options, args) = parser.parse_args()
78
79 if not options.channel or not options.nodex or \
80         not options.nodey or not options.slicename:
81     parser.error("Missing argument channel, nodex, nodey, or slicename!")
82
83 nodex = options.nodex.split(".")[-1]
84 nodey = options.nodey.split(".")[-1]
85 slicename = options.slicename
86 chan = options.channel
87 gateway = options.gateway
88
89 # Create the EC
90 ec = ExperimentController(exp_id="nitos_omf6_ping")
91
92 # Create and Configure the Nodes
93 node1 = ec.register_resource("omf::Node")
94 ec.set(node1, "hostname", nodex)
95 ec.set(node1, "xmppUser", slicename)
96 ec.set(node1, "xmppServer", gateway)
97 ec.set(node1, "xmppPort", "5222")
98 ec.set(node1, "xmppPassword", "1234")
99
100 # Create and Configure the Interfaces
101 iface1 = ec.register_resource("omf::WifiInterface")
102 ec.set(iface1, "name", "wlan0")
103 ec.set(iface1, "mode", "adhoc")
104 ec.set(iface1, "hw_mode", "g")
105 ec.set(iface1, "essid", "ping")
106 ec.set(iface1, "ip", "192.168.0.%s/24" % nodex[-2:]) 
107 ec.register_connection(node1, iface1)
108
109 # Create and Configure the Nodes
110 node2 = ec.register_resource("omf::Node")
111 ec.set(node2, "hostname", nodey)
112 ec.set(node2, "xmppUser", slicename)
113 ec.set(node2, "xmppServer", gateway)
114 ec.set(node2, "xmppPort", "5222")
115 ec.set(node2, "xmppPassword", "1234")
116
117 # Create and Configure the Interfaces
118 iface2 = ec.register_resource("omf::WifiInterface")
119 ec.set(iface2, "name", "wlan0")
120 ec.set(iface2, "mode", "adhoc")
121 ec.set(iface2, "hw_mode", "g")
122 ec.set(iface2, "essid", "ping")
123 ec.set(iface2, "ip", "192.168.0.%s/24" % nodey[-2:]) 
124 ec.register_connection(node2, iface2)
125
126 # Create and Configure the Channel
127 channel = ec.register_resource("omf::Channel")
128 ec.set(channel, "channel", chan)
129 ec.set(channel, "xmppUser", slicename)
130 ec.set(channel, "xmppServer", gateway)
131 ec.set(channel, "xmppPort", "5222")
132 ec.set(channel, "xmppPassword", "1234")
133 ec.register_connection(iface1, channel)
134 ec.register_connection(iface2, channel)
135
136 # Create and Configure the PING Application
137 app1 = ec.register_resource("omf::Application")
138 ec.set(app1, "appid", "Ping#1")
139 ec.set(app1, "command", "/bin/ping -c3 192.168.0.%s" % nodey[-2:])
140 ec.register_connection(app1, node1)
141
142 ## Make sure the ping stops after 30 seconds
143 ec.register_condition(app1, ResourceAction.STOP, app1, 
144         ResourceState.STARTED , "30s")
145
146 # Deploy
147 ec.deploy()
148
149 ec.wait_finished([app1])
150
151 # Retrieve the output of the ping command
152 ping_output = ec.trace(app1, "stdout")
153 print("\n PING OUTPUT\n", ping_output, "\n")
154
155 # Stop Experiment
156 ec.shutdown()
157