2 # -*- coding: utf-8 -*-
6 class ExperimentData(object):
12 return self.data.keys()
14 def add_testbed_data(self, guid, testbed_id, testbed_version):
16 testbed_data["testbed_id"] = testbed_id
17 testbed_data["testbed_version"] = testbed_version
18 self.data[guid] = testbed_data
20 def add_box_data(self, guid, testbed_guid, factory_id):
22 box_data["testbed_guid"] = testbed_guid
23 box_data["factory_id"] = factory_id
24 self.data[guid] = box_data
26 def add_graphical_info_data(self, guid, x, y, width, height, label):
27 data = self.data[guid]
28 if not "graphical_info" in data:
29 data["graphical_info"] = dict()
30 graphical_info_data = data["graphical_info"]
31 graphical_info_data["x"] = x
32 graphical_info_data["y"] = y
33 graphical_info_data["width"] = width
34 graphical_info_data["height"] = height
35 graphical_info_data["label"] = label
37 def add_factory_attribute_data(self, guid, name, value):
38 data = self.data[guid]
39 if not "factory_attributes" in data:
40 data["factory_attributes"] = dict()
41 factory_attributes_data = data["factory_attributes"]
42 factory_attributes_data[name] = value
44 def add_attribute_data(self, guid, name, value):
45 data = self.data[guid]
46 if not "attributes" in data:
47 data["attributes"] = dict()
48 attributes_data = data["attributes"]
49 attributes_data[name] = value
51 def add_trace_data(self, guid, trace_name):
52 data = self.data[guid]
53 if not "traces" in data:
54 data["traces"] = list()
55 traces_data = data["traces"]
56 traces_data.append(trace_name)
58 def add_connection_data(self, guid, connector_type_name, other_guid,
59 other_connector_type_name):
60 data = self.data[guid]
61 if not "connections" in data:
62 data["connections"] = dict()
63 connections_data = data["connections"]
64 if not connector_type_name in connections_data:
65 connections_data[connector_type_name] = dict()
66 connection_data = connections_data[connector_type_name]
67 connection_data[other_guid] = other_connector_type_name
69 def add_address_data(self, guid, autoconf, address, netprefix,
71 data = self.data[guid]
72 if not "addresses" in data:
73 data["addresses"] = list()
74 addresses_data = data["addresses"]
77 address_data["AutoConfigure"] = autoconf
79 address_data["Address"] = address
80 address_data["NetPrefix"] = netprefix
82 address_data["Broadcast"] = broadcast
83 addresses_data.append(address_data)
85 def add_route_data(self, guid, destination, netprefix, nexthop):
86 data = self.data[guid]
87 if not "routes" in data:
88 data["routes"] = list()
89 routes_data = data["routes"]
91 "Destination": destination,
92 "NetPrefix": netprefix,
95 routes_data.append(route_data)
97 def is_testbed_data(self, guid):
98 return True if "testbed_id" in self.data[guid] else None
100 def get_testbed_data(self, guid):
101 testbed_data = self.data[guid]
102 return (testbed_data["testbed_id"], testbed_data["testbed_version"])
104 def get_box_data(self, guid):
105 box_data = self.data[guid]
106 return (box_data["testbed_guid"], box_data["factory_id"])
108 def get_graphical_info_data(self, guid):
109 data = self.data[guid]
110 if not "graphical_info" in data:
111 return (0, 0, 0, 0, "")
112 graphical_info_data = data["graphical_info"]
113 return (graphical_info_data["x"],
114 graphical_info_data["y"],
115 graphical_info_data["width"],
116 graphical_info_data["height"],
117 graphical_info_data["label"])
119 def get_factory_attribute_data(self, guid):
120 data = self.data[guid]
121 if not "factory_attributes" in data:
123 factory_attributes_data = data["factory_attributes"]
124 return [(name, value) for name, value \
125 in factory_attributes_data.iteritems()]
127 def get_attribute_data(self, guid):
128 data = self.data[guid]
129 if not "attributes" in data:
131 attributes_data = data["attributes"]
132 return [(name, value) for name, value \
133 in attributes_data.iteritems()]
135 def get_trace_data(self, guid):
136 data = self.data[guid]
137 if not "traces" in data:
139 return [trace_id for trace_id in data["traces"]]
141 def get_connection_data(self, guid):
142 data = self.data[guid]
143 if not "connections" in data:
145 connections_data = data["connections"]
146 return [(connector_type_name, other_guid, other_connector_type_name) \
147 for connector_type_name, connection_data \
148 in connections_data.iteritems() \
149 for other_guid, other_connector_type_name \
150 in connection_data.iteritems()]
152 def get_address_data(self, guid):
153 data = self.data[guid]
154 if not "addresses" in data:
156 addresses_data = data["addresses"]
157 return [(data["AutoConfigure"] if "AutoConfigure" in data else None,
158 data["Address"] if "Address" in data else None,
159 data["NetPrefix"] if "NetPrefix" in data else None,
160 data["Broadcast"] if "Broadcast" in data else None) \
161 for data in addresses_data]
163 def get_route_data(self, guid):
164 data = self.data[guid]
165 if not "routes" in data:
167 routes_data = data["routes"]
168 return [(data["Destination"],
171 for data in routes_data]
173 class ExperimentParser(object):
174 def to_data(self, experiment_description):
175 data = ExperimentData()
176 for testbed_description in experiment_description.testbed_descriptions:
177 guid = testbed_description.guid
178 testbed_id = testbed_description.provider.testbed_id
179 testbed_version = testbed_description.provider.testbed_version
180 data.add_testbed_data(guid, testbed_id, testbed_version)
181 self.graphical_info_to_data(data, guid,
182 testbed_description.graphical_info)
183 self.attributes_to_data(data, guid, testbed_description.attributes)
184 for box in testbed_description.boxes:
185 data.add_box_data(box.guid, guid, box.factory_id)
186 self.graphical_info_to_data(data, box.guid, box.graphical_info)
187 self.factory_attributes_to_data(data, box.guid,
188 box.factory_attributes)
189 self.attributes_to_data(data, box.guid, box.attributes)
190 self.traces_to_data(data, box.guid, box.traces)
191 self.connections_to_data(data, box.guid, box.connectors)
192 self.addresses_to_data(data, box.guid, box.addresses)
193 self.routes_to_data(data, box.guid, box.routes)
196 def graphical_info_to_data(self, data, guid, g_info):
197 data.add_graphical_info_data(guid, g_info.x, g_info.y, g_info.width,
198 g_info.height, g_info.label)
200 def factory_attributes_to_data(self, data, guid, factory_attributes):
201 for name, value in factory_attributes.iteritems():
202 data.add_factory_attribute_data(guid, name, value)
204 def attributes_to_data(self, data, guid, attributes):
205 for attribute in attributes:
206 if attribute.modified or attribute.has_no_default_value:
207 data.add_attribute_data(guid, attribute.name, attribute.value)
209 def traces_to_data(self, data, guid, traces):
212 data.add_trace_data(guid, trace.trace_id)
214 def connections_to_data(self, data, guid, connectors):
215 for connector in connectors:
216 connector_type_name = connector.connector_type.name
217 for other_connector in connector.connections:
218 other_guid = other_connector.box.guid
219 other_connector_type_name = other_connector.connector_type.name
220 data.add_connection_data(guid, connector_type_name, other_guid,
221 other_connector_type_name)
223 def addresses_to_data(self, data, guid, addresses):
224 for addr in addresses:
225 autoconf = addr.get_attribute_value("AutoConfigure")
226 address = addr.get_attribute_value("Address")
227 netprefix = addr.get_attribute_value("NetPrefix")
228 broadcast = addr.get_attribute_value("Broadcast") \
229 if addr.has_attribute("Broadcast") and \
230 addr.is_attribute_modified("Broadcast") else None
231 data.add_address_data(guid, autoconf, address, netprefix,
234 def routes_to_data(self, data, guid, routes):
236 destination = route.get_attribute_value("Destination")
237 netprefix = route.get_attribute_value("NetPrefix")
238 nexthop = route.get_attribute_value("NextHop")
239 data.add_route_data(guid, destination, netprefix, nexthop)
241 def from_data(self, experiment_description, data):
243 for guid in data.guids:
244 if data.is_testbed_data(guid):
245 self.testbed_from_data(experiment_description, guid, data)
247 self.box_from_data(experiment_description, guid, data)
248 box_guids.append(guid)
249 self.connections_from_data(experiment_description, box_guids, data)
251 def testbed_from_data(self, experiment_description, guid, data):
252 from nepi.core.design import FactoriesProvider
253 (testbed_id, testbed_version) = data.get_testbed_data(guid)
254 provider = FactoriesProvider(testbed_id, testbed_version)
255 experiment_description.add_testbed_description(provider)
256 testbed_description = experiment_description.testbed_description(guid)
257 self.attributes_from_data(testbed_description, data)
259 def box_from_data(self, experiment_description, guid, data):
260 (testbed_guid, factory_id) = data.get_box_data(guid)
261 testbed_description = experiment_description.testbed_description(
263 self.factory_attributes_from_data(testbed_description, factory_id,
265 box = testbed_description.create(factory_id)
266 self.graphical_info_from_data(box, data)
267 self.attributes_from_data(box, data)
268 self.traces_from_data(box, data)
269 self.addresses_from_data(box, data)
270 self.routes_from_data(box, data)
272 def graphical_info_from_data(self, element, data):
273 (x, y, width, height, label) = data.get_graphical_info_data(
275 element.graphical_info.x = x
276 element.graphical_info.y = y
277 element.graphical_info.width = width
278 element.graphical_info.height = height
279 element.graphical_info.label = label
281 def factory_attributes_from_data(self, testbed_description, factory_id,
283 factory = testbed_description.provider.factory(factory_id)
284 for (name, value) in data.get_factory_attribute_data(guid):
285 factory.set_attribute_value(name, value)
287 def attributes_from_data(self, element, data):
288 for name, value in data.get_attribute_data(element.guid):
289 element.set_attribute_value(name, value)
291 def traces_from_data(self, box, data):
292 for name in data.get_trace_data(box.guid):
293 box.enable_trace(name)
295 def addresses_from_data(self, box, data):
296 for (autoconf, address, netprefix, broadcast) \
297 in data.get_address_data(box.guid):
298 addr = box.add_address()
300 addr.set_attribute_value("AutoConfigure", autoconf)
302 addr.set_attribute_value("Address", address)
303 if netprefix != None:
304 addr.set_attribute_value("NetPrefix", netprefix)
306 addr.set_attribute_value("Broadcast", broadcast)
308 def routes_from_data(self, box, data):
309 for (destination, netprefix, nexthop) \
310 in data.get_route_data(box.guid):
311 addr = box.add_route()
312 addr.set_attribute_value("Destination", destination)
313 addr.set_attribute_value("NetPrefix", netprefix)
314 addr.set_attribute_value("NextHop", nexthop)
316 def connections_from_data(self, experiment_description, guids, data):
318 box = experiment_description.box(guid)
319 for (connector_type_name, other_guid, other_connector_type_name) \
320 in data.get_connection_data(guid):
321 other_box = experiment_description.box(other_guid)
322 connector = box.connector(connector_type_name)
323 other_connector = other_box.connector(
324 other_connector_type_name)
325 if not connector.is_connected(other_connector):
326 connector.connect(other_connector)