PY-SIL Library Code Structure

PY-SIL Library File structure

PY-SIL code for YANG module has the following structure:

[module_name]/
    |-- u_[module_name].py
    |-- y_[module_name].py

where:

  • [module-name] is the YANG module name

y_[module name].py

The module defines a class PYSILModule[module_name]Base that inherits from PYSILModuleBase. This is a base class responsible for callback registration and cleanup. It register all callback specified in configuration during init phase 1 and unregister them during cleanup phase.

from pysil.module import PYSILModuleBase

def __init__(self):
    config = {
        "name": "toaster",
        "revision": "2009-11-20",
        "container_name": "toaster",
        "callbacks": [],
    }

    super().__init__(config=config)

PY-SIL config dictionary

In the __init__ method of the class, a configuration dictionary is created. This dictionary contains information about the YANG module, such as its name, revision, container name, callbacks for various paths, and notifications.

  • name: "toaster" (Name of the module)

  • revision: "2009-11-20" (Module revision date)

  • container_name: "toaster" (Name of the container)

  • callbacks: [] (List of callbacks, check details here PY-SIL Callback Definition in the Config)

PY-SIL Callback Definition in the Config

callbacks parameter provides information to the base class about what callbacks should be registered:

"callbacks": [
    {"path": "/toast:toaster", "cb_edit3": "edit_toaster"},
    {
        "path": "/toast:toaster/toasterManufacturer",
        "cb_get2": "get_toasterManufacturer",
        "lvl": 3,
    },
    {
        "path": "/toast:toaster/toasterModelNumber",
        "cb_get2": "get_toasterModelNumber",
        "lvl": 3,
    },
    {
        "path": "/toast:toaster/toasterStatus",
        "cb_get2": "get_toasterStatus",
        "lvl": 3,
    },
    {
        "path": "/toast:make-toast",
        "cb_validate": "validate_make_toast",
        "cb_invoke": "invoke_make_toast",
        "input_params": [
            {
                "name": "toasterDoneness",
                "type": "uint32",
            },
            {
                "name": "toasterToastType",
                "type": "identityref",
            },
        ],
    },
    {
        "path": "/toast:cancel-toast",
        "cb_validate": "validate_cancel_toast",
        "cb_invoke": "invoke_cancel_toast",
    },
],

Common parameters for all callbacks

  • "path": "/address_get_2:addresses/address" - specifies the path to the node where the callback function get_address should be invoked.

  • "lvl": 3 Node level in the YANG tree

GET2 callback specific parameters

  • "cb_get2": "get_address" - specifies the name of the callback function for the GET2 callback that should be invoked when a request is made to the specified path. In this particular example, the function named get_address will be invoked.

EDIT3 callback specific parameters

  • "cb_edit3": "edit_toaster" - specifies the name of the callback function for the EDIT3 callback that should be invoked when a request is made to the specified path. In this particular example, the function named get_address will be invoked.

Common parameters for GET2 and EDIT3 callbacks

  • "keys": List of the keys
    "keys": [
                {
                    "name": "list-name",
                    "pname": "list-name",
                    "type": "string",
                    "lvl": 3,
                }
            ],
    
    • "name": Key name defined in YANG file

    • "pname": Corresponding Python key name

    • "type": Key type

    • "lvl": Key level in the YANG tree

RPC callback specific parameters

  • "cb_validate": "validate_cancel_toast" - specifies the name of the callback function for the RPC validate callback that should be invoked when a request is made to the specified path. In this particular example, the function named validate_cancel_toas will be invoked.

  • "cb_invoke": "invoke_cancel_toast" - specifies the name of the callback function for the RPC invoke callback that should be invoked when a request is made to the specified path. In this particular example, the function named invoke_cancel_toast will be invoked.

ACTION callback specific parameters

  • "cb_action_validate": "validate_cancel_toast" - specifies the name of the callback function for the RPC validate callback that should be invoked when a request is made to the specified path. In this particular example, the function named validate_cancel_toas will be invoked.

  • "cb_action_invoke": "invoke_cancel_toast" - specifies the name of the callback function for the RPC invoke callback that should be invoked when a request is made to the specified path. In this particular example, the function named invoke_cancel_toast will be invoked.

u_[module_name].py

The module defines a class PYSILModule[module_name] that inherits from PYSILModule[module_name]Base. Contain callback and notification functions based on YANG module.

from .y_toaster import PYSILModule[module name]Base

class PYSILModule[module name](PYSILModule[module name]Base):
    #definitions of callback functions and notifications here

Check detailed callback reference and function examples here: PY-SIL Callback Reference