
    VhL'                         d dl mZmZmZ eZdZd dlZd dlZd dl	m
Z
 d dlmZmZ d dlmZ d dlmZmZ 	 d dlmZ d d	lmZ d d
lmZ d dlmZmZmZmZ dZ G d de      Z!y# ee f$ r dZY w xY w)    )absolute_importdivisionprint_functionaI  
author: Ansible Networking Team (@ansible-network)
name: junos
short_description: Use junos netconf plugin to run netconf commands on Juniper JUNOS
  platform
description:
- This junos plugin provides low level abstraction apis for sending and receiving
  netconf commands from Juniper JUNOS network devices.
version_added: 1.0.0
options:
  ncclient_device_handler:
    type: str
    default: junos
    description:
    - Specifies the ncclient device handler name for Juniper junos network os. To
      identify the ncclient device handler name refer ncclient library documentation.
N)AnsibleConnectionFailure)	to_nativeto_text)string_types)NetconfBaseensure_ncclient)manager)RPCError)SSHUnknownHostError)new_elesub_eleto_eleto_xmlTFc                       e Zd Zd Zed        Zd Ze	 	 	 	 dd       Zd Ze	ed               Z
ddZdd	Zd
 Zd Ze	 	 	 	 	 	 	 dd       Zy)Netconfc                     	 t        |j                  |      j                  d      j                         S # t        $ r Y y w xY w)Nsurrogate_then_replace)errors)r   findtextstripAttributeError)selfeletags      o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/junipernetworks/junos/plugins/netconf/junos.pyget_textzNetconf.get_textE   sE    	""/ eg  		s   36 	AAc                 $   t               }d|d<   t        d      }| j                  t        |            }t	        |      }|j                  d      }| j                  |d      |d<   | j                  |d      |d<   | j                  |d	      |d
<   |S )Njunos
network_oszget-software-informationz.//software-informationzjunos-versionnetwork_os_versionz	host-namenetwork_os_hostnamezproduct-modelnetwork_os_model)dictr   execute_rpcr   r   r   r    )r   device_infor   datareplysw_infos         r   get_device_infozNetconf.get_device_infoN   s    f$+L!01s,t**67,0MM-
() .2]].
)* +/--+
&'
     c                 $    | j                  |      S )z
        RPC to be execute on remote device
        :param name: Name of rpc in string format
        :return: Received rpc response from remote host
        )rpc)r   names     r   r(   zNetconf.execute_rpcf   s     xx~r.   Nc                     |r|dk(  rt        |      }	 | j                  j                  ||||      j                  S # t        $ r#}t        t        |j                              d}~ww xY w)a  
        Load given configuration on device
        :param format: Format of configuration (xml, text, set)
        :param action: Action to be performed (merge, replace, override, update)
        :param target: The name of the configuration datastore being edited
        :param config: The configuration to be loaded on remote host in string format
        :return: Received rpc response from remote host in string format
        xml)formatactiontargetconfigN)r   mload_configurationdata_xmlr   	Exceptionr   r3   )r   r4   r5   r6   r7   excs         r   r9   zNetconf.load_configurationn   sp      	-66,,	 - 
 h  	-F377O,,	-s   (= 	A)A$$A)c                    t               }| j                         g dz   |d<   d|d<   | j                         |d<   t        | j                  j
                        |d<   t        | j                  j                        |d<   | j                  j                  |d<   | j                  |d         |d	<   t        j                  |      S )
N)commitdiscard_changesvalidatelockunlock	copy_copyr(   r9   get_configurationcommandreboothaltr0   netconfnetwork_apir)   server_capabilitiesclient_capabilities
session_iddevice_operations)r'   get_base_rpcr-   listr8   rJ   rK   rL   get_device_operationsjsondumps)r   results     r   get_capabilitieszNetconf.get_capabilities   s    ))+ /
 
u !*} $ 4 4 6}(,TVV-G-G(H$%(,TVV-G-G(H$%#vv00|&*&@&@()'
"# zz&!!r.   c                 Z   	 t        j                  | j                  j                  | j                  j                  xs d| j                  j
                  | j                  j                  | j                  | j                  d      | j                  d      | j                  j                  | j                  d      | j                  
      }d}|j                  D ]  }t        j                   d|      sd} |j#                          |S # t        $ r}t        t        |            d}~ww xY w)z
        Guess the remote network os name
        :param obj: Netconf connection class object
        :return: Network OS name
        i>  host_key_checkinglook_for_keyspersistent_connect_timeout)
