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