Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to get all port related attributes from config_db #362

Merged
merged 5 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 39 additions & 11 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,50 @@
from swsssdk import ConfigDBConnector
from natsort import natsorted
from minigraph import parse_device_desc_xml
from portconfig import get_port_config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this import if you are not using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed in the new commit


import aaa
import mlnx

SONIC_CFGGEN_PATH = "sonic-cfggen"
PLATFORM_ROOT_PATH = '/usr/share/sonic/device'
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku'
PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add definitions for PLATFORM_ROOT_PATH, HWSKU_KEY and PLATFORM_KEY? They are not used anywhere in the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Joe, it is added as part of the initiall pull request. This is not necessary. Will remove the lines


#
# Helper functions
#

# Returns platform and HW SKU
def get_platform_and_hwsku():
paavaanan marked this conversation as resolved.
Show resolved Hide resolved
try:
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT)
stdout = proc.communicate()[0]
proc.wait()
platform = stdout.rstrip('\n')

proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT)
stdout = proc.communicate()[0]
proc.wait()
hwsku = stdout.rstrip('\n')
except OSError, e:
raise OSError("Cannot detect platform")

return (platform, hwsku)


# Get platform, hwsku and populate port_dict
(platform, hwsku) = get_platform_and_hwsku()
port_config = os.path.join("/usr/share/sonic/device", platform, hwsku,
"port_config.ini")
(port_dict, _) = get_port_config(platform, hwsku, port_config)
paavaanan marked this conversation as resolved.
Show resolved Hide resolved


def run_command(command, display_cmd=False, ignore_error=False):
"""Run bash command and print output to stdout
Expand All @@ -43,12 +77,9 @@ 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())

if interface_alias is not None:
if not port_dict:
click.echo("Invalid alias interface")
for port_name in natsorted(port_dict.keys()):
if interface_alias == port_dict[port_name]['alias']:
return port_name
Expand All @@ -61,12 +92,9 @@ 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())

if interface_name is not None:
if not port_dict:
click.echo("Invalid alias interface")
for port_name in natsorted(port_dict.keys()):
if interface_name == port_name:
return port_dict[port_name]['alias']
Expand Down
40 changes: 37 additions & 3 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
import sonic_platform
from swsssdk import ConfigDBConnector
from swsssdk import SonicV2Connector
from portconfig import get_port_config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this import if you are not using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed in new commit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? This was not removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in new commit!


import mlnx

PLATFORM_ROOT_PATH = '/usr/share/sonic/device'
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku'
PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add these four constants? They are not used anywhere in the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than SONIC_CFGGEN_PATH removed unused constants


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

# Returns platform and HW SKU
def get_platform_and_hwsku():
paavaanan marked this conversation as resolved.
Show resolved Hide resolved
try:
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT)
stdout = proc.communicate()[0]
proc.wait()
platform = stdout.rstrip('\n')

proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT)
stdout = proc.communicate()[0]
proc.wait()
hwsku = stdout.rstrip('\n')
except OSError, e:
raise OSError("Cannot detect platform")

return (platform, hwsku)


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())

# Get platform and hwsku
(platform, hwsku) = get_platform_and_hwsku()
port_config = os.path.join("/usr/share/sonic/device", platform, hwsku,
"port_config.ini")
(self.port_dict, _) = get_port_config(platform, hwsku, port_config)
paavaanan marked this conversation as resolved.
Show resolved Hide resolved
if not self.port_dict:
click.echo("Invalid alias interface")

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