Current File : //usr/share/netplan/netplan_cli/cli/__pycache__/state_diff.cpython-312.pyc
�

���gˉ���ddlmZddlZddlZddlmZmZddlmZddl	m
Z
ddlmZm
Z
mZddlmZmZGd�d	ej$�ZGd
�d�Zy)�)�defaultdictN)�AF_INET�AF_INET6)�AbstractSet)�NetplanRoute)�SystemConfigState�NetplanConfigState�DEVICE_TYPES)�is_valid_macaddress�route_table_lookupc��eZdZd�Zy)�DiffJSONEncoderc��t|t�r|j�Stjj||�S�N)�
isinstancer�to_dict�json�JSONEncoder�default)�self�objs  �0/usr/share/netplan/netplan_cli/cli/state_diff.pyrzDiffJSONEncoder.defaults3���c�<�(��;�;�=� ����'�'��c�2�2�N)�__name__�
__module__�__qualname__r�rrrrs��3rrc��eZdZdZdedefd�Zdefd�Zd+de	defd�Z
defd	�Zd
e	de	dedefd�Z
d
ededdfd�Zdedefd�Zdedefd�Zde	de	fd�Zd
ededdfd�Zd
ededdfd�Zd
ededdfd�Zd
ededdfd�Zdede	ddfd�Zd
ededdfd�Zdedefd�Zd
edeefd �Zd!eed"ee	d
edefd#�Zdefd$�Zdeddfd%�Z defd&�Z!d'edefd(�Z"d)e	defd*�Z#y),�NetplanDiffStatez�
    DiffState is mainly responsible for getting both system's and Netplan's configuration
    state, compare them and provide a data-structure containing the differences it found.
    �system_state�
netplan_statec�.�||_||_i|_yr)r r!�route_lookup_table_names)rr r!s   r�__init__zNetplanDiffState.__init__,s��(���*���(*��%r�returnc��dii}|j�}|j�}tt|j	��t|j	��z�}|D]
}i|d|<�|j�D]\}}|d|j
|��|j�D]\}}|d|j
|��|jjD]!}|j|d|jd<�#|S)z�
        Return the states of both the system and Netplan in a common representation
        that makes it easier to compare them.
        �
