2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID
5 from nepi.core import testbed_impl
6 from nepi.util.constants import AF_INET, AF_INET6
9 class TestbedInstance(testbed_impl.TestbedInstance):
10 def __init__(self, testbed_version):
11 super(TestbedInstance, self).__init__(TESTBED_ID, testbed_version)
13 self._home_directory = None
17 def home_directory(self):
18 return self._home_directory
25 self._home_directory = self._attributes.\
26 get_attribute_value("homeDirectory")
27 self._ns3 = self._load_ns3_module()
29 def do_configure(self):
31 # configure addressess
32 for guid, addresses in self._add_address.iteritems():
33 element = self._elements[guid]
34 for address in addresses:
35 (family, address, netprefix, broadcast) = address
37 element.add_v4_address(address, netprefix)
39 for guid, routes in self._add_route.iteritems():
40 element = self._elements[guid]
42 (destination, netprefix, nexthop) = route
43 element.add_route(prefix = destination, prefix_len = netprefix,
46 def set(self, time, guid, name, value):
47 super(TestbedInstance, self).set(time, guid, name, value)
48 factory_id = self._crerate[guid]
49 element = self._elements[guid]
50 self._set(element, factory_id, name, value)
52 def get(self, time, guid, name):
53 raise NotImplementedError
54 # TODO: take on account schedule time for the task
55 #element = self._elements[guid]
56 #return getattr(element, name)
58 def action(self, time, guid, action):
59 raise NotImplementedError
61 def trace(self, guid, trace_id):
62 fd = open("%s" % self.trace_filename(guid, trace_id), "r")
68 for trace in self._traces.values():
70 for element in self._elements.values():
73 def trace_filename(self, guid, trace_id):
74 # TODO: Need to be defined inside a home!!!! with and experiment id_code
75 return os.path.join(self.home_directory, "%d_%s" % (guid, trace_id))
77 def follow_trace(self, trace_id, trace):
78 self._traces[trace_id] = trace
80 def _set(self, element, factory_id, name, value):
81 TypeId = self.ns3.TypeId()
82 typeId = TypeId.LookupByName(factory_id)
84 attr_count = typeId.GetAttributeN()
85 for idx in range(attr_count):
86 if name == typeId.GetAttributeName(idx)
89 checker = typeid.GetAttributeChecker(index)
90 ns3_value = attribute_checker.Create()
92 if isinstance(value, bool):
94 ns3_value.DeserializeFromString(value, checker)
95 element.Set(name, ns3_value)
97 def _load_ns3_module(self):
101 bindings = self._attributes.get_attribute_value("ns3Bindings")
102 libfile = self._attributes.get_attribute_value("ns3Library")
103 simu_impl_type = self._attributes.get_attribute_value(
104 "SimulatorImplementationType")
105 checksum = self._attributes.get_attribute_value("ChecksumEnabled")
108 ctypes.CDLL(libfile, ctypes.RTLD_GLOBAL)
110 path = [ os.path.dirname(__file__) ] + sys.path
112 path = [ bindings ] + path
114 module = imp.find_module ('ns3', path)
115 mod = imp.load_module ('ns3', *module)
118 value = mod.StringValue(simu_impl_type)
119 mod.GlobalValue.Bind ("SimulatorImplementationType", value)
121 value = mod.BooleanValue(checksum)
122 mod.GlobalValue.Bind ("ChecksumEnabled", value)