aristanetworks / cloudvision-python

Python resources and libraries for integrating with Arista's CloudVision platform
Apache License 2.0
25 stars 17 forks source link

UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 7: ordinal not in range(128) #5

Closed barryCrunch closed 2 years ago

barryCrunch commented 2 years ago

I'm trying to pull the BGP Peer Status on a particular BGP peer. When I add the peer to the pathElts I get back a 'ascii' codec can't decode error.

Python Version: 3.8.2 Cloudvision Version: cvaas

def main():
    apiserverAddr = "apiserver.arista.io:443"
    token = "./token.txt"
    pathElts = [
        "Devices",
        "SERIALNUMBER",
        "versioned-data",
        "routing",
        "bgp",
        "status",
        "vrf",
        "default",
        "bgpPeerInfoStatusEntry",
        "PEER-IP"
    ]
    query = [
        create_query([(pathElts, [])], "analytics")
    ]

    with GRPCClient(apiserverAddr, token=token, certs=None, key=None, ca=None) as client:
        # test = client.get(query)
        # from IPython import embed; embed()
        # print(test)
        for batch in client.get(query):
            for notif in batch["notifications"]:
                print(notif["updates"])
    return 0

if __name__ == "__main__":
    main()

Here is the Traceback

python bgp.py 
Traceback (most recent call last):
  File "bgp.py", line 35, in <module>
    main()
  File "bgp.py", line 28, in main
    for batch in client.get(query):
  File "/home/mbarry/.virtualenvs/pascal/lib/python3.8/site-packages/cloudvision/Connector/grpc_client/grpcClient.py", line 197, in <genexpr>
    return (self.decode_batch(nb) for nb in stream)
  File "/home/mbarry/.virtualenvs/pascal/lib/python3.8/site-packages/cloudvision/Connector/grpc_client/grpcClient.py", line 263, in decode_batch
    "notifications": [self.decode_notification(n)
  File "/home/mbarry/.virtualenvs/pascal/lib/python3.8/site-packages/cloudvision/Connector/grpc_client/grpcClient.py", line 263, in <listcomp>
    "notifications": [self.decode_notification(n)
  File "/home/mbarry/.virtualenvs/pascal/lib/python3.8/site-packages/cloudvision/Connector/grpc_client/grpcClient.py", line 272, in decode_notification
    "updates": {
  File "/home/mbarry/.virtualenvs/pascal/lib/python3.8/site-packages/cloudvision/Connector/grpc_client/grpcClient.py", line 273, in <dictcomp>
    self.decoder.decode(u.key): self.decoder.decode(u.value)
  File "/home/mbarry/.virtualenvs/pascal/lib/python3.8/site-packages/cloudvision/Connector/codec/decoder.py", line 56, in decode
    return self.__postProcess(res)
  File "/home/mbarry/.virtualenvs/pascal/lib/python3.8/site-packages/cloudvision/Connector/codec/decoder.py", line 45, in __postProcess
    return b.decode("ascii")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 7: ordinal not in range(128)

Not sure where I'm going wrong here.

barryCrunch commented 2 years ago

It looks like the issue may be the "receivedOpenMsg" and the "sentOpenMsg" passed back in the notification. It has some characters that are not able to be decoded by python's byte decode method.

receivedOpenMsg | "\u0004OȀ\u001e‘򓈢\u0002\u0006\u0001\u0004\u0000\u0001\u0000\u0001\u0002\u0002€\u0000\u0002\u0002\u0002\u0000\u0002\u0004@\u0002@x\u0002\u0006A\u0004\u0000\u0000OȂ\u0002G\u0000" |

sentOpenMsg | "\u0004ü0\u0000\u001e\nrÿE\u001a\u0002\u0018\u0001\u0004\u0000\u0001\u0000\u0001\u0002\u0000@\u0002A,A\u0004\u0000\u0000ü0E\u0004\u0000\u0001\u0001\u0001"

If you pass ignore to the decode method in decoder.py under the __postProcess method then everything passes.

    def __postProcess(self, b):
        if isinstance(b, bytes):
            return b.decode("ascii", 'ignore')
        elif isinstance(b, list):
            return self.decode_array(b)
        elif isinstance(b, (dict, FrozenDict)):
            return self.decode_map(b)
        else:
            return b