interfaces�data_sources)�_get_system_interfaces�_get_netplan_interfaces�set�list�keys�items�updater �interface_listr(�name)r�
full_state�system_interfaces�netplan_interfaces�all_interfaces�	interface�configs       r�get_full_statezNetplanDiffState.get_full_state2s0��
�"�
�
�!�7�7�9��!�9�9�;���T�"3�"8�"8�":�;�d�CU�CZ�CZ�C\�>]�]�^��'�	5�I�24�J�|�$�Y�/�	5�"3�!8�!8�!:�	?��I�v��|�$�Y�/�6�6�v�>�	?�"4�!9�!9�!;�	?��I�v��|�$�Y�/�6�6�v�>�	?��*�*�9�9�	^�I�GP�G]�G]�J�|�$�Y�^�^�4�^�D�	^��rr6c��|j�}|j|jdi��}|r|j|�x}r||i}ni}|j�}|j	||�|j�D]�\}}|jdi�jd�}|jdi�jd�}|j
|||�}|j||�|j||�|j||�|j||�|j||�|j||�|dj|���tt|dj�d����|d<|S)a�
        Compare the configuration of interfaces currently found in the system against Netplan configuration.
        A number of heuristics are used to eliminate configuration that is automatically set in the system,
        such as certain routes and IP addresses. That is necessary because this configuration will not be found
        in Netplan. For example, if Netplan is enabling DHCP on an interface and not defining any extra IP addresses,
        we don't count the IPs automatically assigned to the interface as a difference. We do though count the eventual
        absence of addresses that should be assigned by DHCP as a difference.
        r'r �id�indexc�*�|djd�S)N�r;)�get)�ifaces r�<lambda>z+NetplanDiffState.get_diff.<locals>.<lambda>us��[`�ab�[c�[g�[g�ho�[p�r��key)r8�_get_comparable_interfacesr>�_create_new_report�_analyze_missing_interfacesr.�_create_new_iface�_analyze_ip_addresses�_analyze_nameservers�_analyze_search_domains�_analyze_mac_addresses�_analyze_routes�_analyze_parent_linksr/�dict�sorted)	rr6r2r'r7�report�	netdef_idr;r?s	         r�get_diffzNetplanDiffState.get_diffOsx���(�(�*�
��4�4�Z�^�^�L�RT�5U�V�
��#���	�2�2�v�2�'��0�
��
��(�(�*���(�(���;�!+�!1�!1�!3�	/��I�v��
�
�>�2�6�:�:�4�@�I��J�J�~�r�2�6�6�w�?�E��*�*�9�i��G�E��&�&�v�u�5��%�%�f�e�4��(�(���7��'�'���6�� � ���/��&�&�v�u�5��<� �'�'��.�	/� $�F�6�,�+?�+E�+E�+G�Mp�$q�r��|���
rc��iiid�S)N)r'�missing_interfaces_system�missing_interfaces_netplanr)rs rrDz#NetplanDiffState._create_new_reportxs���)+�*,�
�	
rrPr;c��||||iid�iS)N)r;r1r:r r!r)rrPr6r;s    rrFz"NetplanDiffState._create_new_ifaces#����!�� "�!#��
�	
rr7r?Nc�$�t|j��d}|jdi�jdg�D�chc]}|��}}|j|�}|jdi�jdd�}|jdi�jdd�}|jdi�jdg�}t	�}	|jdi�jdi�j�D�]t\}
}t
j|
�}|jd	g�}hd
�j|�s|	j|
�d|vr~|jrrt|jtj�rd|vr|	j|
�t|jtj�rd
|vr|	j|
�t|jtj�rddhj|�rd}t|jtj�rd|vrd}|jdi�jd�}
d|vs��^|
dus��d|	j|
���w|j|	�}|	j|�}|rd||dd<|rd||dd<|r+||dj!dtt#|��i�|r,||dj!dtt#|��i�yycc}w)Nrr!�	addresses�dhcp4F�dhcp6�
link_localr �flags>�ra�dhcp�link�dynamicr^�ipv4�ipv6r]r_�	accept_rar\T�missing_dhcp4_address�missing_dhcp6_address�missing_addresses)r,r-r>�_normalize_ip_addressesr+r.�	ipaddress�ip_interface�intersection�add�
is_link_localr�ip�IPv4Address�IPv6Address�
differencer/rN)rr7r?r1rl�netplan_ipsrcrdrZ�
system_ips�addr�	addr_datar[rb�present_only_in_netplan�present_only_in_systems                rrGz&NetplanDiffState._analyze_ip_addresses�s����E�J�J�L�!�!�$��$*�J�J���$C�$G�$G��UW�$X�Y�b�r�Y��Y��2�2�;�?�� &�
�
�?�B� ?� C� C�G�U� S�� &�
�
�?�B� ?� C� C�G�U� S���Z�Z���4�8�8��r�J�
��U�
�%�z�z�.�"�=�A�A�+�r�R�X�X�Z�"	%�O�D�)��'�'��-�B��M�M�'�2�.�E�5�A�A�%�H����t�$�
���2�#3�#3��b�e�e�Y�%:�%:�;��j�@X��N�N�4�(��b�e�e�Y�%:�%:�;��j�@X��N�N�4�(��"�%�%��!6�!6�7��I�&�3�3�E�:�,1�)��"�%�%��!6�!6�7��U�?�,1�)��
�
�?�B�7�;�;�K�H�I��u�}��e�!3����t�$�E"	%�H#.�"8�"8��"D��!+�!6�!6�{�!C�� �CG�E�$�K��'�(?�@� �CG�E�$�K��'�(?�@�!��$�K��(�/�/�#�T�&�1G�*H�%I�1�
�#��$�K��'�.�.�#�T�&�1H�*I�%J�0�
�#��sZs�	L
r'c���i}|j�D]O\}}|jd��|jd���)|jdi�jd�s�K|||<�Q|S)a� In order to compare interfaces, they must exist in the system AND in Netplan.
            Here we filter out interfaces that don't have a system_state, a netplan_state
            or a netdef ID.

            There is a special case where the interface will have a system_state and a netdef_id
            but will be missing in Netplan. That will happen when the user removes the interface
            only from Netplan but doesn't run netplan apply.
        r r!r:)r.r>)rr'�filteredr6r7s     rrCz+NetplanDiffState._get_comparable_interfaces�ss����!+�!1�!1�!3�	)��I�v��z�z�.�)�1�V�Z�Z��5P�5X���:�:�n�b�1�5�5�d�;��"(�H�Y��	)��rrWc�l�t�}|D]$}|j|�}|j|��&|S)zm Apply some transformations to IP addresses so their representation
        will match the system's.
        )r+�_compress_ipv6_addressrj)rrW�new_ips_setrls    rrfz(NetplanDiffState._normalize_ip_addresses�s>���e���	 �B��,�,�R�0�B��O�O�B��	 ��r�addressc��	tj|�}d|vr|jSt|j�S#t
