NS3Client: replacing socat for ssh
[nepi.git] / src / nepi / resources / linux / ns3 / ns3simulation.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 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.trace import Trace, TraceAttr
22 from nepi.execution.resource import ResourceManager, clsinit_copy, \
23         ResourceState, reschedule_delay
24 from nepi.resources.linux.application import LinuxApplication
25 from nepi.util.timefuncs import tnow, tdiffsec
26 from nepi.resources.ns3.ns3simulation import NS3Simulation
27 from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID, GLOBAL_VALUE_UUID, \
28         IPV4_GLOBAL_ROUTING_HELPER_UUID
29 from nepi.resources.linux.ns3.ns3client import LinuxNS3Client
30
31 import os
32 import time
33
34 @clsinit_copy
35 class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
36     _rtype = "LinuxNS3Simulation"
37
38     @classmethod
39     def _register_attributes(cls):
40         impl_type = Attribute("simulatorImplementationType",
41                 "The object class to use as the simulator implementation",
42             allowed = ["ns3::DefaultSimulatorImpl", "ns3::RealtimeSimulatorImpl"],
43             default = "ns3::DefaultSimulatorImpl",
44             type = Types.Enumerate,
45             flags = Flags.Design)
46
47         sched_type = Attribute("schedulerType",
48                 "The object class to use as the scheduler implementation",
49                 allowed = ["ns3::MapScheduler",
50                             "ns3::ListScheduler",
51                             "ns3::HeapScheduler",
52                             "ns3::MapScheduler",
53                             "ns3::CalendarScheduler"
54                     ],
55             default = "ns3::MapScheduler",
56             type = Types.Enumerate,
57             flags = Flags.Design)
58
59         check_sum = Attribute("checksumEnabled",
60                 "A global switch to enable all checksums for all protocols",
61             default = False,
62             type = Types.Bool,
63             flags = Flags.Design)
64
65         ns_log = Attribute("nsLog",
66             "NS_LOG environment variable. " \
67                     " Will only generate output if ns-3 is compiled in DEBUG mode. ",
68             flags = Flags.Design)
69
70         verbose = Attribute("verbose",
71             "True to output debugging info from the ns3 client-server communication",
72             type = Types.Bool,
73             flags = Flags.Design)
74
75         build_mode = Attribute("buildMode",
76             "Mode used to build ns-3 with waf. One if: debug, release, oprimized ",
77             default = "release", 
78             allowed = ["debug", "release", "optimized"],
79             type = Types.Enumerate,
80             flags = Flags.Design)
81
82         ns3_version = Attribute("ns3Version",
83             "Version of ns-3 to install from nsam repo",
84             default = "ns-3.19", 
85             flags = Flags.Design)
86
87         pybindgen_version = Attribute("pybindgenVersion",
88             "Version of pybindgen to install from bazar repo",
89             default = "834", 
90             flags = Flags.Design)
91
92         populate_routing_tables = Attribute("populateRoutingTables",
93             "Invokes  Ipv4GlobalRoutingHelper.PopulateRoutingTables() ",
94             default = False,
95             type = Types.Bool,
96             flags = Flags.Design)
97
98         cls._register_attribute(impl_type)
99         cls._register_attribute(sched_type)
100         cls._register_attribute(check_sum)
101         cls._register_attribute(ns_log)
102         cls._register_attribute(verbose)
103         cls._register_attribute(build_mode)
104         cls._register_attribute(ns3_version)
105         cls._register_attribute(pybindgen_version)
106         cls._register_attribute(populate_routing_tables)
107
108     def __init__(self, ec, guid):
109         LinuxApplication.__init__(self, ec, guid)
110         NS3Simulation.__init__(self)
111
112         self._client = None
113         self._home = "ns3-simu-%s" % self.guid
114         self._socket_name = "ns3simu-%s" % os.urandom(8).encode('hex')
115
116     @property
117     def socket_name(self):
118         return self._socket_name
119
120     @property
121     def remote_socket(self):
122         return os.path.join(self.run_home, self.socket_name)
123
124     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
125         self._client.flush() 
126         return LinuxApplication.trace(self, name, attr, block, offset)
127
128     def upload_sources(self):
129         self.node.mkdir(os.path.join(self.node.src_dir, "ns3wrapper"))
130
131         # upload ns3 wrapper python script
132         ns3_wrapper = os.path.join(os.path.dirname(__file__), "..", "..", "ns3", 
133                 "ns3wrapper.py")
134
135         self.node.upload(ns3_wrapper,
136                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper.py"),
137                 overwrite = False)
138
139         # upload ns3_server python script
140         ns3_server = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
141                 "ns3server.py")
142
143         self.node.upload(ns3_server,
144                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3server.py"),
145                 overwrite = False)
146
147         if self.node.use_rpm:
148             # upload pygccxml sources
149             pygccxml_tar = os.path.join(os.path.dirname(__file__), "dependencies",
150                     "%s.tar.gz" % self.pygccxml_version)
151
152             self.node.upload(pygccxml_tar,
153                     os.path.join(self.node.src_dir, "%s.tar.gz" % self.pygccxml_version),
154                     overwrite = False)
155
156         # Upload user defined ns-3 sources
157         self.node.mkdir(os.path.join(self.node.src_dir, "ns-3"))
158         src_dir = os.path.join(self.node.src_dir, "ns-3")
159
160         super(LinuxNS3Simulation, self).upload_sources(src_dir = src_dir)
161
162     def upload_start_command(self):
163         command = self.get("command")
164         env = self.get("env")
165
166         # We want to make sure the ccnd is running
167         # before the experiment starts.
168         # Run the command as a bash script in background,
169         # in the host ( but wait until the command has
170         # finished to continue )
171         env = self.replace_paths(env)
172         command = self.replace_paths(command)
173
174         shfile = os.path.join(self.app_home, "start.sh")
175         self.node.upload_command(command, 
176                     shfile = shfile,
177                     env = env,
178                     overwrite = True)
179
180         # Run the ns3wrapper 
181         self._run_in_background()
182
183     def configure(self):
184         if self.has_changed("simulatorImplementationType"):
185             simu_type = self.get("simulatorImplementationType")
186             stype = self.create("StringValue", simu_type)
187             self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
188
189         if self.has_changed("checksumEnabled"):
190             check_sum = self.get("checksumEnabled")
191             btrue = self.create("BooleanValue", check_sum)    
192             self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
193         
194         if self.has_changed("schedulerType"):
195             sched_type = self.get("schedulerType")
196             stype = self.create("StringValue", sched_type)
197             self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
198        
199     def do_deploy(self):
200         if not self.node or self.node.state < ResourceState.READY:
201             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
202             
203             # ccnd needs to wait until node is deployed and running
204             self.ec.schedule(reschedule_delay, self.deploy)
205         else:
206             if not self.get("command"):
207                 self.set("command", self._start_command)
208             
209             if not self.get("depends"):
210                 self.set("depends", self._dependencies)
211
212             if self.get("sources"):
213                 sources = self.get("sources")
214                 source = sources.split(" ")[0]
215                 basename = os.path.basename(source)
216                 version = ( basename.strip().replace(".tar.gz", "")
217                     .replace(".tar","")
218                     .replace(".gz","")
219                     .replace(".zip","") )
220
221                 self.set("ns3Version", version)
222                 self.set("sources", source)
223
224             if not self.get("build"):
225                 self.set("build", self._build)
226
227             if not self.get("install"):
228                 self.set("install", self._install)
229
230             if not self.get("env"):
231                 self.set("env", self._environment)
232
233             self.do_discover()
234             self.do_provision()
235
236             # Create client
237             self._client = LinuxNS3Client(self)
238
239             self.configure()
240             
241             self.set_ready()
242
243     def do_start(self):
244         """ Starts simulation execution
245
246         """
247         self.info("Starting")
248
249         if self.state == ResourceState.READY:
250             if self.get("populateRoutingTables") == True:
251                 self.invoke(IPV4_GLOBAL_ROUTING_HELPER_UUID, "PopulateRoutingTables")
252
253             self._client.start() 
254
255             self.set_started()
256         else:
257             msg = " Failed to execute command '%s'" % command
258             self.error(msg, out, err)
259             raise RuntimeError, msg
260
261     def do_stop(self):
262         """ Stops simulation execution
263
264         """
265         if self.state == ResourceState.STARTED:
266             self._client.stop() 
267             self.set_stopped()
268
269     def do_release(self):
270         self.info("Releasing resource")
271
272         tear_down = self.get("tearDown")
273         if tear_down:
274             self.node.execute(tear_down)
275
276         self.do_stop()
277         self._client.shutdown()
278         LinuxApplication.do_stop(self)
279         
280         super(LinuxApplication, self).do_release()
281
282     @property
283     def _start_command(self):
284         command = [] 
285         command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
286
287         command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % self.remote_socket )
288
289         ns_log = self.get("nsLog")
290         if ns_log:
291             command.append("-L %s" % ns_log)
292
293         if self.get("verbose"):
294             command.append("-v")
295
296         command = " ".join(command)
297         return command
298
299     @property
300     def _dependencies(self):
301         if self.node.use_rpm:
302             return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml unzip")
303         elif self.node.use_deb:
304             return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml unzip")
305         return ""
306
307     @property
308     def ns3_repo(self):
309         return "http://code.nsnam.org"
310
311     @property
312     def pygccxml_version(self):
313         return "pygccxml-1.0.0"
314
315     @property
316     def _build(self):
317         # If the user defined local sources for ns-3, we uncompress the sources
318         # on the remote sources directory. Else we clone ns-3 from the official repo.
319         source = self.get("sources")
320         if not source:
321             copy_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s ${SRC}/ns-3/%(ns3_version)s" \
322                     % ({
323                         'ns3_version': self.get("ns3Version"),
324                         'ns3_repo':  self.ns3_repo,       
325                        })
326         else:
327             if source.find(".tar.gz") > -1:
328                 copy_ns3_cmd = ( 
329                             "tar xzf ${SRC}/ns-3/%(basename)s " 
330                             " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
331                             ) % ({
332                                 'basename': os.path.basename(source),
333                                 'ns3_version': self.get("ns3Version"),
334                                 })
335             elif source.find(".tar") > -1:
336                 copy_ns3_cmd = ( 
337                             "tar xf ${SRC}/ns-3/%(basename)s " 
338                             " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
339                             ) % ({
340                                 'basename': os.path.basename(source),
341                                 'ns3_version': self.get("ns3Version"),
342                                 })
343             elif source.find(".zip") > -1:
344                 basename = os.path.basename(source)
345                 bare_basename = basename.replace(".zip", "") \
346                         .replace(".tar", "") \
347                         .replace(".tar.gz", "")
348
349                 copy_ns3_cmd = ( 
350                             "unzip ${SRC}/ns-3/%(basename)s && "
351                             "mv ${SRC}/ns-3/%(bare_basename)s ${SRC}/ns-3/%(ns3_version)s "
352                             ) % ({
353                                 'bare_basename': basename_name,
354                                 'basename': basename,
355                                 'ns3_version': self.get("ns3Version"),
356                                 })
357
358         return (
359                 # Test if ns-3 is alredy installed
360                 " ( "
361                 " (( "
362                 "    ( test -d ${SRC}/ns-3/%(ns3_version)s ) || "
363                 "    ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) "
364                 "  ) && echo 'binaries found, nothing to do' )"
365                 " ) "
366                 "  || " 
367                 # If not, install ns-3 and its dependencies
368                 " (   "
369                 # Install pygccxml
370                 "   (   "
371                 "     ( "
372                 "       python -c 'import pygccxml' && "
373                 "       echo 'pygccxml not found' "
374                 "     ) "
375                 "      || "
376                 "     ( "
377                 "       tar xf ${SRC}/%(pygccxml_version)s.tar.gz -C ${SRC} && "
378                 "       cd ${SRC}/%(pygccxml_version)s && "
379                 "       sudo -S python setup.py install "
380                 "     ) "
381                 "   ) " 
382                 # Install pybindgen
383                 "  && "
384                 "   (   "
385                 "     ( "
386                 "       test -d ${SRC}/pybindgen/%(pybindgen_version)s && "
387                 "       echo 'binaries found, nothing to do' "
388                 "     ) "
389                 "      || "
390                 # If not, clone and build
391                 "      ( cd ${SRC} && "
392                 "        mkdir -p ${SRC}/pybindgen && "
393                 "        bzr checkout lp:pybindgen -r %(pybindgen_version)s ${SRC}/pybindgen/%(pybindgen_version)s && "
394                 "        cd ${SRC}/pybindgen/%(pybindgen_version)s && "
395                 "        ./waf configure && "
396                 "        ./waf "
397                 "      ) "
398                 "   ) " 
399                 " && "
400                 # Get ns-3 source code
401                 "  ( "
402                 "     mkdir -p ${SRC}/ns-3/%(ns3_version)s && "
403                 "     %(copy_ns3_cmd)s "
404                 "  ) "
405                 " ) "
406              ) % ({ 
407                     'ns3_version': self.get("ns3Version"),
408                     'pybindgen_version': self.get("pybindgenVersion"),
409                     'pygccxml_version': self.pygccxml_version,
410                     'copy_ns3_cmd': copy_ns3_cmd,
411                  })
412
413     @property
414     def _install(self):
415         return (
416                  # Test if ns-3 is alredy cloned
417                 " ( "
418                 "  ( ( (test -d ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build ) || "
419                 "    (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && "
420                 "    echo 'binaries found, nothing to do' )"
421                 " ) "
422                 " ||" 
423                 " (   "
424                  # If not, copy ns-3 build to bin
425                 "  cd ${SRC}/ns-3/%(ns3_version)s && "
426                 "  ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s && "
427                 "  ./waf && "
428                 "  mkdir -p ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s && "
429                 "  mv ${SRC}/ns-3/%(ns3_version)s/build ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build "
430                 " )"
431              ) % ({ 
432                     'ns3_version': self.get("ns3Version"),
433                     'pybindgen_version': self.get("pybindgenVersion"),
434                     'build_mode': self.get("buildMode"),
435                  })
436
437     @property
438     def _environment(self):
439         env = []
440         env.append("NS3BINDINGS=${NS3BINDINGS:=${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build/bindings/python/}" % ({ 
441                     'ns3_version': self.get("ns3Version"),
442                     'build_mode': self.get("buildMode")
443                  }))
444         env.append("NS3LIBRARIES=${NS3LIBRARIES:=${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build/}" % ({ 
445                     'ns3_version': self.get("ns3Version"),
446                     'build_mode': self.get("buildMode")
447                  }))
448
449         return " ".join(env) 
450
451     def valid_connection(self, guid):
452         # TODO: Validate!
453         return True
454