Add interfaces and vlans methods to Qtech model; update TTP template
- Implemented the `interfaces` method to process IP addresses and netmasks, adding a calculated mask to the output. - Added the `vlans` method to handle VLAN data, supporting both individual and grouped VLAN IDs. - Updated the QTECH TTP template to include a new version field and improved formatting for VLAN definitions.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from ipaddress import ip_interface
|
||||
import os
|
||||
from oxi.interfaces import register_parser
|
||||
from oxi.interfaces.base import BaseDevice
|
||||
|
||||
@@ -5,3 +7,43 @@ from oxi.interfaces.base import BaseDevice
|
||||
@register_parser(["QTECH"])
|
||||
class Qtech(BaseDevice):
|
||||
template = "qtech.ttp"
|
||||
|
||||
def interfaces(self) -> list[dict]:
|
||||
interfaces_ttp = self.raw["interfaces"]
|
||||
for item in interfaces_ttp:
|
||||
if item.get("ip_address") and item.get("netmask"):
|
||||
ipaddress = ip_interface(
|
||||
f"{item.get('ip_address')}/{item.get('netmask')}"
|
||||
)
|
||||
item["mask"] = ipaddress.network.prefixlen
|
||||
item.pop("netmask", "Key not found")
|
||||
return interfaces_ttp
|
||||
|
||||
def vlans(self) -> list[dict]:
|
||||
vlans_ttp = self.raw["vlans"]
|
||||
vlans = []
|
||||
named_vlan = set()
|
||||
for item in vlans_ttp:
|
||||
if item.get("vlan_id"):
|
||||
named_vlan.add(item.get("vlan_id"))
|
||||
vlans.append(item)
|
||||
else:
|
||||
ids = item.get("vlan_ids", "")
|
||||
tail = item.get("vlan_tail")
|
||||
if tail:
|
||||
ids = f"{ids},{tail}"
|
||||
for vid in ids.split(","):
|
||||
vid = vid.strip()
|
||||
if vid in named_vlan:
|
||||
continue
|
||||
vlans.append({"vlan_id": vid})
|
||||
return vlans
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(os.path.abspath(os.curdir))
|
||||
with open("./test3.txt") as file:
|
||||
data = file.read()
|
||||
qtech = Qtech(data)
|
||||
qt = qtech.parse()
|
||||
# print(qt)
|
||||
|
||||
@@ -3,22 +3,25 @@
|
||||
<vars>
|
||||
default_system = {
|
||||
"model": "",
|
||||
"serial_number": ""
|
||||
"serial_number": "",
|
||||
"version": ""
|
||||
}
|
||||
</vars>
|
||||
|
||||
<group name="system">
|
||||
! {{ model }} Series Software, Version {{ bootrom_ver }} Build {{ version }}, {{ ignore }}
|
||||
! Serial num:{{ serial_number }}, {{ ignore }}
|
||||
<group name="system" default="default_system">
|
||||
! {{ model | ORPHRASE }} Series Software, Version {{ ignore }} Build {{ version | strip(",") }}{{ ignore('.*') }}
|
||||
! Serial num:{{ serial_number | strip(",") }}{{ ignore('.*') }}
|
||||
</group>
|
||||
|
||||
<group name="interfaces">
|
||||
interface {{ interface }}
|
||||
description {{ description | ORPHRASE }}
|
||||
ip address {{ ip_address }} {{ netmask }}
|
||||
description {{ description | ORPHRASE }}
|
||||
ip address {{ ip_address }} {{ netmask }}
|
||||
</group>
|
||||
|
||||
<group name="vlans">
|
||||
vlan {{ vlan_id }}
|
||||
name {{ name }}
|
||||
vlan {{ vlan_ids | contains(",", "-") | unrange("-", ",") | _start_ }}
|
||||
,{{ vlan_tail | unrange("-", ",") }}
|
||||
vlan {{ vlan_id | _start_ }}
|
||||
name {{ name | ORPHRASE }}
|
||||
</group>
|
||||
|
||||
Reference in New Issue
Block a user