diff --git a/hyperglass/plugins/_builtin/__init__.py b/hyperglass/plugins/_builtin/__init__.py index cefb627..8f8bd0f 100644 --- a/hyperglass/plugins/_builtin/__init__.py +++ b/hyperglass/plugins/_builtin/__init__.py @@ -6,11 +6,13 @@ from .bgp_route_arista import BGPRoutePluginArista from .bgp_route_frr import BGPRoutePluginFrr from .bgp_route_juniper import BGPRoutePluginJuniper from .mikrotik_garbage_output import MikrotikGarbageOutput +from .bgp_route_huawei import BGPRoutePluginHuawei __all__ = ( "BGPRoutePluginArista", "BGPRoutePluginFrr", "BGPRoutePluginJuniper", + "BGPRoutePluginHuawei", "MikrotikGarbageOutput", "RemoveCommand", ) diff --git a/hyperglass/plugins/_builtin/bgp_route_huawei.py b/hyperglass/plugins/_builtin/bgp_route_huawei.py new file mode 100644 index 0000000..86e70a3 --- /dev/null +++ b/hyperglass/plugins/_builtin/bgp_route_huawei.py @@ -0,0 +1,42 @@ +from ipaddress import ip_network +from .._input import InputPlugin + +# Standard Library +import typing as t + +# Third Party +from pydantic import PrivateAttr + +if t.TYPE_CHECKING: + # Project + from hyperglass.models.api.query import Query + +InputPluginTransformReturn = t.Union[t.Sequence[str], str] + +class BGPRoutePluginHuawei(InputPlugin): + _hyperglass_builtin: bool = PrivateAttr(True) + platforms: t.Sequence[str] =("huawei", "huawei_vrpv8",) + directives: t.Sequence[str] = ("__hyperglass_huawei_bgp_route__",) + + """ + Huawei BGP Route Input Plugin + + This plugin transforms a query target into a network address and prefix length + ex.: 192.0.2.0/24 -> 192.0.2.0 24 + ex.: 2001:db8::/32 -> 2001:db8:: 32 + """ + def transform(self, query: "Query") -> InputPluginTransformReturn: + (target := query.query_target) + + if not target or not isinstance(target, list) or len(target) == 0: + return None + + target = target[0].strip() + + # Check for the / in the query target + if target.find("/") == -1: + return target + + target_network = ip_network(target) + + return f"{target_network.network_address!s} {target_network.prefixlen!s}" \ No newline at end of file