Enhance BaseDevice validation and update Mikrotik model
- Introduced a new method `_validate_template_groups` in `BaseDevice` to ensure TTP templates declare all required and optional sections. - Updated `_REQUIRED_SECTIONS` and added `_OPTIONAL_SECTIONS` to improve template validation. - Modified the `vlans` method in `Mikrotik` to streamline raw data handling and added debug print statements for clarity. - Revised the Mikrotik TTP template to include structured variable definitions and improved group handling for interfaces and VLANs.
This commit is contained in:
@@ -1,20 +1,22 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from ttp import ttp
|
from ttp import ttp
|
||||||
from oxi.interfaces.contract import Device
|
from oxi.interfaces.contract import Device
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from oxi.interfaces.contract import Interfaces, System, Vlans
|
from oxi.interfaces.contract import Interfaces, System, Vlans
|
||||||
|
|
||||||
|
|
||||||
class BaseDevice(ABC):
|
class BaseDevice(ABC):
|
||||||
_REQUIRED_SECTIONS: frozenset[str] = frozenset({"system", "interfaces", "vlans"})
|
_REQUIRED_SECTIONS: frozenset[str] = frozenset({"system", "interfaces"})
|
||||||
|
_OPTIONAL_SECTIONS: frozenset[str] = frozenset({"vlans"})
|
||||||
|
|
||||||
def __init__(self, config: str):
|
def __init__(self, config: str):
|
||||||
self.config: str = config
|
self.config: str = config
|
||||||
self._loaded_template = self._load_template()
|
self._loaded_template = self._load_template()
|
||||||
|
self._validate_template_groups()
|
||||||
self._raw: dict = self._run_ttp()
|
self._raw: dict = self._run_ttp()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -75,6 +77,24 @@ class BaseDevice(ABC):
|
|||||||
raise FileNotFoundError(f"Template {self.template} not found")
|
raise FileNotFoundError(f"Template {self.template} not found")
|
||||||
return path.read_text(encoding="utf-8")
|
return path.read_text(encoding="utf-8")
|
||||||
|
|
||||||
|
def _validate_template_groups(self) -> None:
|
||||||
|
"""Проверка что TTP темлпейт имеет декларированные группы для всех требуемых и опциональных секций"""
|
||||||
|
try:
|
||||||
|
root = ET.fromstring(self._loaded_template)
|
||||||
|
except ET.ParseError:
|
||||||
|
root = ET.fromstring(f"<template>{self._loaded_template}</template>")
|
||||||
|
|
||||||
|
declared = {g.get("name") for g in root.iter("group") if g.get("name")}
|
||||||
|
expected = self._REQUIRED_SECTIONS | self._OPTIONAL_SECTIONS
|
||||||
|
missing = expected - declared
|
||||||
|
|
||||||
|
if missing:
|
||||||
|
raise ValueError(
|
||||||
|
f"{self.__class__.__name__}: template '{self.template}' "
|
||||||
|
f"missing group declarations: {sorted(missing)}. "
|
||||||
|
f"Declared groups: {sorted(declared)}"
|
||||||
|
)
|
||||||
|
|
||||||
def _run_ttp(self) -> dict:
|
def _run_ttp(self) -> dict:
|
||||||
p = ttp(data=self.config, template=self._loaded_template)
|
p = ttp(data=self.config, template=self._loaded_template)
|
||||||
p.parse()
|
p.parse()
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import re
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
from oxi.interfaces import register_parser
|
from oxi.interfaces import register_parser
|
||||||
from oxi.interfaces.base import BaseDevice
|
from oxi.interfaces.base import BaseDevice
|
||||||
@@ -19,11 +20,13 @@ class Mikrotik(BaseDevice):
|
|||||||
print("-" * 12)
|
print("-" * 12)
|
||||||
|
|
||||||
def vlans(self) -> list["Vlans"]:
|
def vlans(self) -> list["Vlans"]:
|
||||||
print(f"{self._raw["vlans"]=}")
|
raw = self._raw.get("vlans", [])
|
||||||
print("-" * 12)
|
print(raw)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
mikr = Mikrotik()
|
with open("../../test.txt") as file:
|
||||||
mikr.run()
|
data = file.read()
|
||||||
|
mikr = Mikrotik(data)
|
||||||
|
mikr.parse()
|
||||||
print(mikr.load)
|
print(mikr.load)
|
||||||
|
|||||||
@@ -1,31 +1,47 @@
|
|||||||
<doc>
|
<doc>
|
||||||
|
some templates
|
||||||
</doc>
|
</doc>
|
||||||
<template>
|
|
||||||
<vars>
|
<vars>
|
||||||
default_system = {
|
default_system = {
|
||||||
"model": "",
|
"model": "",
|
||||||
"serial_number": ""
|
"serial_number": ""
|
||||||
}
|
}
|
||||||
|
default_interfaces = {
|
||||||
|
"disabled": "False"
|
||||||
|
}
|
||||||
default_vlans = {
|
default_vlans = {
|
||||||
"id": "",
|
"disabled": "False",
|
||||||
"name": ""
|
"mtu": None
|
||||||
}
|
}
|
||||||
</vars>
|
</vars>
|
||||||
|
|
||||||
|
|
||||||
<group name="system" default="default_system">
|
<group name="system" default="default_system">
|
||||||
# version: {{ version }} {{ ignore }}
|
# version: {{ version }}{{ ignore('.*') }}
|
||||||
# model = {{ model }}
|
# model = {{ model }}
|
||||||
# serial number = {{ serial_number }}
|
# serial number = {{ serial_number }}
|
||||||
</group>
|
</group>
|
||||||
|
|
||||||
<group name="interfaces">
|
<group name="interfaces" default="default_interfaces">
|
||||||
/ip address
|
/ip address
|
||||||
add address={{ address }} interface={{ interface }} network={{ network }}
|
## not disabled and no comment
|
||||||
|
add address={{ ip | _start_ }}/{{ mask }} interface={{ interface }} network={{ network }}
|
||||||
|
## not disabled and comment with/without quotes
|
||||||
|
add address={{ ip | _start_ }}/{{ mask }} comment={{ comment | ORPHRASE | exclude("disabled=") | strip('"')}} interface={{ interface }} network={{ network }}
|
||||||
|
## disabled no comment
|
||||||
|
add address={{ ip | _start_ }}/{{ mask }} disabled={{ disabled | replace("yes","True") | strip('"')}} interface={{ interface }} network={{ network }}
|
||||||
|
## disabled with comment with/without quotes
|
||||||
|
add address={{ ip | _start_ }}/{{ mask }} comment={{ comment | ORPHRASE | exclude("disabled=") | strip('"') }} disabled={{ disabled }} interface={{ interface }} network={{ network }}
|
||||||
</group>
|
</group>
|
||||||
|
|
||||||
<group name="vlans" default="default_vlans">
|
<group name="vlans">
|
||||||
/vlans add {{ id }} name= {{ name }}
|
/interface vlan {{ _start_ }}
|
||||||
|
## not disabled and no comment
|
||||||
|
add interface={{ interface }} name={{ name | ORPHRASE }} vlan-id={{ vlan_id }}
|
||||||
|
## not disabled and comment with/without quotes
|
||||||
|
add comment={{ comment | ORPHRASE | exclude("disabled=") | strip('"')}} interface={{ interface }} name={{ name | ORPHRASE }} vlan-id={{ vlan_id }}
|
||||||
|
## disabled with comment with/without quotes
|
||||||
|
add comment={{ comment | ORPHRASE | exclude("disabled=") | strip('"')}} disabled={{ disabled }} interface={{ interface }} name={{ name | ORPHRASE }} vlan-id={{ vlan_id }}
|
||||||
|
## disabled no comment
|
||||||
|
add interface={{ interface }} name={{ name | ORPHRASE }} vlan-id={{ vlan_id }} disabled={{ disabled }}
|
||||||
</group>
|
</group>
|
||||||
|
|
||||||
</template>
|
|
||||||
Reference in New Issue
Block a user