Merge the openflow changes to the nepi-3-dev branch
[nepi.git] / examples / linux / ccn / ccn_simple_transfer.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 INRIA
4 #
5 #    This program is free software: you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
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 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 #
20 # Instructions to run this example:
21 #
22 # 1. First edit the script file where required (See ASSING messages)
23 #
24 # 2. Then, run the script:
25 #
26 # $ cd <path-to-nepi>
27 # $ PYTHONPATH=$PYTHONPATHS:src python examples/linux/ccn/two_nodes_file_retrieval.py
28 #
29
30 from nepi.execution.ec import ExperimentController
31
32 from optparse import OptionParser, SUPPRESS_HELP
33 import os
34
35 usage = ("usage: %prog -a <hostanme1> -b <hostname2> -u <username> -i <ssh-key>")
36
37 parser = OptionParser(usage = usage)
38 parser.add_option("-a", "--hostname1", dest="hostname1", 
39         help="Remote host 1", type="str")
40 parser.add_option("-b", "--hostname2", dest="hostname2", 
41         help="Remote host 2", type="str")
42 parser.add_option("-u", "--username", dest="username", 
43         help="Username to SSH to remote host", type="str")
44 parser.add_option("-i", "--ssh-key", dest="ssh_key", 
45         help="Path to private SSH key to be used for connection", 
46         type="str")
47 (options, args) = parser.parse_args()
48
49 hostname1 = options.hostname1
50 hostname2 = options.hostname2
51 username = options.username
52 ssh_key = options.ssh_key
53
54 ## Create the experiment controller
55 ec = ExperimentController(exp_id = "ccn_simple_transfer")
56
57 ## Register node 1
58 node1 = ec.register_resource("LinuxNode")
59 # Set the hostname of the first node to use for the experiment
60 ec.set(node1, "hostname", hostname1)
61 # username should be your SSH user 
62 ec.set(node1, "username", username)
63 # Absolute path to the SSH private key
64 ec.set(node1, "identity", ssh_key)
65 # Clean all files, results, etc, from previous experiments wit the same exp_id
66 ec.set(node1, "cleanExperiment", True)
67 # Kill all running processes in the node before running the experiment
68 ec.set(node1, "cleanProcesses", True)
69
70 ## Register node 2 
71 node2 = ec.register_resource("LinuxNode")
72 # Set the hostname of the first node to use for the experiment
73 ec.set(node2, "hostname", hostname2)
74 # username should be your SSH user 
75 ec.set(node2, "username", username)
76 # Absolute path to the SSH private key
77 ec.set(node2, "identity", ssh_key)
78 # Clean all files, results, etc, from previous experiments wit the same exp_id
79 ec.set(node2, "cleanExperiment", True)
80 # Kill all running processes in the node before running the experiment
81 ec.set(node2, "cleanProcesses", True)
82
83 ## Register a CCN daemon in node 1
84 ccnd1 = ec.register_resource("LinuxCCND")
85 # Set ccnd log level to 7
86 ec.set(ccnd1, "debug", 7)
87 ec.register_connection(ccnd1, node1)
88
89 ## Register a CCN daemon in node 2
90 ccnd2 = ec.register_resource("LinuxCCND")
91 # Set ccnd log level to 7
92 ec.set(ccnd2, "debug", 7)
93 ec.register_connection(ccnd2, node2)
94
95 ## Register a repository in node 1
96 ccnr1 = ec.register_resource("LinuxCCNR")
97 ec.register_connection(ccnr1, ccnd1)
98
99 ## Push the file into the repository
100 local_path_to_content = os.path.join(
101         os.path.dirname(os.path.realpath(__file__)),
102             "..", "..",
103             "big_buck_bunny_240p_mpeg4_lq.ts")
104
105 # Register a FIB entry from node 1 to node 2
106 co = ec.register_resource("LinuxCCNContent")
107 ec.set(co, "contentName", "ccnx:/test/FILE1")
108 # NEPI will upload the specified file to the remote node and write it
109 # into the CCN repository
110 ec.set(co, "content", local_path_to_content)
111 ec.register_connection(co, ccnr1)
112
113 # Register a FIB entry from node 1 to node 2
114 entry1 = ec.register_resource("LinuxFIBEntry")
115 ec.set(entry1, "host", hostname2)
116 ec.register_connection(entry1, ccnd1)
117
118 # Register a FIB entry from node 2 to node 1
119 entry2 = ec.register_resource("LinuxFIBEntry")
120 ec.set(entry2, "host", hostname1)
121 ec.register_connection(entry2, ccnd2)
122
123 ## Retrieve the file stored in node 1 from node 2
124 command = "ccncat ccnx:/test/FILE1"
125 app = ec.register_resource("LinuxCCNApplication")
126 ec.set(app, "command", command)
127 ec.register_connection(app, ccnd2)
128
129 # Register a collector to automatically collect the ccnd logs
130 # to a local directory
131 col1 = ec.register_resource("Collector")
132 ec.set(col1, "traceName", "stderr")
133 ec.set(col1, "subDir", hostname1)
134 ec.register_connection(col1, ccnd1)
135
136 col2 = ec.register_resource("Collector")
137 ec.set(col2, "traceName", "stderr")
138 ec.set(col2, "subDir", hostname2)
139 ec.register_connection(col2, ccnd2)
140
141 print "Results stored at", ec.exp_dir
142
143 ## Deploy all resources
144 ec.deploy()
145
146 # Wait until the ccncat is finished
147 ec.wait_finished([app])
148
149 ec.shutdown()