
    Vh2                         d dl mZmZmZ eZdZd dlZd dlZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlm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  G d de      Zy)    )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: Ruckus Wireless (@Commscope)
name: icx
short_description: Use icx cliconf to run command on Ruckus ICX platform
description:
  - This icx plugin provides low level abstraction APIs for
    sending and receiving CLI commands from Ruckus ICX network devices.
N)chain)AnsibleConnectionFailure)to_text)	iteritems)NetworkConfigdumps)to_list)CliconfBaseenable_mode)Mappingc                   |    e Zd Zedd       ZddZedd       ZddZddZd Z	d Z
d	 Zd
 ZddZddZd Zd Zy)CliconfNc                     |dvrt        d|z        |rt        d|z        |sg }|du ry|dk(  rd}nd}|d	j                  t        |            z  }|j                         }| j	                  |      S )
N)runningstartupz/fetching configuration from %s is not supportedz1'format' value %s is not supported for get_configF r   zshow running-config zshow configuration  )
ValueErrorjoinr   stripsend_command)selfsourceflagsformatcomparecmds         i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/network/plugins/cliconf/icx.py
get_configzCliconf.get_config'   s    //NQWWXXPSYYZZEe",+388GEN++C))+C$$S))    c           	      n   i }| j                         }| j                         }	||d   rt        d      ||	d   vr#t        d|ddj                  |	d               ||	d   vr#t        d|ddj                  |	d               t	        d	
      }
| j                  |      \  }}|
j                  |       |r=|dk7  r8| j                  |      \  }}t	        d	||      }|
j                  ||||      }n|
j                  }i }|rt        |d      nd|d<   | j                  ||      }|r|ni |d<   |S )aS	  
        Generate diff between candidate and running configuration. If the
        remote host supports onbox diff capabilities ie. supports_onbox_diff in that case
        candidate and running configurations are not required to be passed as argument.
        In case if onbox diff capability is not supported candidate argument is mandatory
        and running argument is optional.
        :param candidate: The configuration which is expected to be present on remote host.
        :param running: The base configuration which is used to generate diff.
        :param diff_match: Instructs how to match the candidate configuration with current device configuration
                      Valid values are 'line', 'strict', 'exact', 'none'.
                      'line' - commands are matched line by line
                      'strict' - command lines are matched with respect to position
                      'exact' - command lines must be an equal match
                      'none' - will not compare the candidate configuration with the running configuration
        :param diff_ignore_lines: Use this argument to specify one or more lines that should be
                                  ignored during the diff.  This is used for lines in the configuration
                                  that are automatically updated by the system.  This argument takes
                                  a list of regular expressions or exact line matches.
        :param path: The ordered set of parents that uniquely identify the section or hierarchy
                     the commands should be checked against.  If the parents argument
                     is omitted, the commands are checked against the set of top
                    level or global commands.
        :param diff_replace: Instructs on the way to perform the configuration on the device.
                        If the replace argument is set to I(line) then the modified lines are
                        pushed to the device in configuration mode.  If the replace argument is
                        set to I(block) then the entire command block is pushed to the device in
                        configuration mode if any line is not correct.
        :return: Configuration diff in  json format.
               {
                   'config_diff': '',
                   'banner_diff': {}
               }

        supports_generate_diffz4candidate configuration is required to generate diff
diff_matchz'match' value z in invalid, valid values are z, diff_replacez'replace' value    )indentnone)r)   contentsignore_lines)pathmatchreplacecommandsr   config_diffbanner_diff)get_device_operationsget_option_valuesr   r   r
   _extract_bannersload
differenceitemsr   _diff_banners)r   	candidater   r&   diff_ignore_linesr-   r'   diffdevice_operationsoption_valuescandidate_objwant_srcwant_bannershave_srchave_bannersrunning_objconfigdiffobjsbannerss                     r!   get_diffzCliconf.get_diff?   s   F  668..0!23K!LSTT]<88S]_c_h_hiv  xD  jE  `F  G  H  H}^<<Uacgclclmz  |J  nK  dL  M  N  N &Q/!%!6!6y!A,8$zV+%)%:%:7%C"Hl'q8RcdK*55kT^ht5uN +00NLCQeNJ?WY]$$\<@)0gb]r#   c                    i }| j                         }| j                  |||||       g }g }|r| j                  j                         }	d|	v sd|	v sd|	v r| j	                  d       | j	                  d       t        |      D ]\  }
t        |
t              sd|
i}
|
d   }|dk7  s"|d   dk7  s+|j                   | j                  di |
       |j                  |       ^ | j	                  d       nt        d	      ||d
