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

[config][generic-update] Implementing patch sorting #1599

Merged
merged 4 commits into from
Aug 10, 2021

Conversation

ghooo
Copy link
Contributor

@ghooo ghooo commented May 6, 2021

What I did

Implemented JSON Patch Ordering using YANG Models Design Doc

How I did it

How to verify it

Unit-Tests

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

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

@lgtm-com
Copy link

lgtm-com bot commented May 6, 2021

This pull request introduces 10 alerts when merging ba2b1d6bf6a1ddf011327887a697d89b778e1dea into 9a88cb6 - view on LGTM.com

new alerts:

  • 4 for Unused import
  • 3 for Unused local variable
  • 2 for Testing equality to None
  • 1 for Inconsistent equality and hashing

@lgtm-com
Copy link

lgtm-com bot commented May 17, 2021

This pull request introduces 10 alerts when merging 97bc24c4c75dc1b37ecf7bd9d79337c71aba7c08 into ad801bf - view on LGTM.com

new alerts:

  • 5 for Unused import
  • 2 for Testing equality to None
  • 2 for Unused local variable
  • 1 for Inconsistent equality and hashing

@qiluo-msft
Copy link
Contributor

qiluo-msft commented May 17, 2021

Please fix LGTM alerts first.


In reply to: 842690595


In reply to: 842690595

@lgtm-com
Copy link

lgtm-com bot commented May 18, 2021

This pull request introduces 1 alert when merging d61839670813af4455476e788c10fa70280cca82 into ad801bf - view on LGTM.com

new alerts:

  • 1 for Unused import

@ghooo ghooo force-pushed the dev/mghoneim/patch_sorter branch 3 times, most recently from 2cad759 to c20679f Compare May 20, 2021 11:10
from enum import Enum
from .gu_common import OperationWrapper, OperationType, GenericConfigUpdaterError, JsonChange, PathAddressing

# from gu_common import OperationWrapper, OperationType, GenericConfigUpdaterError, JsonChange, PathAddressing
Copy link
Contributor

@qiluo-msft qiluo-msft May 21, 2021

Choose a reason for hiding this comment

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

Remove unused code or comments. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

def __hash__(self):
cc = json.dumps(self.current_config, sort_keys=True)
tc = json.dumps(self.target_config, sort_keys=True)
return hash(cc) ^ hash(tc)
Copy link
Contributor

@qiluo-msft qiluo-msft May 21, 2021

Choose a reason for hiding this comment

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

hash(cc) ^ hash(tc)

How about hash ((cc, tc))?
The benefit is swapping input will result in different value. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good idea. updated

import generic_config_updater.gu_common as gu_common

# from gutest_helpers import create_side_effect_dict, Files
# import sys
# sys.path.insert(0,'../../generic_config_updater')
# import gu_common
Copy link
Contributor

@qiluo-msft qiluo-msft May 21, 2021

Choose a reason for hiding this comment

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

remove unused code. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

tokens = path.split(PathAddressing.PATH_SEPARATOR)[1:]

# Because the characters '~' (%x7E) and '/' (%x2F) have special
# meanings in JSON Pointer, '~' needs to be encoded as '~0' and '/'
Copy link
Contributor

@qiluo-msft qiluo-msft May 26, 2021

Choose a reason for hiding this comment

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

JSON Pointer

# meanings in JSON Pointer, '~' needs to be encoded as '~0' and '/'


Did you try https://pypi.org/project/jsonpointer/ ? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

still looking into this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated


def _is_leaf_node(self, node):
schema = node.schema()
return ly.LYS_LEAF == schema.nodetype() # or ly.LYS_LEAFLIST == schema.nodetype()
Copy link
Contributor

@qiluo-msft qiluo-msft May 26, 2021

Choose a reason for hiding this comment

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

or ly.LYS_LEAFLIST == schema.nodetype()

return ly.LYS_LEAF == schema.nodetype() # or ly.LYS_LEAFLIST == schema.nodetype()


Remove unused code? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

return set(ref_paths)

def _get_inner_leaf_xpaths(self, xpath, sy):
if xpath=="/": # Point to Root element which contains all xpaths
Copy link
Contributor

@qiluo-msft qiluo-msft May 26, 2021

Choose a reason for hiding this comment

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

==

if xpath=="/": # Point to Root element which contains all xpaths


Add blanks before and after == #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

return ly.LYS_LEAF == schema.nodetype() # or ly.LYS_LEAFLIST == schema.nodetype()

# TODO: YANG SONiC lib has this function but with a bug in handling null backlinks.
# It is OK to have null backlinks if there is no backlinks.
Copy link
Contributor

@qiluo-msft qiluo-msft May 26, 2021

Choose a reason for hiding this comment

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

# It is OK to have null backlinks if there is no backlinks.


Could you create a Github issue or submit a PR if you already know how to fix? #Closed

Copy link
Contributor Author

@ghooo ghooo Jun 22, 2021

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

NOTE: unit-tests in the current PR (patch-sorting) will fail until the PR submitted above is completed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

created PR on forked repo: sonic-net/sonic-buildimage#8187

node = sy.root
try:
data_node = sy._find_data_node(data_xpath)
except Exception as e:
Copy link
Contributor

@qiluo-msft qiluo-msft May 26, 2021

Choose a reason for hiding this comment

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

Exception

except Exception as e:


Could you use specific exception type? Here you swallow all the exception types and lose the exception context (like stack). #Closed

Copy link
Contributor Author

@ghooo ghooo Jun 22, 2021

Choose a reason for hiding this comment

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

code removed locally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

code removed

casted = data_set.subtype()
if value == casted.value_str():
ref_list.append(data_set.path())
except Exception as e:
Copy link
Contributor