$r|cYSwxYw)z�
        Compress IPv6 addresses to match the system representation
        Example: 1:2:0:0::123/64 -> 1:2::123/64
                 1:2:0:0::123 -> 1:2::123
        If "address" is not an IPv6Address, return the original value
        �/)rgrh�with_prefixlen�strrl�
ValueError)rr{rrs   rryz'NetplanDiffState._compress_ipv6_address�sL��	��)�)�'�2�D��g�~��*�*�*��t�w�w�<����	��N�	�s�$<�<�A
�	A
c�v�t|j��d}|jdi�jdi�}t|jdi�jdg��}t|jdi�jdg��}|D�chc]
}||dk(s�|��}}|D�chc]
}||dk(s�|��}	}|D�chc]
}||d	k(s�|��}
}|jdi�jd
�du}d|jdi�jd
g�v}|s|	s|
rK|jdi�jd�r||z
}|r||	z
}|D�chc]	}||
vs�|��}
}|r�|r�||
z
}n�|s�|jdi�jd�r<|D�chc]1}t	tj|�t
j�s|��3}}|jdi�jd�r<|D�chc]1}t	tj|�t
j�s|��3}}|j|�}|j|�}|r"||djdt|�i�|r#||djdt|�i�yycc}wcc}wcc}wcc}wcc}wcc}w)Nrr(�dnsr!�nameservers_addressesr �DHCPv4�DHCPv6�NDiscrbFrarZrXrY�missing_nameservers_addresses)r,r-r>r+rrg�
ip_addressrmrnror/)rr7r?r1r(�netplan_nameservers�system_nameservers�ns�dhcp4_ns�dhcp6_ns�ndisc_ns�accept_ra_enabled�link_local_enabled�
link_local_nsrtrus                rrHz%NetplanDiffState._analyze_nameservers�s����E�J�J�L�!�!�$���z�z�.�"�5�9�9�%��D��
"�&�*�*�_�b�"A�"E�"E�F]�_a�"b�c�� ����N�B�!?�!C�!C�D[�]_�!`�a��!-�N�2��b�1A�X�1M�B�N��N�!-�N�2��b�1A�X�1M�B�N��N�!-�M�2��b�1A�W�1L�B�M��M�#�J�J���;�?�?��L�TY�Y��#�v�z�z�/�2�'F�'J�'J�<�Y[�'\�\���x�8��z�z�/�2�.�2�2�7�;�%7�(�%B�"�!�%7�(�%B�"�*<�O�B��h��R�O�M�O�!�&7�%7�-�%G�"�'��:�:�o�r�2�6�6�w�?�7I�*n��1;�I�<P�<P�QS�<T�V_�Vk�Vk�1l�+-�*n�&�*n��:�:�o�r�2�6�6�w�?�7I�*n��1;�I�<P�<P�QS�<T�V_�Vk�Vk�1l�+-�*n�&�*n�#6�"@�"@�AS�"T��!3�!>�!>�?R�!S��!��$�K��(�/�/�/��6L�1M�1�
�#��$�K��'�.�.�/��6M�1N�0�
�#��]O��N��M��P��*n��*ns<�
J�'J�1
J"�?J"�	
J'�J'�	J,�%J,�6J1�96J6c��t|j��d}t|jdi�jdg��}t|jdi�jdg��}|jdi�jdi�}|D�chc]
}||dk(s�|��}}|D�chc]
}||dk(s�|��}	}|s|	rQ|jdi�jd	�r||z
}|jdi�jd
�du}
|
rT||	z
}nN|sL|jdi�jd	�s!|jdi�jd�r
t�}|j	|�}|j	|�}|r"||djd
t|�i�|r#||djd
t|�i�yycc}wcc}w)Nrr!�nameservers_searchr r(�searchr�r�rXrbFrY�missing_nameservers_search)r,r-r+r>ror/)
rr7r?r1�netplan_search_domains�system_search_domainsr(�s�dhcp4_search�dhcp6_searchr�rtrus
             rrIz(NetplanDiffState._analyze_search_domains7s����E�J�J�L�!�!�$��!$�V�Z�Z���%D�%H�%H�I]�_a�%b�!c�� #�F�J�J�~�r�$B�$F�$F�G[�]_�$`� a���z�z�.�"�5�9�9�(�B�G��#/�O�a�<��?�h�3N��O��O�#/�O�a�<��?�h�3N��O��O��<��z�z�/�2�.�2�2�7�;�(=��(L�%�!'�
�
�?�B� ?� C� C�K� P�X]� ]�� �(=��(L�%�*��:�:�o�r�2�6�6�w�?�6�:�:�o�_a�Cb�Cf�Cf�gn�Co�,/�E�)�"8�"C�"C�DY�"Z��!6�!A�!A�BX�!Y��!��$�K��(�/�/�,�d�3I�.J�1�
�#��$�K��'�.�.�,�d�3J�.K�0�
�#��;P��Os�
G�'G�1
G�?Gc�X�t|j��d}|jdi�jd�}|jdi�jd�}|rt|�sy|r<|r9||k7r3||dj	d|i�||dj	d|i�yyyy)Nrr �
macaddressr!�missing_macaddress)r,r-r>rr/)rr7r?r1�system_macaddress�netplan_macaddresss      rrJz'NetplanDiffState._analyze_mac_addresses^s����E�J�J�L�!�!�$��"�J�J�~�r�:�>�>�|�L��#�Z�Z���<�@�@��N���&9�:L�&M���!3� �$6�6��d��N�+�2�2�(�*<�4���d��O�,�3�3�(�*;�5��	7�"4�rc�.�t|j��d}t|jdi�jdg��}t|jdi�jdg��}|j	|�}|j|jdi��x}r|j
|�|jdi�jdi�D�cgc]}|��}}|j|||�}|j|�}	|j|�}
|
r3||dj
dt|
d���D�cgc]}|��c}i�|	r4||dj
dt|	d	���D�cgc]}|��c}i�yycc}wcc}wcc}w)
Nrr!�routesr rW�missing_routesc��|jSr��to��rs rr@z2NetplanDiffState._analyze_routes.<locals>.<lambda>�s
��cd�cg�cg�rrAc��|jSrr�r�s rr@z2NetplanDiffState._analyze_routes.<locals>.<lambda>�s
��de�dh�dh�r)
r,r-r+r>�_normalize_routes�_normalize_gatewaysr/�_filter_system_routesrorN)rr7r?r1�netplan_routes�
system_routes�netplan_gatewaysrl�system_addressesrtru�routes            rrKz NetplanDiffState._analyze_routesqs����E�J�J�L�!�!�$���V�Z�Z���<�@�@��2�N�O���F�J�J�~�r�:�>�>�x��L�M�
��/�/��?�� $�7�7��
�
�?�TV�8W�X�X��X��!�!�"2�3�*0���N�B�)G�)K�)K�K�Y[�)\�]�2�B�]��]��2�2�=�BR�TZ�[�
�"0�";�";�M�"J��!.�!9�!9�.�!I��!��$�K��(�/�/� �f�=S�Yg�6h�"i�U�5�"i�1�
�#��$�K��'�.�.� �f�=T�Zh�6i�"j�U�5�"j�0�
�#��^��#j��
#ks�	F�	F
�5	F
rOc�����jjD�chc]}|��}}�jjD�chc]}|js�|j��}}|j|�}t
t�fd�|��}g}�jjD],}|j|vs�|j|j��.t|�}t|�}�rt�fd�|�}t�fd�|�}�jj�}|D]O}�jjj|�j}	dtj|	d�i|d|<�Q|D]J}|j|�jdd�|j|�jd�d�|d	|<�Lycc}wcc}w)
Nc�h���jjj|�jdk7S)N�wifis)r!�netdefsr>�type)�irs �rr@z>NetplanDiffState._analyze_missing_interfaces.<locals>.<lambda>�s*���T�-?�-?�-G�-G�-K�-K�A�-N�-S�-S�W^�-^�rc���|�k(Srr�r�r6s �rr@z>NetplanDiffState._analyze_missing_interfaces.<locals>.<lambda>�s���A��N�rc���|�k(Srrr�s �rr@z>NetplanDiffState._analyze_missing_interfaces.<locals>.<lambda>�s���1�	�>�rr��otherrSr;)r�r;rT)r!r�r r0rPror,�filter�appendr1rN�get_datar>r�r
)
rrOr6r?r4�system_interfaces_netdef_ids�netplan_only�system_onlyr �
iface_types
` `       rrEz,NetplanDiffState._analyze_missing_interfaces�s����15�1C�1C�1K�1K�L��e�L��L�EI�EV�EV�Ee�Ee�'y�E�in�ix�ix����'y�$�'y�)�4�4�5Q�R���F�#^�`l�m�n�����&�&�5�5�	/�E����&8�8��"�"�5�:�:�.�	/��l�+���[�)���!�":�L�I�L� �!9�;�G�K��(�(�1�1�3��!�	�E��+�+�3�3�7�7��>�C�C�J���(�(��W�=�:�F�.�/��6�	�!�	�E�$�(�(��/�3�3�F�G�D�%�)�)�%�0�4�4�W�=�;�F�/�0��7�	��9M��'ys�	G�G	�G	c�b�t|j��d}|jdi�jd�|jdi�jd�g}|jdi�jd�|jdi�jd�g}|jdi�jd�|jdi�jd�g}|jdi�jdg�|jdi�jdg�g}|ddgk7r1|d|d	k7r&|dr|d||dd
<|d	r|d	||dd
<|ddgk7r1|d|d	k7r&|dr|d||dd<|d	r|d	||dd<|ddgk7r1|d|d	k7r&|dr|d||dd<|d	r|d	||dd<|gggk7rZt|d�}t|d	�}	||	k7r8|	|z
x}
rt|
�||dd
<||	z
x}rt|�||dd
<yyyy)z�
        Analyse if interfaces such as bonds, bridges and VRFs are correctly attached to their
        members and vice versa.
        rr �bondr!�bridge�vrfr'Nr=�missing_bond_link�missing_bridge_link�missing_vrf_link�missing_interfaces)r,r-r>r+)rr7r?r1r�r�r�r'�system�netplan�missing_system�missing_netplans            rrLz&NetplanDiffState._analyze_parent_links�s���
