Skip to content

Commit a42942d

Browse files
committed
Add initial setup for osirules checker
Signed-off-by: Pierre R. Mai <pmai@pmsf.de>
1 parent 29fc33f commit a42942d

17 files changed

+8046
-339
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v2.3.0
3+
rev: v5.0.0
44
hooks:
55
- id: check-yaml
66
- id: end-of-file-fixer
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<Config>
3+
4+
<Param name="InputFile" value="tests/data/deserialization_version_is_set/deserialization_version_is_set_invalid.osi" />
5+
<Param name="osiType" value="SensorView" />
6+
<Param name="osiVersion" value="3.1.0" />
7+
8+
<CheckerBundle application="osiBundle">
9+
<Param name="resultFile" value="osi_bundle_report.xqar" />
10+
<Checker checkerId="validator_osi" maxLevel="1" minLevel="3" />
11+
</CheckerBundle>
12+
13+
<ReportModule application="TextReport">
14+
<Param name="strInputFile" value="Result.xqar" />
15+
<Param name="strReportFile" value="Report.txt" />
16+
</ReportModule>
17+
18+
</Config>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<Config>
3+
4+
<Param name="InputFile" value="tests/data/deserialization_version_is_set/deserialization_version_is_set_invalid.osi" />
5+
<Param name="osiType" value="SensorView" />
6+
7+
<CheckerBundle application="osiBundle">
8+
<Param name="resultFile" value="osi_bundle_report.xqar" />
9+
<Checker checkerId="validator_osi" maxLevel="1" minLevel="3" />
10+
</CheckerBundle>
11+
12+
<ReportModule application="TextReport">
13+
<Param name="strInputFile" value="Result.xqar" />
14+
<Param name="strReportFile" value="Report.txt" />
15+
</ReportModule>
16+
17+
</Config>

poetry.lock

Lines changed: 489 additions & 337 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "qc-osi-trace"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
description = "This project implements the OSI Trace Checker for the ASAM Quality Checker project."
55
authors = ["Pierre R. Mai <pmai@pmsf.de>"]
66
license = "MPL-2.0"
@@ -14,6 +14,7 @@ packages = [
1414
python = "^3.10"
1515
asam-qc-baselib = {git = "https://github.com/asam-ev/qc-baselib-py.git", rev = "develop"}
1616
open-simulation-interface = {git = "https://github.com/OpenSimulationInterface/open-simulation-interface.git", rev = "v3.7.0"}
17+
pyyaml = "^6.0.0"
1718

1819
[tool.poetry.group.dev.dependencies]
1920
pytest = "^8.2.2"

qc_ositrace/checks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from . import deserialization as deserialization
2+
from . import osirules as osirules
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import osirules_constants as osirules_constants
2+
from . import osirules_checker as osirules_checker
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CHECKER_ID = "osirules_osi"
2+
OSI_FALLBACK_VERSION = "3.7.0"

0 commit comments

Comments
 (0)