06a0310033ae8d641293c8918aa83c020f72d8fc
[nepi.git] / src / nepi / util / parser / base.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import sys
5
6 class ExperimentParser(object):
7     def to_data(self, experiment_description):
8         data = dict()
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)
14         return data
15
16     def testbed_to_data(self, testbed_description):
17         elem = dict()
18         elem["testbed_id"] = testbed_description.provider.testbed_id
19         elem["testbed_version"] = testbed_description.provider.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, 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)
103             else:
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
110
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:
116             __import__(mod_name)
117         testbed_mod = sys.modules[mod_name]
118         provider = testbed_mod.TestbedFactoriesProvider(testbed_version)
119         experiment_description.add_testbed_description(provider)
120
121     def box_from_data(self, experiment_description, data):
122         testbed_guid = data["testbed_guid"]
123         testbed_description = experiment_description.testbed_description(
124                 testbed_guid)
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"])
132         if "traces" in data:
133             self.traces_from_data(box, data["traces"])
134         if "addresses" in data:
135             self.addresses_from_data(box, data["addresses"])
136         if "routes" in data:
137             self.routes_from_data(box, experiment_description, data["routes"])
138
139     def factory_attributes_from_data(self, factory_id, testbed_description, 
140             data):
141         factory = testbed_description.provider.factory(factory_id)
142         for name, value in data.iteritems():
143             factory.set_attribute_value(name, value)
144
145     def attributes_from_data(self, box, data):
146         for name, value in data.iteritems():
147             box.set_attribute_value(name, value)
148
149     def traces_from_data(self, box, data):
150         for name in data:
151             box.trace(name).enable()
152
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)
164
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)
170
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)
176