�E�J�J�L�!�!�$���
�
�>�2�.�2�2�6�:�F�J�J��XZ�<[�<_�<_�`f�<g�h���*�*�^�R�0�4�4�X�>��
�
�?�\^�@_�@c�@c�dl�@m�n���z�z�.�"�-�1�1�%�8�&�*�*�_�VX�:Y�:]�:]�^c�:d�e���j�j���4�8�8��r�J�F�J�J�Wf�hj�Lk�Lo�Lo�p|�A�MB�C�
��D�$�<��D��G�t�A�w�$6��A�w�DH��G��d��O�,�-@�A��A�w�CG��7��d��N�+�,?�@��d�D�\�!�f�Q�i�6�!�9�&<��a�y�FL�Q�i��d��O�,�-B�C��a�y�EK�A�Y��d��N�+�,A�B��4��,��3�q�6�S��V�#3��1�v�CF�q�6��d��O�,�-?�@��1�v�BE�a�&��d��N�+�,>�?��"�b��!���A��'�F��*�Q�-�(�G��� �%,�v�%5�5�>�5�HL�^�H\�E�$�K��/�0D�E�&,�w�&6�6�?�6�IM�o�I^�E�$�K��0�1E�F�7�	!�	"rr�c��t�}|D]�}|jtjk(r|j	d�|_|j|j�|_|j|j�|_|j|j�|_|jdk7r5|jjd�}|ddk(s|ddk(r
|d|_|j|���|S)zo Apply some transformations to Netplan routes so their representation
        will match the system's.
        �mainrr}r=�32�128r)r+�tabler�_TABLE_UNSPEC_�$_default_route_tables_name_to_numberryr��	from_addr�via�splitrj)rr��new_routes_setr��	ip_prefixs     rr�z"NetplanDiffState._normalize_routes�s�������	&�E��{�{�l�9�9�9�"�G�G��O����2�2�5�8�8�<�E�H�"�9�9�%�/�/�J�E�O��3�3�E�I�I�>�E�I��x�x�9�$�!�H�H�N�N�3�/�	��Q�<�4�'�9�Q�<�5�+@�(��|�E�H����u�%�#	&�&�rc�2�t�}|jj�}|jd�}|j	�D�cgc]}|jd�|k(r|��}}|D��cgc]}|jdg�D]}|���}}}|jd�x}	r�|D�cgc]N}|jd�dk(r8|jd�t
jk(r|jd�dk(r|��P}
}td|	t
jd�	�}t|
�d
k(rI|
djd�x}r||_	|
djd
�x}r|j|�|_|j|�|jd�x}
r�|D�cgc]N}|jd�dk(r8|jd�tjk(r|jd�dk(r|��P}
}|j|
�}
td|
tjd�	�}t|
�d
k(rI|
djd�x}r||_	|
djd
�x}r|j|�|_|j|�|Scc}wcc}}wcc}wcc}w)a� Convert the gateway4 and gateway6 properties to NetplanRoutes.
        The only information stored by these properties is the destination IP for
        the default route. Networkd and Network Manager will set a default metric
        for it when the route is installed. As we have no control over it, there
        will always be a diff. The same is true for the route table number.
        To avoid the noise in the diff report when these properties are used,
        we use the following heuristic: if there is one and only one static default
        route in the system and gateway4 and/or gateway6 are used, we set the same
        metric and table as found in the system.
        r:r��gateway4r�r�family�protocol�static)r�r�r�r�r=r�metricr��gateway6)r+r r�r>�valuesr�valuer�lenr�r�r�rjrry)rr7�
routes_setr3r6r?�ifacesr�r�r��default_routesr�r�r�s              rr�z$NetplanDiffState._normalize_gateways�s����U�
� �-�-�6�6�8���J�J�t�$�	�%6�%=�%=�%?�3�E��Y�Y�t�_�	�1��3��3�%+�Q�E����8�R�9P�Q��%�Q�%�Q��Q��z�z�*�-�-�8�-�17�E��!&���4��I�!=�"'�)�)�H�"5����"F�"'�)�)�J�"7�8�"C�$�E�N�E�
!�I�8�G�M�M�\d�e�E��>�"�a�'�+�A�.�2�2�8�<�<�6�<�#)�E�L�*�1�-�1�1�'�:�:�5�:�"&�"K�"K�E�"R�E�K��N�N�5�!��z�z�*�-�-�8�-�17�E��!&���4��I�!=�"'�)�)�H�"5����"G�"'�)�)�J�"7�8�"C�$�E�N�E��2�2�8�<�H� �I�8�H�N�N�]e�f�E��>�"�a�'�+�A�.�2�2�8�<�<�6�<�#)�E�L�*�1�-�1�1�'�:�:�5�:�"&�"K�"K�E�"R�E�K��N�N�5�!����O3��Q��E�� Es�J�, J	�&AJ�AJr�r�c�z�|D�cgc]*}ttj|�j���,}}t	td�|��}|D�cgc]*}ttj|�j���,}}|jdi�jdg�}t�}|D�]�}	|	jdk(r9|	jdk7r*tj|	j�js�L|	jdk(r�\|jdi�jd�}
|	jdk(r|
d	ur��|	jdk7rptj|	j�}|jrE|	jtjk(rd
|vr��|	jt jk(rd|vr��|	jdk(rH|	j"d
k(r9|	j|vs)tj|	j�j$r��g|	jtjk(r |	j"dk(r|	jdk(r���|	jtjk(r-|	jdk7r|	j|vs|	j|vr���|j'|	���|Scc}wcc}w)a�
        Some routes found in the system are installed automatically/dynamically without
        being configured in Netplan.
        Here we implement some heuristics to remove these routes from the list we want
        to compare. We do that because these type of routes will probably never be found in the
        Netplan configuration so there is no point in comparing them against Netplan.
        c��|dk7S)Nz	fe80::/64r)�ns rr@z8NetplanDiffState._filter_system_routes.<locals>.<lambda>4s��q�K�/?�rr!rZr^rr]rbr\Frar`�host�local�	multicastzff00::/8)rrgrh�networkr,r�rlr>r+�scoper�rkr�r�rr�rr��is_loopbackrj)rr�r�r7rl�local_networksrWrZr�r�rb�route_tos            rr�z&NetplanDiffState._filter_system_routes)s;��M]�]�b�#�i�4�4�R�8�@�@�A�]��]��f�%?��P�Q��BR�S�B�S��/�/��3�6�6�7�S�	�S��Z�Z���4�8�8��r�J�
