Bugfixing for ns3::MatrixPropagationLossModel ...
[nepi.git] / src / nepi / testbeds / ns3 / execute.py
1 # -*- coding: utf-8 -*-
2
3 from util import  _get_ipv4_protocol_guid, _get_node_guid, _get_dev_number
4 from nepi.core import testbed_impl
5 from nepi.core.attributes import Attribute
6 from constants import TESTBED_ID, TESTBED_VERSION
7 from nepi.util.constants import TIME_NOW, TestbedStatus as TS
8 import os
9 import sys
10 import threading
11 import random
12 import socket
13 import weakref
14
15 def load_ns3_module():
16     import sys
17     if 'ns3' in sys.modules:
18         return
19
20     import ctypes
21     import imp
22     import re
23
24     bindings = os.environ["NEPI_NS3BINDINGS"] \
25                 if "NEPI_NS3BINDINGS" in os.environ else None
26     libdir = os.environ["NEPI_NS3LIBRARY"] \
27                 if "NEPI_NS3LIBRARY" in os.environ else None
28
29     if libdir:
30         files = os.listdir(libdir)
31         regex = re.compile("(.*\.so)$")
32         libs = [m.group(1) for filename in files for m in [regex.search(filename)] if m]
33
34         libscp = list(libs)
35         while len(libs) > 0:
36             for lib in libscp:
37                 libfile = os.path.join(libdir, lib)
38                 try:
39                     ctypes.CDLL(libfile, ctypes.RTLD_GLOBAL)
40                     libs.remove(lib)
41                 except:
42                     pass
43             # if did not load any libraries in the last iteration
44             if len(libscp) == len(libs):
45                 raise RuntimeError("Imposible to load shared libraries %s" % str(libs))
46             libscp = list(libs)
47
48     if bindings:
49         sys.path.append(bindings)
50
51     import ns3_bindings_import as mod
52     sys.modules["ns3"] = mod
53
54 class TestbedController(testbed_impl.TestbedController):
55     from nepi.util.tunchannel_impl import TunChannel
56     
57     LOCAL_FACTORIES = {
58         'ns3::Nepi::TunChannel' : TunChannel,
59     }
60    
61     DUMMY_FACTORIES = ['ns3::Nepi::MobilityPair']
62
63     LOCAL_TYPES = tuple(LOCAL_FACTORIES.values())
64
65     def __init__(self):
66         super(TestbedController, self).__init__(TESTBED_ID, TESTBED_VERSION)
67         self._ns3 = None
68         self._home_directory = None
69         self._traces = dict()
70         self._simulator_thread = None
71         self._condition = None
72
73     @property
74     def home_directory(self):
75         return self._home_directory
76
77     @property
78     def ns3(self):
79         return self._ns3
80
81     def do_setup(self):
82         self._home_directory = self._attributes.\
83             get_attribute_value("homeDirectory")
84         self._ns3 = self._configure_ns3_module()
85         
86         # create home...
87         home = os.path.normpath(self.home_directory)
88         if not os.path.exists(home):
89             os.makedirs(home, 0755)
90         
91         super(TestbedController, self).do_setup()
92
93     def start(self):
94         super(TestbedController, self).start()
95         self._condition = threading.Condition()
96         self._simulator_thread = threading.Thread(target = self._simulator_run,
97                 args = [self._condition])
98         self._simulator_thread.setDaemon(True)
99         self._simulator_thread.start()
100
101     def stop(self, time = TIME_NOW):
102         super(TestbedController, self).stop(time)
103         self._stop_simulation(time)
104
105     def set(self, guid, name, value, time = TIME_NOW):
106         super(TestbedController, self).set(guid, name, value, time)
107
108         # TODO: take on account schedule time for the task
109         factory_id = self._create[guid]
110         if factory_id in self.DUMMY_FACTORIES:
111             return 
112
113         factory = self._factories[factory_id]
114         element = self._elements[guid]
115         if factory_id in self.LOCAL_FACTORIES:
116             setattr(element, name, value)
117         elif not factory.box_attributes.is_attribute_metadata(name):
118             if name == "Up":
119                 ipv4_guid =  _get_ipv4_protocol_guid(self, guid)
120                 if not ipv4_guid in self._elements:
121                     return
122                 ipv4 = self._elements[ipv4_guid]
123                 if value == False:
124                     nint = ipv4.GetNInterfaces()
125                     for i in xrange(0, nint):
126                         ipv4.SetDown(i)
127                 else:
128                     nint = ipv4.GetNInterfaces()
129                     for i in xrange(0, nint):
130                         ipv4.SetUp(i)
131             else:
132                 ns3_value = self._to_ns3_value(guid, name, value)
133                 self._set_attribute(name, ns3_value, element)
134
135     def get(self, guid, name, time = TIME_NOW):
136         value = super(TestbedController, self).get(guid, name, time)
137
138         # TODO: take on account schedule time for the task
139         factory_id = self._create[guid]
140         if factory_id in self.DUMMY_FACTORIES:
141             return 
142
143         factory = self._factories[factory_id]
144         element = self._elements[guid]
145         if factory_id in self.LOCAL_FACTORIES:
146             if hasattr(element, name):
147                 return getattr(element, name)
148             else:
149                 return value
150         else: 
151             if name == "Up":
152                 ipv4_guid =  _get_ipv4_protocol_guid(self, guid)
153                 if not ipv4_guid in self._elements:
154                     return True
155                 ipv4 = self._elements[ipv4_guid]
156                 nint = ipv4.GetNInterfaces()
157                 value = True
158                 for i in xrange(0, nint):
159                     value = ipv4.IsUp(i)
160                     if not value: break
161                 return value
162
163         if factory.box_attributes.is_attribute_metadata(name):
164             return value
165
166         TypeId = self.ns3.TypeId()
167         typeid = TypeId.LookupByName(factory_id)
168         info = TypeId.AttributeInformation()
169         if not typeid or not typeid.LookupAttributeByName(name, info):
170             raise AttributeError("Invalid attribute %s for element type %d" % \
171                 (name, guid))
172         checker = info.checker
173         ns3_value = checker.Create() 
174         self._get_attribute(name, ns3_value, element)
175         value = ns3_value.SerializeToString(checker)
176         attr_type = factory.box_attributes.get_attribute_type(name)
177
178         if attr_type == Attribute.INTEGER:
179             return int(value)
180         if attr_type == Attribute.DOUBLE:
181             return float(value)
182         if attr_type == Attribute.BOOL:
183             return value == "true"
184         return value
185
186     def action(self, time, guid, action):
187         raise NotImplementedError
188
189     def trace_filepath(self, guid, trace_id):
190         filename = self._traces[guid][trace_id]
191         return os.path.join(self.home_directory, filename)
192
193     def trace_filename(self, guid, trace_id):
194         return self._traces[guid][trace_id]
195
196     def follow_trace(self, guid, trace_id, filename):
197         if not guid in self._traces:
198             self._traces[guid] = dict()
199         self._traces[guid][trace_id] = filename
200
201     def shutdown(self):
202         for element in self._elements.itervalues():
203             if isinstance(element, self.LOCAL_TYPES):
204                 # graceful shutdown of locally-implemented objects
205                 element.cleanup()
206         if self.ns3:
207             if not self.ns3.Simulator.IsFinished():
208                 self.stop()
209             
210             # TODO!!!! SHOULD WAIT UNTIL THE THREAD FINISHES
211             if self._simulator_thread:
212                 self._simulator_thread.join()
213             
214             self.ns3.Simulator.Destroy()
215         
216         self._elements.clear()
217         
218         self._ns3 = None
219         sys.stdout.flush()
220         sys.stderr.flush()
221
222     def _simulator_run(self, condition):
223         # Run simulation
224         self.ns3.Simulator.Run()
225         # Signal condition on simulation end to notify waiting threads
226         condition.acquire()
227         condition.notifyAll()
228         condition.release()
229
230     def _schedule_event(self, condition, func, *args):
231         """Schedules event on running experiment"""
232         def execute_event(contextId, condition, has_event_occurred, func, *args):
233             # exec func
234             try:
235                 func(*args)
236             finally:
237                 # flag event occured
238                 has_event_occurred[0] = True
239                 # notify condition indicating attribute was set
240                 condition.acquire()
241                 condition.notifyAll()
242                 condition.release()
243
244         # contextId is defined as general context
245         contextId = long(0xffffffff)
246         # delay 0 means that the event is expected to execute inmediately
247         delay = self.ns3.Seconds(0)
248         # flag to indicate that the event occured
249         # because bool is an inmutable object in python, in order to create a
250         # bool flag, a list is used as wrapper
251         has_event_occurred = [False]
252         condition.acquire()
253         try:
254             if not self.ns3.Simulator.IsFinished():
255                 self.ns3.Simulator.ScheduleWithContext(contextId, delay, execute_event,
256                      condition, has_event_occurred, func, *args)
257                 while not has_event_occurred[0] and not self.ns3.Simulator.IsFinished():
258                     condition.wait()
259         finally:
260             condition.release()
261
262     def _set_attribute(self, name, ns3_value, element):
263         if self.status() == TS.STATUS_STARTED:
264             # schedule the event in the Simulator
265             self._schedule_event(self._condition, self._set_ns3_attribute, 
266                     name, ns3_value, element)
267         else:
268             self._set_ns3_attribute(name, ns3_value, element)
269
270     def _get_attribute(self, name, ns3_value, element):
271         if self.status() == TS.STATUS_STARTED:
272             # schedule the event in the Simulator
273             self._schedule_event(self._condition, self._get_ns3_attribute, 
274                     name, ns3_value, element)
275         else:
276             self._get_ns3_attribute(name, ns3_value, element)
277
278     def _set_ns3_attribute(self, name, ns3_value, element):
279         element.SetAttribute(name, ns3_value)
280
281     def _get_ns3_attribute(self, name, ns3_value, element):
282         element.GetAttribute(name, ns3_value)
283
284     def _stop_simulation(self, time):
285         if self.status() == TS.STATUS_STARTED:
286             # schedule the event in the Simulator
287             self._schedule_event(self._condition, self._stop_ns3_simulation, 
288                     time)
289         else:
290             self._stop_ns3_simulation(time)
291
292     def _stop_ns3_simulation(self, time = TIME_NOW):
293         if not self.ns3:
294             return
295         if time == TIME_NOW:
296             self.ns3.Simulator.Stop()
297         else:
298             self.ns3.Simulator.Stop(self.ns3.Time(time))
299
300     def _to_ns3_value(self, guid, name, value):
301         factory_id = self._create[guid]
302         TypeId = self.ns3.TypeId()
303         typeid = TypeId.LookupByName(factory_id)
304         info = TypeId.AttributeInformation()
305         if not typeid.LookupAttributeByName(name, info):
306             raise RuntimeError("Attribute %s doesn't belong to element %s" \
307                    % (name, factory_id))
308         str_value = str(value)
309         if isinstance(value, bool):
310             str_value = str_value.lower()
311         checker = info.checker
312         ns3_value = checker.Create()
313         ns3_value.DeserializeFromString(str_value, checker)
314         return ns3_value
315
316     def _configure_ns3_module(self):
317         simu_impl_type = self._attributes.get_attribute_value(
318                 "SimulatorImplementationType")
319         sched_impl_type = self._attributes.get_attribute_value(
320                 "SchedulerType")
321         checksum = self._attributes.get_attribute_value("ChecksumEnabled")
322         stop_time = self._attributes.get_attribute_value("StopTime")
323
324         load_ns3_module()
325
326         import ns3 as mod
327  
328         if simu_impl_type:
329             value = mod.StringValue(simu_impl_type)
330             mod.GlobalValue.Bind ("SimulatorImplementationType", value)
331         if sched_impl_type:
332             value = mod.StringValue(sched_impl_type)
333             mod.GlobalValue.Bind ("SchedulerType", value)
334         if checksum:
335             value = mod.BooleanValue(checksum)
336             mod.GlobalValue.Bind ("ChecksumEnabled", value)
337         if stop_time:
338             value = mod.Time(stop_time)
339             mod.Simulator.Stop (value)
340         return mod
341
342     def _get_construct_parameters(self, guid):
343         params = self._get_parameters(guid)
344         construct_params = dict()
345         factory_id = self._create[guid]
346         TypeId = self.ns3.TypeId()
347         typeid = TypeId.LookupByName(factory_id)
348         for name, value in params.iteritems():
349             info = self.ns3.TypeId.AttributeInformation()
350             found = typeid.LookupAttributeByName(name, info)
351             if found and \
352                 (info.flags & TypeId.ATTR_CONSTRUCT == TypeId.ATTR_CONSTRUCT):
353                 construct_params[name] = value
354         return construct_params
355
356
357