|
| 1 | +import logging |
| 2 | + |
| 3 | +from lxml import etree |
| 4 | + |
| 5 | +from qc_baselib import Configuration, Result, StatusType, IssueSeverity |
| 6 | + |
| 7 | +from qc_ositrace import constants |
| 8 | + |
| 9 | +from qc_ositrace.checks.osirules import ( |
| 10 | + osirules_constants, |
| 11 | +) |
| 12 | + |
| 13 | +from osi3trace.osi_trace import OSITrace |
| 14 | + |
| 15 | +from importlib import resources as impresources |
| 16 | +from . import rulesyml |
| 17 | + |
| 18 | +import yaml |
| 19 | + |
| 20 | + |
| 21 | +def run_checks(config: Configuration, result: Result) -> None: |
| 22 | + logging.info("Executing osirules checks") |
| 23 | + |
| 24 | + expected_version = ( |
| 25 | + tuple([int(s) for s in config.get_config_param("osiVersion").split(".")]) |
| 26 | + if config.get_config_param("osiVersion") |
| 27 | + else None |
| 28 | + ) |
| 29 | + fallback_version = tuple( |
| 30 | + [int(s) for s in osirules_constants.OSI_FALLBACK_VERSION.split(".")] |
| 31 | + ) |
| 32 | + expected_type_name = config.get_config_param("osiType") or "SensorView" |
| 33 | + expected_type = OSITrace.map_message_type(expected_type_name) |
| 34 | + |
| 35 | + trace = OSITrace( |
| 36 | + config.get_config_param("InputFile"), config.get_config_param("osiType") |
| 37 | + ) |
| 38 | + |
| 39 | + result.register_checker( |
| 40 | + checker_bundle_name=constants.BUNDLE_NAME, |
| 41 | + checker_id=osirules_constants.CHECKER_ID, |
| 42 | + description="Evaluates messages in the trace file against the OSI Rules of the given OSI version to guarantee they are in conformance with the standard OSI rules.", |
| 43 | + summary=f"Checker validating OSI Rules compliance of messages in a trace file", |
| 44 | + ) |
| 45 | + |
| 46 | + if expected_version is None: |
| 47 | + logging.info( |
| 48 | + f"No expected version, falling back to {'.'.join([str(s) for s in fallback_version])} rules" |
| 49 | + ) |
| 50 | + rules_file = ( |
| 51 | + impresources.files(rulesyml) |
| 52 | + / f"osi_{'_'.join(map(str,expected_version or fallback_version))}.yml" |
| 53 | + ) |
| 54 | + try: |
| 55 | + with rules_file.open("rt") as file: |
| 56 | + rules = yaml.safe_load(file) |
| 57 | + logging.info( |
| 58 | + f"Read rules file for version {'.'.join([str(s) for s in (expected_version or fallback_version)])}" |
| 59 | + ) |
| 60 | + |
| 61 | + except FileNotFoundError: |
| 62 | + logging.info( |
| 63 | + f"No rules file for expected version {'.'.join([str(s) for s in expected_version])}, falling back to {'.'.join([str(s) for s in fallback_version])} rules" |
| 64 | + ) |
| 65 | + fallback_rules_file = ( |
| 66 | + impresources.files(rulesyml) |
| 67 | + / f"osi_{'_'.join(map(str,fallback_version))}.yml" |
| 68 | + ) |
| 69 | + with fallback_rules_file.open("rt") as file: |
| 70 | + rules = yaml.safe_load(file) |
| 71 | + logging.info( |
| 72 | + f"Read rules file for version {'.'.join([str(s) for s in fallback_version])}" |
| 73 | + ) |
| 74 | + |
| 75 | + version_rule_uid = result.register_rule( |
| 76 | + checker_bundle_name=constants.BUNDLE_NAME, |
| 77 | + checker_id=osirules_constants.CHECKER_ID, |
| 78 | + emanating_entity="asam.net", |
| 79 | + standard="osi", |
| 80 | + definition_setting="3.0.0", |
| 81 | + rule_full_name="osirules.version_is_set", |
| 82 | + ) |
| 83 | + |
| 84 | + exp_version_rule_uid = result.register_rule( |
| 85 | + checker_bundle_name=constants.BUNDLE_NAME, |
| 86 | + checker_id=osirules_constants.CHECKER_ID, |
| 87 | + emanating_entity="asam.net", |
| 88 | + standard="osi", |
| 89 | + definition_setting="3.0.0", |
| 90 | + rule_full_name="osirules.expected_version", |
| 91 | + ) |
| 92 | + |
| 93 | + # TODO: Register rules from rules yml |
| 94 | + |
| 95 | + logging.info("Executing osirules.version_is_set check") |
| 96 | + logging.info("Executing osirules.expected_version check") |
| 97 | + |
| 98 | + for message in trace: |
| 99 | + if not message.HasField("version"): |
| 100 | + issue_id = result.register_issue( |
| 101 | + checker_bundle_name=constants.BUNDLE_NAME, |
| 102 | + checker_id=osirules_constants.CHECKER_ID, |
| 103 | + description=f"Version field is not set in top-level message.", |
| 104 | + level=IssueSeverity.ERROR, |
| 105 | + rule_uid=version_rule_uid, |
| 106 | + ) |
| 107 | + elif ( |
| 108 | + expected_version is not None |
| 109 | + and ( |
| 110 | + int(message.version.version_major), |
| 111 | + int(message.version.version_minor), |
| 112 | + int(message.version.version_patch), |
| 113 | + ) |
| 114 | + != expected_version |
| 115 | + ): |
| 116 | + issue_id = result.register_issue( |
| 117 | + checker_bundle_name=constants.BUNDLE_NAME, |
| 118 | + checker_id=osirules_constants.CHECKER_ID, |
| 119 | + description=f"Version field value {message.version.version_major}.{message.version.version_minor}.{message.version.version_patch} is not the expected version {'.'.join([str(s) for s in expected_version])}.", |
| 120 | + level=IssueSeverity.ERROR, |
| 121 | + rule_uid=exp_version_rule_uid, |
| 122 | + ) |
| 123 | + # TODO: Check rules from rulesyml |
| 124 | + |
| 125 | + logging.info( |
| 126 | + f"Issues found - {result.get_checker_issue_count(checker_bundle_name=constants.BUNDLE_NAME, checker_id=osirules_constants.CHECKER_ID)}" |
| 127 | + ) |
| 128 | + |
| 129 | + # TODO: Add logic to deal with error or to skip it |
| 130 | + result.set_checker_status( |
| 131 | + checker_bundle_name=constants.BUNDLE_NAME, |
| 132 | + checker_id=osirules_constants.CHECKER_ID, |
| 133 | + status=StatusType.COMPLETED, |
| 134 | + ) |
0 commit comments