Fixing issues with serialization
[nepi.git] / examples / ccn_flooding / planetlab.py
1 #!/usr/bin/env python
2
3 ###############################################################################
4 #
5 #    NEPI, a framework to manage network experiments
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
24 from nepi.execution.ec import ExperimentController 
25 from nepi.execution.runner import ExperimentRunner
26 from nepi.util.netgraph import NetGraph, TopologyType
27 import nepi.data.processing.ccn.parser as ccn_parser
28
29 import socket
30 import os
31
32 PL_NODES = dict({
33     "0": "iraplab1.iralab.uni-karlsruhe.de",
34     "1": "planetlab1.informatik.uni-goettingen.de",
35     "2": "dfn-ple1.x-win.dfn.de",
36     "3": "mars.planetlab.haw-hamburg.de",
37     "4": "planetlab2.unineuchatel.ch", 
38     "5": "planetlab-node3.it-sudparis.eu",
39     "6": "planetlab2.extern.kuleuven.be",
40     "7": "node2pl.planet-lab.telecom-lille1.eu",
41     "8": "planetvs2.informatik.uni-stuttgart.de",
42     "9": "planetlab1.informatik.uni-wuerzburg.de",
43     "10": "planet1.l3s.uni-hannover.de",
44     "11": "planetlab1.wiwi.hu-berlin.de",
45     "12": "pl2.uni-rostock.de", 
46     "13": "planetlab1.u-strasbg.fr",
47     "14": "peeramidion.irisa.fr"
48     })
49
50 pl_slice = os.environ.get("PL_SLICE")
51 pl_user = os.environ.get("PL_USER")
52 pl_password = os.environ.get("PL_PASS")
53 pl_ssh_key = os.environ.get("PL_SSHKEY")
54
55 content_name = "ccnx:/test/bunny.ts"
56
57 pipeline = 4 # Default value for ccncat
58
59 operating_system = "f14"
60
61 country = "germany"
62
63 repofile = os.path.join(
64         os.path.dirname(os.path.realpath(__file__)), "repoFile1.0.8.2")
65
66 def add_collector(ec, trace_name, subdir, newname = None):
67     collector = ec.register_resource("Collector")
68     ec.set(collector, "traceName", trace_name)
69     ec.set(collector, "subDir", subdir)
70     if newname:
71         ec.set(collector, "rename", newname)
72
73     return collector
74
75 def add_pl_host(ec, nid):
76     hostname = PL_NODES[nid]
77
78     # Add a planetlab host to the experiment description
79     host = ec.register_resource("PlanetlabNode")
80     ec.set(host, "hostname", hostname)
81     ec.set(host, "username", pl_slice)
82     ec.set(host, "identity", pl_ssh_key)
83     #ec.set(host, "pluser", pl_user)
84     #ec.set(host, "plpassword", pl_password)
85     #ec.set(host, "country", country)
86     #ec.set(host, "operatingSystem", operating_system)
87     ec.set(host, "cleanExperiment", True)
88     ec.set(host, "cleanProcesses", True)
89
90     # Annotate the graph
91     ec.netgraph.annotate_node(nid, "hostname", hostname)
92     ec.netgraph.annotate_node(nid, "host", host)
93     
94     # Annotate the graph node with an ip address
95     ip = socket.gethostbyname(hostname)
96     ec.netgraph.annotate_node_ip(nid, ip)
97
98 def add_pl_ccnd(ec, nid):
99     # Retrieve annotation from netgraph
100     host = ec.netgraph.node_annotation(nid, "host")
101     
102     # Add a CCN daemon to the planetlab node
103     ccnd = ec.register_resource("LinuxCCND")
104     ec.set(ccnd, "debug", 7)
105     ec.register_connection(ccnd, host)
106     
107     # Collector to retrieve ccnd log
108     collector = add_collector(ec, "stderr", nid, "log")
109     ec.register_connection(collector, ccnd)
110
111     # Annotate the graph
112     ec.netgraph.annotate_node(nid, "ccnd", ccnd)
113
114 def add_pl_ccnr(ec, nid):
115     # Retrieve annotation from netgraph
116     ccnd = ec.netgraph.node_annotation(nid, "ccnd")
117     
118     # Add a CCN content repository to the planetlab node
119     ccnr = ec.register_resource("LinuxCCNR")
120
121     ec.set(ccnr, "repoFile1", repofile)
122     ec.register_connection(ccnr, ccnd)
123
124 def add_pl_ccncat(ec, nid):
125     # Retrieve annotation from netgraph
126     ccnd = ec.netgraph.node_annotation(nid, "ccnd")
127     
128     # Add a CCN cat application to the planetlab node
129     ccncat = ec.register_resource("LinuxCCNCat")
130     ec.set(ccncat, "pipeline", pipeline)
131     ec.set(ccncat, "contentName", content_name)
132     ec.register_connection(ccncat, ccnd)
133
134 def add_pl_fib_entry(ec, nid1, nid2):
135     # Retrieve annotations from netgraph
136     ccnd1 = ec.netgraph.node_annotation(nid1, "ccnd")
137     hostname2 = ec.netgraph.node_annotation(nid2, "hostname")
138     
139     # Add a FIB entry between one planetlab node and its peer
140     entry = ec.register_resource("LinuxFIBEntry")
141     ec.set(entry, "host", hostname2)
142     ec.register_connection(entry, ccnd1)
143
144     # Collector to retrieve peering ping output (to measure neighbors delay)
145     ec.enable_trace(entry, "ping")
146     collector = add_collector(ec, "ping", nid1)
147     ec.register_connection(collector, entry)
148
149     return entry
150
151 def avg_interests(ec, run):
152     ## Process logs
153     logs_dir = ec.run_dir
154
155     print ec.netgraph
156
157     (graph,
158         content_names,
159         interest_expiry_count,
160         interest_dupnonce_count,
161         interest_count,
162         content_count) = ccn_parser.process_content_history_logs(
163                 logs_dir, 
164                 ec.netgraph)
165
166     shortest_path = networkx.shortest_path(graph, 
167             source = ec.netgraph.sources()[0], 
168             target = ec.netgraph.targets()[0])
169
170     ### Compute metric: Avg number of Interests seen per content name
171     ###                 normalized by the number of nodes in the shortest path
172     content_name_count = len(content_names.values())
173     nodes_in_shortest_path = len(content_names.values())
174     metric = interest_count / float(content_name_count) / float(nodes_in_shortest_path)
175
176     # TODO: DUMP RESULTS TO FILE
177     # TODO: DUMP GRAPH DELAYS!
178
179     return metric
180
181 def add_pl_edge(ec, nid1, nid2):
182     #### Add connections between CCN nodes
183     add_pl_fib_entry(ec, nid1, nid2)
184     add_pl_fib_entry(ec, nid2, nid1)
185
186 def add_pl_node(ec, nid):
187     ### Add CCN nodes (ec.netgraph holds the topology graph)
188     add_pl_host(ec, nid)
189     add_pl_ccnd(ec, nid)
190         
191     if nid == ec.netgraph.targets()[0]:
192         add_pl_ccnr(ec, nid)
193
194     if nid == ec.netgraph.sources()[0]:
195         add_pl_ccncat(ec, nid)
196
197 if __name__ == '__main__':
198
199     #### Create NEPI Experiment Description with LINEAR topology 
200     ec = ExperimentController("pl_ccn", 
201             topo_type = TopologyType.LINEAR, 
202             node_count = 4, 
203             #assign_ips = True,
204             assign_st = True,
205             add_node_callback = add_pl_node, 
206             add_edge_callback = add_pl_edge)
207     
208     print "Results stored at", ec.exp_dir
209
210     #### Retrieve the content producing resource to wait for ot to finish
211     ccncat = ec.filter_resources("LinuxCCNCat")
212    
213     #### Run experiment until metric convergences
214     rnr = ExperimentRunner()
215     runs = rnr.run(ec, min_runs = 10, max_runs = 300, 
216             compute_metric_callback = avg_interests,
217             wait_guids = ccncat,
218             wait_time = 0)
219