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

Common functions for show CLI support on multi ASIC #999

Merged
merged 10 commits into from
Aug 14, 2020

Conversation

arlakshm
Copy link
Contributor

@arlakshm arlakshm commented Jul 20, 2020

Common functions for show CLI support on multi ASIC
Signed-off-by: Arvindsrinivasan Lakshmi Narasimhan arlakshm@microsoft.com

Depends on this PR
sonic-net/sonic-buildimage#4973

- What I did
Add a set of common function will be used to support SONiC CLIs for multi ASIC
- How I did it

- How to verify it

- Previous command output (if the output of a command-line utility has changed)
NA
- New command output (if the output of a command-line utility has changed)
NA

Signed-off-by: Arvindsrinivasan Lakshmi Narasimhan <arlakshm@microsoft.com>
@lgtm-com
Copy link

lgtm-com bot commented Jul 20, 2020

This pull request introduces 3 alerts when merging 768ab07 into 82dfe50 - view on LGTM.com

new alerts:

  • 3 for Unused import

swsssdk.SonicDBConfig.load_sonic_global_db_config()
self.current_namespace = None

def connect_dbs_for_ns(self,namespace=DEFAULT_NAMESPACE):
Copy link
Contributor

@jleveque jleveque Jul 20, 2020

Choose a reason for hiding this comment

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

