diff --git a/board/netconf/rootfs/libexec/infix/cli-pretty b/board/netconf/rootfs/libexec/infix/cli-pretty index 20b598cd..0eb8363a 100755 --- a/board/netconf/rootfs/libexec/infix/cli-pretty +++ b/board/netconf/rootfs/libexec/infix/cli-pretty @@ -27,6 +27,12 @@ class PadSoftware: state = 10 version = 23 + +class PadUsbPort: + title = 30 + name = 20 + state = 10 + class Decore(): @staticmethod def decorate(sgr, txt, restore="0"): @@ -131,6 +137,18 @@ class Software: print(f"SHA-256 : {self.hash}") print(f"Installed : {self.date}") +class USBport: + def __init__(self, data): + self.data = data + self.name = data.get('name', '') + self.state = get_json_data('', self.data, 'state', 'admin-state') + + def print(self): + #print(self.name) + row = f"{self.name:<{PadUsbPort.name}}" + row += f"{self.state:<{PadUsbPort.state}}" + print(row) + class Iface: def __init__(self, data): self.data = data @@ -468,6 +486,24 @@ def infix_system(json, name): if slot.is_rootfs(): slot.print() +def ietf_hardware(json): + if not json.get("ietf-hardware:hardware"): + print(f"Error, top level \"ietf-hardware:component\" missing") + sys.exit(1) + + hdr = (f"{'USB PORTS':<{PadUsbPort.title}}") + print(Decore.invert(hdr)) + hdr = (f"{'NAME':<{PadUsbPort.name}}" + f"{'STATE':<{PadUsbPort.state}}") + print(Decore.invert(hdr)) + + components = get_json_data({}, json, "ietf-hardware:hardware", "component") + + for component in components: + if component.get("class") == "infix-hardware:usb": + port = USBport(component) + port.print() + def main(): try: json_data = json.load(sys.stdin) @@ -484,6 +520,8 @@ def main(): sys.exit(ietf_routing(json_data, args.name)) if args.module == "infix-system": sys.exit(infix_system(json_data, args.name)) + if args.module == "ietf-hardware": + sys.exit(ietf_hardware(json_data)) else: print(f"Error, unknown module {args.module}") sys.exit(1) diff --git a/board/netconf/rootfs/libexec/infix/yanger b/board/netconf/rootfs/libexec/infix/yanger index 81e144e4..ff0cdc36 100755 --- a/board/netconf/rootfs/libexec/infix/yanger +++ b/board/netconf/rootfs/libexec/infix/yanger @@ -3,6 +3,7 @@ import subprocess import json import sys # (built-in module) +import os import argparse from datetime import datetime @@ -155,13 +156,43 @@ def get_vpd_data(vpd): component["infix-hardware:vpd-data"]["infix-hardware:vendor-extension"] = vendor_extensions return component +def get_usb_ports(usb_ports): + ports=[] + names=[] + for usb_port in usb_ports: + port={} + if usb_port.get("path"): + if usb_port["name"] in names: + continue + + path = usb_port["path"] + if os.path.exists(path): + names.append(usb_port["name"]) + with open(path, "r") as f: + data=int(f.readline().strip()) + enabled="unlocked" if data == 1 else "locked" + port["state"] = {} + port["state"]["admin-state"] = enabled + port["name"] = usb_port["name"] + port["class"] = "infix-hardware:usb" + port["state"]["oper-state"] = "enabled" + ports.append(port) + + return ports + + def add_hardware(hw_out, test): - cmd = ['cat', "/run/system.json"] + if test: + cmd = ['cat', f"{test}/system.json"] + else: + cmd = ['cat', "/run/system.json"] data = run_json_cmd(cmd) components=[] for _,vpd in data.get("vpd").items(): component=get_vpd_data(vpd) components.append(component) + if(data.get("usb-ports", None)): + components.extend(get_usb_ports(data["usb-ports"])) insert(hw_out, "component", components) def get_routes(routes, proto, data): diff --git a/src/klish-plugin-infix/xml/infix.xml b/src/klish-plugin-infix/xml/infix.xml index c37f35da..7e66acfe 100644 --- a/src/klish-plugin-infix/xml/infix.xml +++ b/src/klish-plugin-infix/xml/infix.xml @@ -326,6 +326,12 @@ /libexec/infix/cli-pretty "ietf-routing" -n "$KLISH_PARAM_name" + + + sysrepocfg -f json -X -d operational -x "/ietf-hardware:hardware" | \ + /libexec/infix/cli-pretty "ietf-hardware" + +