Beispiel #1
0
def _set(stub, paths, set_type, username, password, json_value):
    """Create a gNMI SetRequest.

  Args:
    stub: (class) gNMI Stub used to build the secure channel.
    paths: gNMI Path
    set_type: (str) Type of gNMI SetRequest.
    username: (str) Username used when building the channel.
    password: (str) Password used when building the channel.
    json_value: (str) JSON_IETF or file.

  Returns:
    a gnmi_pb2.SetResponse object representing a gNMI SetResponse.
  """
    if json_value:  # Specifying ONLY a path is possible.
        val = _get_val(json_value)
    path_val = gnmi_pb2.Update(
        path=paths,
        val=val,
    )

    kwargs = {}
    if username:
        kwargs = {'metadata': [('username', username), ('password', password)]}
    if set_type == 'delete':
        return stub.Set(gnmi_pb2.SetRequest(delete=[paths]), **kwargs)
    elif set_type == 'update':
        return stub.Set(gnmi_pb2.SetRequest(update=[path_val]), **kwargs)
    return stub.Set(gnmi_pb2.SetRequest(replace=[path_val]), **kwargs)
Beispiel #2
0
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)])
Beispiel #3
0
def main():
    args = vars(_create_parser().parse_args())

    kwargs = {
        'root_cert': args['root_cert'],
        'cert_chain': args['cert_chain'],
        'private_key': args['private_key']
    }
    certs = _open_certs(**kwargs)
    stub = _create_stub(certs, args['endpoint'])
    paths = _parse_path(args['target'], _path_names(args['xpath']))

    mode = args['mode']
    if mode == 'get':
        response = stub.Get(
            gnmi_pb2.GetRequest(path=[paths], encoding='JSON_IETF'))
        print(response)
    elif mode == 'set':
        response = stub.Set(
            gnmi_pb2.SetRequest(update=[
                gnmi_pb2.Update(
                    path=paths,
                    val=_get_val(args['value']),
                )
            ]))
        print(response)
    elif mode == 'delete':
        response = stub.Set(gnmi_pb2.SetRequest(delete=[paths]))
        print(response)
Beispiel #4
0
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)])