Skip to content

Commit

Permalink
add static route expiry support
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaiMR committed Nov 16, 2022
1 parent 2698826 commit 5e80ab5
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/sonic-bgpcfgd/bgpcfgd/static_rt_timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from .log import log_err, log_info, log_debug
from swsscommon import swsscommon
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 = swsscommon.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:
expiry = self.db.get(self.db.APPL_DB, sr, "expiry")
if expiry == "false":
continue
refresh = self.db.get(self.db.APPL_DB, sr, "refresh")
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(sr))
else:
self.db.delete(self.db.APPL_DB, sr)
log_debug("Static route {} deleted".format(sr))
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()

0 comments on commit 5e80ab5

Please sign in to comment.