Commit | Line | Data |
---|---|---|
4810b707 PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2015-2020 Philippe Proulx <pproulx@efficios.com> | |
4 | # | |
5 | # Permission is hereby granted, free of charge, to any person obtaining | |
6 | # a copy of this software and associated documentation files (the | |
7 | # "Software"), to deal in the Software without restriction, including | |
8 | # without limitation the rights to use, copy, modify, merge, publish, | |
9 | # distribute, sublicense, and/or sell copies of the Software, and to | |
10 | # permit persons to whom the Software is furnished to do so, subject to | |
11 | # the following conditions: | |
12 | # | |
13 | # The above copyright notice and this permission notice shall be | |
14 | # included in all copies or substantial portions of the Software. | |
15 | # | |
16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
19 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
20 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
21 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
22 | # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
23 | ||
24 | from barectf.config_parse_common import _ConfigurationParseError | |
25 | from barectf.config_parse_common import _append_error_ctx | |
26 | import barectf.config_parse_common as config_parse_common | |
2d55dc7d | 27 | from barectf.config_parse_common import _MapNode |
4810b707 PP |
28 | import collections |
29 | import copy | |
2d55dc7d PP |
30 | from barectf.typing import VersionNumber, _OptStr |
31 | from typing import Optional, List, Dict, TextIO, Union, Callable | |
32 | import typing | |
4810b707 PP |
33 | |
34 | ||
2d55dc7d | 35 | def _del_prop_if_exists(node: _MapNode, prop_name: str): |
4810b707 PP |
36 | if prop_name in node: |
37 | del node[prop_name] | |
38 | ||
39 | ||
2d55dc7d | 40 | def _rename_prop(node: _MapNode, old_prop_name: str, new_prop_name: str): |
4810b707 PP |
41 | if old_prop_name in node: |
42 | node[new_prop_name] = node[old_prop_name] | |
43 | del node[old_prop_name] | |
44 | ||
45 | ||
2d55dc7d PP |
46 | def _copy_prop_if_exists(dst_node: _MapNode, src_node: _MapNode, src_prop_name: str, |
47 | dst_prop_name: _OptStr = None): | |
4810b707 PP |
48 | if dst_prop_name is None: |
49 | dst_prop_name = src_prop_name | |
50 | ||
51 | if src_prop_name in src_node: | |
52 | dst_node[dst_prop_name] = copy.deepcopy(src_node[src_prop_name]) | |
53 | ||
54 | ||
55 | # A barectf 2 YAML configuration parser. | |
56 | # | |
57 | # The only purpose of such a parser is to transform the passed root | |
58 | # configuration node so that it's a valid barectf 3 configuration node. | |
59 | # | |
60 | # The parser's `config_node` property is the equivalent barectf 3 | |
61 | # configuration node. | |
62 | # | |
63 | # See the comments of _parse() for more implementation details about the | |
64 | # parsing stages and general strategy. | |
65 | class _Parser(config_parse_common._Parser): | |
66 | # Builds a barectf 2 YAML configuration parser and parses the root | |
2d55dc7d PP |
67 | # configuration node `node` (already loaded from the file-like |
68 | # object `root_file`). | |
69 | def __init__(self, root_file: TextIO, node: _MapNode, with_pkg_include_dir: bool, | |
70 | include_dirs: Optional[List[str]], ignore_include_not_found: bool): | |
71 | super().__init__(root_file, node, with_pkg_include_dir, include_dirs, | |
72 | ignore_include_not_found, VersionNumber(2)) | |
73 | self._ft_cls_name_to_conv_method: Dict[str, Callable[[_MapNode], _MapNode]] = { | |
4810b707 PP |
74 | 'int': self._conv_int_ft_node, |
75 | 'integer': self._conv_int_ft_node, | |
76 | 'enum': self._conv_enum_ft_node, | |
77 | 'enumeration': self._conv_enum_ft_node, | |
78 | 'flt': self._conv_real_ft_node, | |
79 | 'float': self._conv_real_ft_node, | |
80 | 'floating-point': self._conv_real_ft_node, | |
81 | 'str': self._conv_string_ft_node, | |
82 | 'string': self._conv_string_ft_node, | |
be9f12dc | 83 | 'array': self._conv_array_ft_node, |
4810b707 PP |
84 | 'struct': self._conv_struct_ft_node, |
85 | 'structure': self._conv_struct_ft_node, | |
86 | } | |
87 | self._parse() | |
88 | ||
89 | # Converts a v2 field type node to a v3 field type node and returns | |
90 | # it. | |
2d55dc7d | 91 | def _conv_ft_node(self, v2_ft_node: _MapNode) -> _MapNode: |
4810b707 PP |
92 | assert 'class' in v2_ft_node |
93 | cls = v2_ft_node['class'] | |
94 | assert cls in self._ft_cls_name_to_conv_method | |
95 | return self._ft_cls_name_to_conv_method[cls](v2_ft_node) | |
96 | ||
2d55dc7d | 97 | def _conv_ft_node_if_exists(self, v2_parent_node: Optional[_MapNode], key: str) -> Optional[_MapNode]: |
4810b707 | 98 | if v2_parent_node is None: |
2d55dc7d | 99 | return None |
4810b707 PP |
100 | |
101 | if key not in v2_parent_node: | |
2d55dc7d | 102 | return None |
4810b707 PP |
103 | |
104 | return self._conv_ft_node(v2_parent_node[key]) | |
105 | ||
106 | # Converts a v2 integer field type node to a v3 integer field type | |
107 | # node and returns it. | |
2d55dc7d | 108 | def _conv_int_ft_node(self, v2_ft_node: _MapNode) -> _MapNode: |
4810b707 PP |
109 | # copy v2 integer field type node |
110 | v3_ft_node = copy.deepcopy(v2_ft_node) | |
111 | ||
112 | # signedness depends on the class, not a property | |
113 | cls_name = 'uint' | |
114 | prop_name = 'signed' | |
115 | is_signed_node = v3_ft_node.get(prop_name) | |
116 | ||
117 | if is_signed_node is True: | |
118 | cls_name = 'sint' | |
119 | ||
120 | v3_ft_node['class'] = cls_name | |
121 | _del_prop_if_exists(v3_ft_node, prop_name) | |
122 | ||
123 | # rename `align` property to `alignment` | |
124 | _rename_prop(v3_ft_node, 'align', 'alignment') | |
125 | ||
126 | # rename `base` property to `preferred-display-base` | |
127 | _rename_prop(v3_ft_node, 'base', 'preferred-display-base') | |
128 | ||
129 | # remove `encoding` property | |
130 | _del_prop_if_exists(v3_ft_node, 'encoding') | |
131 | ||
4c91e769 PP |
132 | # remove `byte-order` property (always target BO in v3) |
133 | _del_prop_if_exists(v3_ft_node, 'byte-order') | |
134 | ||
4810b707 PP |
135 | # remove `property-mappings` property |
136 | _del_prop_if_exists(v3_ft_node, 'property-mappings') | |
137 | ||
138 | return v3_ft_node | |
139 | ||
140 | # Converts a v2 enumeration field type node to a v3 enumeration | |
141 | # field type node and returns it. | |
2d55dc7d | 142 | def _conv_enum_ft_node(self, v2_ft_node: _MapNode) -> _MapNode: |
4810b707 PP |
143 | # An enumeration field type _is_ an integer field type, so use a |
144 | # copy of the converted v2 value field type node. | |
145 | v3_ft_node = copy.deepcopy(self._conv_ft_node(v2_ft_node['value-type'])) | |
146 | ||
147 | # transform class name accordingly | |
148 | prop_name = 'class' | |
149 | cls_name = 'uenum' | |
150 | ||
151 | if v3_ft_node[prop_name] == 'sint': | |
152 | cls_name = 'senum' | |
153 | ||
154 | v3_ft_node[prop_name] = cls_name | |
155 | ||
156 | # convert members to mappings | |
157 | prop_name = 'members' | |
158 | members_node = v2_ft_node.get(prop_name) | |
159 | ||
160 | if members_node is not None: | |
2d55dc7d | 161 | mappings_node: _MapNode = collections.OrderedDict() |
4810b707 PP |
162 | cur = 0 |
163 | ||
164 | for member_node in members_node: | |
2d55dc7d PP |
165 | v3_value_node: Union[int, List[int]] |
166 | ||
4810b707 PP |
167 | if type(member_node) is str: |
168 | label = member_node | |
169 | v3_value_node = cur | |
170 | cur += 1 | |
171 | else: | |
172 | assert type(member_node) is collections.OrderedDict | |
173 | label = member_node['label'] | |
174 | v2_value_node = member_node['value'] | |
175 | ||
176 | if type(v2_value_node) is int: | |
177 | cur = v2_value_node + 1 | |
178 | v3_value_node = v2_value_node | |
179 | else: | |
180 | assert type(v2_value_node) is list | |
181 | assert len(v2_value_node) == 2 | |
182 | v3_value_node = list(v2_value_node) | |
183 | cur = v2_value_node[1] + 1 | |
184 | ||
185 | if label not in mappings_node: | |
186 | mappings_node[label] = [] | |
187 | ||
188 | mappings_node[label].append(v3_value_node) | |
189 | ||
190 | v3_ft_node['mappings'] = mappings_node | |
191 | ||
192 | return v3_ft_node | |
193 | ||
194 | # Converts a v2 real field type node to a v3 real field type node | |
195 | # and returns it. | |
2d55dc7d | 196 | def _conv_real_ft_node(self, v2_ft_node: _MapNode) -> _MapNode: |
4810b707 PP |
197 | # copy v2 real field type node |
198 | v3_ft_node = copy.deepcopy(v2_ft_node) | |
199 | ||
200 | # set class to `real` | |
201 | v3_ft_node['class'] = 'real' | |
202 | ||
203 | # rename `align` property to `alignment` | |
204 | _rename_prop(v3_ft_node, 'align', 'alignment') | |
205 | ||
206 | # set `size` property to a single integer (total size, in bits) | |
207 | prop_name = 'size' | |
208 | v3_ft_node[prop_name] = v3_ft_node[prop_name]['exp'] + v3_ft_node[prop_name]['mant'] | |
209 | ||
210 | return v3_ft_node | |
211 | ||
212 | # Converts a v2 string field type node to a v3 string field type | |
213 | # node and returns it. | |
2d55dc7d | 214 | def _conv_string_ft_node(self, v2_ft_node: _MapNode) -> _MapNode: |
4810b707 PP |
215 | # copy v2 string field type node |
216 | v3_ft_node = copy.deepcopy(v2_ft_node) | |
217 | ||
218 | # remove `encoding` property | |
219 | _del_prop_if_exists(v3_ft_node, 'encoding') | |
220 | ||
221 | return v3_ft_node | |
222 | ||
223 | # Converts a v2 array field type node to a v3 (static) array field | |
224 | # type node and returns it. | |
be9f12dc PP |
225 | def _conv_array_ft_node(self, v2_ft_node: _MapNode) -> _MapNode: |
226 | # class renamed to `static-array` or `dynamic-array` | |
227 | is_dynamic = v2_ft_node['length'] == 'dynamic' | |
228 | array_type = 'dynamic' if is_dynamic else 'static' | |
229 | v3_ft_node: _MapNode = collections.OrderedDict({'class': f'{array_type}-array'}) | |
230 | ||
231 | # copy `length` property if it's a static array field type | |
232 | if not is_dynamic: | |
233 | _copy_prop_if_exists(v3_ft_node, v2_ft_node, 'length') | |
4810b707 PP |
234 | |
235 | # convert element field type | |
236 | v3_ft_node['element-field-type'] = self._conv_ft_node(v2_ft_node['element-type']) | |
237 | ||
238 | return v3_ft_node | |
239 | ||
240 | # Converts a v2 structure field type node to a v3 structure field | |
241 | # type node and returns it. | |
2d55dc7d | 242 | def _conv_struct_ft_node(self, v2_ft_node: _MapNode) -> _MapNode: |
4810b707 PP |
243 | # Create fresh v3 structure field type node, reusing the class |
244 | # of `v2_ft_node`. | |
245 | v3_ft_node = collections.OrderedDict({'class': v2_ft_node['class']}) | |
246 | ||
247 | # rename `min-align` property to `minimum-alignment` | |
248 | _copy_prop_if_exists(v3_ft_node, v2_ft_node, 'min-align', 'minimum-alignment') | |
249 | ||
250 | # convert fields to members | |
251 | prop_name = 'fields' | |
252 | ||
253 | if prop_name in v2_ft_node: | |
254 | members_node = [] | |
255 | ||
256 | for member_name, v2_member_ft_node in v2_ft_node[prop_name].items(): | |
257 | members_node.append(collections.OrderedDict({ | |
258 | member_name: collections.OrderedDict({ | |
259 | 'field-type': self._conv_ft_node(v2_member_ft_node) | |
260 | }) | |
261 | })) | |
262 | ||
263 | v3_ft_node['members'] = members_node | |
264 | ||
265 | return v3_ft_node | |
266 | ||
267 | # Converts a v2 clock type node to a v3 clock type node and returns | |
268 | # it. | |
2d55dc7d | 269 | def _conv_clk_type_node(self, v2_clk_type_node: _MapNode) -> _MapNode: |
4810b707 PP |
270 | # copy v2 clock type node |
271 | v3_clk_type_node = copy.deepcopy(v2_clk_type_node) | |
272 | ||
273 | # rename `freq` property to `frequency` | |
274 | _rename_prop(v3_clk_type_node, 'freq', 'frequency') | |
275 | ||
276 | # rename `error-cycles` property to `precision` | |
277 | _rename_prop(v3_clk_type_node, 'error-cycles', 'precision') | |
278 | ||
279 | # rename `absolute` property to `origin-is-unix-epoch` | |
280 | _rename_prop(v3_clk_type_node, 'absolute', 'origin-is-unix-epoch') | |
281 | ||
282 | # rename `$return-ctype`/`return-ctype` property to `$c-type` | |
283 | new_prop_name = '$c-type' | |
284 | _rename_prop(v3_clk_type_node, 'return-ctype', new_prop_name) | |
285 | _rename_prop(v3_clk_type_node, '$return-ctype', new_prop_name) | |
286 | ||
287 | return v3_clk_type_node | |
288 | ||
289 | # Converts a v2 event type node to a v3 event type node and returns | |
290 | # it. | |
2d55dc7d | 291 | def _conv_ev_type_node(self, v2_ev_type_node: _MapNode) -> _MapNode: |
4810b707 | 292 | # create empty v3 event type node |
2d55dc7d | 293 | v3_ev_type_node: _MapNode = collections.OrderedDict() |
4810b707 PP |
294 | |
295 | # copy `log-level` property | |
296 | _copy_prop_if_exists(v3_ev_type_node, v2_ev_type_node, 'log-level') | |
297 | ||
298 | # convert specific context field type node | |
299 | v2_ft_node = v2_ev_type_node.get('context-type') | |
300 | ||
301 | if v2_ft_node is not None: | |
302 | v3_ev_type_node['specific-context-field-type'] = self._conv_ft_node(v2_ft_node) | |
303 | ||
304 | # convert payload field type node | |
305 | v2_ft_node = v2_ev_type_node.get('payload-type') | |
306 | ||
307 | if v2_ft_node is not None: | |
308 | v3_ev_type_node['payload-field-type'] = self._conv_ft_node(v2_ft_node) | |
309 | ||
310 | return v3_ev_type_node | |
311 | ||
312 | @staticmethod | |
2d55dc7d PP |
313 | def _set_v3_feature_ft_if_exists(v3_features_node: _MapNode, key: str, |
314 | node: Union[Optional[_MapNode], bool]): | |
4810b707 PP |
315 | val = node |
316 | ||
317 | if val is None: | |
318 | val = False | |
319 | ||
320 | v3_features_node[key] = val | |
321 | ||
322 | # Converts a v2 stream type node to a v3 stream type node and | |
323 | # returns it. | |
2d55dc7d | 324 | def _conv_stream_type_node(self, v2_stream_type_node: _MapNode) -> _MapNode: |
4810b707 PP |
325 | # This function creates a v3 stream type features node from the |
326 | # packet context and event header field type nodes of a | |
327 | # v2 stream type node. | |
2d55dc7d PP |
328 | def v3_features_node_from_v2_ft_nodes(v2_pkt_ctx_ft_fields_node: _MapNode, |
329 | v2_ev_header_ft_fields_node: Optional[_MapNode]) -> _MapNode: | |
4810b707 PP |
330 | if v2_ev_header_ft_fields_node is None: |
331 | v2_ev_header_ft_fields_node = collections.OrderedDict() | |
332 | ||
333 | v3_pkt_total_size_ft_node = self._conv_ft_node(v2_pkt_ctx_ft_fields_node['packet_size']) | |
334 | v3_pkt_content_size_ft_node = self._conv_ft_node(v2_pkt_ctx_ft_fields_node['content_size']) | |
335 | v3_pkt_beg_time_ft_node = self._conv_ft_node_if_exists(v2_pkt_ctx_ft_fields_node, | |
336 | 'timestamp_begin') | |
337 | v3_pkt_end_time_ft_node = self._conv_ft_node_if_exists(v2_pkt_ctx_ft_fields_node, | |
338 | 'timestamp_end') | |
339 | v3_pkt_disc_ev_counter_ft_node = self._conv_ft_node_if_exists(v2_pkt_ctx_ft_fields_node, | |
340 | 'events_discarded') | |
341 | v3_ev_type_id_ft_node = self._conv_ft_node_if_exists(v2_ev_header_ft_fields_node, 'id') | |
342 | v3_ev_time_ft_node = self._conv_ft_node_if_exists(v2_ev_header_ft_fields_node, | |
343 | 'timestamp') | |
2d55dc7d PP |
344 | v3_features_node: _MapNode = collections.OrderedDict() |
345 | v3_pkt_node: _MapNode = collections.OrderedDict() | |
346 | v3_ev_node: _MapNode = collections.OrderedDict() | |
4810b707 PP |
347 | v3_pkt_node['total-size-field-type'] = v3_pkt_total_size_ft_node |
348 | v3_pkt_node['content-size-field-type'] = v3_pkt_content_size_ft_node | |
349 | self._set_v3_feature_ft_if_exists(v3_pkt_node, 'beginning-time-field-type', | |
350 | v3_pkt_beg_time_ft_node) | |
351 | self._set_v3_feature_ft_if_exists(v3_pkt_node, 'end-time-field-type', | |
352 | v3_pkt_end_time_ft_node) | |
353 | self._set_v3_feature_ft_if_exists(v3_pkt_node, 'discarded-events-counter-field-type', | |
354 | v3_pkt_disc_ev_counter_ft_node) | |
355 | self._set_v3_feature_ft_if_exists(v3_ev_node, 'type-id-field-type', | |
356 | v3_ev_type_id_ft_node) | |
357 | self._set_v3_feature_ft_if_exists(v3_ev_node, 'time-field-type', v3_ev_time_ft_node) | |
358 | v3_features_node['packet'] = v3_pkt_node | |
359 | v3_features_node['event'] = v3_ev_node | |
360 | return v3_features_node | |
361 | ||
2d55dc7d | 362 | def clk_type_name_from_v2_int_ft_node(v2_int_ft_node: Optional[_MapNode]) -> _OptStr: |
4810b707 | 363 | if v2_int_ft_node is None: |
2d55dc7d | 364 | return None |
4810b707 PP |
365 | |
366 | assert v2_int_ft_node['class'] in ('int', 'integer') | |
367 | prop_mappings_node = v2_int_ft_node.get('property-mappings') | |
368 | ||
369 | if prop_mappings_node is not None and len(prop_mappings_node) > 0: | |
370 | return prop_mappings_node[0]['name'] | |
371 | ||
2d55dc7d PP |
372 | return None |
373 | ||
4810b707 | 374 | # create empty v3 stream type node |
2d55dc7d | 375 | v3_stream_type_node: _MapNode = collections.OrderedDict() |
4810b707 PP |
376 | |
377 | # rename `$default` property to `$is-default` | |
378 | _copy_prop_if_exists(v3_stream_type_node, v2_stream_type_node, '$default', '$is-default') | |
379 | ||
380 | # set default clock type node | |
381 | pct_prop_name = 'packet-context-type' | |
382 | v2_pkt_ctx_ft_fields_node = v2_stream_type_node[pct_prop_name]['fields'] | |
383 | eht_prop_name = 'event-header-type' | |
384 | v2_ev_header_ft_fields_node = None | |
385 | v2_ev_header_ft_node = v2_stream_type_node.get(eht_prop_name) | |
386 | ||
387 | if v2_ev_header_ft_node is not None: | |
388 | v2_ev_header_ft_fields_node = v2_ev_header_ft_node['fields'] | |
389 | ||
390 | def_clk_type_name = None | |
391 | ||
392 | try: | |
393 | ts_begin_prop_name = 'timestamp_begin' | |
394 | ts_begin_clk_type_name = clk_type_name_from_v2_int_ft_node(v2_pkt_ctx_ft_fields_node.get(ts_begin_prop_name)) | |
395 | ts_end_prop_name = 'timestamp_end' | |
396 | ts_end_clk_type_name = clk_type_name_from_v2_int_ft_node(v2_pkt_ctx_ft_fields_node.get(ts_end_prop_name)) | |
397 | ||
398 | if ts_begin_clk_type_name is not None and ts_end_clk_type_name is not None: | |
399 | if ts_begin_clk_type_name != ts_end_clk_type_name: | |
400 | raise _ConfigurationParseError(f'`{ts_begin_prop_name}`/`{ts_end_prop_name}` properties', | |
401 | 'Field types are not mapped to the same clock type') | |
402 | except _ConfigurationParseError as exc: | |
403 | _append_error_ctx(exc, f'`{pct_prop_name}` property') | |
404 | ||
405 | try: | |
406 | if def_clk_type_name is None and v2_ev_header_ft_fields_node is not None: | |
407 | def_clk_type_name = clk_type_name_from_v2_int_ft_node(v2_ev_header_ft_fields_node.get('timestamp')) | |
408 | ||
409 | if def_clk_type_name is None and ts_begin_clk_type_name is not None: | |
410 | def_clk_type_name = ts_begin_clk_type_name | |
411 | ||
412 | if def_clk_type_name is None and ts_end_clk_type_name is not None: | |
413 | def_clk_type_name = ts_end_clk_type_name | |
414 | except _ConfigurationParseError as exc: | |
415 | _append_error_ctx(exc, f'`{eht_prop_name}` property') | |
416 | ||
417 | if def_clk_type_name is not None: | |
418 | v3_stream_type_node['$default-clock-type-name'] = def_clk_type_name | |
419 | ||
420 | # set features node | |
421 | v3_stream_type_node['$features'] = v3_features_node_from_v2_ft_nodes(v2_pkt_ctx_ft_fields_node, | |
422 | v2_ev_header_ft_fields_node) | |
423 | ||
424 | # set extra packet context field type members node | |
425 | pkt_ctx_ft_extra_members = [] | |
426 | ctf_member_names = [ | |
427 | 'packet_size', | |
428 | 'content_size', | |
429 | 'timestamp_begin', | |
430 | 'timestamp_end', | |
431 | 'events_discarded', | |
432 | 'packet_seq_num', | |
433 | ] | |
434 | ||
435 | for member_name, v2_ft_node in v2_pkt_ctx_ft_fields_node.items(): | |
436 | if member_name in ctf_member_names: | |
437 | continue | |
438 | ||
439 | pkt_ctx_ft_extra_members.append(collections.OrderedDict({ | |
440 | member_name: collections.OrderedDict({ | |
441 | 'field-type': self._conv_ft_node(v2_ft_node) | |
442 | }) | |
443 | })) | |
444 | ||
445 | if len(pkt_ctx_ft_extra_members) > 0: | |
446 | v3_stream_type_node['packet-context-field-type-extra-members'] = pkt_ctx_ft_extra_members | |
447 | ||
448 | # convert event common context field type node | |
449 | v2_ft_node = v2_stream_type_node.get('event-context-type') | |
450 | ||
451 | if v2_ft_node is not None: | |
452 | v3_stream_type_node['event-common-context-field-type'] = self._conv_ft_node(v2_ft_node) | |
453 | ||
454 | # convert event type nodes | |
455 | v3_event_types_node = collections.OrderedDict() | |
456 | ||
457 | for ev_type_name, v2_ev_type_node in v2_stream_type_node['events'].items(): | |
458 | try: | |
459 | v3_event_types_node[ev_type_name] = self._conv_ev_type_node(v2_ev_type_node) | |
460 | except _ConfigurationParseError as exc: | |
461 | _append_error_ctx(exc, f'Event type `{ev_type_name}`') | |
462 | ||
463 | v3_stream_type_node['event-types'] = v3_event_types_node | |
464 | ||
465 | return v3_stream_type_node | |
466 | ||
467 | # Converts a v2 metadata node to a v3 trace node and returns it. | |
2d55dc7d PP |
468 | def _conv_meta_node(self, v2_meta_node: _MapNode) -> _MapNode: |
469 | def v3_features_node_from_v2_ft_node(v2_pkt_header_ft_node: Optional[_MapNode]) -> _MapNode: | |
4810b707 PP |
470 | def set_if_exists(key, node): |
471 | return self._set_v3_feature_ft_if_exists(v3_features_node, key, node) | |
472 | ||
473 | v2_pkt_header_ft_fields_node = collections.OrderedDict() | |
474 | ||
475 | if v2_pkt_header_ft_node is not None: | |
476 | v2_pkt_header_ft_fields_node = v2_pkt_header_ft_node['fields'] | |
477 | ||
478 | v3_magic_ft_node = self._conv_ft_node_if_exists(v2_pkt_header_ft_fields_node, 'magic') | |
479 | v3_uuid_ft_node = self._conv_ft_node_if_exists(v2_pkt_header_ft_fields_node, 'uuid') | |
480 | v3_stream_type_id_ft_node = self._conv_ft_node_if_exists(v2_pkt_header_ft_fields_node, | |
481 | 'stream_id') | |
2d55dc7d | 482 | v3_features_node: _MapNode = collections.OrderedDict() |
4810b707 PP |
483 | set_if_exists('magic-field-type', v3_magic_ft_node) |
484 | set_if_exists('uuid-field-type', v3_uuid_ft_node) | |
485 | set_if_exists('stream-type-id-field-type', v3_stream_type_id_ft_node) | |
486 | return v3_features_node | |
487 | ||
2d55dc7d PP |
488 | v3_trace_node: _MapNode = collections.OrderedDict() |
489 | v3_trace_type_node: _MapNode = collections.OrderedDict() | |
4810b707 PP |
490 | v2_trace_node = v2_meta_node['trace'] |
491 | ||
7fffc7d1 PP |
492 | # Move `byte-order` property to root node's `target-byte-order` |
493 | # property. | |
5c841bbf | 494 | typing.cast(_MapNode, self._root_node)['target-byte-order'] = v2_trace_node['byte-order'] |
4810b707 PP |
495 | |
496 | # copy `uuid` property | |
497 | _copy_prop_if_exists(v3_trace_type_node, v2_trace_node, 'uuid') | |
498 | ||
499 | # copy `$log-levels`/`log-levels` property | |
500 | new_prop_name = '$log-level-aliases' | |
501 | _copy_prop_if_exists(v3_trace_type_node, v2_meta_node, 'log-levels', new_prop_name) | |
502 | _copy_prop_if_exists(v3_trace_type_node, v2_meta_node, '$log-levels', new_prop_name) | |
503 | ||
504 | # copy `clocks` property, converting clock type nodes | |
505 | v2_clk_types_node = v2_meta_node.get('clocks') | |
506 | ||
507 | if v2_clk_types_node is not None: | |
508 | v3_clk_types_node = collections.OrderedDict() | |
509 | ||
510 | for name, v2_clk_type_node in v2_clk_types_node.items(): | |
511 | v3_clk_types_node[name] = self._conv_clk_type_node(v2_clk_type_node) | |
512 | ||
513 | v3_trace_type_node['clock-types'] = v3_clk_types_node | |
514 | ||
515 | # set features node | |
516 | v2_pkt_header_ft_node = v2_trace_node.get('packet-header-type') | |
517 | v3_trace_type_node['$features'] = v3_features_node_from_v2_ft_node(v2_pkt_header_ft_node) | |
518 | ||
519 | # convert stream type nodes | |
520 | v3_stream_types_node = collections.OrderedDict() | |
521 | ||
522 | for stream_type_name, v2_stream_type_node in v2_meta_node['streams'].items(): | |
523 | try: | |
524 | v3_stream_types_node[stream_type_name] = self._conv_stream_type_node(v2_stream_type_node) | |
525 | except _ConfigurationParseError as exc: | |
526 | _append_error_ctx(exc, f'Stream type `{stream_type_name}`') | |
527 | ||
528 | v3_trace_type_node['stream-types'] = v3_stream_types_node | |
529 | ||
530 | # If `v2_meta_node` has a `$default-stream` property, find the | |
531 | # corresponding v3 stream type node and set its `$is-default` | |
532 | # property to `True`. | |
533 | prop_name = '$default-stream' | |
534 | v2_def_stream_type_node = v2_meta_node.get(prop_name) | |
535 | ||
536 | if v2_def_stream_type_node is not None: | |
537 | found = False | |
538 | ||
539 | for stream_type_name, v3_stream_type_node in v3_stream_types_node.items(): | |
540 | if stream_type_name == v2_def_stream_type_node: | |
541 | v3_stream_type_node['$is-default'] = True | |
542 | found = True | |
543 | break | |
544 | ||
545 | if not found: | |
546 | raise _ConfigurationParseError(f'`{prop_name}` property', | |
547 | f'Stream type `{v2_def_stream_type_node}` does not exist') | |
548 | ||
549 | # set environment node | |
550 | v2_env_node = v2_meta_node.get('env') | |
551 | ||
552 | if v2_env_node is not None: | |
553 | v3_trace_node['environment'] = copy.deepcopy(v2_env_node) | |
554 | ||
555 | # set v3 trace node's type node | |
556 | v3_trace_node['type'] = v3_trace_type_node | |
557 | ||
558 | return v3_trace_node | |
559 | ||
560 | # Transforms the root configuration node into a valid v3 | |
561 | # configuration node. | |
562 | def _transform_config_node(self): | |
563 | # remove the `version` property | |
564 | del self._root_node['version'] | |
565 | ||
566 | # relocate prefix and option nodes | |
567 | prefix_prop_name = 'prefix' | |
568 | v2_prefix_node = self._root_node.get(prefix_prop_name, 'barectf_') | |
569 | _del_prop_if_exists(self._root_node, prefix_prop_name) | |
570 | opt_prop_name = 'options' | |
571 | v2_options_node = self._root_node.get(opt_prop_name) | |
572 | _del_prop_if_exists(self._root_node, opt_prop_name) | |
573 | code_gen_node = collections.OrderedDict() | |
574 | v3_prefixes = config_parse_common._v3_prefixes_from_v2_prefix(v2_prefix_node) | |
575 | v3_prefix_node = collections.OrderedDict([ | |
576 | ('identifier', v3_prefixes.identifier), | |
577 | ('file-name', v3_prefixes.file_name), | |
578 | ]) | |
579 | code_gen_node[prefix_prop_name] = v3_prefix_node | |
580 | ||
581 | if v2_options_node is not None: | |
582 | header_node = collections.OrderedDict() | |
583 | _copy_prop_if_exists(header_node, v2_options_node, 'gen-prefix-def', | |
584 | 'identifier-prefix-definition') | |
585 | _copy_prop_if_exists(header_node, v2_options_node, 'gen-default-stream-def', | |
586 | 'default-stream-type-name-definition') | |
587 | code_gen_node['header'] = header_node | |
588 | ||
589 | self._root_node[opt_prop_name] = collections.OrderedDict({ | |
590 | 'code-generation': code_gen_node, | |
591 | }) | |
592 | ||
593 | # convert the v2 metadata node into a v3 trace node | |
594 | try: | |
595 | self._root_node['trace'] = self._conv_meta_node(self._root_node['metadata']) | |
596 | except _ConfigurationParseError as exc: | |
597 | _append_error_ctx(exc, 'Metadata object') | |
598 | ||
599 | del self._root_node['metadata'] | |
600 | ||
601 | # Expands the field type aliases found in the metadata node. | |
602 | # | |
603 | # This method modifies the metadata node. | |
604 | # | |
605 | # When this method returns: | |
606 | # | |
607 | # * Any field type alias is replaced with its full field type node | |
608 | # equivalent. | |
609 | # | |
610 | # * The `type-aliases` property of metadata node is removed. | |
611 | def _expand_ft_aliases(self): | |
612 | meta_node = self._root_node['metadata'] | |
613 | ft_aliases_node = meta_node['type-aliases'] | |
614 | ||
615 | # Expand field type aliases within trace, stream, and event | |
616 | # types now. | |
617 | try: | |
618 | self._resolve_ft_alias_from(ft_aliases_node, meta_node['trace'], 'packet-header-type') | |
619 | except _ConfigurationParseError as exc: | |
620 | _append_error_ctx(exc, 'Trace type') | |
621 | ||
622 | for stream_type_name, stream_type_node in meta_node['streams'].items(): | |
623 | try: | |
624 | self._resolve_ft_alias_from(ft_aliases_node, stream_type_node, | |
625 | 'packet-context-type') | |
626 | self._resolve_ft_alias_from(ft_aliases_node, stream_type_node, 'event-header-type') | |
627 | self._resolve_ft_alias_from(ft_aliases_node, stream_type_node, | |
628 | 'event-context-type') | |
629 | ||
630 | for ev_type_name, ev_type_node in stream_type_node['events'].items(): | |
631 | try: | |
632 | self._resolve_ft_alias_from(ft_aliases_node, ev_type_node, 'context-type') | |
633 | self._resolve_ft_alias_from(ft_aliases_node, ev_type_node, 'payload-type') | |
634 | except _ConfigurationParseError as exc: | |
635 | _append_error_ctx(exc, f'Event type `{ev_type_name}`') | |
636 | except _ConfigurationParseError as exc: | |
637 | _append_error_ctx(exc, f'Stream type `{stream_type_name}`') | |
638 | ||
639 | # remove the (now unneeded) `type-aliases` node | |
640 | del meta_node['type-aliases'] | |
641 | ||
642 | # Applies field type inheritance to all field type nodes found in | |
643 | # the metadata node. | |
644 | # | |
645 | # This method modifies the metadata node. | |
646 | # | |
647 | # When this method returns, no field type node has an `$inherit` or | |
648 | # `inherit` property. | |
649 | def _apply_fts_inheritance(self): | |
650 | meta_node = self._root_node['metadata'] | |
651 | self._apply_ft_inheritance(meta_node['trace'], 'packet-header-type') | |
652 | ||
653 | for stream_type_node in meta_node['streams'].values(): | |
654 | self._apply_ft_inheritance(stream_type_node, 'packet-context-type') | |
655 | self._apply_ft_inheritance(stream_type_node, 'event-header-type') | |
656 | self._apply_ft_inheritance(stream_type_node, 'event-context-type') | |
657 | ||
658 | for ev_type_node in stream_type_node['events'].values(): | |
659 | self._apply_ft_inheritance(ev_type_node, 'context-type') | |
660 | self._apply_ft_inheritance(ev_type_node, 'payload-type') | |
661 | ||
662 | # Calls _expand_ft_aliases() and _apply_fts_inheritance() if the | |
663 | # metadata node has a `type-aliases` property. | |
664 | def _expand_fts(self): | |
665 | # Make sure that the current configuration node is valid | |
666 | # considering field types are not expanded yet. | |
667 | self._schema_validator.validate(self._root_node, | |
c3fa1a14 | 668 | 'config/2/config-pre-field-type-expansion') |
4810b707 PP |
669 | |
670 | meta_node = self._root_node['metadata'] | |
671 | ft_aliases_node = meta_node.get('type-aliases') | |
672 | ||
673 | if ft_aliases_node is None: | |
674 | # If there's no `type-aliases` node, then there's no field | |
675 | # type aliases and therefore no possible inheritance. | |
676 | return | |
677 | ||
678 | # first, expand field type aliases | |
679 | self._expand_ft_aliases() | |
680 | ||
681 | # next, apply inheritance to create effective field types | |
682 | self._apply_fts_inheritance() | |
683 | ||
684 | # Processes the inclusions of the event type node `ev_type_node`, | |
685 | # returning the effective node. | |
2d55dc7d | 686 | def _process_ev_type_node_include(self, ev_type_node: _MapNode) -> _MapNode: |
4810b707 PP |
687 | # Make sure the event type node is valid for the inclusion |
688 | # processing stage. | |
c3fa1a14 | 689 | self._schema_validator.validate(ev_type_node, 'config/2/event-type-pre-include') |
4810b707 PP |
690 | |
691 | # process inclusions | |
692 | return self._process_node_include(ev_type_node, self._process_ev_type_node_include) | |
693 | ||
694 | # Processes the inclusions of the stream type node | |
695 | # `stream_type_node`, returning the effective node. | |
2d55dc7d | 696 | def _process_stream_type_node_include(self, stream_type_node: _MapNode) -> _MapNode: |
4810b707 PP |
697 | def process_children_include(stream_type_node): |
698 | prop_name = 'events' | |
699 | ||
700 | if prop_name in stream_type_node: | |
701 | ev_types_node = stream_type_node[prop_name] | |
702 | ||
703 | for key in list(ev_types_node): | |
704 | ev_types_node[key] = self._process_ev_type_node_include(ev_types_node[key]) | |
705 | ||
706 | # Make sure the stream type node is valid for the inclusion | |
707 | # processing stage. | |
c3fa1a14 | 708 | self._schema_validator.validate(stream_type_node, 'config/2/stream-type-pre-include') |
4810b707 PP |
709 | |
710 | # process inclusions | |
711 | return self._process_node_include(stream_type_node, self._process_stream_type_node_include, | |
712 | process_children_include) | |
713 | ||
714 | # Processes the inclusions of the trace type node `trace_type_node`, | |
715 | # returning the effective node. | |
2d55dc7d | 716 | def _process_trace_type_node_include(self, trace_type_node: _MapNode) -> _MapNode: |
4810b707 PP |
717 | # Make sure the trace type node is valid for the inclusion |
718 | # processing stage. | |
c3fa1a14 | 719 | self._schema_validator.validate(trace_type_node, 'config/2/trace-type-pre-include') |
4810b707 PP |
720 | |
721 | # process inclusions | |
722 | return self._process_node_include(trace_type_node, self._process_trace_type_node_include) | |
723 | ||
724 | # Processes the inclusions of the clock type node `clk_type_node`, | |
725 | # returning the effective node. | |
2d55dc7d | 726 | def _process_clk_type_node_include(self, clk_type_node: _MapNode) -> _MapNode: |
4810b707 PP |
727 | # Make sure the clock type node is valid for the inclusion |
728 | # processing stage. | |
c3fa1a14 | 729 | self._schema_validator.validate(clk_type_node, 'config/2/clock-type-pre-include') |
4810b707 PP |
730 | |
731 | # process inclusions | |
732 | return self._process_node_include(clk_type_node, self._process_clk_type_node_include) | |
733 | ||
734 | # Processes the inclusions of the metadata node `meta_node`, | |
735 | # returning the effective node. | |
2d55dc7d PP |
736 | def _process_meta_node_include(self, meta_node: _MapNode) -> _MapNode: |
737 | def process_children_include(meta_node: _MapNode): | |
4810b707 PP |
738 | prop_name = 'trace' |
739 | ||
740 | if prop_name in meta_node: | |
741 | meta_node[prop_name] = self._process_trace_type_node_include(meta_node[prop_name]) | |
742 | ||
743 | prop_name = 'clocks' | |
744 | ||
745 | if prop_name in meta_node: | |
746 | clk_types_node = meta_node[prop_name] | |
747 | ||
748 | for key in list(clk_types_node): | |
749 | clk_types_node[key] = self._process_clk_type_node_include(clk_types_node[key]) | |
750 | ||
751 | prop_name = 'streams' | |
752 | ||
753 | if prop_name in meta_node: | |
754 | stream_types_node = meta_node[prop_name] | |
755 | ||
756 | for key in list(stream_types_node): | |
757 | stream_types_node[key] = self._process_stream_type_node_include(stream_types_node[key]) | |
758 | ||
759 | # Make sure the metadata node is valid for the inclusion | |
760 | # processing stage. | |
c3fa1a14 | 761 | self._schema_validator.validate(meta_node, 'config/2/metadata-pre-include') |
4810b707 PP |
762 | |
763 | # process inclusions | |
764 | return self._process_node_include(meta_node, self._process_meta_node_include, | |
765 | process_children_include) | |
766 | ||
767 | # Processes the inclusions of the configuration node, modifying it | |
768 | # during the process. | |
769 | def _process_config_includes(self): | |
770 | # Process inclusions in this order: | |
771 | # | |
772 | # 1. Clock type node, event type nodes, and trace type nodes | |
773 | # (the order between those is not important). | |
774 | # | |
775 | # 2. Stream type nodes. | |
776 | # | |
777 | # 3. Metadata node. | |
778 | # | |
779 | # This is because: | |
780 | # | |
781 | # * A metadata node can include clock type nodes, a trace type | |
782 | # node, stream type nodes, and event type nodes (indirectly). | |
783 | # | |
784 | # * A stream type node can include event type nodes. | |
785 | # | |
786 | # First, make sure the configuration node itself is valid for | |
787 | # the inclusion processing stage. | |
788 | self._schema_validator.validate(self._root_node, | |
c3fa1a14 | 789 | 'config/2/config-pre-include') |
4810b707 PP |
790 | |
791 | # Process metadata node inclusions. | |
792 | # | |
793 | # self._process_meta_node_include() returns a new (or the same) | |
794 | # metadata node without any `$include` property in it, | |
795 | # recursively. | |
796 | prop_name = 'metadata' | |
797 | self._root_node[prop_name] = self._process_meta_node_include(self._root_node[prop_name]) | |
798 | ||
799 | def _parse(self): | |
800 | # Make sure the configuration node is minimally valid, that is, | |
801 | # it contains a valid `version` property. | |
802 | # | |
803 | # This step does not validate the whole configuration node yet | |
804 | # because we don't have an effective configuration node; we | |
805 | # still need to: | |
806 | # | |
807 | # * Process inclusions. | |
808 | # * Expand field types (aliases and inheritance). | |
c3fa1a14 | 809 | self._schema_validator.validate(self._root_node, 'config/2/config-min') |
4810b707 PP |
810 | |
811 | # process configuration node inclusions | |
812 | self._process_config_includes() | |
813 | ||
814 | # Expand field type nodes. | |
815 | # | |
816 | # This process: | |
817 | # | |
818 | # 1. Replaces field type aliases with "effective" field type | |
819 | # nodes, recursively. | |
820 | # | |
821 | # After this step, the `type-aliases` property of the | |
822 | # metadata node is gone. | |
823 | # | |
824 | # 2. Applies inheritance, following the `$inherit`/`inherit` | |
825 | # properties. | |
826 | # | |
827 | # After this step, field type nodes do not contain `$inherit` | |
828 | # or `inherit` properties. | |
829 | # | |
830 | # This is done blindly, in that the process _doesn't_ validate | |
831 | # field type nodes at this point. | |
832 | # | |
833 | # The reason we must do this here for a barectf 2 configuration, | |
834 | # considering that barectf 3 also supports field type node | |
835 | # aliases and inheritance, is that we need to find specific | |
836 | # packet header and packet context field type member nodes (for | |
837 | # example, `stream_id`, `packet_size`, or `timestamp_end`) to | |
838 | # set the `$features` properties of barectf 3 trace type and | |
839 | # stream type nodes. Those field type nodes can be aliases, | |
840 | # contain aliases, or inherit from other nodes. | |
841 | self._expand_fts() | |
842 | ||
843 | # Validate the whole, (almost) effective configuration node. | |
844 | # | |
845 | # It's almost effective because the `log-level` property of | |
846 | # event type nodes can be log level aliases. Log level aliases | |
847 | # are also a feature of a barectf 3 configuration node, | |
848 | # therefore this is compatible. | |
c3fa1a14 | 849 | self._schema_validator.validate(self._root_node, 'config/2/config') |
4810b707 PP |
850 | |
851 | # Transform the current configuration node into a valid v3 | |
852 | # configuration node. | |
853 | self._transform_config_node() | |
854 | ||
855 | @property | |
2d55dc7d PP |
856 | def config_node(self) -> config_parse_common._ConfigNodeV3: |
857 | return config_parse_common._ConfigNodeV3(typing.cast(_MapNode, self._root_node)) |