ns-3.11
[nepi.git] / src / nepi / testbeds / ns3 / execute.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
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 not bindings:
49         import ns3
50         sys.modules["ns3"] = ns3
51         return
52     
53     sys.path.append(bindings)
54     import ns3_bindings_import as mod
55     sys.modules["ns3"] = mod
56
57 class TestbedController(testbed_impl.TestbedController):
58     from nepi.util.tunchannel_impl import TunChannel
59     
60     LOCAL_FACTORIES = {
61         'ns3::Nepi::TunChannel' : TunChannel,
62     }
63     
64     LOCAL_TYPES = tuple(LOCAL_FACTORIES.values())
65
66     def __init__(self):
67         super(TestbedController, self).__init__(TESTBED_ID, TESTBED_VERSION)
68         self._ns3 = None
69         self._home_directory = None
70         self._traces = dict()
71         self._simulator_thread = None
72         self._condition = None
73
74     @property
75     def home_directory(self):
76         return self._home_directory
77
78     @property
79     def ns3(self):
80         return self._ns3
81
82     def do_setup(self):
83         self._home_directory = self._attributes.\
84             get_attribute_value("homeDirectory")
85         self._ns3 = self._configure_ns3_module()
86         
87         # create home...
88         home = os.path.normpath(self.home_directory)
89         if not os.path.exists(home):
90             os.makedirs(home, 0755)
91         
92         super(TestbedController, self).do_setup()
93
94     def start(self):
95         super(TestbedController, self).start()
96         self._condition = threading.Condition()
97         self._simulator_thread = threading.Thread(target = self._simulator_run,
98                 args = [self._condition])
99         self._simulator_thread.setDaemon(True)
100         self._simulator_thread.start()
101
102     def stop(self, time = TIME_NOW):
103         super(TestbedController, self).stop(time)
104         #self.ns3.Simulator.Stop()
105         self._stop_simulation(time)
106
107     def set(self, guid, name, value, time = TIME_NOW):
108         super(TestbedController, self).set(guid, name, value, time)
109         # TODO: take on account schedule time for the task
110         factory_id = self._create[guid]
111         factory = self._factories[factory_id]
112         element = self._elements[guid]
113         if factory_id in self.LOCAL_FACTORIES:
114             setattr(element, name, value)
115         elif not factory.box_attributes.is_attribute_metadata(name):
116             ns3_value = self._to_ns3_value(guid, name, value)
117             self._set_attribute(name, ns3_value, element)
118
119     def get(self, guid, name, time = TIME_NOW):
120         value = super(TestbedController, self).get(guid, name, time)
121         # TODO: take on account schedule time for the task
122         factory_id = self._create[guid]
123         factory = self._factories[factory_id]
124         element = self._elements[guid]
125         if factory_id in self.LOCAL_FACTORIES:
126             if hasattr(element, name):
127                 return getattr(element, name)
128             else:
129                 return value
130         if factory.box_attributes.is_attribute_metadata(name):
131             return value
132
133         TypeId = self.ns3.TypeId()
134         typeid = TypeId.LookupByName(factory_id)
135         info = TypeId.AttributeInfo()
136         if not typeid or not typeid.LookupAttributeByName(name, info):
137             raise AttributeError("Invalid attribute %s for element type %d" % \
138                 (name, guid))
139         checker = info.checker
140         ns3_value = checker.Create() 
141         self._get_attribute(name, ns3_value, element)
142         value = ns3_value.SerializeToString(checker)
143         attr_type = factory.box_attributes.get_attribute_type(name)
144         if attr_type == Attribute.INTEGER:
145             return int(value)
146         if attr_type == Attribute.DOUBLE:
147             return float(value)
148         if attr_type == Attribute.BOOL:
149             return value == "true"
150         return value
151
152     def action(self, time, guid, action):
153         raise NotImplementedError
154
155     def trace_filepath(self, guid, trace_id):
156         filename = self._traces[guid][trace_id]
157         return os.path.join(self.home_directory, filename)
158
159     def follow_trace(self, guid, trace_id, filename):
160         if not guid in self._traces:
161             self._traces[guid] = dict()
162         self._traces[guid][trace_id] = filename
163
164     def shutdown(self):
165         for element in self._elements.itervalues():
166             if isinstance(element, self.LOCAL_TYPES):
167                 # graceful shutdown of locally-implemented objects
168                 element.Cleanup()
169         if self.ns3:
170             self.ns3.Simulator.Stop()
171             
172             # Wait for it to stop, with a 30s timeout
173             for i in xrange(300):
174                 if self.ns3.Simulator.IsFinished():
175                     break
176                 time.sleep(0.1)
177             #self._stop_simulation("0s")
178         
179         self._elements.clear()
180         
181         if self.ns3:
182             # TODO!!!! SHOULD WAIT UNTIL THE THREAD FINISHES
183             #   if self._simulator_thread:
184             #       self._simulator_thread.join()
185             self.ns3.Simulator.Destroy()
186         
187         self._ns3 = None
188         sys.stdout.flush()
189         sys.stderr.flush()
190
191     def _simulator_run(self, condition):
192         # Run simulation
193         self.ns3.Simulator.Run()
194         # Signal condition on simulation end to notify waiting threads
195         condition.acquire()
196         condition.notifyAll()
197         condition.release()
198
199     def _schedule_event(self, condition, func, *args):
200         """Schedules event on running experiment"""
201         def execute_event(condition, has_event_occurred, func, *args):
202             # exec func
203             try:
204                 func(*args)
205             finally:
206                 # flag event occured
207                 has_event_occurred[0] = True
208                 # notify condition indicating attribute was set
209                 condition.acquire()
210                 condition.notifyAll()
211                 condition.release()
212
213         # contextId is defined as general context
214         contextId = long(0xffffffff)
215         # delay 0 means that the event is expected to execute inmediately
216         delay = self.ns3.Seconds(0)
217         # flag to indicate that the event occured
218         # because bool is an inmutable object in python, in order to create a
219         # bool flag, a list is used as wrapper
220         has_event_occurred = [False]
221         condition.acquire()
222         if not self.ns3.Simulator.IsFinished():
223             self.ns3.Simulator.ScheduleWithContext(contextId, delay, execute_event,
224                  condition, has_event_occurred, func, *args)
225             while not has_event_occurred[0] and not self.ns3.Simulator.IsFinished():
226                 condition.wait()
227                 condition.release()
228
229     def _set_attribute(self, name, ns3_value, element):
230         if self.status() == TS.STATUS_STARTED:
231             # schedule the event in the Simulator
232             self._schedule_event(self._condition, self._set_ns3_attribute, 
233                     name, ns3_value, element)
234         else:
235             self._set_ns3_attribute(name, ns3_value, element)
236
237     def _get_attribute(self, name, ns3_value, element):
238         if self.status() == TS.STATUS_STARTED:
239             # schedule the event in the Simulator
240             self._schedule_event(self._condition, self._get_ns3_attribute, 
241                     name, ns3_value, element)
242         else:
243             self._get_ns3_attribute(name, ns3_value, element)
244
245     def _set_ns3_attribute(self, name, ns3_value, element):
246         element.SetAttribute(name, ns3_value)
247
248     def _get_ns3_attribute(self, name, ns3_value, element):
249         element.GetAttribute(name, ns3_value)
250
251     def _stop_simulation(self, time):
252         if self.status() == TS.STATUS_STARTED:
253             # schedule the event in the Simulator
254             self._schedule_event(self._condition, self._stop_ns3_simulation, 
255                     time)
256         else:
257             self._stop_ns3_simulation(time)
258
259     def _stop_ns3_simulation(self, time = TIME_NOW):
260         if not self.ns3:
261             return
262         if time == TIME_NOW:
263             self.ns3.Simulator.Stop()
264         else:
265             self.ns3.Simulator.Stop(self.ns3.Time(time))
266
267     def _to_ns3_value(self, guid, name, value):
268         factory_id = self._create[guid]
269         TypeId = self.ns3.TypeId()
270         typeid = TypeId.LookupByName(factory_id)
271         info = TypeId.AttributeInfo()
272         if not typeid.LookupAttributeByName(name, info):
273             raise RuntimeError("Attribute %s doesn't belong to element %s" \
274                    % (name, factory_id))
275         str_value = str(value)
276         if isinstance(value, bool):
277             str_value = str_value.lower()
278         checker = info.checker
279         ns3_value = checker.Create()
280         ns3_value.DeserializeFromString(str_value, checker)
281         return ns3_value
282
283     def _configure_ns3_module(self):
284         simu_impl_type = self._attributes.get_attribute_value(
285                 "SimulatorImplementationType")
286         checksum = self._attributes.get_attribute_value("ChecksumEnabled")
287         stop_time = self._attributes.get_attribute_value("StopTime")
288
289         load_ns3_module()
290
291         import ns3 as mod
292  
293         if simu_impl_type:
294             value = mod.StringValue(simu_impl_type)
295             mod.GlobalValue.Bind ("SimulatorImplementationType", value)
296         if checksum:
297             value = mod.BooleanValue(checksum)
298             mod.GlobalValue.Bind ("ChecksumEnabled", value)
299         if stop_time:
300             value = mod.Time(stop_time)
301             mod.Simulator.Stop (value)
302         return mod
303
304     def _get_construct_parameters(self, guid):
305         params = self._get_parameters(guid)
306         construct_params = dict()
307         factory_id = self._create[guid]
308         TypeId = self.ns3.TypeId()
309         typeid = TypeId.LookupByName(factory_id)
310         for name, value in params.iteritems():
311             info = self.ns3.TypeId.AttributeInfo()
312             found = typeid.LookupAttributeByName(name, info)
313             if found and \
314                 (info.flags & TypeId.ATTR_CONSTRUCT == TypeId.ATTR_CONSTRUCT):
315                 construct_params[name] = value
316         return construct_params
317
318
319