2 # -*- coding: utf-8 -*-
6 class ExperimentParser(object):
7 def to_data(self, experiment_description):
9 for testbed_description in experiment_description.testbed_descriptions:
10 guid = testbed_description.guid
11 data[guid] = self.testbed_to_data(testbed_description)
12 for box in testbed_description.boxes:
13 data[box.guid] = self.box_to_data(guid, box)
16 def testbed_to_data(self, testbed_description):
18 elem["testbed_id"] = testbed_description.provider.testbed_id
19 elem["testbed_version"] = testbed_description.provider.testbed_version
22 def box_to_data(self, testbed_guid, box):
24 elem["testbed_guid"] = testbed_guid
25 elem["factory_id"] = box.factory_id
26 fattrs = self.factory_attributes_to_data(box.factory_attributes)
28 elem["factory_attributes"] = fattrs
29 attrs = self.attributes_to_data(box.attributes)
31 elem["attributes"] = attrs
32 traces = self.traces_to_data(box.traces)
34 elem["traces"] = traces
35 connections = self.connections_to_data(box.connectors)
37 elem["connections"] = connections
38 addresses = self.addresses_to_data(box.addresses)
40 elem["addresses"] = addresses
41 routes = self.routes_to_data(box.routes)
43 elem["routes"] = routes
46 def factory_attributes_to_data(self, attributes):
48 for attribute in attributes:
49 if attribute.modified:
50 fattrs[attribute.name] = attribute.value
51 return fattrs if len(fattrs) > 0 else None
53 def attributes_to_data(self, attributes):
55 for attribute in attributes:
56 if attribute.modified:
57 attrs[attribute.name] = attribute.value
58 return attrs if len(attrs) > 0 else None
60 def traces_to_data(self, traces):
64 trcs.append(trace.name)
65 return trcs if len(trcs) > 0 else None
67 def connections_to_data(self, connectors):
69 for connector in connectors:
71 for other_connector in connector.connections:
72 guid = other_connector.box.guid
73 cnxs[guid] = other_connector.connector_type.name
75 cnctrs[connector.connector_type.name] = cnxs
76 return cnctrs if len(cnctrs) > 0 else None
78 def addresses_to_data(self, addresses):
80 for address in addresses:
82 for attribute in address.attributes:
83 if attribute.modified:
84 addr[attribute.name] = attribute.value
86 return addrs if len(addrs) > 0 else None
88 def routes_to_data(self, routes):
92 for attribute in route.attibutes:
93 if attribute.modified:
94 rt[attribute.name] = attribute.value
96 return rts if len(rts) > 0 else None
98 def from_data(self, experiment_description, data):
99 connections_data = dict()
100 for guid, elem_data in data.iteritems():
101 if "testbed_id" in elem_data:
102 self.testbed_from_data(experiment_description, elem_data)
104 self.box_from_data(experiment_description, elem_data)
105 if "connections" in elem_data:
106 connections_data[guid] = elem_data["connections"]
107 # Connections need all boxes to be created
108 self.connections_from_data(experiment_description, connections_data)
109 return experiment_description
111 def testbed_from_data(self, experiment_description, data):
112 testbed_id = data["testbed_id"]
113 testbed_version = data["testbed_version"]
114 mod_name = 'nepi.testbeds.%s' % testbed_id
115 if not mod_name in sys.modules:
117 testbed_mod = sys.modules[mod_name]
118 provider = testbed_mod.TestbedFactoriesProvider(testbed_version)
119 experiment_description.add_testbed_description(provider)
121 def box_from_data(self, experiment_description, data):
122 testbed_guid = data["testbed_guid"]
123 testbed_description = experiment_description.testbed_description(
125 factory_id = data["factory_id"]
126 if "factory_attributes" in data:
127 self.factory_attributes_from_data(factory_id, testbed_description,
128 data["factory_attributes"])
129 box = testbed_description.create(factory_id)
130 if "attributes" in data:
131 self.attributes_from_data(box, data["attributes"])
133 self.traces_from_data(box, data["traces"])
134 if "addresses" in data:
135 self.addresses_from_data(box, data["addresses"])
137 self.routes_from_data(box, experiment_description, data["routes"])
139 def factory_attributes_from_data(self, factory_id, testbed_description,
141 factory = testbed_description.provider.factory(factory_id)
142 for name, value in data.iteritems():
143 factory.set_attribute_value(name, value)
145 def attributes_from_data(self, box, data):
146 for name, value in data.iteritems():
147 box.set_attribute_value(name, value)
149 def traces_from_data(self, box, data):
151 box.trace(name).enable()
153 def connections_from_data(self, experiment_description, data):
154 for guid, connector_data in data.iteritems():
155 box = experiment_description.box(guid)
156 for connector_type_name, connections in connector_data.iteritems():
157 for guid, other_connector_type_name in connections.iteritems():
158 other_box = experiment_description.box(guid)
159 connector = box.connector(connector_type_name)
160 other_connector = other_box.connector(
161 other_connector_type_name)
162 if not connector.is_connected(other_connector):
163 connector.connect(other_connector)
165 def addresses_from_data(self, box, data):
166 for address_attrs in data:
167 addr = box.add_address()
168 for name, value in address_attrs.iteritems():
169 addr.set_attribute_value(name, value)
171 def routes_from_data(self, box, experiment_description, data):
172 for route_attrs in data:
173 route = box.add_route()
174 for name, value in route_attrs.iteritems():
175 route.set_attribute_value(name, value)