1 # The MIT License (MIT)
3 # Copyright (c) 2015-2020 Philippe Proulx <pproulx@efficios.com>
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:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
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.
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
31 def _del_prop_if_exists(node
, prop_name
):
36 def _rename_prop(node
, old_prop_name
, new_prop_name
):
37 if old_prop_name
in node
:
38 node
[new_prop_name
] = node
[old_prop_name
]
39 del node
[old_prop_name
]
42 def _copy_prop_if_exists(dst_node
, src_node
, src_prop_name
, dst_prop_name
=None):
43 if dst_prop_name
is None:
44 dst_prop_name
= src_prop_name
46 if src_prop_name
in src_node
:
47 dst_node
[dst_prop_name
] = copy
.deepcopy(src_node
[src_prop_name
])
50 # A barectf 2 YAML configuration parser.
52 # The only purpose of such a parser is to transform the passed root
53 # configuration node so that it's a valid barectf 3 configuration node.
55 # The parser's `config_node` property is the equivalent barectf 3
58 # See the comments of _parse() for more implementation details about the
59 # parsing stages and general strategy.
60 class _Parser(config_parse_common
._Parser
):
61 # Builds a barectf 2 YAML configuration parser and parses the root
62 # configuration node `node` (already loaded from `path`).
63 def __init__(self
, path
, node
, with_pkg_include_dir
, include_dirs
, ignore_include_not_found
):
64 super().__init
__(path
, node
, with_pkg_include_dir
, include_dirs
, ignore_include_not_found
, 2)
65 self
._ft
_cls
_name
_to
_conv
_method
= {
66 'int': self
._conv
_int
_ft
_node
,
67 'integer': self
._conv
_int
_ft
_node
,
68 'enum': self
._conv
_enum
_ft
_node
,
69 'enumeration': self
._conv
_enum
_ft
_node
,
70 'flt': self
._conv
_real
_ft
_node
,
71 'float': self
._conv
_real
_ft
_node
,
72 'floating-point': self
._conv
_real
_ft
_node
,
73 'str': self
._conv
_string
_ft
_node
,
74 'string': self
._conv
_string
_ft
_node
,
75 'array': self
._conv
_static
_array
_ft
_node
,
76 'struct': self
._conv
_struct
_ft
_node
,
77 'structure': self
._conv
_struct
_ft
_node
,
81 # Converts a v2 field type node to a v3 field type node and returns
83 def _conv_ft_node(self
, v2_ft_node
):
84 assert 'class' in v2_ft_node
85 cls
= v2_ft_node
['class']
86 assert cls
in self
._ft
_cls
_name
_to
_conv
_method
87 return self
._ft
_cls
_name
_to
_conv
_method
[cls
](v2_ft_node
)
89 def _conv_ft_node_if_exists(self
, v2_parent_node
, key
):
90 if v2_parent_node
is None:
93 if key
not in v2_parent_node
:
96 return self
._conv
_ft
_node
(v2_parent_node
[key
])
98 # Converts a v2 integer field type node to a v3 integer field type
99 # node and returns it.
100 def _conv_int_ft_node(self
, v2_ft_node
):
101 # copy v2 integer field type node
102 v3_ft_node
= copy
.deepcopy(v2_ft_node
)
104 # signedness depends on the class, not a property
107 is_signed_node
= v3_ft_node
.get(prop_name
)
109 if is_signed_node
is True:
112 v3_ft_node
['class'] = cls_name
113 _del_prop_if_exists(v3_ft_node
, prop_name
)
115 # rename `align` property to `alignment`
116 _rename_prop(v3_ft_node
, 'align', 'alignment')
118 # rename `base` property to `preferred-display-base`
119 _rename_prop(v3_ft_node
, 'base', 'preferred-display-base')
121 # remove `encoding` property
122 _del_prop_if_exists(v3_ft_node
, 'encoding')
124 # remove `property-mappings` property
125 _del_prop_if_exists(v3_ft_node
, 'property-mappings')
129 # Converts a v2 enumeration field type node to a v3 enumeration
130 # field type node and returns it.
131 def _conv_enum_ft_node(self
, v2_ft_node
):
132 # An enumeration field type _is_ an integer field type, so use a
133 # copy of the converted v2 value field type node.
134 v3_ft_node
= copy
.deepcopy(self
._conv
_ft
_node
(v2_ft_node
['value-type']))
136 # transform class name accordingly
140 if v3_ft_node
[prop_name
] == 'sint':
143 v3_ft_node
[prop_name
] = cls_name
145 # convert members to mappings
146 prop_name
= 'members'
147 members_node
= v2_ft_node
.get(prop_name
)
149 if members_node
is not None:
150 mappings_node
= collections
.OrderedDict()
153 for member_node
in members_node
:
154 if type(member_node
) is str:
159 assert type(member_node
) is collections
.OrderedDict
160 label
= member_node
['label']
161 v2_value_node
= member_node
['value']
163 if type(v2_value_node
) is int:
164 cur
= v2_value_node
+ 1
165 v3_value_node
= v2_value_node
167 assert type(v2_value_node
) is list
168 assert len(v2_value_node
) == 2
169 v3_value_node
= list(v2_value_node
)
170 cur
= v2_value_node
[1] + 1
172 if label
not in mappings_node
:
173 mappings_node
[label
] = []
175 mappings_node
[label
].append(v3_value_node
)
177 v3_ft_node
['mappings'] = mappings_node
181 # Converts a v2 real field type node to a v3 real field type node
183 def _conv_real_ft_node(self
, v2_ft_node
):
184 # copy v2 real field type node
185 v3_ft_node
= copy
.deepcopy(v2_ft_node
)
187 # set class to `real`
188 v3_ft_node
['class'] = 'real'
190 # rename `align` property to `alignment`
191 _rename_prop(v3_ft_node
, 'align', 'alignment')
193 # set `size` property to a single integer (total size, in bits)
195 v3_ft_node
[prop_name
] = v3_ft_node
[prop_name
]['exp'] + v3_ft_node
[prop_name
]['mant']
199 # Converts a v2 string field type node to a v3 string field type
200 # node and returns it.
201 def _conv_string_ft_node(self
, v2_ft_node
):
202 # copy v2 string field type node
203 v3_ft_node
= copy
.deepcopy(v2_ft_node
)
205 # remove `encoding` property
206 _del_prop_if_exists(v3_ft_node
, 'encoding')
210 # Converts a v2 array field type node to a v3 (static) array field
211 # type node and returns it.
212 def _conv_static_array_ft_node(self
, v2_ft_node
):
213 # class renamed to `static-array`
214 v3_ft_node
= collections
.OrderedDict({'class': 'static-array'})
216 # copy `length` property
217 _copy_prop_if_exists(v3_ft_node
, v2_ft_node
, 'length')
219 # convert element field type
220 v3_ft_node
['element-field-type'] = self
._conv
_ft
_node
(v2_ft_node
['element-type'])
224 # Converts a v2 structure field type node to a v3 structure field
225 # type node and returns it.
226 def _conv_struct_ft_node(self
, v2_ft_node
):
227 # Create fresh v3 structure field type node, reusing the class
229 v3_ft_node
= collections
.OrderedDict({'class': v2_ft_node
['class']})
231 # rename `min-align` property to `minimum-alignment`
232 _copy_prop_if_exists(v3_ft_node
, v2_ft_node
, 'min-align', 'minimum-alignment')
234 # convert fields to members
237 if prop_name
in v2_ft_node
:
240 for member_name
, v2_member_ft_node
in v2_ft_node
[prop_name
].items():
241 members_node
.append(collections
.OrderedDict({
242 member_name
: collections
.OrderedDict({
243 'field-type': self
._conv
_ft
_node
(v2_member_ft_node
)
247 v3_ft_node
['members'] = members_node
251 # Converts a v2 clock type node to a v3 clock type node and returns
253 def _conv_clk_type_node(self
, v2_clk_type_node
):
254 # copy v2 clock type node
255 v3_clk_type_node
= copy
.deepcopy(v2_clk_type_node
)
257 # rename `freq` property to `frequency`
258 _rename_prop(v3_clk_type_node
, 'freq', 'frequency')
260 # rename `error-cycles` property to `precision`
261 _rename_prop(v3_clk_type_node
, 'error-cycles', 'precision')
263 # rename `absolute` property to `origin-is-unix-epoch`
264 _rename_prop(v3_clk_type_node
, 'absolute', 'origin-is-unix-epoch')
266 # rename `$return-ctype`/`return-ctype` property to `$c-type`
267 new_prop_name
= '$c-type'
268 _rename_prop(v3_clk_type_node
, 'return-ctype', new_prop_name
)
269 _rename_prop(v3_clk_type_node
, '$return-ctype', new_prop_name
)
271 return v3_clk_type_node
273 # Converts a v2 event type node to a v3 event type node and returns
275 def _conv_ev_type_node(self
, v2_ev_type_node
):
276 # create empty v3 event type node
277 v3_ev_type_node
= collections
.OrderedDict()
279 # copy `log-level` property
280 _copy_prop_if_exists(v3_ev_type_node
, v2_ev_type_node
, 'log-level')
282 # convert specific context field type node
283 v2_ft_node
= v2_ev_type_node
.get('context-type')
285 if v2_ft_node
is not None:
286 v3_ev_type_node
['specific-context-field-type'] = self
._conv
_ft
_node
(v2_ft_node
)
288 # convert payload field type node
289 v2_ft_node
= v2_ev_type_node
.get('payload-type')
291 if v2_ft_node
is not None:
292 v3_ev_type_node
['payload-field-type'] = self
._conv
_ft
_node
(v2_ft_node
)
294 return v3_ev_type_node
297 def _set_v3_feature_ft_if_exists(v3_features_node
, key
, node
):
303 v3_features_node
[key
] = val
305 # Converts a v2 stream type node to a v3 stream type node and
307 def _conv_stream_type_node(self
, v2_stream_type_node
):
308 # This function creates a v3 stream type features node from the
309 # packet context and event header field type nodes of a
310 # v2 stream type node.
311 def v3_features_node_from_v2_ft_nodes(v2_pkt_ctx_ft_fields_node
,
312 v2_ev_header_ft_fields_node
):
313 if v2_ev_header_ft_fields_node
is None:
314 v2_ev_header_ft_fields_node
= collections
.OrderedDict()
316 v3_pkt_total_size_ft_node
= self
._conv
_ft
_node
(v2_pkt_ctx_ft_fields_node
['packet_size'])
317 v3_pkt_content_size_ft_node
= self
._conv
_ft
_node
(v2_pkt_ctx_ft_fields_node
['content_size'])
318 v3_pkt_beg_time_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_ctx_ft_fields_node
,
320 v3_pkt_end_time_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_ctx_ft_fields_node
,
322 v3_pkt_disc_ev_counter_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_ctx_ft_fields_node
,
324 v3_ev_type_id_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_ev_header_ft_fields_node
, 'id')
325 v3_ev_time_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_ev_header_ft_fields_node
,
327 v3_features_node
= collections
.OrderedDict()
328 v3_pkt_node
= collections
.OrderedDict()
329 v3_ev_node
= collections
.OrderedDict()
330 v3_pkt_node
['total-size-field-type'] = v3_pkt_total_size_ft_node
331 v3_pkt_node
['content-size-field-type'] = v3_pkt_content_size_ft_node
332 self
._set
_v
3_feature
_ft
_if
_exists
(v3_pkt_node
, 'beginning-time-field-type',
333 v3_pkt_beg_time_ft_node
)
334 self
._set
_v
3_feature
_ft
_if
_exists
(v3_pkt_node
, 'end-time-field-type',
335 v3_pkt_end_time_ft_node
)
336 self
._set
_v
3_feature
_ft
_if
_exists
(v3_pkt_node
, 'discarded-events-counter-field-type',
337 v3_pkt_disc_ev_counter_ft_node
)
338 self
._set
_v
3_feature
_ft
_if
_exists
(v3_ev_node
, 'type-id-field-type',
339 v3_ev_type_id_ft_node
)
340 self
._set
_v
3_feature
_ft
_if
_exists
(v3_ev_node
, 'time-field-type', v3_ev_time_ft_node
)
341 v3_features_node
['packet'] = v3_pkt_node
342 v3_features_node
['event'] = v3_ev_node
343 return v3_features_node
345 def clk_type_name_from_v2_int_ft_node(v2_int_ft_node
):
346 if v2_int_ft_node
is None:
349 assert v2_int_ft_node
['class'] in ('int', 'integer')
350 prop_mappings_node
= v2_int_ft_node
.get('property-mappings')
352 if prop_mappings_node
is not None and len(prop_mappings_node
) > 0:
353 return prop_mappings_node
[0]['name']
355 # create empty v3 stream type node
356 v3_stream_type_node
= collections
.OrderedDict()
358 # rename `$default` property to `$is-default`
359 _copy_prop_if_exists(v3_stream_type_node
, v2_stream_type_node
, '$default', '$is-default')
361 # set default clock type node
362 pct_prop_name
= 'packet-context-type'
363 v2_pkt_ctx_ft_fields_node
= v2_stream_type_node
[pct_prop_name
]['fields']
364 eht_prop_name
= 'event-header-type'
365 v2_ev_header_ft_fields_node
= None
366 v2_ev_header_ft_node
= v2_stream_type_node
.get(eht_prop_name
)
368 if v2_ev_header_ft_node
is not None:
369 v2_ev_header_ft_fields_node
= v2_ev_header_ft_node
['fields']
371 def_clk_type_name
= None
374 ts_begin_prop_name
= 'timestamp_begin'
375 ts_begin_clk_type_name
= clk_type_name_from_v2_int_ft_node(v2_pkt_ctx_ft_fields_node
.get(ts_begin_prop_name
))
376 ts_end_prop_name
= 'timestamp_end'
377 ts_end_clk_type_name
= clk_type_name_from_v2_int_ft_node(v2_pkt_ctx_ft_fields_node
.get(ts_end_prop_name
))
379 if ts_begin_clk_type_name
is not None and ts_end_clk_type_name
is not None:
380 if ts_begin_clk_type_name
!= ts_end_clk_type_name
:
381 raise _ConfigurationParseError(f
'`{ts_begin_prop_name}`/`{ts_end_prop_name}` properties',
382 'Field types are not mapped to the same clock type')
383 except _ConfigurationParseError
as exc
:
384 _append_error_ctx(exc
, f
'`{pct_prop_name}` property')
387 if def_clk_type_name
is None and v2_ev_header_ft_fields_node
is not None:
388 def_clk_type_name
= clk_type_name_from_v2_int_ft_node(v2_ev_header_ft_fields_node
.get('timestamp'))
390 if def_clk_type_name
is None and ts_begin_clk_type_name
is not None:
391 def_clk_type_name
= ts_begin_clk_type_name
393 if def_clk_type_name
is None and ts_end_clk_type_name
is not None:
394 def_clk_type_name
= ts_end_clk_type_name
395 except _ConfigurationParseError
as exc
:
396 _append_error_ctx(exc
, f
'`{eht_prop_name}` property')
398 if def_clk_type_name
is not None:
399 v3_stream_type_node
['$default-clock-type-name'] = def_clk_type_name
402 v3_stream_type_node
['$features'] = v3_features_node_from_v2_ft_nodes(v2_pkt_ctx_ft_fields_node
,
403 v2_ev_header_ft_fields_node
)
405 # set extra packet context field type members node
406 pkt_ctx_ft_extra_members
= []
416 for member_name
, v2_ft_node
in v2_pkt_ctx_ft_fields_node
.items():
417 if member_name
in ctf_member_names
:
420 pkt_ctx_ft_extra_members
.append(collections
.OrderedDict({
421 member_name
: collections
.OrderedDict({
422 'field-type': self
._conv
_ft
_node
(v2_ft_node
)
426 if len(pkt_ctx_ft_extra_members
) > 0:
427 v3_stream_type_node
['packet-context-field-type-extra-members'] = pkt_ctx_ft_extra_members
429 # convert event common context field type node
430 v2_ft_node
= v2_stream_type_node
.get('event-context-type')
432 if v2_ft_node
is not None:
433 v3_stream_type_node
['event-common-context-field-type'] = self
._conv
_ft
_node
(v2_ft_node
)
435 # convert event type nodes
436 v3_event_types_node
= collections
.OrderedDict()
438 for ev_type_name
, v2_ev_type_node
in v2_stream_type_node
['events'].items():
440 v3_event_types_node
[ev_type_name
] = self
._conv
_ev
_type
_node
(v2_ev_type_node
)
441 except _ConfigurationParseError
as exc
:
442 _append_error_ctx(exc
, f
'Event type `{ev_type_name}`')
444 v3_stream_type_node
['event-types'] = v3_event_types_node
446 return v3_stream_type_node
448 # Converts a v2 metadata node to a v3 trace node and returns it.
449 def _conv_meta_node(self
, v2_meta_node
):
450 def v3_features_node_from_v2_ft_node(v2_pkt_header_ft_node
):
451 def set_if_exists(key
, node
):
452 return self
._set
_v
3_feature
_ft
_if
_exists
(v3_features_node
, key
, node
)
454 v2_pkt_header_ft_fields_node
= collections
.OrderedDict()
456 if v2_pkt_header_ft_node
is not None:
457 v2_pkt_header_ft_fields_node
= v2_pkt_header_ft_node
['fields']
459 v3_magic_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_header_ft_fields_node
, 'magic')
460 v3_uuid_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_header_ft_fields_node
, 'uuid')
461 v3_stream_type_id_ft_node
= self
._conv
_ft
_node
_if
_exists
(v2_pkt_header_ft_fields_node
,
463 v3_features_node
= collections
.OrderedDict()
464 set_if_exists('magic-field-type', v3_magic_ft_node
)
465 set_if_exists('uuid-field-type', v3_uuid_ft_node
)
466 set_if_exists('stream-type-id-field-type', v3_stream_type_id_ft_node
)
467 return v3_features_node
469 v3_trace_node
= collections
.OrderedDict()
470 v3_trace_type_node
= collections
.OrderedDict()
471 v2_trace_node
= v2_meta_node
['trace']
473 # rename `byte-order` property to `$default-byte-order`
474 _copy_prop_if_exists(v3_trace_type_node
, v2_trace_node
, 'byte-order', '$default-byte-order')
476 # copy `uuid` property
477 _copy_prop_if_exists(v3_trace_type_node
, v2_trace_node
, 'uuid')
479 # copy `$log-levels`/`log-levels` property
480 new_prop_name
= '$log-level-aliases'
481 _copy_prop_if_exists(v3_trace_type_node
, v2_meta_node
, 'log-levels', new_prop_name
)
482 _copy_prop_if_exists(v3_trace_type_node
, v2_meta_node
, '$log-levels', new_prop_name
)
484 # copy `clocks` property, converting clock type nodes
485 v2_clk_types_node
= v2_meta_node
.get('clocks')
487 if v2_clk_types_node
is not None:
488 v3_clk_types_node
= collections
.OrderedDict()
490 for name
, v2_clk_type_node
in v2_clk_types_node
.items():
491 v3_clk_types_node
[name
] = self
._conv
_clk
_type
_node
(v2_clk_type_node
)
493 v3_trace_type_node
['clock-types'] = v3_clk_types_node
496 v2_pkt_header_ft_node
= v2_trace_node
.get('packet-header-type')
497 v3_trace_type_node
['$features'] = v3_features_node_from_v2_ft_node(v2_pkt_header_ft_node
)
499 # convert stream type nodes
500 v3_stream_types_node
= collections
.OrderedDict()
502 for stream_type_name
, v2_stream_type_node
in v2_meta_node
['streams'].items():
504 v3_stream_types_node
[stream_type_name
] = self
._conv
_stream
_type
_node
(v2_stream_type_node
)
505 except _ConfigurationParseError
as exc
:
506 _append_error_ctx(exc
, f
'Stream type `{stream_type_name}`')
508 v3_trace_type_node
['stream-types'] = v3_stream_types_node
510 # If `v2_meta_node` has a `$default-stream` property, find the
511 # corresponding v3 stream type node and set its `$is-default`
512 # property to `True`.
513 prop_name
= '$default-stream'
514 v2_def_stream_type_node
= v2_meta_node
.get(prop_name
)
516 if v2_def_stream_type_node
is not None:
519 for stream_type_name
, v3_stream_type_node
in v3_stream_types_node
.items():
520 if stream_type_name
== v2_def_stream_type_node
:
521 v3_stream_type_node
['$is-default'] = True
526 raise _ConfigurationParseError(f
'`{prop_name}` property',
527 f
'Stream type `{v2_def_stream_type_node}` does not exist')
529 # set environment node
530 v2_env_node
= v2_meta_node
.get('env')
532 if v2_env_node
is not None:
533 v3_trace_node
['environment'] = copy
.deepcopy(v2_env_node
)
535 # set v3 trace node's type node
536 v3_trace_node
['type'] = v3_trace_type_node
540 # Transforms the root configuration node into a valid v3
541 # configuration node.
542 def _transform_config_node(self
):
543 # remove the `version` property
544 del self
._root
_node
['version']
546 # relocate prefix and option nodes
547 prefix_prop_name
= 'prefix'
548 v2_prefix_node
= self
._root
_node
.get(prefix_prop_name
, 'barectf_')
549 _del_prop_if_exists(self
._root
_node
, prefix_prop_name
)
550 opt_prop_name
= 'options'
551 v2_options_node
= self
._root
_node
.get(opt_prop_name
)
552 _del_prop_if_exists(self
._root
_node
, opt_prop_name
)
553 code_gen_node
= collections
.OrderedDict()
554 v3_prefixes
= config_parse_common
._v
3_prefixes
_from
_v
2_prefix
(v2_prefix_node
)
555 v3_prefix_node
= collections
.OrderedDict([
556 ('identifier', v3_prefixes
.identifier
),
557 ('file-name', v3_prefixes
.file_name
),
559 code_gen_node
[prefix_prop_name
] = v3_prefix_node
561 if v2_options_node
is not None:
562 header_node
= collections
.OrderedDict()
563 _copy_prop_if_exists(header_node
, v2_options_node
, 'gen-prefix-def',
564 'identifier-prefix-definition')
565 _copy_prop_if_exists(header_node
, v2_options_node
, 'gen-default-stream-def',
566 'default-stream-type-name-definition')
567 code_gen_node
['header'] = header_node
569 self
._root
_node
[opt_prop_name
] = collections
.OrderedDict({
570 'code-generation': code_gen_node
,
573 # convert the v2 metadata node into a v3 trace node
575 self
._root
_node
['trace'] = self
._conv
_meta
_node
(self
._root
_node
['metadata'])
576 except _ConfigurationParseError
as exc
:
577 _append_error_ctx(exc
, 'Metadata object')
579 del self
._root
_node
['metadata']
581 # Expands the field type aliases found in the metadata node.
583 # This method modifies the metadata node.
585 # When this method returns:
587 # * Any field type alias is replaced with its full field type node
590 # * The `type-aliases` property of metadata node is removed.
591 def _expand_ft_aliases(self
):
592 meta_node
= self
._root
_node
['metadata']
593 ft_aliases_node
= meta_node
['type-aliases']
595 # Expand field type aliases within trace, stream, and event
598 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, meta_node
['trace'], 'packet-header-type')
599 except _ConfigurationParseError
as exc
:
600 _append_error_ctx(exc
, 'Trace type')
602 for stream_type_name
, stream_type_node
in meta_node
['streams'].items():
604 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, stream_type_node
,
605 'packet-context-type')
606 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, stream_type_node
, 'event-header-type')
607 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, stream_type_node
,
608 'event-context-type')
610 for ev_type_name
, ev_type_node
in stream_type_node
['events'].items():
612 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, ev_type_node
, 'context-type')
613 self
._resolve
_ft
_alias
_from
(ft_aliases_node
, ev_type_node
, 'payload-type')
614 except _ConfigurationParseError
as exc
:
615 _append_error_ctx(exc
, f
'Event type `{ev_type_name}`')
616 except _ConfigurationParseError
as exc
:
617 _append_error_ctx(exc
, f
'Stream type `{stream_type_name}`')
619 # remove the (now unneeded) `type-aliases` node
620 del meta_node
['type-aliases']
622 # Applies field type inheritance to all field type nodes found in
625 # This method modifies the metadata node.
627 # When this method returns, no field type node has an `$inherit` or
628 # `inherit` property.
629 def _apply_fts_inheritance(self
):
630 meta_node
= self
._root
_node
['metadata']
631 self
._apply
_ft
_inheritance
(meta_node
['trace'], 'packet-header-type')
633 for stream_type_node
in meta_node
['streams'].values():
634 self
._apply
_ft
_inheritance
(stream_type_node
, 'packet-context-type')
635 self
._apply
_ft
_inheritance
(stream_type_node
, 'event-header-type')
636 self
._apply
_ft
_inheritance
(stream_type_node
, 'event-context-type')
638 for ev_type_node
in stream_type_node
['events'].values():
639 self
._apply
_ft
_inheritance
(ev_type_node
, 'context-type')
640 self
._apply
_ft
_inheritance
(ev_type_node
, 'payload-type')
642 # Calls _expand_ft_aliases() and _apply_fts_inheritance() if the
643 # metadata node has a `type-aliases` property.
644 def _expand_fts(self
):
645 # Make sure that the current configuration node is valid
646 # considering field types are not expanded yet.
647 self
._schema
_validator
.validate(self
._root
_node
,
648 '2/config/config-pre-field-type-expansion')
650 meta_node
= self
._root
_node
['metadata']
651 ft_aliases_node
= meta_node
.get('type-aliases')
653 if ft_aliases_node
is None:
654 # If there's no `type-aliases` node, then there's no field
655 # type aliases and therefore no possible inheritance.
658 # first, expand field type aliases
659 self
._expand
_ft
_aliases
()
661 # next, apply inheritance to create effective field types
662 self
._apply
_fts
_inheritance
()
664 # Processes the inclusions of the event type node `ev_type_node`,
665 # returning the effective node.
666 def _process_ev_type_node_include(self
, ev_type_node
):
667 # Make sure the event type node is valid for the inclusion
669 self
._schema
_validator
.validate(ev_type_node
, '2/config/event-type-pre-include')
672 return self
._process
_node
_include
(ev_type_node
, self
._process
_ev
_type
_node
_include
)
674 # Processes the inclusions of the stream type node
675 # `stream_type_node`, returning the effective node.
676 def _process_stream_type_node_include(self
, stream_type_node
):
677 def process_children_include(stream_type_node
):
680 if prop_name
in stream_type_node
:
681 ev_types_node
= stream_type_node
[prop_name
]
683 for key
in list(ev_types_node
):
684 ev_types_node
[key
] = self
._process
_ev
_type
_node
_include
(ev_types_node
[key
])
686 # Make sure the stream type node is valid for the inclusion
688 self
._schema
_validator
.validate(stream_type_node
, '2/config/stream-type-pre-include')
691 return self
._process
_node
_include
(stream_type_node
, self
._process
_stream
_type
_node
_include
,
692 process_children_include
)
694 # Processes the inclusions of the trace type node `trace_type_node`,
695 # returning the effective node.
696 def _process_trace_type_node_include(self
, trace_type_node
):
697 # Make sure the trace type node is valid for the inclusion
699 self
._schema
_validator
.validate(trace_type_node
, '2/config/trace-type-pre-include')
702 return self
._process
_node
_include
(trace_type_node
, self
._process
_trace
_type
_node
_include
)
704 # Processes the inclusions of the clock type node `clk_type_node`,
705 # returning the effective node.
706 def _process_clk_type_node_include(self
, clk_type_node
):
707 # Make sure the clock type node is valid for the inclusion
709 self
._schema
_validator
.validate(clk_type_node
, '2/config/clock-type-pre-include')
712 return self
._process
_node
_include
(clk_type_node
, self
._process
_clk
_type
_node
_include
)
714 # Processes the inclusions of the metadata node `meta_node`,
715 # returning the effective node.
716 def _process_meta_node_include(self
, meta_node
):
717 def process_children_include(meta_node
):
720 if prop_name
in meta_node
:
721 meta_node
[prop_name
] = self
._process
_trace
_type
_node
_include
(meta_node
[prop_name
])
725 if prop_name
in meta_node
:
726 clk_types_node
= meta_node
[prop_name
]
728 for key
in list(clk_types_node
):
729 clk_types_node
[key
] = self
._process
_clk
_type
_node
_include
(clk_types_node
[key
])
731 prop_name
= 'streams'
733 if prop_name
in meta_node
:
734 stream_types_node
= meta_node
[prop_name
]
736 for key
in list(stream_types_node
):
737 stream_types_node
[key
] = self
._process
_stream
_type
_node
_include
(stream_types_node
[key
])
739 # Make sure the metadata node is valid for the inclusion
741 self
._schema
_validator
.validate(meta_node
, '2/config/metadata-pre-include')
744 return self
._process
_node
_include
(meta_node
, self
._process
_meta
_node
_include
,
745 process_children_include
)
747 # Processes the inclusions of the configuration node, modifying it
748 # during the process.
749 def _process_config_includes(self
):
750 # Process inclusions in this order:
752 # 1. Clock type node, event type nodes, and trace type nodes
753 # (the order between those is not important).
755 # 2. Stream type nodes.
761 # * A metadata node can include clock type nodes, a trace type
762 # node, stream type nodes, and event type nodes (indirectly).
764 # * A stream type node can include event type nodes.
766 # First, make sure the configuration node itself is valid for
767 # the inclusion processing stage.
768 self
._schema
_validator
.validate(self
._root
_node
,
769 '2/config/config-pre-include')
771 # Process metadata node inclusions.
773 # self._process_meta_node_include() returns a new (or the same)
774 # metadata node without any `$include` property in it,
776 prop_name
= 'metadata'
777 self
._root
_node
[prop_name
] = self
._process
_meta
_node
_include
(self
._root
_node
[prop_name
])
780 # Make sure the configuration node is minimally valid, that is,
781 # it contains a valid `version` property.
783 # This step does not validate the whole configuration node yet
784 # because we don't have an effective configuration node; we
787 # * Process inclusions.
788 # * Expand field types (aliases and inheritance).
789 self
._schema
_validator
.validate(self
._root
_node
, '2/config/config-min')
791 # process configuration node inclusions
792 self
._process
_config
_includes
()
794 # Expand field type nodes.
798 # 1. Replaces field type aliases with "effective" field type
799 # nodes, recursively.
801 # After this step, the `type-aliases` property of the
802 # metadata node is gone.
804 # 2. Applies inheritance, following the `$inherit`/`inherit`
807 # After this step, field type nodes do not contain `$inherit`
808 # or `inherit` properties.
810 # This is done blindly, in that the process _doesn't_ validate
811 # field type nodes at this point.
813 # The reason we must do this here for a barectf 2 configuration,
814 # considering that barectf 3 also supports field type node
815 # aliases and inheritance, is that we need to find specific
816 # packet header and packet context field type member nodes (for
817 # example, `stream_id`, `packet_size`, or `timestamp_end`) to
818 # set the `$features` properties of barectf 3 trace type and
819 # stream type nodes. Those field type nodes can be aliases,
820 # contain aliases, or inherit from other nodes.
823 # Validate the whole, (almost) effective configuration node.
825 # It's almost effective because the `log-level` property of
826 # event type nodes can be log level aliases. Log level aliases
827 # are also a feature of a barectf 3 configuration node,
828 # therefore this is compatible.
829 self
._schema
_validator
.validate(self
._root
_node
, '2/config/config')
831 # Transform the current configuration node into a valid v3
832 # configuration node.
833 self
._transform
_config
_node
()
836 def config_node(self
):
837 return config_parse_common
._ConfigNodeV
3(self
._root
_node
)