From 936679ee47fbfee2f7204fb1703aa649ce0da8b2 Mon Sep 17 00:00:00 2001 From: jcaiMR <111116206+jcaiMR@users.noreply.github.com> Date: Thu, 9 Feb 2023 05:40:05 +0800 Subject: [PATCH] Set 'origin' and 'AS Path' for T1 SLB routes (#13613) * set origin and as-path prepend for routes from SLB --- src/sonic-bgpcfgd/bgpcfgd/managers_rm.py | 29 ++++++++++++++++++++---- src/sonic-bgpcfgd/tests/test_rm.py | 16 +++++++++++-- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/sonic-bgpcfgd/bgpcfgd/managers_rm.py b/src/sonic-bgpcfgd/bgpcfgd/managers_rm.py index 08609c68f9..35a3f864d5 100644 --- a/src/sonic-bgpcfgd/bgpcfgd/managers_rm.py +++ b/src/sonic-bgpcfgd/bgpcfgd/managers_rm.py @@ -1,8 +1,9 @@ from .manager import Manager +from swsscommon import swsscommon from .log import log_err, log_debug ROUTE_MAPS = ["FROM_SDN_SLB_ROUTES"] - +FROM_SDN_SLB_DEPLOYMENT_ID = '2' class RouteMapMgr(Manager): """This class add route-map when BGP_PROFILE_TABLE in APPL_DB is updated""" @@ -71,8 +72,28 @@ class RouteMapMgr(Manager): return False return True + def __read_asn(self): + if not 'deployment_id_asn_map' in self.constants: + log_err("BGPRouteMapMgr:: 'deployment_id_asn_map' key is not found in constants") + return None + if FROM_SDN_SLB_DEPLOYMENT_ID in self.constants['deployment_id_asn_map']: + return self.constants['deployment_id_asn_map'][FROM_SDN_SLB_DEPLOYMENT_ID] + log_err("BGPRouteMapMgr:: deployment id %s is not found in constants" % (FROM_SDN_SLB_DEPLOYMENT_ID)) + return None + def __update_rm(self, rm, data): - cmds = ["route-map %s permit 100" % ("%s_RM" % rm), " set community %s" % data["community_id"]] - log_debug("BGPRouteMapMgr:: update route-map %s community %s" % ("%s_RM" % rm, data["community_id"])) - self.cfg_mgr.push_list(cmds) + cmds = [] + if rm == "FROM_SDN_SLB_ROUTES": + cmds.append("route-map %s permit 100" % ("%s_RM" % rm)) + bgp_asn = self.__read_asn() + if bgp_asn is None or bgp_asn is '': + log_debug("BGPRouteMapMgr:: update route-map %s, but asn is not found in constants" % ("%s_RM" % rm)) + return + cmds.append(" set as-path prepend %s %s" % (bgp_asn, bgp_asn)) + cmds.append(" set community %s" % data["community_id"]) + cmds.append(" set origin incomplete") + log_debug("BGPRouteMapMgr:: update route-map %s community %s origin incomplete as-path prepend %s %s" % \ + ("%s_RM" % rm, data["community_id"], bgp_asn, bgp_asn)) + if cmds: + self.cfg_mgr.push_list(cmds) log_debug("BGPRouteMapMgr::Done") diff --git a/src/sonic-bgpcfgd/tests/test_rm.py b/src/sonic-bgpcfgd/tests/test_rm.py index fe89055d27..867ae1a7a5 100644 --- a/src/sonic-bgpcfgd/tests/test_rm.py +++ b/src/sonic-bgpcfgd/tests/test_rm.py @@ -3,13 +3,23 @@ from bgpcfgd.directory import Directory from bgpcfgd.managers_rm import RouteMapMgr from swsscommon import swsscommon + +test_rm_constants = { + "deployment_id_asn_map": { + "1": 12345, + "2": 12346, + "3": 12347, + "4": 12348, + } +} + def constructor(): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, - 'constants': {}, + 'constants': test_rm_constants, } mgr = RouteMapMgr(common_objs, "APPL_DB", "BGP_PROFILE_TABLE") @@ -47,7 +57,9 @@ def test_set_del(): True, [ ["route-map FROM_SDN_SLB_ROUTES_RM permit 100", - " set community 1234:1234"] + " set as-path prepend 12346 12346", + " set community 1234:1234", + " set origin incomplete"] ] )