akarneliuk / pygnmi

The pure Python implementation of the gNMI client.
https://training.karneliuk.com
BSD 3-Clause "New" or "Revised" License
129 stars 44 forks source link

telemetryParser raises an exception, if leaflist_val.element is a TypedValue #160

Open jjochum opened 1 month ago

jjochum commented 1 month ago

When querying a whole subtree, the telemetryParser fails to parse the response from our gNMI server. The update_msg contains a leaflist_val whose elements are TypedValues instead of the expected string.

Proposal for a fix in client.py:1356 (not the cleanest of fixes unfortunately)

    element_str = ""
    element_dict = {}
    for element in val_leaflist.leaflist_val.element:
        if isinstance(element, TypedValue):
            if "json_val" in element:
                element_dict.update(json.loads(element.json_val))
            elif "json_ietf_val" in element:
                element_dict.update(json.loads(element.json_ietf_val))
            else:
                raise TypeError(f"Neither json_val nor json_ietf_val found in element.")
        elif isinstance(element, str):
            element_str += element
        else:
            raise TypeError("Unsupported element type")
    if element_str != "":
        update_container.update({"val": element_str})
    else:
        update_container.update({"val": element_dict})

This either creates a dictionary containing all (parsed) TypedValues or appends to the string, with the string taking precedence over the dict in the final update call.

akarneliuk commented 1 month ago

Hey @jjochum , Thanks for reaching out. That's a good shout, would you raise a PR? Best, Anton