2 # -*- coding: utf-8 -*-
4 from nepi.core.experiment import ExperimentDescription
7 def to_data(self, experiment_description):
9 for testbed_description in experiment_description.testbed_descriptions:
10 guid = testbed_description.guid
11 exp[guid] = self.testbed_to_data(testbed_description)
12 for box in testbed_description.boxes:
13 exp[box.guid] = self.box_to_data(guid, box)
16 def testbed_to_data(self, testbed_description):
18 elem["testbed_id"] = testbed_description.testbed_id
19 elem["testbed_version"] = testbed_description.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, data):
99 experiment = ExperimentDescription()
100 connections_data = dict()
101 for guid, elem_data in data.iteritems():
102 if "testbed_id" in elem_data:
103 self.testbed_from_data(experiment, elem_data)
105 self.box_from_data(experiment, elem_data)
106 if "connections" in elem_data:
107 connections_data[guid] = elem_data["connections"]
108 # Connections need all boxes to be created
109 self.connections_from_data(experiment, connections_data)
112 def testbed_from_data(self, experiment, data):
113 testbed_id = data["testbed_id"]
114 testbed_version = data["testbed_version"]
115 experiment.add_testbed_description(testbed_id, testbed_version)
117 def box_from_data(self, experiment, data):
118 testbed_guid = data["testbed_guid"]
119 testbed_description = experiment.testbed_description(testbed_guid)
120 factory_id = data["factory_id"]
121 if "factory_attributes" in data:
122 self.factory_attributes_from_data(factory_id, testbed_description,
123 data["factory_attributes"])
124 box = testbed_description.create(factory_id)
125 if "attributes" in data:
126 self.attributes_from_data(box, data["attributes"])
128 self.traces_from_data(box, data["traces"])
129 if "addresses" in data:
130 self.addresses_from_data(box, data["addresses"])
132 self.routes_from_data(box, experiment, data["routes"])
134 def factory_attributes_from_data(self, factory_id, testbed_description,
136 factory = testbed_description.provider.factory(factory_id)
137 for name, value in data.iteritems():
138 factory.set_attribute_value(name, value)
140 def attributes_from_data(self, box, data):
141 for name, value in data.iteritems():
142 box.set_attribute_value(name, value)
144 def traces_from_data(self, box, data):
146 box.trace(name).enable()
148 def connections_from_data(self, experiment, data):
149 for guid, connector_data in data.iteritems():
150 box = experiment.box(guid)
151 for connector_type_name, connections in connector_data.iteritems():
152 for guid, other_connector_type_name in connections.iteritems():
153 other_box = experiment.box(guid)
154 connector = box.connector(connector_type_name)
155 other_connector = other_box.connector(
156 other_connector_type_name)
157 if not connector.is_connected(other_connector):
158 connector.connect(other_connector)
160 def addresses_from_data(self, box, data):
161 for address_attrs in data:
162 addr = box.add_address()
163 for name, value in address_attrs.iteritems():
164 addr.set_attribute_value(name, value)
166 def routes_from_data(self, box, experiment, data):
167 for route_attrs in data:
168 route = box.add_route()
169 for name, value in route_attrs.iteritems():
170 route.set_attribute_value(name, value)