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