From 95ce67ac00af09c3209ea8c948b2dfadd6c880e6 Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sun, 14 Apr 2024 12:57:28 +0530 Subject: [PATCH 1/2] Added a decode function to convert.py for MAC, IPs, and numeric values. --- utils/p4runtime_lib/convert.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/utils/p4runtime_lib/convert.py b/utils/p4runtime_lib/convert.py index 2ac33c541..2ba8f72c5 100644 --- a/utils/p4runtime_lib/convert.py +++ b/utils/p4runtime_lib/convert.py @@ -102,6 +102,16 @@ def encode(x, bitwidth): assert(len(encoded_bytes) == byte_len) return encoded_bytes +def decode(enc_val): + decode_functions = [decodeIPv4, decodeMac, decodeNum, decodeIPv6] + for func in decode_functions: + try: + return func(enc_val) + except: + pass + raise Exception("Not compatible with the encoded value format") + + if __name__ == '__main__': # TODO These tests should be moved out of main eventually mac = "aa:bb:cc:dd:ee:ff" From 55ef8243c3550aa73cf22f6952e6c2962d36f53e Mon Sep 17 00:00:00 2001 From: Abhinav kumar <126642111+Abhinavcode13@users.noreply.github.com> Date: Sun, 14 Apr 2024 13:04:49 +0530 Subject: [PATCH 2/2] fixed ordering of decode func --- utils/p4runtime_lib/convert.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/utils/p4runtime_lib/convert.py b/utils/p4runtime_lib/convert.py index 2ba8f72c5..af9828583 100644 --- a/utils/p4runtime_lib/convert.py +++ b/utils/p4runtime_lib/convert.py @@ -103,7 +103,7 @@ def encode(x, bitwidth): return encoded_bytes def decode(enc_val): - decode_functions = [decodeIPv4, decodeMac, decodeNum, decodeIPv6] + decode_functions = [decodeIPv6, decodeIPv4, decodeMac, decodeNum] for func in decode_functions: try: return func(enc_val) @@ -111,7 +111,6 @@ def decode(enc_val): pass raise Exception("Not compatible with the encoded value format") - if __name__ == '__main__': # TODO These tests should be moved out of main eventually mac = "aa:bb:cc:dd:ee:ff"