def _get_val(json_value): """Get the gNMI val for path definition. Args: json_value: (str) JSON_IETF or file. Returns: gnmi_pb2.TypedValue() """ val = gnmi_pb2.TypedValue() if '@' in json_value: try: set_json = json.loads( six.moves.builtins.open(json_value.strip('@'), 'rb').read()) except (IOError, ValueError) as e: raise JsonReadError('Error while loading JSON: %s' % str(e)) val.json_ietf_val = json.dumps(set_json) return val coerced_val = _format_type(json_value) type_to_value = { bool: 'bool_val', int: 'int_val', float: 'float_val', str: 'string_val' } if type_to_value.get(type(coerced_val)): setattr(val, type_to_value.get(type(coerced_val)), coerced_val) return val
def Set(stub: gnmi_pb2_grpc.gNMIStub, paths: gnmi_pb2.Path, username: Text, password: Text, json_value: Text, set_type: Text) -> gnmi_pb2.SetResponse: """Creates a gNMI SetRequest. Args: stub: (class) gNMI Stub used to build the secure channel. paths: gNMI Path. username: (str) Username used when building the channel. password: (str) Password used when building the channel. json_value: (str) JSON_IETF Value or file. set_type: (str) Type of gNMI SetRequest to build. Returns: a gnmi_pb2.SetResponse object representing a gNMI SetResponse. """ if json_value: # Specifying ONLY a path is possible for Delete. val = gnmi_pb2.TypedValue() val.json_ietf_val = json.dumps(json_value).encode('utf8') path_val = gnmi_pb2.Update( path=paths, val=val, ) if set_type == 'update': return stub.Set(gnmi_pb2.SetRequest(update=[path_val]), metadata=[('username', username), ('password', password)]) elif set_type == 'replace': return stub.Set(gnmi_pb2.SetRequest(replace=[path_val]), metadata=[('username', username), ('password', password)]) elif set_type == 'delete': return stub.Set(gnmi_pb2.SetRequest(delete=[paths]), metadata=[('username', username), ('password', password)])
def Set(stub, paths, username, password, json_value, set_type): """Create a gNMI SetRequest. Args: stub: (class) gNMI Stub used to build the secure channel. paths: gNMI Path. username: (str) Username used when building the channel. password: (str) Password used when building the channel. json_value: (str) JSON_IETF Value or file. set_type: (str) Type of gNMI SetRequest to build. Returns: set_request: (class) gNMI SetRequest. """ val = gnmi_pb2.TypedValue() val.json_ietf_val = json.dumps(json_value) path_val = gnmi_pb2.Update( path=paths, val=val, ) if set_type == 'update': return stub.Set(gnmi_pb2.SetRequest(update=[path_val]), metadata=[('username', username), ('password', password)]) elif set_type == 'replace': return stub.Set(gnmi_pb2.SetRequest(replace=[path_val]), metadata=[('username', username), ('password', password)]) elif set_type == 'delete': return stub.Set(gnmi_pb2.SetRequest(prefix=paths), metadata=[('username', username), ('password', password)])
def _get_val(json_value): """Get the gNMI val for path definition. Args: json_value: (str) JSON_IETF or file. Returns: gnmi_pb2.TypedValue() """ val = gnmi_pb2.TypedValue() set_json = json.loads(six.moves.builtins.open(json_value, 'rb').read()) val.json_ietf_val = json.dumps(set_json).encode() return val
def _get_val(json_value): val = gnmi_pb2.TypedValue() coerced_val = _format_type(json_value) type_to_value = { bool: 'bool_val', int: 'int_val', float: 'float_val', str: 'string_val' } value_type = type_to_value.get(type(coerced_val)) if value_type: setattr(val, value_type, coerced_val) return val