@qiluo-msft qiluo-msft May 26, 2021

Choose a reason for hiding this comment

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

Exception

except Exception as e:


Could you use specific exception type? Seems you know the exception is related to "Failed to find node or dependencies" #Closed

Copy link
Contributor Author

@ghooo ghooo Jun 22, 2021

Choose a reason for hiding this comment

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

code removed locally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

code removed

# src: https://tools.ietf.org/html/rfc6901
mod_tokens = []
for token in tokens:
mod_token = str(token)
Copy link
Contributor

@qiluo-msft qiluo-msft Jun 6, 2021

Choose a reason for hiding this comment

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

str

mod_token = str(token)


Why you need to str convert? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, updated locally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

return [token]
list_config = config[token]
value=list_config[int(path_tokens[token_index+1])]
return [f"{token}[.='{value}']"]
Copy link
Contributor

@qiluo-msft qiluo-msft Jun 6, 2021

Choose a reason for hiding this comment

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

{token}[.='{value}

It's difficult to read, could you add some comment? Is the . a mistake? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To get a leaf-list instance with the value 'val'
/module-name:container/leaf-list[.='val']

Source: Check examples in https://netopeer.liberouter.org/doc/libyang/master/html/howto_x_path.html

Added a comment to explain this '.' locally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

clist = model.get('list')
# Container contains a single list, just return it
# TODO: check if matching also by name is necessary
if isinstance(clist, dict): # and clist['@name'] == token+"_LIST":
Copy link
Contributor

@qiluo-msft qiluo-msft Jun 6, 2021

Choose a reason for hiding this comment

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

and clist['@name'] == token+"_LIST":

if isinstance(clist, dict): # and clist['@name'] == token+"_LIST":


Remove unused code? #Closed

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

if len(xpath_tokens)-1 == token_index:
return path_tokens

nxt_token = xpath_tokens[token_index+1]
Copy link
Contributor

@qiluo-msft qiluo-msft Jun 6, 2021

Choose a reason for hiding this comment

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

nxt_token

nxt_token = xpath_tokens[token_index+1]


Do you mean next_token? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

"""
def __init__(self, diff, op_type, current_config_tokens, target_config_tokens=None):
operation = JsonMove._to_jsonpatch_operation(diff, op_type, current_config_tokens, target_config_tokens)
self.patch=jsonpatch.JsonPatch([operation])
Copy link
Contributor

@qiluo-msft qiluo-msft Jun 6, 2021

Choose a reason for hiding this comment

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

=

self.patch=jsonpatch.JsonPatch([operation])


Add a blank before and after operators. #Closed

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

return False

def __hash__(self):
return hash(self.op_type) ^ hash(self.path) ^ hash(json.dumps(self.value))
Copy link
Contributor

@qiluo-msft qiluo-msft Jun 6, 2021

Choose a reason for hiding this comment

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

hash(self.op_type) ^ hash(self.path) ^ hash(json.dumps(self.value))

return hash(self.op_type) ^ hash(self.path) ^ hash(json.dumps(self.value))


hash((a, b, c)) ? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

Copy link
Contributor

@qiluo-msft qiluo-msft left a comment

Choose a reason for hiding this comment

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

As comments

@ghooo ghooo force-pushed the dev/mghoneim/patch_sorter branch from d3d04d2 to c554f6d Compare July 17, 2021 10:38
@ghooo ghooo merged commit 920bb87 into sonic-net:master Aug 10, 2021
vivekrnv added a commit to vivekrnv/sonic-utilities that referenced this pull request Aug 14, 2021
lguohan added a commit that referenced this pull request Aug 17, 2021
lguohan added a commit that referenced this pull request Aug 17, 2021
@lguohan
Copy link
Contributor

lguohan commented Aug 17, 2021

@ghooo , please note since this pr is causing unit testing failure, it has been reverted. Please check the error here. https://dev.azure.com/mssonic/build/_build/results?buildId=29578&view=logs&j=cef3d8a9-152e-5193-620b-567dc18af272&t=359769c4-8b5e-5976-a793-85da132e0a6f&s=ff05ad62-bb9a-53b6-ce9f-72f329a63e7c

please check attempt #1

vivekrnv added a commit to vivekrnv/sonic-utilities that referenced this pull request Aug 19, 2021
qiluo-msft pushed a commit that referenced this pull request Sep 2, 2021
)

#### What I did
Implemented [JSON Patch Ordering using YANG Models Design Doc](https://github.com/Azure/SONiC/blob/master/doc/config-generic-update-rollback/Json_Patch_Ordering_using_YANG_Models_Design.md)

#### How to verify it
Unit-Tests

**NOTE: The code in this PR was [reverted](github.com/Azure/sonic-utilities/commit/0a145e8027380e8d4decb36bdfc647062c722612) before because of some [build issues](#1761). Build issues have been fixed [here](sonic-net/sonic-buildimage#8632). To check the original PR comments please go [here](#1599
malletvapid23 added a commit to malletvapid23/Sonic-Utility that referenced this pull request Aug 3, 2023
…794)

#### What I did
Implemented [JSON Patch Ordering using YANG Models Design Doc](https://github.com/Azure/SONiC/blob/master/doc/config-generic-update-rollback/Json_Patch_Ordering_using_YANG_Models_Design.md)

#### How to verify it
Unit-Tests

**NOTE: The code in this PR was [reverted](github.com/Azure/sonic-utilities/commit/0a145e8027380e8d4decb36bdfc647062c722612) before because of some [build issues](sonic-net/sonic-utilities#1761). Build issues have been fixed [here](sonic-net/sonic-buildimage#8632). To check the original PR comments please go [here](sonic-net/sonic-utilities#1599
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants