Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/sonic-bgpcfgd/bgpcfgd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import syslog
import traceback
import threading

from swsscommon import swsscommon

Expand All @@ -18,6 +19,7 @@
from .managers_setsrc import ZebraSetSrc
from .managers_static_rt import StaticRouteMgr
from .managers_rm import RouteMapMgr
from .static_rt_timer import StaticRouteTimer
from .runner import Runner, signal_handler
from .template import TemplateFabric
from .utils import read_constants
Expand All @@ -27,6 +29,9 @@

def do_work():
""" Main function """
st_rt_timer = StaticRouteTimer()
thr = threading.Thread(target = st_rt_timer.run)
thr.start()
frr = FRR(["bgpd", "zebra", "staticd"])
frr.wait_for_daemons(seconds=20)
#
Expand Down Expand Up @@ -59,6 +64,7 @@ def do_work():
BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR"),
# Static Route Managers
StaticRouteMgr(common_objs, "CONFIG_DB", "STATIC_ROUTE"),
StaticRouteMgr(common_objs, "APPL_DB", "STATIC_ROUTE"),
# Route Advertisement Managers
AdvertiseRouteMgr(common_objs, "STATE_DB", swsscommon.STATE_ADVERTISE_NETWORK_TABLE_NAME),
RouteMapMgr(common_objs, "APPL_DB", swsscommon.APP_BGP_PROFILE_TABLE_NAME),
Expand All @@ -67,6 +73,7 @@ def do_work():
for mgr in managers:
runner.add_manager(mgr)
runner.run()
thr.join()


def main():
Expand Down
8 changes: 5 additions & 3 deletions src/sonic-bgpcfgd/bgpcfgd/managers_static_rt.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ def split_key(key):
:param key: key to split
:return: vrf name extracted from the key, ip prefix extracted from the key
"""
if '|' not in key:
return 'default', key
else:
if '|' in key:
return tuple(key.split('|', 1))
elif ':' in key:
return tuple(key.split(':', 1))
else:
return 'default', key

def static_route_commands(self, ip_nh_set, cur_nh_set, ip_prefix, vrf):
diff_set = ip_nh_set.symmetric_difference(cur_nh_set)
Expand Down
55 changes: 55 additions & 0 deletions src/sonic-bgpcfgd/bgpcfgd/static_rt_timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from .log import log_err, log_info, log_debug
from swsssdk import SonicV2Connector
import time

class StaticRouteTimer(object):
""" This class checks the static routes and deletes those entries that have not been refreshed """
def __init__(self):
self.db = SonicV2Connector()
self.db.connect(self.db.APPL_DB)
self.timer = None
self.start = None

DEFAULT_TIMER = 180
DEFAULT_SLEEP = 60
MAX_TIMER = 1800

def set_timer(self):
""" Check for custom route expiry time in STATIC_ROUTE:EXPIRY_TIME """
timer = self.db.get(self.db.APPL_DB, "STATIC_ROUTE_EXPIRY_TIME", "time")
if timer is not None:
timer = int(timer)
if timer > 0 and timer <= self.MAX_TIMER:
self.timer = timer
return
log_err("Custom static route expiry time of {}s is invalid!".format(timer))
return

def alarm(self):
""" Clear unrefreshed static routes """
static_routes = self.db.keys(self.db.APPL_DB, "STATIC_ROUTE:*:*")
if static_routes is not None:
for sr in static_routes:
refresh = self.db.get(self.db.APPL_DB, sr, "refresh")
val = sr.split(":")
if refresh == "true":
self.db.set(self.db.APPL_DB, sr, "refresh", "false")
log_debug("Refresh status of static route {}:{} is set to false".format(val[0], val[1]))
else:
self.db.delete(self.db.APPL_DB, sr)
log_debug("Static route {}:{} deleted".format(val[0], val[1]))
self.start = time.time()
return

def run(self):
self.start = time.time()
while True:
self.set_timer()
if self.timer:
log_info("Static route expiry set to {}s".format(self.timer))
time.sleep(self.timer)
self.alarm()
else:
time.sleep(self.DEFAULT_SLEEP)
if time.time() - self.start >= self.DEFAULT_TIMER:
self.alarm()