nornir-netconf


Namenornir-netconf JSON
Version 2.0.0 PyPI version JSON
download
home_pagehttps://github.com/h4ndzdatm0ld/nornir_netconf
SummaryNETCONF plugin for Nornir
upload_time2023-03-15 06:45:03
maintainer
docs_urlNone
authorHugo Tinoco
requires_python>=3.8,<4.0
licenseApache-2.0
keywords nornir netconf ncclient
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # Nornir NETCONF

[![codecov](https://codecov.io/gh/h4ndzdatm0ld/nornir_netconf/branch/develop/graph/badge.svg?token=MRI39YHOOR)](https://codecov.io/gh/h4ndzdatm0ld/nornir_netconf) [![CI](https://github.com/h4ndzdatm0ld/nornir_netconf/actions/workflows/ci.yml/badge.svg)](https://github.com/h4ndzdatm0ld/nornir_netconf/actions/workflows/ci.yml)

Collection of NETCONF tasks and connection plugin for [Nornir](https://github.com/nornir-automation/nornir)

## Installation

---

```bash
pip install nornir_netconf
```

## Plugins

---

### Connections

---

- **netconf** - Connect to network devices using [ncclient](https://github.com/ncclient/ncclient)

### Tasks

---

- **netconf_capabilities** - Return server capabilities from target -> `Result.result -> RpcResult`
- **netconf_commit** - Commits a change -> `Result.result -> RpcResult`
- **netconf_edit_config** - Edits configuration on specified datastore (default="running") -> `Result.result -> RpcResult`
- **netconf_get** - Returns state data based on the supplied xpath -> `Result.result -> RpcResult`
- **netconf_get_config** - Returns configuration from specified configuration store (default="running") -> `Result.result -> RpcResult`
- **netconf_get_schemas** - Retrieves schemas and saves aggregates content into a directory with schema output -> `Result.result -> SchemaResult`
- **netconf_lock** - Locks or Unlocks a specified datastore (default="lock") -> `Result.result -> RpcResult`
- **netconf_validate** - Validates configuration datastore. Requires the `validate` capability. -> `Result.result -> RpcResult`

## Response Result

The goal of the task results is to put the NETCONF RPC-reply back in your hands. In most cases, the Nornir `Result.result` attribute will return back a `dataclass` depending on the task operation. It's important that you understand the object you will be working with. Please see the `dataclasses` section below and review the code if you want to see what attributes to expect.

### Dataclasses

> Defined in `nornir_netconf/plugins/helpers/models.py`

- `RpcResult` -> This will return an attribute of `rpc` and `manager`. You will encounter this object in most Nornir `Results` as the return value to the `result` attribute. NETCONF / XML payloads can be overwhelming, especially with large configurations and it's just not efficient or useful to display thousands of lines of code in any result.
- `SchemaResult` -> An aggregation of interesting information when grabbing schemas from NETCONF servers.

## Global Lock

The `netconf_lock` task will always return the Manager object, which is the established (and locked) agent used to send RPC's back and forth. The idea of retrieving the Manager is to carry this established locked session from task to task and only lock and unlock once during a run of tasks. Please review the examples below to see how to extract the manager and store it under the `task.host` dictionary as a variable that can be used across multiple tasks. The Manager is passed into other tasks and re-used to send RPCs to the remote server.

## Examples

Head over to the [Examples directory](https://github.com/h4ndzdatm0ld/nornir_netconf/tree/develop/examples) if you'd like to review the files.

<details><summary>Directory Structure</summary>

```bash
├── example-project
│   ├── config.yml
│   ├── inventory
│   │   ├── groups.yml
│   │   ├── hosts-local.yml
│   │   └── ssh_config
│   ├── logs
│   │   └── nornir.log
│   └── nr-get-config.py
└── README.md
```

</details>

<details><summary>Netconf Connection Plugin</summary>

Below is the snippet of a host inside the host-local.yml file and its associated group, `sros`.

```yaml
nokia_rtr:
  hostname: "192.168.1.205"
  port: 830
  groups:
    - "sros"
```

```yaml
sros:
  username: "netconf"
  password: "NCadmin123"
  port: 830
  platform: "sros"
  connection_options:
    netconf:
      extras:
        hostkey_verify: false
        timeout: 300
        allow_agent: false
        look_for_keys: false
```

</details>

<details><summary>Task: Get Config</summary>

```python
    """Nornir NETCONF Example Task: 'get-config'."""
    from nornir import InitNornir
    from nornir.core.task import Task
    from nornir_utils.plugins.functions import print_result

    from nornir_netconf.plugins.tasks import netconf_get_config

    __author__ = "Hugo Tinoco"
    __email__ = "hugotinoco@icloud.com"

    nr = InitNornir("config.yml")

    # Filter the hosts by 'west-region' assignment
    west_region = nr.filter(region="west-region")


    def example_netconf_get_config(task: Task) -> str:
        """Test get config."""
        config = task.run(
            netconf_get_config,
            source="running",
            path="""
            <configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf">
                <router>
                    <router-name>Base</router-name>
                </router>
            </configure>
            """,
            filter_type="subtree",
        )
        return config.result.rpc.data_xml


    def main():
        """Execute Nornir Script."""
        print_result(west_region.run(task=example_netconf_get_config))


    if __name__ == "__main__":
        main()
```

This returns the following

```bash
    vvvv example_netconf_get_config ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
    <?xml version="1.0" encoding="UTF-8"?><rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:918bf169-523f-4bb0-b00c-c97c01a48ecd">
        <data>
            <configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf">
                <router>
                    <router-name>Base</router-name>
                    <interface>
                        <interface-name>L3-OAM-eNodeB069420-X1</interface-name>
                        <admin-state>disable</admin-state>
                        <ingress-stats>false</ingress-stats>
                    </interface>
                </router>
            </configure>
        </data>
    </rpc-reply>
    ---- netconf_get_config ** changed : False ------------------------------------- INFO
    RpcResult(rpc=<ncclient.xml_.NCElement object at 0x7f4b1e08a440>)
    ^^^^ END example_netconf_get_config ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    (nornir-netconf-Ky5gYI2O-py3.10) ➜  example-project git:(feature/validate-tasks) ✗ 
```

</details>

<details><summary>Task: Get Capabilities</summary>

```python
    """Nornir NETCONF Example Task: 'capabilities'."""
    from nornir import InitNornir
    from nornir.core.task import Task
    from nornir_utils.plugins.functions import print_result

    from nornir_netconf.plugins.tasks import netconf_capabilities

    __author__ = "Hugo Tinoco"
    __email__ = "hugotinoco@icloud.com"

    nr = InitNornir("config.yml")

    # Filter the hosts by 'west-region' assignment
    west_region = nr.filter(region="west-region")


    def example_netconf_get_capabilities(task: Task) -> str:
        """Test get capabilities."""
        capabilities = task.run(netconf_capabilities)
        # This may be a lot, so for example we'll just print the first one
        return [cap for cap in capabilities.result.rpc][0]


    def main():
        """Execute Nornir Script."""
        print_result(west_region.run(task=example_netconf_get_capabilities))


    if __name__ == "__main__":
        main()
```

This returns the following

```bash
    (nornir-netconf-Ky5gYI2O-py3.10) ➜  example-project git:(feature/validate-tasks) ✗ python3 nr_get_capabilities.py 
    example_netconf_get_capabilities************************************************
    * nokia_rtr ** changed : False *************************************************
    vvvv example_netconf_get_capabilities ** changed : False vvvvvvvvvvvvvvvvvvvvvvv INFO
    urn:ietf:params:netconf:base:1.0
    ---- netconf_capabilities ** changed : False ----------------------------------- INFO
    RpcResult(rpc=<dict_keyiterator object at 0x7f7111328c70>)
    ^^^^ END example_netconf_get_capabilities ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    (nornir-netconf-Ky5gYI2O-py3.10) ➜  example-project git:(feature/validate-tasks) ✗ 
```

</details>

<details><summary>Task: Edit-Config with Global Lock</summary>

```python
    """Nornir NETCONF Example Task: 'edit-config', 'netconf_lock'."""
    from nornir import InitNornir
    from nornir_utils.plugins.functions import print_result
    from nornir_netconf.plugins.tasks import netconf_edit_config, netconf_lock, netconf_commit


    __author__ = "Hugo Tinoco"
    __email__ = "hugotinoco@icloud.com"

    nr = InitNornir("config.yml")

    # Filter the hosts by 'west-region' assignment
    west_region = nr.filter(region="west-region")


    def example_global_lock(task):
        """Test global lock operation of 'candidate' datastore."""
        lock = task.run(netconf_lock, datastore="candidate", operation="lock")
        # Retrieve the Manager(agent) from lock operation and store for further
        # operations.
        task.host["manager"] = lock.result.manager


    def example_edit_config(task):
        """Test edit-config with global lock using manager agent."""

        config_payload = """
        <config>
            <configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf">
                <router>
                    <router-name>Base</router-name>
                    <interface>
                        <interface-name>L3-OAM-eNodeB069420-X1</interface-name>
                        <admin-state>disable</admin-state>
                        <ingress-stats>false</ingress-stats>
                    </interface>
                </router>
            </configure>
        </config>
        """

        result = task.run(
            netconf_edit_config, config=config_payload, target="candidate", manager=task.host["manager"]
        )
        # Validate configuration
        task.run(netconf_validate)
        # Commit
        task.run(netconf_commit, manager=task.host["manager"])

    def example_unlock(task):
        """Unlock candidate datastore."""
        task.run(netconf_lock, datastore="candidate", operation="unlock", manager=task.host["manager"])


    def main():
        """Execute Nornir Script."""
        print_result(west_region.run(task=example_global_lock))
        print_result(west_region.run(task=example_edit_config))
        print_result(west_region.run(task=example_unlock))


    if __name__ == "__main__":
        main()

```

</details>

<details><summary>Task: Get Schemas</summary>

```python
    """Get Schemas from NETCONF device."""
    from nornir import InitNornir
    from nornir.core import Task
    from nornir.core.task import Result
    from nornir_utils.plugins.functions import print_result

    from nornir_netconf.plugins.tasks import netconf_get, netconf_get_schemas
    from tests.conftest import xml_dict

    __author__ = "Hugo Tinoco"
    __email__ = "hugotinoco@icloud.com"

    nr = InitNornir("config.yml")


    # Filter the hosts by 'west-region' assignment
    west_region = nr.filter(region="west-region")

    SCHEMA_FILTER = """
    <netconf-state xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring">
        <schemas>
        </schemas>
    </netconf-state>
    """


    def example_task_get_schemas(task: Task) -> Result:
        """Get Schemas from NETCONF device."""
        result = task.run(netconf_get, path=SCHEMA_FILTER, filter_type="subtree")
        # xml_dict is a custom function to convert XML to Python dictionary. Not part of Nornir Plugin.
        # See the code example if you want to use it.
        parsed = xml_dict(result.result.rpc.data_xml)
        first_schema = parsed["rpc-reply"]["data"]["netconf-state"]["schemas"]["schema"][0]
        return task.run(netconf_get_schemas, schemas=[first_schema["identifier"]], schema_path="./output/schemas")


    def main():
        """Execute Nornir Script."""
        print_result(west_region.run(task=example_task_get_schemas))


    if __name__ == "__main__":
        main()

```

This returns the following

```bash
    (nornir-netconf-Ky5gYI2O-py3.10) ➜  example-project git:(feature/validate-tasks) ✗ python3 nr_get_schemas.py 
    example_task_get_schemas********************************************************
    * nokia_rtr ** changed : False *************************************************
    vvvv example_task_get_schemas ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
    ---- netconf_get ** changed : False -------------------------------------------- INFO
    RpcResult(rpc=<ncclient.xml_.NCElement object at 0x7f36391540d0>)
    ---- netconf_get_schemas ** changed : False ------------------------------------ INFO
    SchemaResult(directory='./output/schemas')
    ^^^^ END example_task_get_schemas ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

</details>

## Additional Documentation

- [NCClient](https://ncclient.readthedocs.io/en/latest/)

## Contributions

> Github actions spins up a Containerlab instance to do full integration tests once linting has been satisfied.

---

No line of code shall go untested! Any contribution will need to be accounted for by the coverage report and satisfy all linting.

Linters:

- Ruff (Flake8/Pydocstyle)
- Black
- Yamllint
- Pylint
- Bandit
- MyPy

## Testing

To test within a local docker environment

```bash
git clone https://github.com/h4ndzdatm0ld/nornir_netconf
```

```bash
docker-compose build && docker-compose run test
```

To test locally with pytest

If you'd like to run integration tests with ContainerLab

```bash
export SKIP_INTEGRATION_TESTS=False
```

```bash
docker-compose up -d
```

```bash
poetry install && poetry shell
```

```bash
pytest --cov=nornir_netconf --color=yes --disable-pytest-warnings -vvv
```

### Integration Tests

Devices with full integration tests with ContainerLab

- Nokia SROS - TiMOS-B-21.2.R1
- Cisco IOSxR - Cisco IOS XR Software, Version 6.1.3
- Cisco IOSXE - Cisco IOS XE Software, Version 17.03.02
- Arista CEOS - 4.28.0F-26924507.4280F (engineering build)

## Documentation

Documentation is generated with Sphinx and hosted with Github Pages. [Documentation](https://h4ndzdatm0ld.github.io/nornir_netconf/)


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/h4ndzdatm0ld/nornir_netconf",
    "name": "nornir-netconf",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "nornir,netconf,ncclient",
    "author": "Hugo Tinoco",
    "author_email": "hugotinoco@icloud.com",
    "download_url": "https://files.pythonhosted.org/packages/ee/44/1efd2f7ab801b1f4be4fa3c4b8f509a8667c71a43817409fbf47f20d1c1a/nornir_netconf-2.0.0.tar.gz",
    "platform": null,
    "description": "# Nornir NETCONF\n\n[![codecov](https://codecov.io/gh/h4ndzdatm0ld/nornir_netconf/branch/develop/graph/badge.svg?token=MRI39YHOOR)](https://codecov.io/gh/h4ndzdatm0ld/nornir_netconf) [![CI](https://github.com/h4ndzdatm0ld/nornir_netconf/actions/workflows/ci.yml/badge.svg)](https://github.com/h4ndzdatm0ld/nornir_netconf/actions/workflows/ci.yml)\n\nCollection of NETCONF tasks and connection plugin for [Nornir](https://github.com/nornir-automation/nornir)\n\n## Installation\n\n---\n\n```bash\npip install nornir_netconf\n```\n\n## Plugins\n\n---\n\n### Connections\n\n---\n\n- **netconf** - Connect to network devices using [ncclient](https://github.com/ncclient/ncclient)\n\n### Tasks\n\n---\n\n- **netconf_capabilities** - Return server capabilities from target -> `Result.result -> RpcResult`\n- **netconf_commit** - Commits a change -> `Result.result -> RpcResult`\n- **netconf_edit_config** - Edits configuration on specified datastore (default=\"running\") -> `Result.result -> RpcResult`\n- **netconf_get** - Returns state data based on the supplied xpath -> `Result.result -> RpcResult`\n- **netconf_get_config** - Returns configuration from specified configuration store (default=\"running\") -> `Result.result -> RpcResult`\n- **netconf_get_schemas** - Retrieves schemas and saves aggregates content into a directory with schema output -> `Result.result -> SchemaResult`\n- **netconf_lock** - Locks or Unlocks a specified datastore (default=\"lock\") -> `Result.result -> RpcResult`\n- **netconf_validate** - Validates configuration datastore. Requires the `validate` capability. -> `Result.result -> RpcResult`\n\n## Response Result\n\nThe goal of the task results is to put the NETCONF RPC-reply back in your hands. In most cases, the Nornir `Result.result` attribute will return back a `dataclass` depending on the task operation. It's important that you understand the object you will be working with. Please see the `dataclasses` section below and review the code if you want to see what attributes to expect.\n\n### Dataclasses\n\n> Defined in `nornir_netconf/plugins/helpers/models.py`\n\n- `RpcResult` -> This will return an attribute of `rpc` and `manager`. You will encounter this object in most Nornir `Results` as the return value to the `result` attribute. NETCONF / XML payloads can be overwhelming, especially with large configurations and it's just not efficient or useful to display thousands of lines of code in any result.\n- `SchemaResult` -> An aggregation of interesting information when grabbing schemas from NETCONF servers.\n\n## Global Lock\n\nThe `netconf_lock` task will always return the Manager object, which is the established (and locked) agent used to send RPC's back and forth. The idea of retrieving the Manager is to carry this established locked session from task to task and only lock and unlock once during a run of tasks. Please review the examples below to see how to extract the manager and store it under the `task.host` dictionary as a variable that can be used across multiple tasks. The Manager is passed into other tasks and re-used to send RPCs to the remote server.\n\n## Examples\n\nHead over to the [Examples directory](https://github.com/h4ndzdatm0ld/nornir_netconf/tree/develop/examples) if you'd like to review the files.\n\n<details><summary>Directory Structure</summary>\n\n```bash\n\u251c\u2500\u2500 example-project\n\u2502   \u251c\u2500\u2500 config.yml\n\u2502   \u251c\u2500\u2500 inventory\n\u2502   \u2502   \u251c\u2500\u2500 groups.yml\n\u2502   \u2502   \u251c\u2500\u2500 hosts-local.yml\n\u2502   \u2502   \u2514\u2500\u2500 ssh_config\n\u2502   \u251c\u2500\u2500 logs\n\u2502   \u2502   \u2514\u2500\u2500 nornir.log\n\u2502   \u2514\u2500\u2500 nr-get-config.py\n\u2514\u2500\u2500 README.md\n```\n\n</details>\n\n<details><summary>Netconf Connection Plugin</summary>\n\nBelow is the snippet of a host inside the host-local.yml file and its associated group, `sros`.\n\n```yaml\nnokia_rtr:\n  hostname: \"192.168.1.205\"\n  port: 830\n  groups:\n    - \"sros\"\n```\n\n```yaml\nsros:\n  username: \"netconf\"\n  password: \"NCadmin123\"\n  port: 830\n  platform: \"sros\"\n  connection_options:\n    netconf:\n      extras:\n        hostkey_verify: false\n        timeout: 300\n        allow_agent: false\n        look_for_keys: false\n```\n\n</details>\n\n<details><summary>Task: Get Config</summary>\n\n```python\n    \"\"\"Nornir NETCONF Example Task: 'get-config'.\"\"\"\n    from nornir import InitNornir\n    from nornir.core.task import Task\n    from nornir_utils.plugins.functions import print_result\n\n    from nornir_netconf.plugins.tasks import netconf_get_config\n\n    __author__ = \"Hugo Tinoco\"\n    __email__ = \"hugotinoco@icloud.com\"\n\n    nr = InitNornir(\"config.yml\")\n\n    # Filter the hosts by 'west-region' assignment\n    west_region = nr.filter(region=\"west-region\")\n\n\n    def example_netconf_get_config(task: Task) -> str:\n        \"\"\"Test get config.\"\"\"\n        config = task.run(\n            netconf_get_config,\n            source=\"running\",\n            path=\"\"\"\n            <configure xmlns=\"urn:nokia.com:sros:ns:yang:sr:conf\">\n                <router>\n                    <router-name>Base</router-name>\n                </router>\n            </configure>\n            \"\"\",\n            filter_type=\"subtree\",\n        )\n        return config.result.rpc.data_xml\n\n\n    def main():\n        \"\"\"Execute Nornir Script.\"\"\"\n        print_result(west_region.run(task=example_netconf_get_config))\n\n\n    if __name__ == \"__main__\":\n        main()\n```\n\nThis returns the following\n\n```bash\n    vvvv example_netconf_get_config ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO\n    <?xml version=\"1.0\" encoding=\"UTF-8\"?><rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"urn:uuid:918bf169-523f-4bb0-b00c-c97c01a48ecd\">\n        <data>\n            <configure xmlns=\"urn:nokia.com:sros:ns:yang:sr:conf\">\n                <router>\n                    <router-name>Base</router-name>\n                    <interface>\n                        <interface-name>L3-OAM-eNodeB069420-X1</interface-name>\n                        <admin-state>disable</admin-state>\n                        <ingress-stats>false</ingress-stats>\n                    </interface>\n                </router>\n            </configure>\n        </data>\n    </rpc-reply>\n    ---- netconf_get_config ** changed : False ------------------------------------- INFO\n    RpcResult(rpc=<ncclient.xml_.NCElement object at 0x7f4b1e08a440>)\n    ^^^^ END example_netconf_get_config ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n    (nornir-netconf-Ky5gYI2O-py3.10) \u279c  example-project git:(feature/validate-tasks) \u2717 \n```\n\n</details>\n\n<details><summary>Task: Get Capabilities</summary>\n\n```python\n    \"\"\"Nornir NETCONF Example Task: 'capabilities'.\"\"\"\n    from nornir import InitNornir\n    from nornir.core.task import Task\n    from nornir_utils.plugins.functions import print_result\n\n    from nornir_netconf.plugins.tasks import netconf_capabilities\n\n    __author__ = \"Hugo Tinoco\"\n    __email__ = \"hugotinoco@icloud.com\"\n\n    nr = InitNornir(\"config.yml\")\n\n    # Filter the hosts by 'west-region' assignment\n    west_region = nr.filter(region=\"west-region\")\n\n\n    def example_netconf_get_capabilities(task: Task) -> str:\n        \"\"\"Test get capabilities.\"\"\"\n        capabilities = task.run(netconf_capabilities)\n        # This may be a lot, so for example we'll just print the first one\n        return [cap for cap in capabilities.result.rpc][0]\n\n\n    def main():\n        \"\"\"Execute Nornir Script.\"\"\"\n        print_result(west_region.run(task=example_netconf_get_capabilities))\n\n\n    if __name__ == \"__main__\":\n        main()\n```\n\nThis returns the following\n\n```bash\n    (nornir-netconf-Ky5gYI2O-py3.10) \u279c  example-project git:(feature/validate-tasks) \u2717 python3 nr_get_capabilities.py \n    example_netconf_get_capabilities************************************************\n    * nokia_rtr ** changed : False *************************************************\n    vvvv example_netconf_get_capabilities ** changed : False vvvvvvvvvvvvvvvvvvvvvvv INFO\n    urn:ietf:params:netconf:base:1.0\n    ---- netconf_capabilities ** changed : False ----------------------------------- INFO\n    RpcResult(rpc=<dict_keyiterator object at 0x7f7111328c70>)\n    ^^^^ END example_netconf_get_capabilities ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n    (nornir-netconf-Ky5gYI2O-py3.10) \u279c  example-project git:(feature/validate-tasks) \u2717 \n```\n\n</details>\n\n<details><summary>Task: Edit-Config with Global Lock</summary>\n\n```python\n    \"\"\"Nornir NETCONF Example Task: 'edit-config', 'netconf_lock'.\"\"\"\n    from nornir import InitNornir\n    from nornir_utils.plugins.functions import print_result\n    from nornir_netconf.plugins.tasks import netconf_edit_config, netconf_lock, netconf_commit\n\n\n    __author__ = \"Hugo Tinoco\"\n    __email__ = \"hugotinoco@icloud.com\"\n\n    nr = InitNornir(\"config.yml\")\n\n    # Filter the hosts by 'west-region' assignment\n    west_region = nr.filter(region=\"west-region\")\n\n\n    def example_global_lock(task):\n        \"\"\"Test global lock operation of 'candidate' datastore.\"\"\"\n        lock = task.run(netconf_lock, datastore=\"candidate\", operation=\"lock\")\n        # Retrieve the Manager(agent) from lock operation and store for further\n        # operations.\n        task.host[\"manager\"] = lock.result.manager\n\n\n    def example_edit_config(task):\n        \"\"\"Test edit-config with global lock using manager agent.\"\"\"\n\n        config_payload = \"\"\"\n        <config>\n            <configure xmlns=\"urn:nokia.com:sros:ns:yang:sr:conf\">\n                <router>\n                    <router-name>Base</router-name>\n                    <interface>\n                        <interface-name>L3-OAM-eNodeB069420-X1</interface-name>\n                        <admin-state>disable</admin-state>\n                        <ingress-stats>false</ingress-stats>\n                    </interface>\n                </router>\n            </configure>\n        </config>\n        \"\"\"\n\n        result = task.run(\n            netconf_edit_config, config=config_payload, target=\"candidate\", manager=task.host[\"manager\"]\n        )\n        # Validate configuration\n        task.run(netconf_validate)\n        # Commit\n        task.run(netconf_commit, manager=task.host[\"manager\"])\n\n    def example_unlock(task):\n        \"\"\"Unlock candidate datastore.\"\"\"\n        task.run(netconf_lock, datastore=\"candidate\", operation=\"unlock\", manager=task.host[\"manager\"])\n\n\n    def main():\n        \"\"\"Execute Nornir Script.\"\"\"\n        print_result(west_region.run(task=example_global_lock))\n        print_result(west_region.run(task=example_edit_config))\n        print_result(west_region.run(task=example_unlock))\n\n\n    if __name__ == \"__main__\":\n        main()\n\n```\n\n</details>\n\n<details><summary>Task: Get Schemas</summary>\n\n```python\n    \"\"\"Get Schemas from NETCONF device.\"\"\"\n    from nornir import InitNornir\n    from nornir.core import Task\n    from nornir.core.task import Result\n    from nornir_utils.plugins.functions import print_result\n\n    from nornir_netconf.plugins.tasks import netconf_get, netconf_get_schemas\n    from tests.conftest import xml_dict\n\n    __author__ = \"Hugo Tinoco\"\n    __email__ = \"hugotinoco@icloud.com\"\n\n    nr = InitNornir(\"config.yml\")\n\n\n    # Filter the hosts by 'west-region' assignment\n    west_region = nr.filter(region=\"west-region\")\n\n    SCHEMA_FILTER = \"\"\"\n    <netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n        <schemas>\n        </schemas>\n    </netconf-state>\n    \"\"\"\n\n\n    def example_task_get_schemas(task: Task) -> Result:\n        \"\"\"Get Schemas from NETCONF device.\"\"\"\n        result = task.run(netconf_get, path=SCHEMA_FILTER, filter_type=\"subtree\")\n        # xml_dict is a custom function to convert XML to Python dictionary. Not part of Nornir Plugin.\n        # See the code example if you want to use it.\n        parsed = xml_dict(result.result.rpc.data_xml)\n        first_schema = parsed[\"rpc-reply\"][\"data\"][\"netconf-state\"][\"schemas\"][\"schema\"][0]\n        return task.run(netconf_get_schemas, schemas=[first_schema[\"identifier\"]], schema_path=\"./output/schemas\")\n\n\n    def main():\n        \"\"\"Execute Nornir Script.\"\"\"\n        print_result(west_region.run(task=example_task_get_schemas))\n\n\n    if __name__ == \"__main__\":\n        main()\n\n```\n\nThis returns the following\n\n```bash\n    (nornir-netconf-Ky5gYI2O-py3.10) \u279c  example-project git:(feature/validate-tasks) \u2717 python3 nr_get_schemas.py \n    example_task_get_schemas********************************************************\n    * nokia_rtr ** changed : False *************************************************\n    vvvv example_task_get_schemas ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO\n    ---- netconf_get ** changed : False -------------------------------------------- INFO\n    RpcResult(rpc=<ncclient.xml_.NCElement object at 0x7f36391540d0>)\n    ---- netconf_get_schemas ** changed : False ------------------------------------ INFO\n    SchemaResult(directory='./output/schemas')\n    ^^^^ END example_task_get_schemas ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n```\n\n</details>\n\n## Additional Documentation\n\n- [NCClient](https://ncclient.readthedocs.io/en/latest/)\n\n## Contributions\n\n> Github actions spins up a Containerlab instance to do full integration tests once linting has been satisfied.\n\n---\n\nNo line of code shall go untested! Any contribution will need to be accounted for by the coverage report and satisfy all linting.\n\nLinters:\n\n- Ruff (Flake8/Pydocstyle)\n- Black\n- Yamllint\n- Pylint\n- Bandit\n- MyPy\n\n## Testing\n\nTo test within a local docker environment\n\n```bash\ngit clone https://github.com/h4ndzdatm0ld/nornir_netconf\n```\n\n```bash\ndocker-compose build && docker-compose run test\n```\n\nTo test locally with pytest\n\nIf you'd like to run integration tests with ContainerLab\n\n```bash\nexport SKIP_INTEGRATION_TESTS=False\n```\n\n```bash\ndocker-compose up -d\n```\n\n```bash\npoetry install && poetry shell\n```\n\n```bash\npytest --cov=nornir_netconf --color=yes --disable-pytest-warnings -vvv\n```\n\n### Integration Tests\n\nDevices with full integration tests with ContainerLab\n\n- Nokia SROS - TiMOS-B-21.2.R1\n- Cisco IOSxR - Cisco IOS XR Software, Version 6.1.3\n- Cisco IOSXE - Cisco IOS XE Software, Version 17.03.02\n- Arista CEOS - 4.28.0F-26924507.4280F (engineering build)\n\n## Documentation\n\nDocumentation is generated with Sphinx and hosted with Github Pages. [Documentation](https://h4ndzdatm0ld.github.io/nornir_netconf/)\n\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "NETCONF plugin for Nornir",
    "version": "2.0.0",
    "split_keywords": [
        "nornir",
        "netconf",
        "ncclient"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3b7f5d21fbc90eecadefea5ba7868b2971b51d8c03b4b3ad792a5e12f80efc56",
                "md5": "4c3104c3bdadbadb6fc58dc015b8a17b",
                "sha256": "44724f2909476cfecbdb9d9f77717321b8c7e4d43391c3351142f603ccce570b"
            },
            "downloads": -1,
            "filename": "nornir_netconf-2.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4c3104c3bdadbadb6fc58dc015b8a17b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 20843,
            "upload_time": "2023-03-15T06:45:01",
            "upload_time_iso_8601": "2023-03-15T06:45:01.671632Z",
            "url": "https://files.pythonhosted.org/packages/3b/7f/5d21fbc90eecadefea5ba7868b2971b51d8c03b4b3ad792a5e12f80efc56/nornir_netconf-2.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ee441efd2f7ab801b1f4be4fa3c4b8f509a8667c71a43817409fbf47f20d1c1a",
                "md5": "7bec0660d75ea5a4bcf7072fe778a015",
                "sha256": "47b20ba5e7be05cee80859ba027af1752c948bab140d3affed7a6d1228fd3951"
            },
            "downloads": -1,
            "filename": "nornir_netconf-2.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "7bec0660d75ea5a4bcf7072fe778a015",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 18334,
            "upload_time": "2023-03-15T06:45:03",
            "upload_time_iso_8601": "2023-03-15T06:45:03.457225Z",
            "url": "https://files.pythonhosted.org/packages/ee/44/1efd2f7ab801b1f4be4fa3c4b8f509a8667c71a43817409fbf47f20d1c1a/nornir_netconf-2.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-15 06:45:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "h4ndzdatm0ld",
    "github_project": "nornir_netconf",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "nornir-netconf"
}
        
Elapsed time: 0.06930s