
    Vh&                         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
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 dZ	 d dlmZ  G d de      Z!y# eef$ r dZY w xY w# e$ r	 d dl mZ Y (w xY w)    )absolute_importdivisionprint_functiona  
---
deprecated:
  removed_in: 6.0.0
  why: This collection and all content in it is unmaintained and deprecated.
  alternative: Unknown.
author: Unknown (!UNKNOWN)
name: ce
short_description: Use ce netconf plugin to run netconf commands on Huawei Cloudengine platform
description:
  - This ce plugin provides low level abstraction apis for
    sending and receiving netconf commands from Huawei Cloudengine network devices.
options:
  ncclient_device_handler:
    type: str
    default: huawei
    description:
      - Specifies the ncclient device handler name for Huawei Cloudengine.
        To identify the ncclient device handler name refer ncclient library documentation.
N)to_textto_bytes)AnsibleConnectionFailure)NetconfBaseensure_ncclient)manager)RPCError)SSHUnknownHostError)to_eleto_xmlnew_eleTF)
fromstringc                       e Zd Zed        Zed        Zd Zed        Zd Ze	ed               Z
d Zd Zed	        Zd
 Zd Zed        Zed        Zed        Zed        Ze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      h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/network/plugins/netconf/ce.pyget_textzNetconf.get_textC   s>    	388C=--6NOUUWW 		s   36 	AAc                 n   t               }d|d<   d}| j                  |      }t        j                  dd|      }t	        t        |d            }|j                  d      }| j                  |d	      |d
<   | j                  |d      |d<   | j                  |d      |d<   | j                  |d      |d<   |S )Nce
network_osa>  <filter type="subtree">
                          <system xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
                            <systemInfo>
                              <sysName></sysName>
                              <sysContact></sysContact>
                              <productVer></productVer>
                              <platformVer></platformVer>
                              <productName></productName>
                            </systemInfo>
                          </system>
                        </filter>zxmlns=".+?" surrogate_or_strictr   z.//systemInfo
productVernetwork_os_versionsysNamenetwork_os_hostnameplatformVernetwork_os_platform_versionproductNamenetwork_os_platform)dictgetresubr   r   r   r    )r   device_info
filter_xmldatareplysw_infos         r   get_device_infozNetconf.get_device_infoJ   s    f$(L!
%
 xx
#vvnc408D1FGH**_-,0MM'<,P()-1]]7I-N)*59]]7M5Z12-1]]7M-R)*    c                 $    | j                  |      S )zQRPC to be execute on remote device
           :name: Name of rpc in string format)rpc)r   names     r   execute_rpczNetconf.execute_rpce   s     xx~r8   c                 B   |j                  d      r9t        |d   d      |d<   |j                  dd      dk(  rt        |d         |d<   	  | j                  j                  |i |j
                  S # t        $ r#}t        t        |j                              d}~ww xY w)a)  Loads given configuration on device
        :format: Format of configuration (xml, text, set)
        :action: Action to be performed (merge, replace, override, update)
        :target: is the name of the configuration datastore being edited
        :config: is the configuration in string format.configr%   r   formatxmlN)
r/   r   r   mload_configurationdata_xmlr   	Exceptionr   r@   r   argskwargsexcs       r   rB   zNetconf.load_configurationj   s     ::h'x(8AVWF8zz(E*e3#)&*:#;x 	-,466,,d=f=FFF 	-F377O,,	-s   %A2 2	B;BBc                 Z   t               }| j                         g dz   |d<   d|d<   | j                         |d<   t        | j                  j
                        |d<   t        | j                  j                        |d<   | j                  j                  |d<   t        j                  |      S )	N)	r<   rB   get_configurationcompare_configurationexecute_actionhaltrebootexecute_nc_clidispatch_rpcr:   netconfnetwork_apir2   server_capabilitiesclient_capabilities