hostportusernamepasswordkey_filenamehostkey_verifyrW   allow_agenttimeout
ssh_configNr"   )r   connect_play_contextremote_addrrZ   remote_userr\   r]   
get_optionr_   _ssh_configr   r   r   rJ   researchclose_session)objr8   r<   
guessed_oscs        r   guess_network_oszNetconf.guess_network_os   s    	;&&22&&++2s**66**33 --"~~.AB!nn_=--99'CD ??A" 
&& 	%Ayy!$$
	% 	
 # 	;*9S>::	;s   CD 	D*D%%D*c                     |6t        |t              st        d|dt        |      d      t	        |      }| j
                  j                  ||      j                  S )aj  
        Retrieve all or part of a specified configuration.
        :param format: format in which configuration should be retrieved
        :param filter: specifies the portion of the configuration to retrieve
               as either xml string rooted in <configuration> element
        :return: Received rpc response from remote host in string format
        zCget configuration filter should be of type string, received value 'z' is of type '')r4   filter)
isinstancer	   r   typer   r8   rD   r:   )r   r4   rq   s      r   rD   zNetconf.get_configuration   s[     fl3.>Dd6lT  F^Fvv''vf'ENNNr.   c                 N    | j                   j                  |      j                  S )ah  
        Compare the candidate configuration with running configuration
        by default. The candidate configuration can be compared with older
        committed configuration by providing rollback id.
        :param rollback: Rollback id of previously commited configuration
        :return: Received rpc response from remote host in string format
        )rollback)r8   compare_configurationr:   )r   ru   s     r   rv   zNetconf.compare_configuration   s"     vv++X+>GGGr.   c                 J    | j                   j                         j                  S zreboot the device)r8   rG   r:   r   s    r   rG   zNetconf.halt   s    vv{{}%%%r.   c                 J    | j                   j                         j                  S rx   )r8   rF   r:   ry   s    r   rF   zNetconf.reboot   s    vv}}'''r.   c                 B   t        d      }|rt        |d       |rt        |d       |rt        |d       |rt        |d      }	t        |      |	_        |rt        |d      }	t        |      |	_        |rt        |d      }	t        |      |	_        | j	                  |      S )a  
        Commit the candidate configuration as the device's new current configuration.
        Depends on the `:candidate` capability.
        A confirmed commit (i.e. if *confirmed* is `True`) is reverted if there is no
        followup commit within the *timeout* interval. If no timeout is specified the
        confirm timeout defaults to 600 seconds (10 minutes).
        A confirming commit may have the *confirmed* parameter but this is not required.
        Depends on the `:confirmed-commit` capability.
        :param confirmed: whether this is a confirmed commit
        :param check: Check correctness of syntax
        :param timeout: specifies the confirm timeout in seconds
        :param comment: Message to write to commit log
        :param synchronize: Synchronize commit on remote peers
        :param at_time: Time at which to activate configuration changes
        :return: Received rpc response from remote host
        zcommit-configuration	confirmedchecksynchronizezat-timelogzconfirm-timeout)r   r   strr   r0   )
r   r|   r`   persistr}   commentr~   at_timerk   subeles
             r   r>   zNetconf.commit   s    6 ,-C%C!C'S),Fg,FKS%(Fg,FKS"34Fg,FKxx}r.   )r3   merge	candidateN)r3   N)r   )FNNFNFN)__name__
__module____qualname__r    r   r-   r(   r9   rT   staticmethodrn   rD   rv   rG   rF   r>    r.   r   r   r   D   s      .  - -:"4   @O$H&(  * *r.   r   )"
__future__r   r   r   rs   __metaclass__DOCUMENTATIONrQ   rh   ansible.errorsr   ansible.module_utils._textr   r   ansible.module_utils.sixr	   Gansible_collections.ansible.netcommon.plugins.plugin_utils.netconf_baser
   r   ncclientr   ncclient.operationsr   ncclient.transport.errorsr   ncclient.xml_r   r   r   r   HAS_NCCLIENTImportErrorr   r   r   r.   r   <module>r      sy   & A @ $  	 3 9 1 ,=>>LWk W   L	s    A   	A,+A,