PyangBind generates bindings for RPCs that are specified within a YANG module. The assumption is made that an RPC is not bound to any particular location within the data tree (the YANG 1.1 action statement is intended to meet this requirement). To this end, bindings are generated within a module named <yang module name>_rpcs.

All RPC bindings have the property register_paths set to False. This results in them never using a path_helper object that is handed to them for register() or unregister() purposes. A path_helper class will still be used to resolve leafref values (and other XPATH expressions) if required.

An class generated for an RPC has two member containers - input and output as per the specification provided in RFC6020. The corresponding data definitions are within these two elements (which act as per YANG containers).


Example RPC

An example simple RPC could be defined as:

 1    rpc test {
 2        input {
 3            container input-container {
 4                leaf argument-one {
 5                    type string;
 6                }
 8                leaf argument-two {
 9                    type uint8;
10                }
11            }
12        }
14        output {
15            leaf response-id {
16                type uint32;
17            }
19            list elements {
20                leaf response-value {
21                    type string;
22                }
23            }
25        }
26    }

In this definition, the RPC test has an input that takes a container with two arguments (argument-one and argument-two) specified within it. It outputs an object that has a single response-id and a list of elements wtihin the reply. This list is not keyed (RPC outputs are defined to be config false).

Generating an RPC Input

To generate an input for an RPC, the input container can be directly imported. If the example module above is generated with --split-class-dir into a module directory named rbindings then, for example:

1from rbindings.simple_rpc_rpc.test.input import input

The input class can be instantiated and populated as per any other PyangBind class that represents a container:

1rpc_input = input()
2rpc_input.input_container.argument_one = "test_call"
3rpc_input.input_container.argument_two = 32

The object generated can be serialised to IETF JSON as per any other container using dumps from pyangbind.lib.pybindJSON:

1>> print(dumps(rpc_input, mode="ietf"))
3    "simple_rpc:input-container": {
4        "argument-two": 32, 
5        "argument-one": "test_call"
6    }

Parsing an RPC Output

In a similar manner, an RPC output can be read back into the corresponding output class using the standard deserialisation functionality in PyangBind. For example:

1from rbindings.simple_rpc_rpc.test.output import output
2rpc_output = output()
3fn = os.path.join("json", "rpc-output.json")
4json_obj = json.load(open(fn, 'r'))
5pybindJSONDecoder.load_ietf_json(json_obj, None, None, obj=rpc_output)

The output class can then be manipulated and/or read in Python:

1>>> print(rpc_output.response_id)

Example Code

The RPC example here can be found in docs/example/simple-rpc.