session_id)
r.   get_base_rpcr7   listrA   rS   rT   rU   jsondumps)r   results     r   get_capabilitieszNetconf.get_capabilities{   s    ))+ /u uu )} $ 4 4 6}(,TVV-G-G(H$%(,TVV-G-G(H$%#vv00|zz&!!r8   c                 \   	 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} n |j#                          |S # t        $ r}t        t        |            d }~ww xY w)Ni>  host_key_checkinglook_for_keyspersistent_connect_timeout)
hostportusernamepasswordkey_filenamehostkey_verifyr^   allow_agenttimeout
ssh_confighuaweir"   )r   connect_play_contextremote_addrra   remote_userrc   rd   
get_optionrf   _ssh_configr   r   r   rS   r0   searchclose_session)objrA   rH   
guessed_oscs        r   guess_network_oszNetconf.guess_network_os   s   	9&&22&&++2s**66**33 --"~~.AB!nn_=--99'CD ??A" 
&& 	Ayy1%!
	
 	
 # 	9*73<88	9s   CD	 		D+D&&D+c                 N     | j                   j                  |i |j                  S )zRetrieve all or part of a specified configuration.
           :format: format in configuration should be retrieved
           :filter: specifies the portion of the configuration to retrieve
           (by default entire configuration is retrieved))rA   rJ   rC   r   rF   rG   s      r   rJ   zNetconf.get_configuration   s&    
 (tvv''88AAAr8   c                 N     | j                   j                  |i |j                  S )z7Compare configuration
           :rollback: rollback id)rA   rK   rC   rw   s      r   rK   zNetconf.compare_configuration   s&     ,tvv++T<V<EEEr8   c                     d}	 | j                   j                  |      }|j
                  S # t        $ r#}t        t	        |j
                              d}~ww xY w)zhuawei execute-actionN)action)rA   rz   r   rD   r   r@   )r   xml_strcon_objrH   s       r   rL   zNetconf.execute_action   sS     	-ffmm7m3G {{  	-F377O,,	-s   , 	AAAc                 J    | j                   j                         j                  S zreboot the device)rA   rM   rC   r   s    r   rM   zNetconf.halt   s    vv{{}%%%r8   c                 J    | j                   j                         j                  S r~   )rA   rN   rC   r   s    r   rN   zNetconf.reboot   s    vv}}'''r8   c                 "   	 |j                  dd      }|r& | j                  j                  |i |j                  S  | j                  j                  |i |j                  S # t
        $ r#}t        t        |j                              d }~ww xY w)Nif_rpc_replyF)poprA   r/   r@   rC   r   rD   r   )r   rF   rG   r   rH   s        r   r/   zNetconf.get   s}    	-!::ne<L!tvvzz4262666466::t.v.777 	-F377O,,	-s   9A" %A" "	B+B		Bc                     	  | j                   j                  |i |j                  S # t        $ r#}t	        t        |j                              d }~ww xY wN)rA   
get_configrC   r   rD   r   r@   rE   s       r   r   zNetconf.get_config   sL    	-$466$$d5f5>>> 	-F377O,,	-   %( 	AAAc                     	  | j                   j                  |i |j                  S # t        $ r#}t	        t        |j                              d }~ww xY wr   )rA   edit_configr@   r   rD   r   rE   s       r   r   zNetconf.edit_config   sL    	-%466%%t6v6::: 	-F377O,,	-r   c                     	  | j                   j                  |i |j                  S # t        $ r#}t	        t        |j                              d }~ww xY wr   )rA   clir@   r   rD   r   rE   s       r   rO   zNetconf.execute_nc_cli   sJ    	-466::t.v.222 	-F377O,,	-r   c                     	  | j                   j                  |i |j                  S # t        $ r#}t	        t        |j                              d }~ww xY wr   )rA   commitrC   r   rD   r   r@   rE   s       r   r   zNetconf.commit   sJ    	- 466==$1&1::: 	-F377O,,	-r   c                 N     | j                   j                  |i |j                  S r   )rA   validaterC   rw   s      r   r   zNetconf.validate   s"    tvv//888r8   c                 N     | j                   j                  |i |j                  S r   )rA   discard_changesrC   rw   s      r   r   zNetconf.discard_changes   s$    %tvv%%t6v6???r8   Nc                     |t        d      | j                  j                  t        |      ||      }|j                  S )a  
        Execute rpc on the remote device eg. dispatch('get-next')
        :param rpc_command: specifies rpc command to be dispatched either in plain text or in xml element format (depending on command)
        :param source: name of the configuration datastore being queried
        :param filter: specifies the portion of the configuration to retrieve (by default entire configuration is retrieved)
        :return: Returns xml string containing the rpc-reply response received from remote host
        z"rpc_command value must be provided)sourcefilter)
ValueErrorrA   dispatchr   r@   )r   rpc_commandr   r   resps        r   rP   zNetconf.dispatch_rpc   s<     ABBvvz+6vfUxxr8   )NNN)__name__
__module____qualname__r
   r    r7   r<   rB   r[   staticmethodru   rJ   rK   rL   rM   rN   r/   r   r   rO   r   r   r   rP    r8   r   r   r   A   s      4
 - - 	"   8BF
  &( - - - - - - - - - -9@  r8   r   )"
__future__r   r   r   type__metaclass__DOCUMENTATIONrX   r0   ansible.module_utils._textr   r   ansible.errorsr   ansible.plugins.netconfr	   r
   ncclientr   ncclient.operationsr   ncclient.transport.errorsr   ncclient.xml_r   r   r   HAS_NCCLIENTImportErrorr   
lxml.etreer   xml.etree.ElementTreer   r   r8   r   <module>r      s   & C B*  	 8 3 @ ,=55L1%
{k { 	^$ L
  101s#   A A. 	A+*A+.A<;A<