I recently opened a PR to add a "sonic-py-common" library in sonic-buildimage to house common Python functions (sonic-net/sonic-buildimage#5003). It will replace sonic_device_util. I think some of these functions like this one (which are not CLI-specific) can eventually be moved there, in a multi_asic.py file. That way they can be shared by more than just sonic-utilities. This file should hold multi-ASIC functions that are specific to the CLI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jleveque I agree. I will move the common functions 'sonic-py-common' once the PRs are merged.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great! I have created an issue here to track it: #1000

@jleveque
Copy link
Contributor

Retest this please

Comment on lines 1 to 4
import subprocess
import json
import click
import functools
Copy link
Contributor

Choose a reason for hiding this comment

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

alphabetical order?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the latest commit

Comment on lines 13 to 15
import swsssdk
from swsssdk import ConfigDBConnector
import argparse
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, follow PEP8 import ordering convention:

Imports should be grouped in the following order:

Standard library imports.
Related third party imports.
Local application/library specific imports.
You should put a blank line between each group of imports.

https://www.python.org/dev/peps/pep-0008/#imports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the latest commit.

Comment on lines 17 to 22
DEFAULT_NAMESPACE = ''
DISPLAY_ALL = 'all'
DISPLAY_EXTERNAL = 'frontend'
PORT_CHANNEL_OBJ = 'PORT_CHANNEL'
PORT_OBJ = 'PORT'
BGP_NEIGH_OBJ = 'BGP_NEIGH'
Copy link
Contributor

Choose a reason for hiding this comment

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

wondering if some of these constants can potentially be used in other files. If that's the case lets create utilities_common/consts.py. It's cleaner and more readable if all constants are in one place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

add a new file constants.py with all the constants in it

Comment on lines 38 to 39
In case of multi ASIC, the default namespace in the database instance running the on the host
In case of single ASIC, the namespace has to DEFAULT_NAMESPACE
Copy link
Contributor

Choose a reason for hiding this comment

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

"is the database instance"
"has to be .."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the latest commit

'''
The function connects to the config DB for a given namespace and
returns the handle
If no namespace is provide, it will connect to the db in the
Copy link
Contributor

Choose a reason for hiding this comment

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

"...provided.."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the latest commit

Comment on lines 56 to 57
In case of multi ASIC, the default namespace in the database instance running the on the host
In case of single ASIC, the namespace has to DEFAULT_NAMESPACE
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above.. "copy - pasta :)"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the latest commit

Comment on lines 65 to 66
The function check if a CLI object is internal and returns true or false.
For single asic, this function is not applicable
Copy link
Contributor

Choose a reason for hiding this comment

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

can you also add the definition of "internal".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added in the latest commit

returns false, if the cli option is all or if it the platform is single ASIC.

'''
if not is_multi_npu():
Copy link
Contributor

Choose a reason for hiding this comment

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

perhaps this can be added in the constuctor.

self.is_multi_npu = is_multi_npu()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed

Comment on lines 99 to 100
else:
ns_list = [self.namespace_option]
Copy link
Contributor

Choose a reason for hiding this comment

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

We should validate namespace here. With this class we don't expect CLIs to get namespace list and validate the namespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added validation in the latest commit

Copy link
Contributor

@smaheshm smaheshm left a comment

Choose a reason for hiding this comment

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

Minor changes.

PORT_OBJ = 'PORT'
BGP_NEIGH_OBJ = 'BGP_NEIGH'

class MultiAsic(object):
Copy link
Contributor

Choose a reason for hiding this comment

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

This sounds like the class is for 'MultiAsic' only where as, If I understand correctly, all CLIs will use it on both single ASIC and multi ASIC platforms. Wondering if we should rename to something like 'SwitchAsic'. Either way is fine, lets hear others opinion.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree the name is a bit misleading, but I'm not sure if SwitchAsic is better. I feel like it needs a more generic name, but I can't think of anything at the moment. Maybe as more functionality is added to it we'll have a better idea of what to call it. I'm OK with saving a renaming for a future PR.


'''
@functools.wraps(func)
def wrapped_run_on_all_asics(self, *args, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

This is accessing class instance inside the decorator. Either decorator should be inside the CLI class, or we should come up with another way to run command in multiple namespaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The decorator is for a function in the CLI class.

Copy link
Contributor

@smaheshm smaheshm Jul 31, 2020

Choose a reason for hiding this comment

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

Would prefer not changing the object state outside its domain. How about adding an API inside class, and call it from here.

def connect_dbs(ns):
            self.multi_asic.current_namespace = ns
            self.db = connect_to_all_dbs_for_ns(ns)
            self.config_db = connect_config_db_for_ns(ns)

Signed-off-by: Arvindsrinivasan Lakshmi Narasimhan <arlakshm@microsoft.com>
@lgtm-com

This comment has been minimized.

utilities_common/multi_asic.py Outdated Show resolved Hide resolved
Signed-off-by: Arvindsrinivasan Lakshmi Narasimhan <arlakshm@microsoft.com>
@lgtm-com
Copy link

lgtm-com bot commented Jul 24, 2020

This pull request introduces 4 alerts when merging 15dbd2f into aa1b072 - view on LGTM.com

new alerts:

  • 3 for Unused import
  • 1 for 'import *' may pollute namespace

Signed-off-by: Arvindsrinivasan Lakshmi Narasimhan <arlakshm@microsoft.com>
Signed-off-by: Arvindsrinivasan Lakshmi Narasimhan <arlakshm@microsoft.com>
@arlakshm
Copy link
Contributor Author

arlakshm commented Aug 6, 2020

retest this please

3 similar comments
@arlakshm
Copy link
Contributor Author

arlakshm commented Aug 6, 2020

retest this please

@arlakshm
Copy link
Contributor Author

retest this please

@arlakshm
Copy link
Contributor Author

retest this please

@arlakshm
Copy link
Contributor Author

retest this please

@arlakshm
Copy link
Contributor Author

@jleveque, @lguohan, @smaheshm All review comments are addressed. Please approve if there are no other comments

lguohan
lguohan previously approved these changes Aug 12, 2020
@arlakshm arlakshm requested review from jleveque and smaheshm August 13, 2020 20:33
smaheshm
smaheshm previously approved these changes Aug 13, 2020
Copy link
Contributor

@smaheshm smaheshm left a comment

Choose a reason for hiding this comment

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

looks good.

Signed-off-by: Arvindsrinivasan Lakshmi Narasimhan <arlakshm@microsoft.com>
@arlakshm arlakshm dismissed stale reviews from smaheshm and lguohan via a47ad2f August 14, 2020 14:27
@arlakshm arlakshm requested a review from smaheshm August 14, 2020 14:28
return ns_list


def multi_asic_ns_choices():
Copy link
Contributor

Choose a reason for hiding this comment

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

This API may not be required as multi_asic.get_namespace_list() already gives the namespacelist correctly. But, it is ok for now - could remove later as well.

@arlakshm arlakshm merged commit a15b6bf into sonic-net:master Aug 14, 2020
abdosi pushed a commit that referenced this pull request Aug 16, 2020
Common changes will be used to support SONiC CLIs for multi ASIC
- New MultiAsic class  to support not displaying of internal object 
- Common CLI options which needed for multi ASIC platforms
- a new decorator to execute a function on all namespaces

Signed-off-by: Arvindsrinivasan Lakshmi Narasimhan <arlakshm@microsoft.com>
stepanblyschak pushed a commit to stepanblyschak/sonic-utilities that referenced this pull request Apr 28, 2022
 Common functions for show CLI support on multi ASIC (sonic-net#999)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants