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 | ||
313a0bd9 | 132 | # remove `byte-order` property (always native BO in v3) |
4c91e769 PP |
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 | ||
e8f0d548 PP |
289 | # Converts a v2 event record type node to a v3 event record type |
290 | # node and returns it. | |
291 | def _conv_ert_node(self, v2_ert_node: _MapNode) -> _MapNode: | |
292 | # create empty v3 event record type node | |
293 | v3_ert_node: _MapNode = collections.OrderedDict() | |
4810b707 PP |
294 | |
295 | # copy `log-level` property | |
e8f0d548 | 296 | _copy_prop_if_exists(v3_ert_node, v2_ert_node, 'log-level') |
4810b707 PP |
297 | |
298 | # convert specific context field type node | |
e8f0d548 | 299 | v2_ft_node = v2_ert_node.get('context-type') |
4810b707 PP |
300 | |
301 | if v2_ft_node is not None: | |
e8f0d548 | 302 | v3_ert_node['specific-context-field-type'] = self._conv_ft_node(v2_ft_node) |
4810b707 PP |
303 | |
304 | # convert payload field type node | |
e8f0d548 | 305 | v2_ft_node = v2_ert_node.get('payload-type') |
4810b707 PP |
306 | |
307 | if v2_ft_node is not None: | |
e8f0d548 | 308 | v3_ert_node['payload-field-type'] = self._conv_ft_node(v2_ft_node) |
4810b707 | 309 | |
e8f0d548 | 310 | return v3_ert_node |
4810b707 PP |
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 | ||
e8f0d548 PP |
322 | # Converts a v2 data stream type node to a v3 data stream type node |
323 | # and returns it. | |
324 | def _conv_dst_node(self, v2_dst_node: _MapNode) -> _MapNode: | |
325 | # This function creates a v3 data stream type features node from | |
326 | # the packet context and event record header field type nodes of | |
327 | # a v2 data stream type node. | |
2d55dc7d | 328 | def v3_features_node_from_v2_ft_nodes(v2_pkt_ctx_ft_fields_node: _MapNode, |
e8f0d548 PP |
329 | v2_er_header_ft_fields_node: Optional[_MapNode]) -> _MapNode: |
330 | if v2_er_header_ft_fields_node is None: | |
331 | v2_er_header_ft_fields_node = collections.OrderedDict() | |
4810b707 PP |
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']) | |
462e49b3 PP |
335 | v3_pkt_beg_ts_ft_node = self._conv_ft_node_if_exists(v2_pkt_ctx_ft_fields_node, |
336 | 'timestamp_begin') | |
337 | v3_pkt_end_ts_ft_node = self._conv_ft_node_if_exists(v2_pkt_ctx_ft_fields_node, | |
338 | 'timestamp_end') | |
e8f0d548 PP |
339 | v3_pkt_disc_er_counter_snap_ft_node = self._conv_ft_node_if_exists(v2_pkt_ctx_ft_fields_node, |
340 | 'events_discarded') | |
341 | v3_ert_id_ft_node = self._conv_ft_node_if_exists(v2_er_header_ft_fields_node, 'id') | |
462e49b3 PP |
342 | v3_er_ts_ft_node = self._conv_ft_node_if_exists(v2_er_header_ft_fields_node, |
343 | 'timestamp') | |
2d55dc7d PP |
344 | v3_features_node: _MapNode = collections.OrderedDict() |
345 | v3_pkt_node: _MapNode = collections.OrderedDict() | |
e8f0d548 | 346 | v3_er_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 | |
462e49b3 PP |
349 | self._set_v3_feature_ft_if_exists(v3_pkt_node, 'beginning-timestamp-field-type', |
350 | v3_pkt_beg_ts_ft_node) | |
351 | self._set_v3_feature_ft_if_exists(v3_pkt_node, 'end-timestamp-field-type', | |
352 | v3_pkt_end_ts_ft_node) | |
e8f0d548 PP |
353 | self._set_v3_feature_ft_if_exists(v3_pkt_node, |
354 | 'discarded-event-records-counter-snapshot-field-type', | |
355 | v3_pkt_disc_er_counter_snap_ft_node) | |
356 | self._set_v3_feature_ft_if_exists(v3_er_node, 'type-id-field-type', v3_ert_id_ft_node) | |
462e49b3 | 357 | self._set_v3_feature_ft_if_exists(v3_er_node, 'timestamp-field-type', v3_er_ts_ft_node) |
4810b707 | 358 | v3_features_node['packet'] = v3_pkt_node |
e8f0d548 | 359 | v3_features_node['event-record'] = v3_er_node |
4810b707 PP |
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 | ||
e8f0d548 PP |
374 | # create empty v3 data stream type node |
375 | v3_dst_node: _MapNode = collections.OrderedDict() | |
4810b707 PP |
376 | |
377 | # rename `$default` property to `$is-default` | |
e8f0d548 | 378 | _copy_prop_if_exists(v3_dst_node, v2_dst_node, '$default', '$is-default') |
4810b707 PP |
379 | |
380 | # set default clock type node | |
381 | pct_prop_name = 'packet-context-type' | |
e8f0d548 | 382 | v2_pkt_ctx_ft_fields_node = v2_dst_node[pct_prop_name]['fields'] |
4810b707 | 383 | eht_prop_name = 'event-header-type' |
e8f0d548 PP |
384 | v2_er_header_ft_fields_node = None |
385 | v2_er_header_ft_node = v2_dst_node.get(eht_prop_name) | |
4810b707 | 386 | |
e8f0d548 PP |
387 | if v2_er_header_ft_node is not None: |
388 | v2_er_header_ft_fields_node = v2_er_header_ft_node['fields'] | |
4810b707 PP |
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: | |
e8f0d548 PP |
406 | if def_clk_type_name is None and v2_er_header_ft_fields_node is not None: |
407 | def_clk_type_name = clk_type_name_from_v2_int_ft_node(v2_er_header_ft_fields_node.get('timestamp')) | |
4810b707 PP |
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: | |
e8f0d548 | 418 | v3_dst_node['$default-clock-type-name'] = def_clk_type_name |
4810b707 PP |
419 | |
420 | # set features node | |
e8f0d548 PP |
421 | v3_dst_node['$features'] = v3_features_node_from_v2_ft_nodes(v2_pkt_ctx_ft_fields_node, |
422 | v2_er_header_ft_fields_node) | |
4810b707 PP |
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: | |
e8f0d548 | 446 | v3_dst_node['packet-context-field-type-extra-members'] = pkt_ctx_ft_extra_members |
4810b707 | 447 | |
e8f0d548 PP |
448 | # convert event record common context field type node |
449 | v2_ft_node = v2_dst_node.get('event-context-type') | |
4810b707 PP |
450 | |
451 | if v2_ft_node is not None: | |
e8f0d548 | 452 | v3_dst_node['event-record-common-context-field-type'] = self._conv_ft_node(v2_ft_node) |
4810b707 | 453 | |
e8f0d548 PP |
454 | # convert event record type nodes |
455 | v3_erts_node = collections.OrderedDict() | |
4810b707 | 456 | |
e8f0d548 | 457 | for ert_name, v2_ert_node in v2_dst_node['events'].items(): |
4810b707 | 458 | try: |
e8f0d548 | 459 | v3_erts_node[ert_name] = self._conv_ert_node(v2_ert_node) |
4810b707 | 460 | except _ConfigurationParseError as exc: |
e8f0d548 | 461 | _append_error_ctx(exc, f'Event record type `{ert_name}`') |
4810b707 | 462 | |
e8f0d548 | 463 | v3_dst_node['event-record-types'] = v3_erts_node |
4810b707 | 464 | |
e8f0d548 | 465 | return v3_dst_node |
4810b707 PP |
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') | |
e8f0d548 PP |
480 | v3_dst_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) | |
e8f0d548 | 485 | set_if_exists('data-stream-type-id-field-type', v3_dst_id_ft_node) |
4810b707 PP |
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 | ||
313a0bd9 PP |
492 | # copy `byte-order` property as `native-byte-order` property |
493 | _copy_prop_if_exists(v3_trace_type_node, v2_trace_node, 'byte-order', 'native-byte-order') | |
4810b707 PP |
494 | |
495 | # copy `uuid` property | |
496 | _copy_prop_if_exists(v3_trace_type_node, v2_trace_node, 'uuid') | |
497 | ||
498 | # copy `$log-levels`/`log-levels` property | |
499 | new_prop_name = '$log-level-aliases' | |
500 | _copy_prop_if_exists(v3_trace_type_node, v2_meta_node, 'log-levels', new_prop_name) | |
501 | _copy_prop_if_exists(v3_trace_type_node, v2_meta_node, '$log-levels', new_prop_name) | |
502 | ||
503 | # copy `clocks` property, converting clock type nodes | |
504 | v2_clk_types_node = v2_meta_node.get('clocks') | |
505 | ||
506 | if v2_clk_types_node is not None: | |
507 | v3_clk_types_node = collections.OrderedDict() | |
508 | ||
509 | for name, v2_clk_type_node in v2_clk_types_node.items(): | |
510 | v3_clk_types_node[name] = self._conv_clk_type_node(v2_clk_type_node) | |
511 | ||
512 | v3_trace_type_node['clock-types'] = v3_clk_types_node | |
513 | ||
514 | # set features node | |
515 | v2_pkt_header_ft_node = v2_trace_node.get('packet-header-type') | |
516 | v3_trace_type_node['$features'] = v3_features_node_from_v2_ft_node(v2_pkt_header_ft_node) | |
517 | ||
e8f0d548 PP |
518 | # convert data stream type nodes |
519 | v3_dsts_node = collections.OrderedDict() | |
4810b707 | 520 | |
e8f0d548 | 521 | for dst_name, v2_dst_node in v2_meta_node['streams'].items(): |
4810b707 | 522 | try: |
e8f0d548 | 523 | v3_dsts_node[dst_name] = self._conv_dst_node(v2_dst_node) |
4810b707 | 524 | except _ConfigurationParseError as exc: |
e8f0d548 | 525 | _append_error_ctx(exc, f'Data stream type `{dst_name}`') |
4810b707 | 526 | |
e8f0d548 | 527 | v3_trace_type_node['data-stream-types'] = v3_dsts_node |
4810b707 PP |
528 | |
529 | # If `v2_meta_node` has a `$default-stream` property, find the | |
e8f0d548 PP |
530 | # corresponding v3 data stream type node and set its |
531 | # `$is-default` property to `True`. | |
4810b707 | 532 | prop_name = '$default-stream' |
e8f0d548 | 533 | v2_def_dst_node = v2_meta_node.get(prop_name) |
4810b707 | 534 | |
e8f0d548 | 535 | if v2_def_dst_node is not None: |
4810b707 PP |
536 | found = False |
537 | ||
e8f0d548 PP |
538 | for dst_name, v3_dst_node in v3_dsts_node.items(): |
539 | if dst_name == v2_def_dst_node: | |
540 | v3_dst_node['$is-default'] = True | |
4810b707 PP |
541 | found = True |
542 | break | |
543 | ||
544 | if not found: | |
545 | raise _ConfigurationParseError(f'`{prop_name}` property', | |
e8f0d548 | 546 | f'Data stream type `{v2_def_dst_node}` does not exist') |
4810b707 PP |
547 | |
548 | # set environment node | |
549 | v2_env_node = v2_meta_node.get('env') | |
550 | ||
551 | if v2_env_node is not None: | |
552 | v3_trace_node['environment'] = copy.deepcopy(v2_env_node) | |
553 | ||
554 | # set v3 trace node's type node | |
555 | v3_trace_node['type'] = v3_trace_type_node | |
556 | ||
557 | return v3_trace_node | |
558 | ||
559 | # Transforms the root configuration node into a valid v3 | |
560 | # configuration node. | |
561 | def _transform_config_node(self): | |
562 | # remove the `version` property | |
563 | del self._root_node['version'] | |
564 | ||
565 | # relocate prefix and option nodes | |
566 | prefix_prop_name = 'prefix' | |
567 | v2_prefix_node = self._root_node.get(prefix_prop_name, 'barectf_') | |
568 | _del_prop_if_exists(self._root_node, prefix_prop_name) | |
569 | opt_prop_name = 'options' | |
570 | v2_options_node = self._root_node.get(opt_prop_name) | |
571 | _del_prop_if_exists(self._root_node, opt_prop_name) | |
572 | code_gen_node = collections.OrderedDict() | |
573 | v3_prefixes = config_parse_common._v3_prefixes_from_v2_prefix(v2_prefix_node) | |
574 | v3_prefix_node = collections.OrderedDict([ | |
575 | ('identifier', v3_prefixes.identifier), | |
576 | ('file-name', v3_prefixes.file_name), | |
577 | ]) | |
578 | code_gen_node[prefix_prop_name] = v3_prefix_node | |
579 | ||
580 | if v2_options_node is not None: | |
581 | header_node = collections.OrderedDict() | |
582 | _copy_prop_if_exists(header_node, v2_options_node, 'gen-prefix-def', | |
583 | 'identifier-prefix-definition') | |
584 | _copy_prop_if_exists(header_node, v2_options_node, 'gen-default-stream-def', | |
e8f0d548 | 585 | 'default-data-stream-type-name-definition') |
4810b707 PP |
586 | code_gen_node['header'] = header_node |
587 | ||
588 | self._root_node[opt_prop_name] = collections.OrderedDict({ | |
589 | 'code-generation': code_gen_node, | |
590 | }) | |
591 | ||
592 | # convert the v2 metadata node into a v3 trace node | |
593 | try: | |
594 | self._root_node['trace'] = self._conv_meta_node(self._root_node['metadata']) | |
595 | except _ConfigurationParseError as exc: | |
596 | _append_error_ctx(exc, 'Metadata object') | |
597 | ||
598 | del self._root_node['metadata'] | |
599 | ||
600 | # Expands the field type aliases found in the metadata node. | |
601 | # | |
602 | # This method modifies the metadata node. | |
603 | # | |
604 | # When this method returns: | |
605 | # | |
606 | # * Any field type alias is replaced with its full field type node | |
607 | # equivalent. | |
608 | # | |
609 | # * The `type-aliases` property of metadata node is removed. | |
610 | def _expand_ft_aliases(self): | |
611 | meta_node = self._root_node['metadata'] | |
612 | ft_aliases_node = meta_node['type-aliases'] | |
613 | ||
e8f0d548 PP |
614 | # Expand field type aliases within trace, data stream, and event |
615 | # record types now. | |
4810b707 PP |
616 | try: |
617 | self._resolve_ft_alias_from(ft_aliases_node, meta_node['trace'], 'packet-header-type') | |
618 | except _ConfigurationParseError as exc: | |
619 | _append_error_ctx(exc, 'Trace type') | |
620 | ||
e8f0d548 | 621 | for dst_name, dst_node in meta_node['streams'].items(): |
4810b707 | 622 | try: |
e8f0d548 PP |
623 | self._resolve_ft_alias_from(ft_aliases_node, dst_node, 'packet-context-type') |
624 | self._resolve_ft_alias_from(ft_aliases_node, dst_node, 'event-header-type') | |
625 | self._resolve_ft_alias_from(ft_aliases_node, dst_node, 'event-context-type') | |
4810b707 | 626 | |
e8f0d548 | 627 | for ert_name, ert_node in dst_node['events'].items(): |
4810b707 | 628 | try: |
e8f0d548 PP |
629 | self._resolve_ft_alias_from(ft_aliases_node, ert_node, 'context-type') |
630 | self._resolve_ft_alias_from(ft_aliases_node, ert_node, 'payload-type') | |
4810b707 | 631 | except _ConfigurationParseError as exc: |
e8f0d548 | 632 | _append_error_ctx(exc, f'Event record type `{ert_name}`') |
4810b707 | 633 | except _ConfigurationParseError as exc: |
e8f0d548 | 634 | _append_error_ctx(exc, f'Data stream type `{dst_name}`') |
4810b707 PP |
635 | |
636 | # remove the (now unneeded) `type-aliases` node | |
637 | del meta_node['type-aliases'] | |
638 | ||
639 | # Applies field type inheritance to all field type nodes found in | |
640 | # the metadata node. | |
641 | # | |
642 | # This method modifies the metadata node. | |
643 | # | |
644 | # When this method returns, no field type node has an `$inherit` or | |
645 | # `inherit` property. | |
646 | def _apply_fts_inheritance(self): | |
647 | meta_node = self._root_node['metadata'] | |
648 | self._apply_ft_inheritance(meta_node['trace'], 'packet-header-type') | |
649 | ||
e8f0d548 PP |
650 | for dst_node in meta_node['streams'].values(): |
651 | self._apply_ft_inheritance(dst_node, 'packet-context-type') | |
652 | self._apply_ft_inheritance(dst_node, 'event-header-type') | |
653 | self._apply_ft_inheritance(dst_node, 'event-context-type') | |
4810b707 | 654 | |
e8f0d548 PP |
655 | for ert_node in dst_node['events'].values(): |
656 | self._apply_ft_inheritance(ert_node, 'context-type') | |
657 | self._apply_ft_inheritance(ert_node, 'payload-type') | |
4810b707 PP |
658 | |
659 | # Calls _expand_ft_aliases() and _apply_fts_inheritance() if the | |
660 | # metadata node has a `type-aliases` property. | |
661 | def _expand_fts(self): | |
662 | # Make sure that the current configuration node is valid | |
663 | # considering field types are not expanded yet. | |
664 | self._schema_validator.validate(self._root_node, | |
c3fa1a14 | 665 | 'config/2/config-pre-field-type-expansion') |
4810b707 PP |
666 | |
667 | meta_node = self._root_node['metadata'] | |
668 | ft_aliases_node = meta_node.get('type-aliases') | |
669 | ||
670 | if ft_aliases_node is None: | |
671 | # If there's no `type-aliases` node, then there's no field | |
672 | # type aliases and therefore no possible inheritance. | |
673 | return | |
674 | ||
675 | # first, expand field type aliases | |
676 | self._expand_ft_aliases() | |
677 | ||
678 | # next, apply inheritance to create effective field types | |
679 | self._apply_fts_inheritance() | |
680 | ||
e8f0d548 | 681 | # Processes the inclusions of the event record type node `ert_node`, |
4810b707 | 682 | # returning the effective node. |
e8f0d548 PP |
683 | def _process_ert_node_include(self, ert_node: _MapNode) -> _MapNode: |
684 | # Make sure the event record type node is valid for the | |
685 | # inclusion processing stage. | |
686 | self._schema_validator.validate(ert_node, 'config/2/ert-pre-include') | |
4810b707 PP |
687 | |
688 | # process inclusions | |
e8f0d548 | 689 | return self._process_node_include(ert_node, self._process_ert_node_include) |
4810b707 | 690 | |
e8f0d548 PP |
691 | # Processes the inclusions of the data stream type node `dst_node`, |
692 | # returning the effective node. | |
693 | def _process_dst_node_include(self, dst_node: _MapNode) -> _MapNode: | |
694 | def process_children_include(dst_node): | |
4810b707 PP |
695 | prop_name = 'events' |
696 | ||
e8f0d548 PP |
697 | if prop_name in dst_node: |
698 | erts_node = dst_node[prop_name] | |
4810b707 | 699 | |
e8f0d548 PP |
700 | for key in list(erts_node): |
701 | erts_node[key] = self._process_ert_node_include(erts_node[key]) | |
4810b707 | 702 | |
e8f0d548 | 703 | # Make sure the data stream type node is valid for the inclusion |
4810b707 | 704 | # processing stage. |
e8f0d548 | 705 | self._schema_validator.validate(dst_node, 'config/2/dst-pre-include') |
4810b707 PP |
706 | |
707 | # process inclusions | |
e8f0d548 | 708 | return self._process_node_include(dst_node, self._process_dst_node_include, |
4810b707 PP |
709 | process_children_include) |
710 | ||
711 | # Processes the inclusions of the trace type node `trace_type_node`, | |
712 | # returning the effective node. | |
2d55dc7d | 713 | def _process_trace_type_node_include(self, trace_type_node: _MapNode) -> _MapNode: |
4810b707 PP |
714 | # Make sure the trace type node is valid for the inclusion |
715 | # processing stage. | |
c3fa1a14 | 716 | self._schema_validator.validate(trace_type_node, 'config/2/trace-type-pre-include') |
4810b707 PP |
717 | |
718 | # process inclusions | |
719 | return self._process_node_include(trace_type_node, self._process_trace_type_node_include) | |
720 | ||
721 | # Processes the inclusions of the clock type node `clk_type_node`, | |
722 | # returning the effective node. | |
2d55dc7d | 723 | def _process_clk_type_node_include(self, clk_type_node: _MapNode) -> _MapNode: |
4810b707 PP |
724 | # Make sure the clock type node is valid for the inclusion |
725 | # processing stage. | |
c3fa1a14 | 726 | self._schema_validator.validate(clk_type_node, 'config/2/clock-type-pre-include') |
4810b707 PP |
727 | |
728 | # process inclusions | |
729 | return self._process_node_include(clk_type_node, self._process_clk_type_node_include) | |
730 | ||
731 | # Processes the inclusions of the metadata node `meta_node`, | |
732 | # returning the effective node. | |
2d55dc7d PP |
733 | def _process_meta_node_include(self, meta_node: _MapNode) -> _MapNode: |
734 | def process_children_include(meta_node: _MapNode): | |
4810b707 PP |
735 | prop_name = 'trace' |
736 | ||
737 | if prop_name in meta_node: | |
738 | meta_node[prop_name] = self._process_trace_type_node_include(meta_node[prop_name]) | |
739 | ||
740 | prop_name = 'clocks' | |
741 | ||
742 | if prop_name in meta_node: | |
743 | clk_types_node = meta_node[prop_name] | |
744 | ||
745 | for key in list(clk_types_node): | |
746 | clk_types_node[key] = self._process_clk_type_node_include(clk_types_node[key]) | |
747 | ||
748 | prop_name = 'streams' | |
749 | ||
750 | if prop_name in meta_node: | |
e8f0d548 | 751 | dsts_node = meta_node[prop_name] |
4810b707 | 752 | |
e8f0d548 PP |
753 | for key in list(dsts_node): |
754 | dsts_node[key] = self._process_dst_node_include(dsts_node[key]) | |
4810b707 PP |
755 | |
756 | # Make sure the metadata node is valid for the inclusion | |
757 | # processing stage. | |
c3fa1a14 | 758 | self._schema_validator.validate(meta_node, 'config/2/metadata-pre-include') |
4810b707 PP |
759 | |
760 | # process inclusions | |
761 | return self._process_node_include(meta_node, self._process_meta_node_include, | |
762 | process_children_include) | |
763 | ||
764 | # Processes the inclusions of the configuration node, modifying it | |
765 | # during the process. | |
766 | def _process_config_includes(self): | |
767 | # Process inclusions in this order: | |
768 | # | |
e8f0d548 PP |
769 | # 1. Clock type node, event record type nodes, and trace type |
770 | # nodes (the order between those is not important). | |
4810b707 | 771 | # |
e8f0d548 | 772 | # 2. Data stream type nodes. |
4810b707 PP |
773 | # |
774 | # 3. Metadata node. | |
775 | # | |
776 | # This is because: | |
777 | # | |
778 | # * A metadata node can include clock type nodes, a trace type | |
e8f0d548 PP |
779 | # node, data stream type nodes, and event record type nodes |
780 | # (indirectly). | |
4810b707 | 781 | # |
e8f0d548 | 782 | # * A data stream type node can include event record type nodes. |
4810b707 PP |
783 | # |
784 | # First, make sure the configuration node itself is valid for | |
785 | # the inclusion processing stage. | |
786 | self._schema_validator.validate(self._root_node, | |
c3fa1a14 | 787 | 'config/2/config-pre-include') |
4810b707 PP |
788 | |
789 | # Process metadata node inclusions. | |
790 | # | |
791 | # self._process_meta_node_include() returns a new (or the same) | |
792 | # metadata node without any `$include` property in it, | |
793 | # recursively. | |
794 | prop_name = 'metadata' | |
795 | self._root_node[prop_name] = self._process_meta_node_include(self._root_node[prop_name]) | |
796 | ||
797 | def _parse(self): | |
798 | # Make sure the configuration node is minimally valid, that is, | |
799 | # it contains a valid `version` property. | |
800 | # | |
801 | # This step does not validate the whole configuration node yet | |
802 | # because we don't have an effective configuration node; we | |
803 | # still need to: | |
804 | # | |
805 | # * Process inclusions. | |
806 | # * Expand field types (aliases and inheritance). | |
c3fa1a14 | 807 | self._schema_validator.validate(self._root_node, 'config/2/config-min') |
4810b707 PP |
808 | |
809 | # process configuration node inclusions | |
810 | self._process_config_includes() | |
811 | ||
812 | # Expand field type nodes. | |
813 | # | |
814 | # This process: | |
815 | # | |
816 | # 1. Replaces field type aliases with "effective" field type | |
817 | # nodes, recursively. | |
818 | # | |
819 | # After this step, the `type-aliases` property of the | |
820 | # metadata node is gone. | |
821 | # | |
822 | # 2. Applies inheritance, following the `$inherit`/`inherit` | |
823 | # properties. | |
824 | # | |
825 | # After this step, field type nodes do not contain `$inherit` | |
826 | # or `inherit` properties. | |
827 | # | |
828 | # This is done blindly, in that the process _doesn't_ validate | |
829 | # field type nodes at this point. | |
830 | # | |
831 | # The reason we must do this here for a barectf 2 configuration, | |
832 | # considering that barectf 3 also supports field type node | |
833 | # aliases and inheritance, is that we need to find specific | |
834 | # packet header and packet context field type member nodes (for | |
835 | # example, `stream_id`, `packet_size`, or `timestamp_end`) to | |
836 | # set the `$features` properties of barectf 3 trace type and | |
e8f0d548 | 837 | # data stream type nodes. Those field type nodes can be aliases, |
4810b707 PP |
838 | # contain aliases, or inherit from other nodes. |
839 | self._expand_fts() | |
840 | ||
841 | # Validate the whole, (almost) effective configuration node. | |
842 | # | |
843 | # It's almost effective because the `log-level` property of | |
e8f0d548 PP |
844 | # event record type nodes can be log level aliases. Log level |
845 | # aliases are also a feature of a barectf 3 configuration node, | |
4810b707 | 846 | # therefore this is compatible. |
c3fa1a14 | 847 | self._schema_validator.validate(self._root_node, 'config/2/config') |
4810b707 PP |
848 | |
849 | # Transform the current configuration node into a valid v3 | |
850 | # configuration node. | |
851 | self._transform_config_node() | |
852 | ||
853 | @property | |
2d55dc7d PP |
854 | def config_node(self) -> config_parse_common._ConfigNodeV3: |
855 | return config_parse_common._ConfigNodeV3(typing.cast(_MapNode, self._root_node)) |