Skip to content

Commit

Permalink
Fix to get all port related attributes from config_db (sonic-net#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
paavaanan authored and jleveque committed Nov 13, 2018
1 parent c25fd0a commit ac67208
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
24 changes: 13 additions & 11 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import aaa
import mlnx

SONIC_CFGGEN_PATH = "sonic-cfggen"
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'

#
# Helper functions
Expand All @@ -42,13 +42,14 @@ def run_command(command, display_cmd=False, ignore_error=False):
def interface_alias_to_name(interface_alias):
"""Return default interface name if alias name is given as argument
"""

cmd = 'sonic-cfggen -d --var-json "PORT"'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

port_dict = json.loads(p.stdout.read())
config_db = ConfigDBConnector()
config_db.connect()
port_dict = config_db.get_table('PORT')

if interface_alias is not None:
if not port_dict:
click.echo("port_dict is None!")
raise click.Abort()
for port_name in natsorted(port_dict.keys()):
if interface_alias == port_dict[port_name]['alias']:
return port_name
Expand All @@ -60,13 +61,14 @@ def interface_alias_to_name(interface_alias):
def interface_name_to_alias(interface_name):
"""Return alias interface name if default name is given as argument
"""

cmd = 'sonic-cfggen -d --var-json "PORT"'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)

port_dict = json.loads(p.stdout.read())
config_db = ConfigDBConnector()
config_db.connect()
port_dict = config_db.get_table('PORT')

if interface_name is not None:
if not port_dict:
click.echo("port_dict is None!")
raise click.Abort()
for port_name in natsorted(port_dict.keys()):
if interface_name == port_name:
return port_dict[port_name]['alias']
Expand Down
14 changes: 10 additions & 4 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import mlnx

SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'

try:
# noinspection PyPep8Naming
import ConfigParser as configparser
Expand All @@ -43,15 +45,19 @@ def read_config(self, filename):
except configparser.NoSectionError:
pass


class InterfaceAliasConverter(object):
"""Class which handles conversion between interface name and alias"""

def __init__(self):
self.alias_max_length = 0
cmd = 'sonic-cfggen -d --var-json "PORT"'
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
self.port_dict = json.loads(p.stdout.read())

config_db = ConfigDBConnector()
config_db.connect()
self.port_dict = config_db.get_table('PORT')

if not self.port_dict:
click.echo("port_dict is None!")
raise click.Abort()

for port_name in self.port_dict.keys():
if self.alias_max_length < len(
Expand Down

0 comments on commit ac67208

Please sign in to comment.