2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID
5 from nepi.core import testbed_impl
6 from nepi.core.attributes import Attribute
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):
30 # configure addressess
31 for guid, addresses in self._add_address.iteritems():
32 element = self._elements[guid]
33 for address in addresses:
34 (address, netprefix, broadcast) = address
37 for guid, routes in self._add_route.iteritems():
38 element = self._elements[guid]
40 (destination, netprefix, nexthop) = route
43 context = self.server.modules.ns3
45 for interface in self._interface2addr:
46 ifindex = ipv4.AddInterface(interface._object)
47 for addr in self._interface2addr[interface]:
48 inaddr = context.Ipv4InterfaceAddress(
50 addr.get_attribute("Address").value),
52 addr.get_attribute("NetPrefix").value))
53 ipv4.AddAddress(ifindex, inaddr)
54 ipv4.SetMetric(ifindex, 1)
56 self._interface_addrs[addr] = inaddr
57 self._interfaces[interface] = ifindex
58 for entry in self.get_node().routing_table.get_entries(self._af):
61 def _rt_add(self, entry):
62 # Called both at install-time (by NS3Ipv4Stack.post_install) and at
63 # run-time (by RoutingTable.add_entry).
64 context = self.server.modules.ns3
65 ifindex = self._interfaces[entry.interface]
66 prefixlen = entry.prefixlen
67 # print "rt_add %s %s %s %d"% (prefix, prefixlen, entry.nexthop, ifindex)
69 self._static_routing.AddNetworkRouteTo(
70 context.Ipv4Address(entry.prefix.address),
71 context.Ipv4Mask(entry.mask.address),
72 context.Ipv4Address(entry.nexthop.address),
75 self._static_routing.AddNetworkRouteTo(
76 context.Ipv4Address(entry.prefix.address),
77 context.Ipv4Mask(entry.mask.address),
81 def set(self, time, guid, name, value):
82 super(TestbedInstance, self).set(time, guid, name, value)
83 # TODO: take on account schedule time for the task
84 factory_id = self._crerate[guid]
85 element = self._elements[guid]
86 TypeId = self.ns3.TypeId()
87 typeid = TypeId.LookupByName(factory_id)
88 info = TypeId.AttributeInfo()
89 if not typeid.LookupAttributeByName(name, info):
90 raise RuntimeError("Attribute %s doesn't belong to element %s" \
93 if isinstance(value, bool):
95 ns3_value = info.checker.Create()
96 ns3_value.DeserializeFromString(value, checker)
97 element.SetAttribute(name, ns3_value)
99 def get(self, time, guid, name):
100 # TODO: take on account schedule time for the task
101 TypeId = self.ns3.TypeId()
102 typeid = TypeId.LookupByName(factory_id)
103 info = TypeId.AttributeInfo()
104 if not typeid.LookupAttributeByName(name, info):
105 raise RuntimeError("Attribute %s doesn't belong to element %s" \
106 % (name, factory_id))
107 checker = info.checker
108 ns3_value = checker.Create()
109 element = self._elements[guid]
110 element.GetAttribute(name, ns3_value)
111 value = ns3_value.SerializeToString(checker)
112 factory_id = self._create[guid]
113 factory = self._factories[factory_id]
114 attr_type = factory.box_attributes.get_attribute_type(name)
115 if attr_type == Attribute.INTEGER:
117 if attr_type == Attribute.DOUBLE:
119 if attr_type == Attribute.BOOL:
120 return value == "true"
123 def action(self, time, guid, action):
124 raise NotImplementedError
126 def trace(self, guid, trace_id):
127 fd = open("%s" % self.trace_filename(guid, trace_id), "r")
132 def trace_filename(self, guid, trace_id):
133 # TODO: Need to be defined inside a home!!!! with and experiment id_code
134 filename = self._trace_filenames[guid][trace_id]
135 return os.path.join(self.home_directory, filename)
137 def follow_trace(self, guid, trace_id, filename):
138 if guid not in self._traces:
139 self._traces[guid] = dict()
140 self._traces[guid][trace_id] = filename
143 for element in self._elements.values():
146 def _load_ns3_module(self):
150 bindings = self._attributes.get_attribute_value("ns3Bindings")
151 libfile = self._attributes.get_attribute_value("ns3Library")
152 simu_impl_type = self._attributes.get_attribute_value(
153 "SimulatorImplementationType")
154 checksum = self._attributes.get_attribute_value("ChecksumEnabled")
157 ctypes.CDLL(libfile, ctypes.RTLD_GLOBAL)
159 path = [ os.path.dirname(__file__) ] + sys.path
161 path = [ bindings ] + path
163 module = imp.find_module ('ns3', path)
164 mod = imp.load_module ('ns3', *module)
167 value = mod.StringValue(simu_impl_type)
168 mod.GlobalValue.Bind ("SimulatorImplementationType", value)
170 value = mod.BooleanValue(checksum)
171 mod.GlobalValue.Bind ("ChecksumEnabled", value)
174 def _get_construct_parameters(self, guid):
175 params = self._get_parameters(guid)
176 construct_params = dict()
177 factory_id = self._create[guid]
178 TypeId = self.ns3.TypeId()
179 typeid = TypeId.LookupByName(factory_id)
180 for name, value in params:
181 info = self.ns3.TypeId.AttributeInfo()
182 typeid.LookupAttributeByName(name, info)
183 if info.flags & TypeId.ATTR_CONSTRUCT == TypeId.ATTR_CONSTRUCT:
184 construct_params[name] = value
185 return construct_params