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