---
- debug:
    msg="START netconf_get iosxr/basic.yaml on connection={{ ansible_connection
    }}"

- name: Setup interface
  vars:
    ansible_connection: ansible.netcommon.network_cli
  cisco.iosxr.iosxr_config:
    commands:
      - description this is test interface Loopback999
      - no shutdown
    parents:
      - interface Loopback999
    match: none

- name: Get running interface configuration with filter
  register: result
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    source: running
    filter: <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"></interface-configurations>

- assert:
    that:
      - "'<description>this is test interface Loopback999</description>' in result.stdout"
      - "'<usernames>' not in result.stdout"

- name: Test lock=never, get-config, running interface configuration with filter without lock
  register: result
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    source: running
    lock: never
    filter: <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"></interface-configurations>

- assert:
    that:
      - "'<description>this is test interface Loopback999</description>' in result.stdout"
      - "'<usernames>' not in result.stdout"

- name: Test lock=if-supported, get-config, running interface configuration with filter without lock
  register: result
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    source: running
    lock: if-supported
    filter: <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"></interface-configurations>

- assert:
    that:
      - "'<description>this is test interface Loopback999</description>' in result.stdout"
      - "'<usernames>' not in result.stdout"

- name: Failure scenario, fetch config from startup
  register: result
  ignore_errors: true
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    source: startup

- assert:
    that:
      - "'startup source is not supported' in result.msg"

- name: Test get, information from running datastore without lock
  register: result
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    lock: never
    filter: <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"></interface-configurations>

- assert:
    that:
      - "'<description>this is test interface Loopback999</description>' in result.stdout"

- name: Test get, information from running datastore with lock if supported
  register: result
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    lock: if-supported
    filter: <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"></interface-configurations>

- assert:
    that:
      - "'<description>this is test interface Loopback999</description>' in result.stdout"

- name: Get configuration and state data in JSON format
  register: result
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    source: running
    display: json

- assert:
    that:
      - "{{ result['output']['data']['aaa'] is defined}}"

- name: Get configuration data in XML pretty format
  register: result
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    source: running
    display: pretty

- assert:
    that:
      - "{{ result['output'] is defined}}"

- name: Get configuration data in XML with namespace stripped
  register: result
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    source: running
    display: xml

- assert:
    that:
      - "{{ result['output'] is defined}}"
      - "{{ 'xmlns' not in result.output }}"

- name: Failure scenario, unsupported filter
  register: result
  ignore_errors: true
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    filter: configuration/state

- assert:
    that:
      - "'filter value \\'configuration/state\\' of type xpath is not supported'\
        \ in result.msg"

- name: "get configuration with json filter and native output (using xmltodict)"
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    filter: |
      {
          "interface-configurations": {
              "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
              "interface-configuration": null
          }
      }
    display: native
  register: result

- assert:
    that:
      - "{{ result['output']['data'] is defined}}"
      - "{{ result['output']['data']['@xmlns'] == 'urn:ietf:params:xml:ns:netconf:base:1.0' }}"
      - "{{ result['output']['data']['@xmlns:nc'] == 'urn:ietf:params:xml:ns:netconf:base:1.0' }}"
      - "{{ result['output']['data']['interface-configurations']['interface-configuration'] is defined }}"

- name: Define the interface filter
  set_fact:
    filter:
      interface-configurations:
        "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"
        interface-configuration: null

- name: "get configuration with native filter type using set_facts"
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    filter: "{{ filter }}"
    display: native
  register: result

- assert:
    that:
      - "{{ result['output']['data'] is defined}}"
      - "{{ result['output']['data']['@xmlns'] == 'urn:ietf:params:xml:ns:netconf:base:1.0' }}"
      - "{{ result['output']['data']['@xmlns:nc'] == 'urn:ietf:params:xml:ns:netconf:base:1.0' }}"
      - "{{ result['output']['data']['interface-configurations']['interface-configuration'] is defined }}"

- name: "get configuration with direct native filter type"
  vars:
    ansible_connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_get:
    filter:
      {
        "interface-configurations":
          {
            "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
            "interface-configuration": null,
          },
      }
    display: native
  register: result

- assert:
    that:
      - "{{ result['output']['data'] is defined}}"
      - "{{ result['output']['data']['@xmlns'] == 'urn:ietf:params:xml:ns:netconf:base:1.0' }}"
      - "{{ result['output']['data']['@xmlns:nc'] == 'urn:ietf:params:xml:ns:netconf:base:1.0' }}"
      - "{{ result['output']['data']['interface-configurations']['interface-configuration'] is defined }}"

- name: Get running configuration and state data with  ansible_netconf_libssh set as true
  ansible.netcommon.netconf_get:
    source: running
    display: pretty
  vars:
    ansible_connection: ansible.netcommon.netconf
    ansible_netconf_libssh: true
  register: result
- assert:
    that:
      - "{{ result['output'] is defined}}"

- name: Setup - teardown
  vars:
    ansible_connection: ansible.netcommon.network_cli
  cisco.iosxr.iosxr_config:
    commands:
      - no description
      - shutdown
    parents:
      - interface Loopback999
    match: none

- debug:
    msg="END netconf_get iosxr/basic.yaml on connection={{ ansible_connection
    }}"
