Removing +x from examples
[nepi.git] / examples / linux / ccn / ccncat_extended_ring_topo.py
1 #!/usr/bin/env python
2
3 #
4 #    NEPI, a framework to manage network experiments
5 #    Copyright (C) 2013 INRIA
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
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 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
21
22 #
23 # CCN topology:
24 #
25 #                h2
26 #                0 
27 #  content   l1 / \ l2         ccncat
28 #  b1          /l5 \           b2
29 #  0 ----- h1 0 --- 0 h3 ------ 0
30 #              \   / 
31 #            l4 \ / l3
32 #                0
33 #                h4
34 # Experiment:
35 # - t0 : b2 retrives video published in b1
36 # - t1 : l1 goes down
37 # - t2 : l2 goes down
38 # - t3 : l5 goes up
39 #
40
41 from nepi.execution.ec import ExperimentController, ECState 
42 from nepi.execution.resource import ResourceState, ResourceAction 
43 from nepi.execution.trace import TraceAttr
44
45 import subprocess
46 from optparse import OptionParser, SUPPRESS_HELP
47
48 import os
49 import time
50
51 def add_node(ec, host, user, ssh_key = None):
52     node = ec.register_resource("LinuxNode")
53     ec.set(node, "hostname", host)
54     ec.set(node, "username", user)
55     ec.set(node, "identity", ssh_key)
56     ec.set(node, "cleanHome", True)
57     ec.set(node, "cleanProcesses", True)
58     return node
59
60 def add_ccnd(ec, node):
61     ccnd = ec.register_resource("LinuxCCND")
62     ec.set(ccnd, "debug", 7)
63     ec.register_connection(ccnd, node)
64     return ccnd
65
66 def add_ccnr(ec, ccnd):
67     ccnr = ec.register_resource("LinuxCCNR")
68     ec.register_connection(ccnr, ccnd)
69     return ccnr
70
71 def add_fib_entry(ec, ccnd, peer_host):
72     entry = ec.register_resource("LinuxFIBEntry")
73     ec.set(entry, "host", peer_host)
74     ec.register_connection(entry, ccnd)
75     return entry
76
77 def add_content(ec, ccnr, content_name, content):
78     co = ec.register_resource("LinuxCCNContent")
79     ec.set(co, "contentName", content_name)
80     ec.set(co, "content", content)
81     ec.register_connection(co, ccnr)
82     return co
83
84 def add_stream(ec, ccnd, content_name):
85     # ccnx v7.2 issue 101007 
86     command = "ccnpeek %(content_name)s; ccncat %(content_name)s" % {
87             "content_name" : content_name}
88
89     app = ec.register_resource("LinuxCCNApplication")
90     ec.set(app, "command", command)
91     ec.register_connection(app, ccnd)
92
93     return app
94
95 def add_collector(ec, trace_name, store_dir):
96     collector = ec.register_resource("Collector")
97     ec.set(collector, "traceName", trace_name)
98     ec.set(collector, "storeDir", store_dir)
99
100     return collector
101
102 def get_options():
103     pl_slice = os.environ.get("PL_SLICE")
104
105     # We use a specific SSH private key for PL if the PL_SSHKEY is specified or the
106     # id_rsa_planetlab exists 
107     default_key = "%s/.ssh/id_rsa_planetlab" % (os.environ['HOME'])
108     default_key = default_key if os.path.exists(default_key) else None
109     pl_ssh_key = os.environ.get("PL_SSHKEY", default_key)
110
111     usage = "usage: %prog -s <pl-user> -m <movie> -e <exp-id> -i <ssh_key> -r <results>"
112
113     parser = OptionParser(usage=usage)
114     parser.add_option("-s", "--pl-user", dest="pl_user", 
115             help="PlanetLab slicename", default = pl_slice, type="str")
116     parser.add_option("-m", "--movie", dest="movie", 
117             help="Stream movie", type="str")
118     parser.add_option("-e", "--exp-id", dest="exp_id", 
119             help="Label to identify experiment", type="str")
120     parser.add_option("-i", "--pl-ssh-key", dest="pl_ssh_key", 
121             help="Path to private SSH key to be used for connection", 
122             default = pl_ssh_key, type="str")
123     parser.add_option("-r", "--results", dest="results", default = "/tmp",  
124             help="Path to directory where to store results", type="str") 
125
126     (options, args) = parser.parse_args()
127
128     if not options.movie:
129         parser.error("movie is a required argument")
130
131     return (options.pl_user, options.movie, options.exp_id, options.pl_ssh_key,
132             options.results)
133
134 if __name__ == '__main__':
135     content_name = "ccnx:/test/VIDEO"
136     
137     ( pl_user, movie, exp_id, pl_ssh_key, results_dir ) = get_options()
138
139     ec = ExperimentController(exp_id = exp_id)
140
141     # hosts in the US
142     #host1 = "planetlab4.wail.wisc.edu"
143     #host2 = "planetlab2.cs.columbia.edu"
144     #host3 = "ricepl-2.cs.rice.edu"
145     #host4 = "node1.planetlab.albany.edu"
146     #host5 = "earth.cs.brown.edu"
147     #host6 = "planetlab2.engr.uconn.edu"
148
149     # hosts in EU
150     host1 = "planetlab2.fct.ualg.pt"
151     host2 = "planet2.unipr.it"
152     host3 = "planetlab1.aston.ac.uk"
153     host4 = "itchy.comlab.bth.se"
154     host5 = "rochefort.infonet.fundp.ac.be"
155     host6 = "planetlab1.u-strasbg.fr"
156
157     # describe nodes in the central ring 
158     ring_hosts = [host1, host2, host3, host4]
159     ccnds = dict()
160
161     for i in xrange(len(ring_hosts)):
162         host = ring_hosts[i]
163         node = add_node(ec, host, pl_user, pl_ssh_key)
164         ccnd = add_ccnd(ec, node)
165         ccnr = add_ccnr(ec, ccnd)
166         ccnds[host] = ccnd
167     
168     ## Add ccn ring links
169     # l1 : h1 - h2 , h2 - h1
170     l1u = add_fib_entry(ec, ccnds[host1], host2)
171     l1d = add_fib_entry(ec, ccnds[host2], host1)
172
173     # l2 : h2 - h3 , h3 - h2
174     l2u = add_fib_entry(ec, ccnds[host2], host3)
175     l2d = add_fib_entry(ec, ccnds[host3], host2)
176
177     # l3 : h3 - h4 , h4 - h3
178     l3u = add_fib_entry(ec, ccnds[host3], host4)
179     l3d = add_fib_entry(ec, ccnds[host4], host3)
180
181     # l4 : h4 - h1 , h1 - h4
182     l4u = add_fib_entry(ec, ccnds[host4], host1)
183     l4d = add_fib_entry(ec, ccnds[host1], host4)
184
185     # l5 : h1 - h3 , h3 - h1
186     l5u = add_fib_entry(ec, ccnds[host1], host3)
187     l5d = add_fib_entry(ec, ccnds[host3], host1)
188     
189     # border node 1
190     bnode1 = add_node(ec, host5, pl_user, pl_ssh_key)
191     ccndb1 = add_ccnd(ec, bnode1)
192     ccnrb1 = add_ccnr(ec, ccndb1)
193     ccnds[host5] = ccndb1
194     co = add_content(ec, ccnrb1, content_name, movie)
195
196     # border node 2
197     bnode2 = add_node(ec, host6, pl_user, pl_ssh_key)
198     ccndb2 = add_ccnd(ec, bnode2)
199     ccnrb2 = add_ccnr(ec, ccndb2)
200     ccnds[host6] = ccndb2
201     app = add_stream(ec, ccndb2, content_name)
202  
203     # connect border nodes
204     add_fib_entry(ec, ccndb1, host1)
205     add_fib_entry(ec, ccnds[host1], host5)
206
207     add_fib_entry(ec, ccndb2, host3)
208     add_fib_entry(ec, ccnds[host3], host6)
209
210     # Put down l5 10s after transfer started
211     ec.register_condition(l5u, ResourceAction.STOP, 
212             app, ResourceState.STARTED, time = "10s")
213     ec.register_condition(l5d, ResourceAction.STOP, 
214             app, ResourceState.STARTED, time = "10s")
215
216     # Register a collector to automatically collect traces
217     collector = add_collector(ec, "stderr", results_dir)
218     for ccnd in ccnds.values():
219         ec.register_connection(collector, ccnd)
220
221     # deploy all ResourceManagers
222     ec.deploy()
223
224     # Wait until ccncat has started retrieving the content
225     ec.wait_started([app])
226
227     rvideo_path = ec.trace(app, "stdout", attr = TraceAttr.PATH)
228     command = 'tail -f %s' % rvideo_path
229
230     # pulling the content of the video received
231     # on b2, to stream it locally
232     proc1 = subprocess.Popen(['ssh',
233         '-o', 'StrictHostKeyChecking=no',
234         '-l', pl_user, host6,
235         command],
236         stdout = subprocess.PIPE, 
237         stderr = subprocess.PIPE)
238     
239     proc2 = subprocess.Popen(['vlc', 
240         '--ffmpeg-threads=1',
241         '--sub-filter', 'marq', 
242         '--marq-marquee', 
243         '(c) copyright 2008, Blender Foundation / www.bigbuckbunny.org', 
244         '--marq-position=8', 
245         '--no-video-title-show', '-'], 
246         stdin=proc1.stdout, 
247         stdout=subprocess.PIPE, 
248         stderr=subprocess.PIPE)
249
250     (stdout, stderr) = proc2.communicate()
251
252     # shutdown the experiment controller
253     ec.shutdown()
254