2f6b67a1be5a48ddc80328bee7ba2262dbc3fdc0
[nepi.git] / src / nepi / util / parser / base.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from nepi.core.experiment import ExperimentDescription
5
6 class Parser(object):
7     def to_data(self, experiment_description):
8         exp = dict()
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)
14         return exp
15
16     def testbed_to_data(self, testbed_description):
17         elem = dict()
18         elem["testbed_id"] = testbed_description.testbed_id
19         elem["testbed_version"] = testbed_description.testbed_version
20         return elem 
21
22     def box_to_data(self, testbed_guid, box):
23         elem = dict()
24         elem["testbed_guid"] = testbed_guid
25         elem["factory_id"] = box.factory_id
26         fattrs = self.factory_attributes_to_data(box.factory_attributes)
27         if fattrs:
28             elem["factory_attributes"] = fattrs
29         attrs = self.attributes_to_data(box.attributes)
30         if attrs:
31             elem["attributes"] = attrs
32         traces = self.traces_to_data(box.traces)
33         if traces:
34             elem["traces"] = traces
35         connections = self.connections_to_data(box.connectors)
36         if connections:
37             elem["connections"] = connections
38         addresses = self.addresses_to_data(box.addresses)
39         if addresses:
40             elem["addresses"] = addresses
41         routes = self.routes_to_data(box.routes)
42         if routes:
43             elem["routes"] = routes
44         return elem
45
46     def factory_attributes_to_data(self, attributes):
47         fattrs = dict()
48         for attribute in attributes:
49             if attribute.modified:
50                 fattrs[attribute.name] = attribute.value
51         return fattrs if len(fattrs) > 0 else None
52
53     def attributes_to_data(self, attributes):
54         attrs = dict()
55         for attribute in attributes:
56             if attribute.modified:
57                 attrs[attribute.name] = attribute.value
58         return attrs if len(attrs) > 0 else None
59
60     def traces_to_data(self, traces):
61         trcs = list()
62         for trace in traces:
63             if trace.enabled:
64                 trcs.append(trace.name)
65         return trcs if len(trcs) > 0 else None
66
67     def connections_to_data(self, connectors):
68         cnctrs = dict()
69         for connector in connectors:
70             cnxs = dict()
71             for other_connector in connector.connections:
72                 guid = other_connector.box.guid
73                 cnxs[guid] = other_connector.connector_type.name
74             if len(cnxs) > 0:
75                 cnctrs[connector.connector_type.name] = cnxs
76         return cnctrs if len(cnctrs) > 0 else None
77
78     def addresses_to_data(self, addresses):
79         addrs = list()
80         for address in addresses:
81             addr = dict()
82             for attribute in address.attributes:
83                 if attribute.modified:
84                     addr[attribute.name] = attribute.value
85             addrs.append(addr)
86         return addrs if len(addrs) > 0 else None
87
88     def routes_to_data(self, routes):
89         rts = list()
90         for route in routes:
91             rt = dict()
92             for attribute in route.attibutes:
93                 if attribute.modified:
94                     rt[attribute.name] = attribute.value
95             rts.append(rt)
96         return rts if len(rts) > 0 else None
97
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)
104             else:
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)
110         return experiment
111
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)
116
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"])
127         if "traces" in data:
128             self.traces_from_data(box, data["traces"])
129         if "addresses" in data:
130             self.addresses_from_data(box, data["addresses"])
131         if "routes" in data:
132             self.routes_from_data(box, experiment, data["routes"])
133
134     def factory_attributes_from_data(self, factory_id, testbed_description, 
135             data):
136         factory = testbed_description.provider.factory(factory_id)
137         for name, value in data.iteritems():
138             factory.set_attribute_value(name, value)
139
140     def attributes_from_data(self, box, data):
141         for name, value in data.iteritems():
142             box.set_attribute_value(name, value)
143
144     def traces_from_data(self, box, data):
145         for name in data:
146             box.trace(name).enable()
147
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)
159
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)
165
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)
171