����"�&	�E��{�{�f�$����Y�)>�y�G]�G]�^c�^f�^f�Gg�Gu�Gu���~�~��'���
�
�?�B�7�;�;�K�H�I��~�~��%�)�5�*@���x�x�9�$�$�1�1�%�(�(�;���)�)��|�|�x�~�~�5�&�J�:N� ��|�|�w�}�}�4��:�9M� ����v�%�%�*�*��*?��X�X��*�i�.D�.D�U�X�X�.N�.Z�.Z���|�|�x�~�~�-�%�*�*��2K�PU�PX�PX�\f�Pf��������.�5�>�>�T�3I����^�3�u�x�x�9�7L���J�J�u��M&	�N�
��[^��Ts�/J3�/J8c��|jj�}i}|jjj	�D�]q\}}i}dd|ii||<||d}tj|jd�|d<|j|d<|j|d<|j|d<|j�|j|d<|jD�cgc]}|��}}|rWi|d	<|D]M}i}	|jr|j|	d
<|jr|j|	d<d|	i|d	t|�<�Ot!|j"�x}
r|
|d
<t!|j$�x}r||d<t!|j&�x}r||d<|j(x}
r|
|d<|j*x}r||d<|j,x}r||d<|j.j
d�x}r|j0|d<|j.j
d�x}r|j0|d<|j.j
d�x}r|j0|d<||vrUd}|j	�D])\}}|j
d�x}s�||k(s� d}||||<�+|r��N|j3|���a|j3|���t|j5|�|Scc}w)Nr!r:r�r�rXrYrZrbrW�label�lifetimer[r�r�r�r�r�r�r�r�r�FT)r r�r!r�r.r
r>r�rXrYrZrbrWr�r�rr,�nameserver_addresses�nameserver_searchr��	_gateway4�	_gateway6r��linksr:r/�_netplan_state_find_parents)rr3r'r6r7r?�	iface_refrrrWr[�nameserversr�r�r�r��macr�r�r��
found_somerBr�rPs                       rr*z(NetplanDiffState._get_netplan_interfacesas�� �-�-�6�6�8���
�!%�!3�!3�!;�!;�!A�!A�!C�J	)��I�v��E� /�$�	�1B�C�E�)���i�(��9�I� ,� 0� 0����g� F�I�f��!'���I�g��!'���I�g��&,�&7�&7�I�l�#����+�)/�)9�)9�	�+�&�*0�*:�*:�;�$��;�I�;��)+�	�+�&�%�I�D��E��z�z�)-����g���}�}�,0�M�M��j�)�9@�%�8H�I�k�*�3�t�9�5�
I�#�6�#>�#>�?�?�{�?�5@�	�1�2��f�6�6�7�7�v�7�28�	�.�/��f�m�m�,�,�v�,�&,�	�(�#�!�+�+�+�x�+�(0�	�*�%�!�+�+�+�x�+�(0�	�*�%��'�'�'�s�'�*-�	�,�'����)�)�(�3�3�v�3�&,�i�i�	�(�#��|�|�'�'��/�/�t�/�$(�G�G�	�&�!��l�l�&�&�u�-�-�s�-�#&�6�6�	�%� �� 1�1�#�
�"3�"9�"9�";�?�J�C��$)�I�I�d�O�3�y�3�$�	�1�)-�J�.3�I�.>�J�s�O�	?�"��%�%�e�,��!�!�%�(�UJ	)�X	
�(�(��4����{<s�	K	c��tt�}|j�D]�\}}|djd�x}r||j	|�|djd�x}r||j	|�|djd�x}s�q||j	|���|j�D]\}}t|�||dd<�y)z* Associates interfaces with their parents r!r�r�r�r'N)rr+r.r>rjr,)rr'�parentsr6r7r^�memberss       rr�z,NetplanDiffState._netplan_state_find_parents�s����c�"��!+�!1�!1�!3�	-��I�v��o�.�2�2�8�<�<�t�<���
�!�!�)�,��o�.�2�2�6�:�:�t�:���
�!�!�)�,��o�.�2�2�5�9�9�t�9���
�!�!�)�,�
	-�#*�-�-�/�	Q��I�w�CG��=�J�y�!�/�2�<�@�	Qrc��i}|jj�j�D�]�\}}|dk(r�
