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