<   ||d<   |S )Ns
   (config-ifs   (configs   (config-lag-ifendzconfigure terminalcommandr   !zcheck mode is not supportedrequestresponse )
r3   check_edit_config_capability_connection
get_promptr   r   
isinstancer   appendr   )r   r:   commitr/   commentresp
operationsresultsrequestspromptliner    s               r!   edit_configzCliconf.edit_config   s!   //1
))*iRYZ%%002F'Z6-AGX\bGb!!%(23	* )!$0%t,D9o%<CFcMNN#44#4#4#<t#<=OOC() e$:;;"Y"Zr#   c                 h    |st        d      |rt        d|z        | j                  |||||      S )N(must provide value of command to executez*'output' value %s is not supported for getrJ   rZ   answersendonly	check_all)r   r   )r   rJ   rZ   r`   ra   outputrb   s          r!   getzCliconf.get   sC    GHHIFRSS  Yamv wwr#   c                     |st        d      ddg}|#|| j                  j                  j                  g}n||g}| j	                  |||dd      S )Nr^   z
User name:z	Password:FTr_   )r   rP   _play_contextpasswordr   )r   rJ   scp_userscp_passrZ   r`   s         r!   scpzCliconf.scp   se    GHH, 0 0 > > G GHF)F  Y^jn oor#   c                    i }d|d<   | j                  d      }t        |d      j                         }t        j                  d|      }|r#|j                  d      j                  d	      |d
<   t        j                  d|t        j                        }|r|j                  d      |d<   t        j                  d|t        j                        }|r|j                  d      |d<   |S )Nicx
network_oszshow version)rJ   surrogate_or_strict)errorszVersion (\S+)r(   ,network_os_versionz^Cisco (.+) \(revisionnetwork_os_modelz^(.+) uptimenetwork_os_hostname)rd   r   r   researchgroupM)r   device_inforeplydatar.   s        r!   get_device_infozCliconf.get_device_info   s    $)L!0u%:;AAC		*D105A0D0DS0IK,-		3T244@.3kk!nK*+		/4616QK-.r#   c                     ddddddddddddS )NTF)supports_diff_replacesupports_commitsupports_rollbacksupports_defaultssupports_onbox_diffsupports_commit_commentsupports_multiline_delimitersupports_diff_matchsupports_diff_ignore_linesr%   supports_replacerN   r   s    r!   r3   zCliconf.get_device_operations   s-    %)$!&!%#(',,0#'*.&* %
 	
r#   c                     dgg dddgg dS )Ntext)r[   strictexactr*   r[   block)r   r&   r'   rc   rN   r   s    r!   r4   zCliconf.get_option_values   s    h=#W-	
 	
r#   c                     t               }| j                         g dz   |d<   d|d<   | j                         |d<   |j                  | j	                                t        j                  |      S )N)edit_bannerrG   run_commandsget_defaults_flagrpccliconfnetwork_apir=   )dictget_base_rpcr3   updater4   jsonr   )r   results     r!   get_capabilitieszCliconf.get_capabilities   sc    ))+.nnu )}&*&@&@&B"#d,,./zz&!!r#   c           	         i }t        j                  |      }g }g }|rt        |      D ]  \  }}	|d|z  z  }| j                  dd       ||	|fD ]9  }
|
dd}|j	                   | j                  di |       |j	                  |
       ; | j                  dd       t        j                  d       |j	                  | j                  d             |j	                  d        ||d	<   ||d
<   |S )ab  
        Edit banner on remote device
        :param banners: Banners to be loaded in json format
        :param multiline_delimiter: Line delimiter for banner
        :param commit: Boolean value that indicates if the device candidate
               configuration should be  pushed in the running configuration or discarded.
        :param diff: Boolean flag to indicate if configuration that is applied on remote host should
                     generated and returned in response or not
        :return: Returns response of executing the configuration command received
             from remote host
        z %szconfig terminalT)ra   )rJ   ra   rI   g?
rL   rM   rN   )r   loadsr	   r   rS   timesleep)r   r:   multiline_delimiterrT   rV   banners_objrX   rY   keyvaluer    objs               r!   r   zCliconf.edit_banner   s    jj+'4 &
Uu222!!"3d!C(;< )C&)t<CNN#44#4#4#;s#;<OOC()
 !!%$!7

3t0067%& #Y"Zr#   c           	      f   |t        d      t               }t        |      D ]\  }t        |t              sd|i}|j                  dd       }|rt        d|z        	  | j                  di |}|j                  |       ^ |S # t        $ r#}|r t        |dt        |            }Y d }~<d }~ww xY w)Nz'commands' value is requiredrJ   rc   z3'output' value %s is not supported for run_commandserrrN   )r   listr   rR   r   popr   r   getattrr   rS   )r   r0   check_rc	responsesr    rc   outes           r!   r   zCliconf.run_commands  s    ;<<F	8$ 	"Cc7+ #&WWXt,F !VY_!_``4'd''.#. S!	""  , 4a
34s   B	B0B++B0c                    i }t        j                  d|t         j                        }|D ]V  }d|z  }t        j                  ||t         j                        }|s0d|z  }|j                  d      j                         ||<   X |D ]Y  }d|z  }t        j                  ||t         j                        }|s0|j                  t        |j                  d            d      }[ t        j                  dd|      }||fS )Nz^banner (\w+)zbanner %s \$(.+?)(?=\$)z	banner %sr(   r   zbanner \w+ \$\$z!! banner removed)
rt   findallrw   ru   Srv   r   r/   strsub)r   configrF   banner_cmdsr    regexr.   r   s           r!   r5   zCliconf._extract_banners&  s    jj!16244@ 	6C.4EIIeVRTT2E!C'${{1~335	6  	AC.4EIIeVRTT2EEKKN(;R@		A *,?Hwr#   c                 d    i }t        |      D ]  \  }}||j                  |      k7  s|||<   ! |S )N)r	   rd   )r   wanthaver:   r   r   s         r!   r9   zCliconf._diff_banners9  s?    	#D/ 	'JC%!&	#	' r#   )r   NNN)NNr[   NNr[   )NTNN)NNNFNF)NNN)N@T)NT)__name__
__module____qualname__r   r"   rG   r\   rd   rj   r{   r3   r4   r   r   r   r5   r9   rN   r#   r!   r   r   %   sd    * *.DL  >xp*

"!F0&r#   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONrt   r   r   os	itertoolsr   ansible.errorsr   ansible.module_utils._textr   ansible.module_utils.sixr	   Pansible_collections.ansible.netcommon.plugins.module_utils.network.common.configr
   r   Oansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr   ansible.plugins.cliconfr   r   /ansible.module_utils.common._collections_compatr   r   rN   r#   r!   <module>r      sN    C B 
   	  3 . . q c < CYk Yr#   