7decb71f7fe4b95ee5afcbe8eb609f56284e996a
[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 nepi.execution.ec import ExperimentController
46 from nepi.execution.resource import ResourceAction, ResourceState
47
48 from optparse import OptionParser
49 import os
50
51 usage = ("usage: %prog -x <nodex> -z <nodez> -s <slice-name> -c <channel>")
52
53 parser = OptionParser(usage = usage)
54 parser.add_option("-x", "--nodex", dest="nodex", 
55         help="Nitos first reserved node "
56             "(e.g. hostname must be of form: omf.nitos.node0XX)", 
57         type="str")
58 parser.add_option("-z", "--nodez", dest="nodez", 
59         help="Nitos second reserved node "
60             "(e.g. hostname must be of form: omf.nitos.node0ZZ)", 
61         type="str")
62 parser.add_option("-c", "--channel", dest="channel", 
63         help="Nitos reserved channel",
64         type="str")
65 parser.add_option("-s", "--slice-name", dest="slicename", 
66         help="Nitos slice name", type="str")
67 (options, args) = parser.parse_args()
68
69 nodex = options.nodex
70 nodez = options.nodez
71 slicename = options.slicename
72 chan = options.channel
73
74 # Create the EC
75 ec = ExperimentController(exp_id="nitos_omf5_ping")
76
77 # Create and Configure the Nodes
78 node1 = ec.register_resource("omf::Node")
79 ec.set(node1, "hostname", nodex)
80 ec.set(node1, "xmppUser", slicename)
81 ec.set(node1, "xmppServer", "nitlab.inf.uth.gr")
82 ec.set(node1, "xmppPort", "5222")
83 ec.set(node1, "xmppPassword", "1234")
84 ec.set(node1, "version", "5")
85
86 # Create and Configure the Interfaces
87 iface1 = ec.register_resource("omf::WifiInterface")
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.set(iface1, "version", "5")
94 ec.register_connection(node1, iface1)
95
96 # Create and Configure the Channel
97 channel = ec.register_resource("omf::Channel")
98 ec.set(channel, "channel", chan)
99 ec.set(channel, "xmppUser", slicename)
100 ec.set(channel, "xmppServer", "nitlab.inf.uth.gr")
101 ec.set(channel, "xmppPort", "5222")
102 ec.set(channel, "xmppPassword", "1234")
103 ec.set(channel, "version", "5")
104 ec.register_connection(iface1, channel)
105
106 # Create and Configure the PING Application
107 app1 = ec.register_resource("omf::Application")
108 ec.set(app1, "appid", "Ping#1")
109 ec.set(app1, "command", "/bin/ping -c3 192.168.0.%s" % nodex[-2:])
110 ec.set(app1, "version", "5")
111 ec.register_connection(app1, node1)
112
113 app2 = ec.register_resource("omf::Application")
114 ec.set(app2, "appid", "Kill#1")
115 ec.set(app2, "command", "/usr/bin/killall ping")
116 ec.set(app2, "version", "5")
117 ec.register_connection(app2, node1)
118
119 # User Behaviour
120 ec.register_condition(app1, ResourceAction.STOP, app1, ResourceState.STARTED , "10s")
121 ec.register_condition(app2, ResourceAction.START, app1, ResourceState.STARTED , "12s")
122 ec.register_condition(app2, ResourceAction.STOP, app2, ResourceState.STARTED , "1s")
123
124 # Deploy
125 ec.deploy()
126
127 ec.wait_finished([app1, app2])
128
129 print ec.trace(app1, "stdout")
130
131 # Stop Experiment
132 ec.shutdown()
133