@@ -56,6 +56,8 @@ def run(self, tmp=None, task_vars=None):
5656 existing_link ['sw1-info' ]['sw-sys-name' ] = switch ['management' ]['management_ipv4_address' ]
5757 if existing_link ['sw2-info' ]['sw-sys-name' ].lower () == switch ['name' ].lower ():
5858 existing_link ['sw2-info' ]['sw-sys-name' ] = switch ['management' ]['management_ipv4_address' ]
59+
60+ # Check if the existing link matches the fabric link in either direction
5961 if ((existing_link ['sw1-info' ]['sw-sys-name' ].lower () == link ['src_device' ].lower () and
6062 existing_link ['sw1-info' ]['if-name' ].lower () == link ['src_interface' ].lower () and
6163 existing_link ['sw2-info' ]['sw-sys-name' ].lower () == link ['dst_device' ].lower () and
@@ -64,11 +66,44 @@ def run(self, tmp=None, task_vars=None):
6466 existing_link ['sw1-info' ]['if-name' ].lower () == link ['dst_interface' ].lower () and
6567 existing_link ['sw2-info' ]['sw-sys-name' ].lower () == link ['src_device' ].lower () and
6668 existing_link ['sw2-info' ]['if-name' ].lower () == link ['src_interface' ].lower ())):
69+
70+ # If the link is in reverse order, swap the src and dst to match
71+ # swap also in profile peer1 and peer2
72+ # Don't swap IP addresses, bc they are assigned based on existing link
73+ # and should not change
74+ if (
75+ existing_link ['sw1-info' ]['sw-sys-name' ].lower () == link ['dst_device' ].lower ()
76+ and existing_link ['sw1-info' ]['if-name' ].lower () == link ['dst_interface' ].lower ()
77+ and existing_link ['sw2-info' ]['sw-sys-name' ].lower () == link ['src_device' ].lower ()
78+ and existing_link ['sw2-info' ]['if-name' ].lower () == link ['src_interface' ].lower ()
79+ ):
80+ link ['src_device' ], link ['dst_device' ] = link ['dst_device' ], link ['src_device' ]
81+ link ['src_interface' ], link ['dst_interface' ] = link ['dst_interface' ], link ['src_interface' ]
82+ # Safely swap descriptions without creating keys when absent
83+ p1_desc = link ['profile' ].pop ('peer1_description' , None )
84+ p2_desc = link ['profile' ].pop ('peer2_description' , None )
85+ if p2_desc is not None :
86+ link ['profile' ]['peer1_description' ] = p2_desc
87+ if p1_desc is not None :
88+ link ['profile' ]['peer2_description' ] = p1_desc
89+ # Safely swap freeform without creating keys when absent
90+ p1_free = link ['profile' ].pop ('peer1_freeform' , None )
91+ p2_free = link ['profile' ].pop ('peer2_freeform' , None )
92+ if p2_free is not None :
93+ link ['profile' ]['peer1_freeform' ] = p2_free
94+ if p1_free is not None :
95+ link ['profile' ]['peer2_freeform' ] = p1_free
96+
6797 if 'templateName' not in existing_link :
6898 not_required_links .append (link )
6999 elif existing_link ['templateName' ] == 'int_pre_provision_intra_fabric_link' :
70100 required_links .append (link )
71101 elif existing_link ['templateName' ] == 'int_intra_fabric_num_link' :
102+ # Populate additional fields from existing link
103+ # into the required link. IPs are assigned by ND and not managed in NaC Fabric link
104+ # at this time.
105+ # When template is defined as int_pre_provision_intra_fabric_link, template is converted
106+ # by ND to int_intra_fabric_num_link, when fabric-link is P2P and IPs are assigned.
72107 link ['template' ] = 'int_intra_fabric_num_link'
73108 link ['profile' ]['peer1_ipv4_addr' ] = existing_link ['nvPairs' ]['PEER1_IP' ]
74109 link ['profile' ]['peer2_ipv4_addr' ] = existing_link ['nvPairs' ]['PEER2_IP' ]
0 commit comments