|jd�}dd|ii||<|jd�x}r|||dd<||d}|jd�x}r||d<i}|jdg�D]k}	t	|	j��d}
|	j|
�jd�}|
�d	|��}d
|	j|
�jd
g�i||<�m|r||d<|jd�x}
r|
|d<|jd
�x}r||d<|jd�x}r"|D�cgc]}|j
|���c}|d<|jd�x}r||d<|jd�x}r||d<|jd�x}r||d<|jd�x}r||d<|jd�x}s���||d<���|Scc}w)Nznetplan-global-stater�r r:r;rWr�prefixr}r[�
dns_addressesr��
dns_searchr�r�r�r'r�r�r�)r r�r.r>r,r-�_system_route_to_netplan)rr'r6r7�device_typerPr�r;rWrrrlr�	full_addrr�r�r�r�r��uplink_interfacesr�r�r�s                      rr)z'NetplanDiffState._get_system_interfaces�sa���
�!%�!2�!2�!;�!;�!=�!C�!C�!E�/	'��I�v��2�2�� �*�*�V�,�K�%3�f�k�5J�$K�J�y�!�"�J�J�t�,�,�y�,�>G�
�9�%�n�5�d�;�"�9�-�n�=�I��
�
�7�+�+�u�+�%*�	�'�"��I��
�
�;��3�
P���$�)�)�+�&�q�)�����"��)�)�(�3��!�d�!�F�8�,�	�(/����"��1A�1A�'�2�1N�'O�	�)�$�
P��)2�	�+�&�$�j�j��9�9�{�9�5@�	�1�2����L�1�1�v�1�28�	�.�/����H�-�-�v�-�Y_�&`�PU�t�'D�'D�U�'K�&`�	�(�#��j�j��.�.�s�.�*-�	�,�'�$*�J�J�|�$<�<� �<�*;�	�,�'��z�z�&�)�)�t�)�$(�	�&�!����H�-�-�v�-�&,�	�(�#��j�j��'�'�s�'�#&�	�%� �_/	'�b���#'as� G=�system_routec���i}|jd�x}r||d<|jd�x}r||d<|jd�x}r||d<|jd�x}r||d<|jd�x}r||d<|jd�x}r||d<|jd�x}	r|	|d<|jd	�x}
r|
|d	<|jd
�x}r|j|�|d
<tdi|��S)Nr�r�r��fromr�r�r�r�r�r�r)r>r�r)rr
r�r�r�r�r�r�r��
route_typer�r�s            rrz)NetplanDiffState._system_route_to_netplan�s9����!�%�%�h�/�/�6�/�$�E�(�O��!�!�$�'�'�2�'��E�$�K��"�"�5�)�)�3�)��E�%�L�$�(�(��0�0�9�0�!*�E�+��!�%�%�h�/�/�6�/�$�E�(�O� �$�$�W�-�-�5�-�"�E�'�N�%�)�)�&�1�1�:�1�&�E�&�M�#�'�'�
�3�3�8�3� (�E�*�� �$�$�W�-�-�5�-�!�F�F�u�M�E�'�N��$�e�$�$rr1c��|j�rt|�S|jst�|_|jj	|d�S)Nr)�isdigit�intr#rr>)rr1s  rr�z5NetplanDiffState._default_route_tables_name_to_numbersB���<�<�>��t�9���,�,�,>�,@�D�)��,�,�0�0��q�9�9r)�)$rrr�__doc__rr	r$rMr8rrQrDrrFrGrCr+rfryrHrIrJrKrErLr�rrr�r,r�r*r�r)rr�rrrrr&s���
+�%6�+�GY�+����:'�#�'�t�'�R
�D�
�	
�3�	
�3�	
�s�	
�t�	
�>�D�>��>�$�>�@�T��d��,	��	��	�
�c�
�c�
�;�4�;��;��;�z%�d�%�4�%�D�%�N�T��$��4��&�d��4��D��6!�$�!�3�!�4�!�F&_�D�&_��&_�$�&_�P�����46�$�6�;�|�3L�6�p6�;�|�3L�6�`d�eh�`i�6�sw�6�|�6�pP��P�dQ�d�Q�t�Q�4��4�l%�T�%�l�%�0:��:��:rr)�collectionsrrgr�socketrr�typingr�netplan.netdefr�netplan_cli.cli.staterr	r
�netplan_cli.cli.utilsrrrrrrrr�<module>rs@��$$���$��'�U�U�I�3�d�&�&�3�p:�p:r