Skip to content

Commit

Permalink
Added Master Arbitration support for Get and Set
Browse files Browse the repository at this point in the history
Created Extension for Master Arbitration
  • Loading branch information
sai1274 committed Feb 22, 2024
1 parent 1c0cccf commit c5bdf4c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pygnmi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def convert_encoding(self, requested_encoding: str, is_encoding_explicitly_set:
encoding = requested_encoding or self.__encoding
return Encoding.Value(encoding.upper()) # may raise ValueError

def get(self, prefix: str = "", path: list = None, target: str = None, datatype: str = "all", encoding: str = None):
def get(self, prefix: str = "", path: list = None, target: str = None, datatype: str = "all", encoding: str = None, extension: dict = None):
"""
Collecting the information about the resources from defined paths.
Expand Down Expand Up @@ -426,6 +426,7 @@ def get(self, prefix: str = "", path: list = None, target: str = None, datatype:

# Set Protobuf value for encoding
pb_encoding = self.convert_encoding(encoding)
gnmi_extension = get_gnmi_extension(ext=extension)

# Gnmi PREFIX
try:
Expand All @@ -448,7 +449,10 @@ def get(self, prefix: str = "", path: list = None, target: str = None, datatype:
raise gNMIException("Conversion of gNMI paths to the Protobuf format failed", e)

try:
gnmi_message_request = GetRequest(prefix=protobuf_prefix, path=protobuf_paths, type=pb_datatype, encoding=pb_encoding)
if gnmi_extension:
gnmi_message_request = GetRequest(prefix=protobuf_prefix, path=protobuf_paths, type=pb_datatype, encoding=pb_encoding, extension = [gnmi_extension])
else:
gnmi_message_request = GetRequest(prefix=protobuf_prefix, path=protobuf_paths, type=pb_datatype, encoding=pb_encoding)
debug_gnmi_msg(self.__debug, gnmi_message_request, "gNMI request")

gnmi_message_response = self.__stub.Get(gnmi_message_request, metadata=self.__metadata)
Expand Down Expand Up @@ -553,6 +557,7 @@ def set(
encoding: str = None,
prefix: str = "",
target: str = None,
extension: dict = None,
):
"""
Changing the configuration on the destination network elements.
Expand Down Expand Up @@ -583,6 +588,7 @@ def set(

# Set the encoding to auto-discovered value, unless overridden
encoding = encoding or self.__encoding
gnmi_extension = get_gnmi_extension(ext=extension)

# Gnmi PREFIX
try:
Expand Down Expand Up @@ -630,9 +636,12 @@ def set(
encoding=encoding,
datatype='config')

gnmi_message_request = SetRequest(
prefix=protobuf_prefix, delete=del_protobuf_paths, update=update_msg, replace=replace_msg
)
if gnmi_extension:
gnmi_message_request = SetRequest(prefix=protobuf_prefix, delete=del_protobuf_paths, update=update_msg, replace=replace_msg, extension=[gnmi_extension])
else:
gnmi_message_request = SetRequest(
prefix=protobuf_prefix, delete=del_protobuf_paths, update=update_msg, replace=replace_msg
)
debug_gnmi_msg(self.__debug, gnmi_message_request, "gNMI request")

gnmi_message_response = self.__stub.Set(gnmi_message_request, metadata=self.__metadata)
Expand Down
8 changes: 8 additions & 0 deletions pygnmi/create_gnmi_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def get_gnmi_extension(ext: dict = None) -> list:
result.history.range.start = _get_time_ns_epoch(ext["history"]["range"]["start"])
result.history.range.end = _get_time_ns_epoch(ext["history"]["range"]["end"])

if "master_arbitration" in ext:

result.master_arbitration.election_id.high = ext["master_arbitration"]["election_id"]["high"]
result.master_arbitration.election_id.low = ext["master_arbitration"]["election_id"]["low"]

if "role" in ext["master_arbitration"]:
result.master_arbitration.role.id = ext["master_arbitration"]["role"]["id"]

return result


Expand Down

0 comments on commit c5bdf4c

Please sign in to comment.