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

- name: Setup task for netconf config iosxr
  connection: ansible.netcommon.network_cli
  vars:
    ansible_connection: ansible.netcommon.network_cli
  cisco.iosxr.iosxr_config:
    commands:
      - no description
      - shutdown
    parents:
      - interface Loopback999
    match: none

- name: Sleep for 30 seconds and continue with play
  wait_for:
    timeout: 30
  delegate_to: localhost

- name: Save config test
  register: result
  connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_config:
    backup: true

- assert:
    that:
      - "'backup_path' in result"

- name: Test confirm commit
  ansible.netcommon.netconf_config:
    target: "candidate"
    error_option: "rollback-on-error"
    commit: true
    confirm: 10
    default_operation: "merge"
    content: |
      <nc:config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
      <cdp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-cdp-cfg">
      <enable>true</enable>
      </cdp>
      </nc:config>
  register: result
  ignore_errors: true
  connection: ansible.netcommon.netconf

- assert:
    that:
      - "result['failed'] == false"

- name: Test invalid JSON string format configuration
  connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_config:
    content: |
      {
          "config": {
              "@xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "@xmlns:nc": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "interface-configurations": {
                  "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
                  "interface-configuration": {
                      "active": "act",
                      "description": "test for ansible Loopback999",
                      "interface-name": "Loopback999",
                  }
              }
          }
      }
    get_filter: |
      {
          "interface-configurations": {
              "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
              "interface-configuration": null
          }
      }
  register: result
  ignore_errors: true
  diff: true

- assert:
    that:
      - result.failed == true
      - "'Value of content option is invalid as per the identified format xml' in result.msg"

- name: Configure using JSON string format configuration
  connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_config:
    format: json
    content: |
      {
          "config": {
              "@xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "@xmlns:nc": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "interface-configurations": {
                  "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
                  "interface-configuration": {
                      "active": "act",
                      "description": "test for ansible Loopback999",
                      "interface-name": "Loopback999"
                  }
              }
          }
      }
    get_filter: |
      {
          "interface-configurations": {
              "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
              "interface-configuration": null
          }
      }
  register: result
  diff: true

- assert:
    that:
      - result.changed == true
      - "'<description>test for ansible Loopback999</description>' in result.diff.after"

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

- name: Configure using direct native format configuration
  connection: ansible.netcommon.netconf
  ansible.netcommon.netconf_config:
    format: json
    content:
      {
        "config":
          {
            "@xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0",
            "@xmlns:nc": "urn:ietf:params:xml:ns:netconf:base:1.0",
            "interface-configurations":
              {
                "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
                "interface-configuration":
                  {
                    "active": "act",
                    "description": "test for ansible Loopback999",
                    "interface-name": "Loopback999",
                  },
              },
          },
      }
    get_filter:
      {
        "interface-configurations":
          {
            "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
            "interface-configuration": null,
          },
      }
  register: result
  diff: true

- assert:
    that:
      - result.changed == true
      - "'<description>test for ansible Loopback999</description>' in result.diff.after"

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

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