Skip to content

Commit

Permalink
Load the database global_db only once for show cli (sonic-net#1712)
Browse files Browse the repository at this point in the history
Introduce a new function `load_db_config()`. This function will load the global database file or database config file based on the platform.
  • Loading branch information
arlakshm authored Jul 20, 2021
1 parent 0efd297 commit 9b7c58b
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 39 deletions.
15 changes: 6 additions & 9 deletions acl_loader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import tabulate
import pyangbind.lib.pybindJSON as pybindJSON
from natsort import natsorted
from sonic_py_common import device_info, multi_asic
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector, SonicDBConfig

from sonic_py_common import multi_asic
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector
from utilities_common.general import load_db_config

def info(msg):
click.echo(click.style("Info: ", fg='cyan') + click.style(str(msg), fg='green'))
Expand Down Expand Up @@ -116,11 +116,8 @@ def __init__(self):
self.rules_db_info = {}
self.rules_info = {}

if multi_asic.is_multi_asic():
# Load global db config
SonicDBConfig.load_sonic_global_db_config()
else:
SonicDBConfig.initialize()
# Load database config files
load_db_config()

self.sessions_db_info = {}
self.configdb = ConfigDBConnector()
Expand All @@ -146,7 +143,7 @@ def __init__(self):

# Getting all front asic namespace and correspding config and state DB connector

namespaces = device_info.get_all_namespaces()
namespaces = multi_asic.get_all_namespaces()
for front_asic_namespaces in namespaces['front_ns']:
self.per_npu_configdb[front_asic_namespaces] = ConfigDBConnector(use_unix_socket_path=True, namespace=front_asic_namespaces)
self.per_npu_configdb[front_asic_namespaces].connect()
Expand Down
12 changes: 5 additions & 7 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
from sonic_py_common import device_info, multi_asic
from sonic_py_common.interface import get_interface_table_name, get_port_table_name
from utilities_common import util_base
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector, SonicDBConfig
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector
from utilities_common.db import Db
from utilities_common.intf_filter import parse_interface_in_filter
from utilities_common import bgp_util
import utilities_common.cli as clicommon
from utilities_common.general import load_db_config

from .utils import log

from . import aaa
Expand Down Expand Up @@ -952,12 +954,8 @@ def config(ctx):
except (KeyError, TypeError):
raise click.Abort()

# Load the global config file database_global.json once.
num_asic = multi_asic.get_num_asics()
if num_asic > 1:
SonicDBConfig.load_sonic_global_db_config()
else:
SonicDBConfig.initialize()
# Load database config files
load_db_config()

if os.geteuid() != 0:
exit("Root privileges are required for this operation")
Expand Down
16 changes: 6 additions & 10 deletions crm/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#!/usr/bin/env python3

import click
from swsscommon.swsscommon import ConfigDBConnector, SonicDBConfig
from swsscommon.swsscommon import ConfigDBConnector
from tabulate import tabulate
from utilities_common import multi_asic as multi_asic_util
from sonic_py_common import multi_asic

from sonic_py_common import multi_asic
from utilities_common.general import load_db_config
from utilities_common import multi_asic as multi_asic_util
class Crm:
def __init__(self, db=None):
self.cli_mode = None
Expand Down Expand Up @@ -213,13 +214,8 @@ def cli(ctx):
# Use the db object if given as input.
db = None if ctx.obj is None else ctx.obj.cfgdb

# Note: SonicDBConfig may be already initialized in unit test, then skip
if not SonicDBConfig.isInit():
if multi_asic.is_multi_asic():
# Load the global config file database_global.json once.
SonicDBConfig.load_sonic_global_db_config()
else:
SonicDBConfig.initialize()
# Load database config files
load_db_config()

context = {
"crm": Crm(db)
Expand Down
3 changes: 2 additions & 1 deletion scripts/ipintutil
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ from tabulate import tabulate
from sonic_py_common import multi_asic
from swsscommon import swsscommon
from utilities_common import constants
from utilities_common.general import load_db_config
from utilities_common import multi_asic as multi_asic_util


Expand Down Expand Up @@ -262,7 +263,7 @@ def main():
else:
sys.exit("Invalid argument -a {}".format(args.address_family))

swsscommon.SonicDBConfig.load_sonic_global_db_config()
load_db_config()
ip_intfs = get_ip_intfs(af, namespace, display)
display_ip_intfs(ip_intfs)

Expand Down
11 changes: 5 additions & 6 deletions scripts/lldpshow
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import subprocess
import sys
import xml.etree.ElementTree as ET

from sonic_py_common import device_info, multi_asic
from swsscommon.swsscommon import ConfigDBConnector, SonicDBConfig
from sonic_py_common import device_info
from swsscommon.swsscommon import ConfigDBConnector
from utilities_common.general import load_db_config
from tabulate import tabulate

BACKEND_ASIC_INTERFACE_NAME_PREFIX = 'Ethernet-BP'
Expand All @@ -48,10 +49,8 @@ class Lldpshow(object):
# if further capability type is supported like WLAN, can just add the tag definition here
self.ctags = {'Router': 'R', 'Bridge': 'B'}

if multi_asic.is_multi_asic():
SonicDBConfig.load_sonic_global_db_config()
else:
SonicDBConfig.initialize()
# Load database config files
load_db_config()

# For multi-asic platforms we will get only front-panel interface to display
namespaces = device_info.get_all_namespaces()
Expand Down
10 changes: 4 additions & 6 deletions scripts/portconfig
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ try:
except KeyError:
pass

from swsscommon.swsscommon import ConfigDBConnector, SonicDBConfig, SonicV2Connector
from swsscommon.swsscommon import ConfigDBConnector, SonicV2Connector
from utilities_common.general import load_db_config

# APPL_DB constants
PORT_TABLE_NAME = "PORT"
Expand Down Expand Up @@ -246,11 +247,8 @@ def main():
help = 'port advertised interface types', default=None)
args = parser.parse_args()

if args.namespace is not None:
SonicDBConfig.load_sonic_global_db_config(namespace=args.namespace)
else:
SonicDBConfig.initialize()

# Load database config files
load_db_config()
try:
port = portconfig(args.verbose, args.port, args.namespace)
if args.list:
Expand Down
3 changes: 3 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from utilities_common import util_base
from utilities_common.db import Db
import utilities_common.constants as constants
from utilities_common.general import load_db_config

from . import acl
from . import bgp_common
Expand Down Expand Up @@ -149,6 +150,8 @@ def is_gearbox_configured():
def cli(ctx):
"""SONiC command line - 'show' command"""

# Load database config files
load_db_config()
ctx.obj = Db()


Expand Down
3 changes: 3 additions & 0 deletions utilities_common/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from natsort import natsorted
from sonic_py_common import multi_asic
from utilities_common.db import Db
from utilities_common.general import load_db_config

VLAN_SUB_INTERFACE_SEPARATOR = '.'

Expand Down Expand Up @@ -118,6 +119,8 @@ class InterfaceAliasConverter(object):

def __init__(self, db=None):

# Load database config files
load_db_config()
if db is None:
self.port_dict = multi_asic.get_port_table()
else:
Expand Down
16 changes: 16 additions & 0 deletions utilities_common/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import importlib.util
import sys

from sonic_py_common.multi_asic import is_multi_asic
from swsscommon import swsscommon

def load_module_from_source(module_name, file_path):
"""
This function will load the Python source file specified by <file_path>
Expand All @@ -15,3 +18,16 @@ def load_module_from_source(module_name, file_path):
sys.modules[module_name] = module

return module

def load_db_config():
'''
Load the correct database config file:
- database_global.json for multi asic
- database_config.json for single asic
'''
if is_multi_asic():
if not swsscommon.SonicDBConfig.isGlobalInit():
swsscommon.SonicDBConfig.load_sonic_global_db_config()
else:
if not swsscommon.SonicDBConfig.isInit():
swsscommon.SonicDBConfig.load_sonic_db_config()

0 comments on commit 9b7c58b

Please sign in to comment.