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, family, 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
81 address_data["Family"] = family
83 address_data["NetPrefix"] = netprefix
85 address_data["Broadcast"] = broadcast
86 addresses_data.append(address_data)
88 def add_route_data(self, guid, family, destination, netprefix, nexthop):
89 data = self.data[guid]
90 if not "routes" in data:
91 data["routes"] = list()
92 routes_data = data["routes"]
95 "Destination": destination,
96 "NetPrefix": netprefix,
99 routes_data.append(route_data)
101 def is_testbed_data(self, guid):
102 return True if "testbed_id" in self.data[guid] else None
104 def get_testbed_data(self, guid):
105 testbed_data = self.data[guid]
106 return (testbed_data["testbed_id"], testbed_data["testbed_version"])
108 def get_box_data(self, guid):
109 box_data = self.data[guid]
110 return (box_data["testbed_guid"], box_data["factory_id"])
112 def get_graphical_info_data(self, guid):
113 data = self.data[guid]
114 if not "graphical_info" in data:
115 return (0, 0, 0, 0, "")
116 graphical_info_data = data["graphical_info"]
117 return (graphical_info_data["x"],
118 graphical_info_data["y"],
119 graphical_info_data["width"],
120 graphical_info_data["height"],
121 graphical_info_data["label"])
123 def get_factory_attribute_data(self, guid):
124 data = self.data[guid]
125 if not "factory_attributes" in data:
127 factory_attributes_data = data["factory_attributes"]
128 return [(name, value) for name, value \
129 in factory_attributes_data.iteritems()]
131 def get_attribute_data(self, guid):
132 data = self.data[guid]
133 if not "attributes" in data:
135 attributes_data = data["attributes"]
136 return [(name, value) for name, value \
137 in attributes_data.iteritems()]
139 def get_trace_data(self, guid):
140 data = self.data[guid]
141 if not "traces" in data:
143 return [trace_id for trace_id in data["traces"]]
145 def get_connection_data(self, guid):
146 data = self.data[guid]
147 if not "connections" in data:
149 connections_data = data["connections"]
150 return [(connector_type_name, other_guid, other_connector_type_name) \
151 for connector_type_name, connection_data \
152 in connections_data.iteritems() \
153 for other_guid, other_connector_type_name \
154 in connection_data.iteritems()]
156 def get_address_data(self, guid):
157 data = self.data[guid]
158 if not "addresses" in data:
160 addresses_data = data["addresses"]
161 return [(data["AutoConfigure"] if "AutoConfigure" in data else None,
162 data["Address"] if "Address" in data else None,
163 data["Family"] if "Family" in data else None,
164 data["NetPrefix"] if "NetPrefix" in data else None,
165 data["Broadcast"] if "Broadcast" in data else None) \
166 for data in addresses_data]
168 def get_route_data(self, guid):
169 data = self.data[guid]
170 if not "routes" in data:
172 routes_data = data["routes"]
173 return [(data["Family"],
177 for data in routes_data]
179 class ExperimentParser(object):
180 def to_data(self, experiment_description):
181 data = ExperimentData()
182 for testbed_description in experiment_description.testbed_descriptions:
183 guid = testbed_description.guid
184 testbed_id = testbed_description.provider.testbed_id
185 testbed_version = testbed_description.provider.testbed_version
186 data.add_testbed_data(guid, testbed_id, testbed_version)
187 self.graphical_info_to_data(data, guid,
188 testbed_description.graphical_info)
189 self.attributes_to_data(data, guid, testbed_description.attributes)
190 for box in testbed_description.boxes:
191 data.add_box_data(box.guid, guid, box.factory_id)
192 self.graphical_info_to_data(data, box.guid, box.graphical_info)
193 self.factory_attributes_to_data(data, box.guid,
194 box.factory_attributes)
195 self.attributes_to_data(data, box.guid, box.attributes)
196 self.traces_to_data(data, box.guid, box.traces)
197 self.connections_to_data(data, box.guid, box.connectors)
198 self.addresses_to_data(data, box.guid, box.addresses)
199 self.routes_to_data(data, box.guid, box.routes)
202 def graphical_info_to_data(self, data, guid, g_info):
203 data.add_graphical_info_data(guid, g_info.x, g_info.y, g_info.width,
204 g_info.height, g_info.label)
206 def factory_attributes_to_data(self, data, guid, factory_attributes):
207 for name, value in factory_attributes.iteritems():
208 data.add_factory_attribute_data(guid, name, value)
210 def attributes_to_data(self, data, guid, attributes):
211 for attribute in attributes:
212 if attribute.modified:
213 data.add_attribute_data(guid, attribute.name, attribute.value)
215 def traces_to_data(self, data, guid, traces):
218 data.add_trace_data(guid, trace.trace_id)
220 def connections_to_data(self, data, guid, connectors):
221 for connector in connectors:
222 connector_type_name = connector.connector_type.name
223 for other_connector in connector.connections:
224 other_guid = other_connector.box.guid
225 other_connector_type_name = other_connector.connector_type.name
226 data.add_connection_data(guid, connector_type_name, other_guid,
227 other_connector_type_name)
229 def addresses_to_data(self, data, guid, addresses):
230 for addr in addresses:
231 autoconf = addr.get_attribute_value("AutoConfigure") \
232 if addr.is_attribute_modified("AutoConfigure") else None
233 address = addr.get_attribute_value("Address") \
234 if addr.is_attribute_modified("Address") else None
235 netprefix = addr.get_attribute_value("NetPrefix") \
236 if addr.is_attribute_modified("NetPrefix") else None
237 family = addr.get_attribute_value("Family") \
238 if addr.is_attribute_modified("Family") else None
239 broadcast = addr.get_attribute_value("Broadcast") \
240 if addr.has_attribute("Broadcast") and \
241 addr.is_attribute_modified("Broadcast") else None
242 data.add_address_data(guid, autoconf, address, family, netprefix,
245 def routes_to_data(self, data, guid, routes):
247 family = route.get_attribute_value("Family")
248 destination = route.get_attribute_value("Destination")
249 netprefix = route.get_attribute_value("NetPrefix")
250 nexthop = route.get_attribute_value("NextHop")
251 data.add_route_data(guid, family, destination, netprefix, nexthop)
253 def from_data(self, experiment_description, data):
255 for guid in data.guids:
256 if data.is_testbed_data(guid):
257 self.testbed_from_data(experiment_description, guid, data)
259 self.box_from_data(experiment_description, guid, data)
260 box_guids.append(guid)
261 self.connections_from_data(experiment_description, box_guids, data)
263 def testbed_from_data(self, experiment_description, guid, data):
264 from nepi.core.design import FactoriesProvider
265 (testbed_id, testbed_version) = data.get_testbed_data(guid)
266 provider = FactoriesProvider(testbed_id, testbed_version)
267 experiment_description.add_testbed_description(provider)
268 testbed_description = experiment_description.testbed_description(guid)
269 self.attributes_from_data(testbed_description, data)
271 def box_from_data(self, experiment_description, guid, data):
272 (testbed_guid, factory_id) = data.get_box_data(guid)
273 testbed_description = experiment_description.testbed_description(
275 self.factory_attributes_from_data(testbed_description, factory_id,
277 box = testbed_description.create(factory_id)
278 self.graphical_info_from_data(box, data)
279 self.attributes_from_data(box, data)
280 self.traces_from_data(box, data)
281 self.addresses_from_data(box, data)
282 self.routes_from_data(box, data)
284 def graphical_info_from_data(self, element, data):
285 (x, y, width, height, label) = data.get_graphical_info_data(
287 element.graphical_info.x = x
288 element.graphical_info.y = y
289 element.graphical_info.width = width
290 element.graphical_info.height = height
291 element.graphical_info.label = label
293 def factory_attributes_from_data(self, testbed_description, factory_id,
295 factory = testbed_description.provider.factory(factory_id)
296 for (name, value) in data.get_factory_attribute_data(guid):
297 factory.set_attribute_value(name, value)
299 def attributes_from_data(self, element, data):
300 for name, value in data.get_attribute_data(element.guid):
301 element.set_attribute_value(name, value)
303 def traces_from_data(self, box, data):
304 for name in data.get_trace_data(box.guid):
305 box.trace(name).enable()
307 def addresses_from_data(self, box, data):
308 for (autoconf, address, family, netprefix, broadcast) \
309 in data.get_address_data(box.guid):
310 addr = box.add_address()
312 addr.set_attribute_value("AutoConfigure", autoconf)
314 addr.set_attribute_value("Address", address)
316 addr.set_attribute_value("Family", family)
318 addr.set_attribute_value("NetPrefix", netprefix)
320 addr.set_attribute_value("Broadcast", broadcast)
322 def routes_from_data(self, box, data):
323 for (family, destination, netprefix, nexthop) \
324 in data.get_route_data(box.guid):
325 addr = box.add_route(family)
326 addr.set_attribute_value("Destination", destination)
327 addr.set_attribute_value("NetPrefix", netprefix)
328 addr.set_attribute_value("NextHop", nexthop)
330 def connections_from_data(self, experiment_description, guids, data):
332 box = experiment_description.box(guid)
333 for (connector_type_name, other_guid, other_connector_type_name) \
334 in data.get_connection_data(guid):
335 other_box = experiment_description.box(other_guid)
336 connector = box.connector(connector_type_name)
337 other_connector = other_box.connector(
338 other_connector_type_name)
339 if not connector.is_connected(other_connector):
340 connector.connect(other_connector)