Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
435 changes: 433 additions & 2 deletions osism/commands/report.py

Large diffs are not rendered by default.

54 changes: 53 additions & 1 deletion osism/utils/hardware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"""Helpers shared by the live hardware reports (``osism report ...``)."""

import re
from typing import Any, List, Optional
from typing import Any, Dict, List, Optional

_MAC_DIGITS = re.compile(r"^[0-9a-f]{12}$")
_ISO_DATE = re.compile(r"^(\d{4})-(\d{2})-(\d{2})(?!\d)")
_SFF_DATE = re.compile(r"^(\d{2})(\d{2})(\d{2})(?:\s*\S{1,2})?$")


def normalize_mac(value: Any) -> Optional[str]:
Expand All @@ -30,3 +32,53 @@ def natural_key(text: Any) -> List[Any]:
for part in re.split(r"(\d+)", str(text or ""))
if part != ""
]


def normalize_date_code(value: Any) -> Optional[str]:
"""Return the manufacturing date of a module as ``YYYY-MM-DD``.

The EEPROM of a module holds the date as ``YYMMDD``, optionally followed
by a lot code of two characters, and ``ethtool -m`` prints it like that.
SONiC decodes it: Enterprise SONiC writes ``2022-09-07T00:00:00+00:00``,
community SONiC ``2022-09-07`` followed by the lot code. A value in
another format is returned as it is.
"""
text = str(value).strip() if value is not None else ""
if not text:
return None
match = _ISO_DATE.match(text)
if match:
return "-".join(match.groups())
match = _SFF_DATE.match(text)
if match and 1 <= int(match.group(2)) <= 12 and 1 <= int(match.group(3)) <= 31:
return "20{}-{}-{}".format(*match.groups())
return text


TRANSCEIVER_FIELDS = [
"form_factor",
"connector",
"vendor",
"vendor_oui",
"part_number",
"serial",
"revision",
"date_code",
"firmware_active",
"firmware_inactive",
"compliance",
"cable_length",
"wavelength",
]


def transceiver_record(present: Optional[bool]) -> Dict[str, Any]:
"""Return a transceiver record with every identity field unset.

Args:
present: True for a plugged-in module, False for a port without a
module, None when the port has no module of its own
"""
record: Dict[str, Any] = {"present": present}
record.update({name: None for name in TRANSCEIVER_FIELDS})
return record
Loading