
    VhL                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZmZ d	Zd
 Zd Zd Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: iosxr_netconf
author: Kedar Kekan (@kedarX)
short_description: Configures NetConf sub-system service on Cisco IOS-XR devices
description:
- This module provides an abstraction that enables and configures the netconf system
  service running on Cisco IOS-XR Software. This module can be used to easily enable
  the Netconf API. Netconf provides a programmatic interface for working with configuration
  and state resources as defined in RFC 6242.
version_added: 1.0.0
extends_documentation_fragment:
- cisco.iosxr.iosxr
options:
  netconf_port:
    description:
    - This argument specifies the port the netconf service should listen on for SSH
      connections.  The default port as defined in RFC 6242 is 830.
    required: false
    type: int
    default: 830
    aliases:
    - listens_on
  netconf_vrf:
    description:
    - netconf vrf name
    required: false
    type: str
    default: default
    aliases:
    - vrf
  state:
    description:
    - Specifies the state of the C(iosxr_netconf) resource on the remote device.  If
      the I(state) argument is set to I(present) the netconf service will be configured.  If
      the I(state) argument is set to I(absent) the netconf service will be removed
      from the configuration.
    type: str
    required: false
    default: present
    choices:
    - present
    - absent
notes:
- This module works with connection C(network_cli). See L(the IOS-XR Platform Options,../network/user_guide/platform_iosxr.html).
z
- name: enable netconf service on port 830
  cisco.iosxr.iosxr_netconf:
    listens_on: 830
    state: present

- name: disable netconf service
  cisco.iosxr.iosxr_netconf:
    state: absent
z
commands:
  description: Returns the command sent to the remote device
  returned: when changed is True
  type: str
  sample: 'ssh server netconf port 830'
N)AnsibleModule)	iteritems)
get_configload_configTc                    | \  }}t               }|d   dk(  rY|d   dk(  r|j                  d       d|v r|j                  d|d   z         |d   r|d   D ]  }|j                  d|z          |S |d   dk(  r|j                  d	       |d   .|d   |j                  d      k7  r|j                  d
|d   z         |d   !|d   |d   vr|j                  d|d   z         |S )Nstateabsentpresentzno netconf-yang agent sshnetconf_portzno ssh server netconf port %snetconf_vrfzno ssh server netconf vrf %sznetconf-yang agent sshzssh server netconf port %szssh server netconf vrf %s)listappendget)updateswanthavecommandsvrfs        m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/iosxr/plugins/modules/iosxr_netconf.pymap_obj_to_commandsr   [   s   JD$vHG} =I%OO78T!OO;d>>RRSM* F > DEF O =H$OO45+n1ER`Ia1aOO84;OOP*]0C4P]K^0^OO7$}:MMNO    c                 V    t        j                  d|       }|r|j                  d      S y )Nz	vrf (\w+)   )researchgroupconfigmatchs     r   	parse_vrfr#   u   s(    IIlF+E{{1~ r   c                 h    t        j                  d|       }|rt        |j                  d            S y )Nz
port (\d+)r   )r   r   intr   r    s     r   
parse_portr&   {   s-    IImV,E5;;q>"" r   c                    ddi}t        | d      }t        | d      }d |j                         D        D cg c]  }|r| }}g |d<   |D ]D  }d|v r|j                  d	t        |      i       d
|v s(|d   j	                  t        |             F d|v rd	|v s|d   r|j                  ddi       d|v rd	|vr|j                  d	di       |S c c}w )Nr   r   znetconf-yang agent)config_filterz
ssh serverc              3   <   K   | ]  }|j                           y w)N)strip).0lines     r   	<genexpr>z$map_config_to_obj.<locals>.<genexpr>   s     MTDJJLMs   r   znetconf portr   znetconf vrfsshr   >  )r   
splitlinesupdater&   r   r#   )moduleobjnetconf_config
ssh_configconfig_liner!   s         r   map_config_to_objr7      s   H
C6JKNF,?J NZ5J5J5LM 	J 
 C 9V#JJ
6(:;<F"%%i&78	9
 Nc$9S=O

GY'(>#<

NC()J#s   Cc                 D    d| cxk  rdk  sn |j                  d       y y )Nr   i  z(netconf_port must be between 1 and 65535)msg)	fail_json)valuer2   s     r   validate_netconf_portr<      s&    GH r   c                     | j                   d   | j                   d   | j                   d   d}t        |      D ]6  \  }}t               j                  d|z        }t	        |      s. |||        8 |S )Nr   r   r   r   r   r   zvalidate_%s)paramsr   globalsr   callable)r2   r3   keyr;   	validators        r   map_params_to_objrD      st    n5}}]3w'C  n %
UIMM-#"56	IeV$	% Jr   c            	      ~   t        t        dddg      t        dgd      t        ddd	g
            } t        | d      }t               }d|d}t        |      }t	        |      }t        ||f      }||d<   |r1|j                   }t        |||      }|rt        |      |d<   d|d<    |j                  di | y)z%main entry point for module executionr%   r/   
listens_on)typedefaultaliasesr   rH   )rI   rH   r   r   )rH   choicesr>   T)argument_specsupports_check_modeF)changedwarningsr   )commit)prepareddiffrM   N )	dictr   r   rD   r7   r   
check_moder	   	exit_json)	rK   r2   rN   resultr   r   r   rO   rQ   s	            r   mainrW      s    ucL>J%)<9y(.CDM DQFvHH5FV$DV$D"D$<0H!F:&&&68F;!40F6N yFvr   __main__)
__future__r   r   r   rG   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   ansible.module_utils.sixr   Hansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.iosxrr   r	   USE_PERSISTENT_CONNECTIONr   r#   r&   r7   r<   rD   rW   __name__rR   r   r   <module>rc      s{    A @ ,\	
 
 4 . ! 4#4I
 : zF r   