From 8e5ff74f018ec44a7c84907d52c2382ad7efef3f Mon Sep 17 00:00:00 2001
From: anamehra <54692434+anamehra@users.noreply.github.com>
Date: Fri, 19 Apr 2024 13:38:30 -0700
Subject: [PATCH] Revert "Revert "route_check: Skip route checks if bgp feature
 is not enabled"" (#3270)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Revert "Revert "route_check: Skip route checks if bgp feature is not enabled"…"

This reverts commit 01ee98ec6fb11057e0db41334405c6f0f4942214.
---
 scripts/route_check.py         |  16 ++++
 tests/route_check_test.py      |   9 +-
 tests/route_check_test_data.py | 157 ++++++++++++++++++++++++++++++++-
 3 files changed, 177 insertions(+), 5 deletions(-)

diff --git a/scripts/route_check.py b/scripts/route_check.py
index 5349acd0af..ee417dc49c 100755
--- a/scripts/route_check.py
+++ b/scripts/route_check.py
@@ -533,6 +533,18 @@ def filter_out_standalone_tunnel_routes(namespace, routes):
 
     return updated_routes
 
+def is_feature_bgp_enabled(namespace):
+    """
+    Check if bgp feature is enabled or disabled.
+    Return True if enabled else False.
+    """
+    cfg_db = multi_asic.connect_config_db_for_ns(namespace)
+    feature_table = cfg_db.get_table("FEATURE")
+    bgp_enabled = False
+    if 'bgp' in feature_table:
+        if feature_table['bgp']["state"] == "enabled":
+            bgp_enabled = True
+    return bgp_enabled
 
 def check_frr_pending_routes(namespace):
     """
@@ -831,6 +843,10 @@ def main():
     signal.signal(signal.SIGALRM, handler)
     load_db_config()
 
+    if not is_feature_bgp_enabled(namespace):
+        print_message(syslog.LOG_INFO, "BGP feature is disabled, exiting without checking routes!!")
+        return 0, None
+
     while True:
         signal.alarm(TIMEOUT_SECONDS)
         ret, res= check_routes(namespace)
diff --git a/tests/route_check_test.py b/tests/route_check_test.py
index 820f062107..1f92b3d19a 100644
--- a/tests/route_check_test.py
+++ b/tests/route_check_test.py
@@ -271,11 +271,14 @@ def test_timeout(self, mock_dbs, force_hang):
         # Test timeout
         ex_raised = False
         # Use an expected failing test case to trigger the select
-        set_test_case_data(TEST_DATA['2'])
-
+        ct_data = TEST_DATA['2']
+        set_test_case_data(ct_data)
         try:
-            with patch('sys.argv', [route_check.__file__.split('/')[-1]]):
+            with patch('sys.argv', [route_check.__file__.split('/')[-1]]), \
+                patch('route_check.load_db_config', side_effect=lambda: init_db_conns(ct_data[NAMESPACE])):
+
                 ret, res = route_check.main()
+
         except Exception as err:
             ex_raised = True
             expect = "timeout occurred"
diff --git a/tests/route_check_test_data.py b/tests/route_check_test_data.py
index 50c6276f26..c5a606cb90 100644
--- a/tests/route_check_test_data.py
+++ b/tests/route_check_test_data.py
@@ -23,6 +23,7 @@
 VNET_ROUTE_TABLE = 'VNET_ROUTE_TABLE'
 INTF_TABLE = 'INTF_TABLE'
 RT_ENTRY_TABLE = 'ASIC_STATE'
+FEATURE_TABLE = 'FEATURE'
 SEPARATOR = ":"
 DEVICE_METADATA = "DEVICE_METADATA"
 MUX_CABLE = "MUX_CABLE"
@@ -32,7 +33,17 @@
 RT_ENTRY_KEY_PREFIX = 'SAI_OBJECT_TYPE_ROUTE_ENTRY:{\"dest":\"'
 RT_ENTRY_KEY_SUFFIX = '\",\"switch_id\":\"oid:0x21000000000000\",\"vr\":\"oid:0x3000000000023\"}'
 
-DEFAULT_CONFIG_DB = {DEVICE_METADATA: {LOCALHOST: {}}}
+DEFAULT_CONFIG_DB = {
+    DEVICE_METADATA: {
+        LOCALHOST: {
+            }
+        },
+    FEATURE_TABLE: {
+        "bgp": {
+            "state": "enabled"
+            }
+        }
+    }
 
 TEST_DATA = {
     "0": {
@@ -330,6 +341,11 @@
                 CONFIG_DB: {
                     DEVICE_METADATA: {
                         LOCALHOST: {"subtype": "DualToR"}
+                    },
+                    FEATURE_TABLE: {
+                        "bgp": {
+                            "state": "enabled"
+                        }
                     }
                 },
                 APPL_DB: {
@@ -396,6 +412,11 @@
                             "soc_ipv6": "fc02:1000::3/128",
                             "state": "auto"
                         },
+                    },
+                    FEATURE_TABLE: {
+                        "bgp": {
+                            "state": "enabled"
+                        }
                     }
                 },
                 APPL_DB: {
@@ -633,6 +654,11 @@
                 CONFIG_DB: {
                     DEVICE_METADATA: {
                         LOCALHOST: {"subtype": "DualToR"}
+                    },
+                    FEATURE_TABLE: {
+                        "bgp": {
+                            "state": "enabled"
+                        }
                     }
                 },
                 APPL_DB: {
@@ -954,5 +980,132 @@
         },
         RET: -1,
     },
-
+    "22": {
+        DESCR: "basic good one on single asic, bgp disabled",
+        MULTI_ASIC: False,
+        NAMESPACE: [''],
+        ARGS: "route_check -m INFO -i 1000",
+        PRE: {
+            DEFAULTNS: {
+                CONFIG_DB: {
+                    DEVICE_METADATA: {
+                        LOCALHOST: {
+                        }
+                    },
+                    FEATURE_TABLE: {
+                        "bgp": {
+                            "state": "disabled"
+                        }
+                    }
+                },
+                APPL_DB: {
+                    ROUTE_TABLE: {
+                        "0.0.0.0/0" : { "ifname": "portchannel0" },
+                        "10.10.196.12/31" : { "ifname": "portchannel0" },
+                    },
+                    INTF_TABLE: {
+                        "PortChannel1013:10.10.196.24/31": {},
+                        "PortChannel1023:2603:10b0:503:df4::5d/126": {},
+                        "PortChannel1024": {}
+                    }
+                },
+                ASIC_DB: {
+                    RT_ENTRY_TABLE: {
+                        RT_ENTRY_KEY_PREFIX + "10.10.196.12/31" + RT_ENTRY_KEY_SUFFIX: {},
+                        RT_ENTRY_KEY_PREFIX + "10.10.196.24/32" + RT_ENTRY_KEY_SUFFIX: {},
+                        RT_ENTRY_KEY_PREFIX + "2603:10b0:503:df4::5d/128" + RT_ENTRY_KEY_SUFFIX: {},
+                        RT_ENTRY_KEY_PREFIX + "0.0.0.0/0" + RT_ENTRY_KEY_SUFFIX: {}
+                    }
+                },
+            },
+        },
+        FRR_ROUTES: {
+            DEFAULTNS: {
+                "0.0.0.0/0": [
+                    {
+                        "prefix": "0.0.0.0/0",
+                        "vrfName": "default",
+                        "protocol": "bgp",
+                        "offloaded": "true",
+                    },
+                ],
+                "10.10.196.12/31": [
+                    {
+                        "prefix": "10.10.196.12/31",
+                        "vrfName": "default",
+                        "protocol": "bgp",
+                    },
+                ],
+                "10.10.196.24/31": [
+                    {
+                        "protocol": "connected",
+                    },
+                ],
+            },
+        },
+    },
+    "23": {
+        DESCR: "basic good one on multi-asic, bgp disabled",
+        MULTI_ASIC: True,
+        NAMESPACE: ['asic0'],
+        ARGS: "route_check -m INFO -i 1000",
+        PRE: {
+            ASIC0: {
+                CONFIG_DB: {
+                    DEVICE_METADATA: {
+                        LOCALHOST: {
+                        }
+                    },
+                    FEATURE_TABLE: {
+                        "bgp": {
+                            "state": "disabled"
+                        }
+                    }
+                },
+                APPL_DB: {
+                    ROUTE_TABLE: {
+                        "0.0.0.0/0" : { "ifname": "portchannel0" },
+                        "10.10.196.12/31" : { "ifname": "portchannel0" },
+                    },
+                    INTF_TABLE: {
+                        "PortChannel1013:10.10.196.24/31": {},
+                        "PortChannel1023:2603:10b0:503:df4::5d/126": {},
+                        "PortChannel1024": {}
+                    }
+                },
+                ASIC_DB: {
+                    RT_ENTRY_TABLE: {
+                        RT_ENTRY_KEY_PREFIX + "10.10.196.12/31" + RT_ENTRY_KEY_SUFFIX: {},
+                        RT_ENTRY_KEY_PREFIX + "10.10.196.24/32" + RT_ENTRY_KEY_SUFFIX: {},
+                        RT_ENTRY_KEY_PREFIX + "2603:10b0:503:df4::5d/128" + RT_ENTRY_KEY_SUFFIX: {},
+                        RT_ENTRY_KEY_PREFIX + "0.0.0.0/0" + RT_ENTRY_KEY_SUFFIX: {}
+                    }
+                },
+            },
+        },
+        FRR_ROUTES: {
+            ASIC0: {
+                "0.0.0.0/0": [
+                    {
+                        "prefix": "0.0.0.0/0",
+                        "vrfName": "default",
+                        "protocol": "bgp",
+                        "offloaded": "true",
+                    },
+                ],
+                "10.10.196.12/31": [
+                    {
+                        "prefix": "10.10.196.12/31",
+                        "vrfName": "default",
+                        "protocol": "bgp",
+                    },
+                ],
+                "10.10.196.24/31": [
+                    {
+                        "protocol": "connected",
+                    },
+                ],
+            },
+        },
+    },
 }