From bd1a24d33a498f0b2e1a95b1fd3c213f30df9624 Mon Sep 17 00:00:00 2001 From: Jakub Krajewski Date: Mon, 18 Mar 2024 12:26:29 +0100 Subject: [PATCH] Add SNMP converter. Change IP field to correct types. --- .../feature_profile/sdwan/system/snmp.py | 2 +- .../feature_template/factory_method.py | 2 + .../converters/feature_template/snmp.py | 50 +++++++++++++++++++ catalystwan/workflows/config_migration.py | 5 +- 4 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 catalystwan/utils/config_migration/converters/feature_template/snmp.py diff --git a/catalystwan/models/configuration/feature_profile/sdwan/system/snmp.py b/catalystwan/models/configuration/feature_profile/sdwan/system/snmp.py index bbddffa0..dbefda36 100644 --- a/catalystwan/models/configuration/feature_profile/sdwan/system/snmp.py +++ b/catalystwan/models/configuration/feature_profile/sdwan/system/snmp.py @@ -106,7 +106,7 @@ class TargetItem(BaseModel): validation_alias="vpnId", description="Set VPN in which SNMP server is located", ) - ip: Union[Global[Union[IPv4Address, IPv6Address]], Variable] = Field( + ip: Union[Global[IPv4Address], Global[IPv6Address], Variable] = Field( ..., description="Set IPv4/IPv6 address of SNMP server" ) port: Union[Global[int], Variable] = Field(..., description="Set UDP port number to connect to SNMP server") diff --git a/catalystwan/utils/config_migration/converters/feature_template/factory_method.py b/catalystwan/utils/config_migration/converters/feature_template/factory_method.py index 45d7a39e..5f96ebec 100644 --- a/catalystwan/utils/config_migration/converters/feature_template/factory_method.py +++ b/catalystwan/utils/config_migration/converters/feature_template/factory_method.py @@ -6,6 +6,7 @@ from catalystwan.exceptions import CatalystwanException from catalystwan.models.configuration.feature_profile.sdwan.system import AnySystemParcel from catalystwan.utils.config_migration.converters.feature_template.dhcp import DhcpTemplateConverter +from catalystwan.utils.config_migration.converters.feature_template.snmp import SNMPTemplateConverter from catalystwan.utils.feature_template.find_template_values import find_template_values from .aaa import AAATemplateConverter @@ -39,6 +40,7 @@ ThousandEyesTemplateConverter, UcseTemplateConverter, DhcpTemplateConverter, + SNMPTemplateConverter, ] diff --git a/catalystwan/utils/config_migration/converters/feature_template/snmp.py b/catalystwan/utils/config_migration/converters/feature_template/snmp.py new file mode 100644 index 00000000..bce41441 --- /dev/null +++ b/catalystwan/utils/config_migration/converters/feature_template/snmp.py @@ -0,0 +1,50 @@ +import logging +from copy import deepcopy + +from catalystwan.api.configuration_groups.parcel import Variable, as_global, as_variable +from catalystwan.models.configuration.feature_profile.sdwan.system.snmp import Authorization, SNMPParcel + +logger = logging.getLogger(__name__) + + +class SNMPTemplateConverter: + """ + A class for converting template values into a SecurityParcel object. + + Attributes: + supported_template_types (tuple): A tuple of supported template types. + """ + + supported_template_types = ("cisco_snmp",) + + @staticmethod + def create_parcel(name: str, description: str, template_values: dict) -> SNMPParcel: + """ + Creates a SecurityParcel object based on the provided template values. + + Args: + name (str): The name of the SecurityParcel. + description (str): The description of the SecurityParcel. + template_values (dict): A dictionary containing the template values. + + Returns: + SecurityParcel: A SecurityParcel object with the provided template values. + """ + values = deepcopy(template_values) + for community_item in values.get("community", []): + if authorization := community_item.get("authorization"): + community_item["authorization"] = as_global(authorization.value, Authorization) + + values["target"] = values.pop("trap", {}).get("target", []) + default_view_oid_id = "{{{{l_snmpView_1_snmpOid_{}_id}}}}" + for view in values.get("view", []): + for i, oid in enumerate(view.get("oid", [])): + id_ = oid.get("id", as_variable(default_view_oid_id.format(i + 1))) + if isinstance(id_, Variable): + logger.info( + f"OID ID is not set, using device specific variable {default_view_oid_id.format(i + 1)}" + ) + oid["id"] = id_ + + parcel_values = {"parcel_name": name, "parcel_description": description, **values} + return SNMPParcel(**parcel_values) # type: ignore diff --git a/catalystwan/workflows/config_migration.py b/catalystwan/workflows/config_migration.py index e7fe38d2..1d3431d4 100644 --- a/catalystwan/workflows/config_migration.py +++ b/catalystwan/workflows/config_migration.py @@ -74,8 +74,9 @@ "omp-vsmart", "cisco_ntp", "ntp", - # "bgp", - # "cisco_bgp", + "bgp", + "cisco_bgp", + "cisco_snmp", ] FEATURE_PROFILE_TRANSPORT = ["dhcp", "cisco_dhcp_server", "dhcp-server"]