Refactor and clean up some repetitive code

This commit is contained in:
Pim van Pelt
2022-04-12 11:04:13 +00:00
parent 594e21afa4
commit eee718c77c
2 changed files with 17 additions and 35 deletions

View File

@ -516,15 +516,11 @@ class Reconciler():
config_ifname, config_iface = interface.get_by_lcp_name(self.cfg, lcp.host_if_name) config_ifname, config_iface = interface.get_by_lcp_name(self.cfg, lcp.host_if_name)
if not config_iface: if not config_iface:
## Interface doesn't exist in the config ## Interface doesn't exist in the config
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
if not 'lcp' in config_iface: if not 'lcp' in config_iface:
## Interface doesn't have an LCP ## Interface doesn't have an LCP
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
if vpp_iface.sub_number_of_tags == 2: if vpp_iface.sub_number_of_tags == 2:
vpp_parent_idx = self.__parent_iface_by_encap(vpp_iface.sup_sw_if_index, vpp_iface.sub_outer_vlan_id, vpp_iface.sub_if_flags&8) vpp_parent_idx = self.__parent_iface_by_encap(vpp_iface.sup_sw_if_index, vpp_iface.sub_outer_vlan_id, vpp_iface.sub_if_flags&8)
@ -533,29 +529,21 @@ class Reconciler():
config_parent_ifname, config_parent_iface = interface.get_by_lcp_name(self.cfg, parent_lcp.host_if_name) config_parent_ifname, config_parent_iface = interface.get_by_lcp_name(self.cfg, parent_lcp.host_if_name)
if not config_parent_iface: if not config_parent_iface:
## QinX's parent doesn't exist in the config ## QinX's parent doesn't exist in the config
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
if not 'lcp' in config_parent_iface: if not 'lcp' in config_parent_iface:
## QinX's parent doesn't have an LCP ## QinX's parent doesn't have an LCP
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
if parent_lcp.host_if_name != config_parent_iface['lcp']: if parent_lcp.host_if_name != config_parent_iface['lcp']:
## QinX's parent LCP name mismatch ## QinX's parent LCP name mismatch
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
config_parent_encap = interface.get_encapsulation(self.cfg, config_parent_ifname) config_parent_encap = interface.get_encapsulation(self.cfg, config_parent_ifname)
vpp_parent_encap = self.__get_encapsulation(vpp_parent_iface) vpp_parent_encap = self.__get_encapsulation(vpp_parent_iface)
if config_parent_encap != vpp_parent_encap: if config_parent_encap != vpp_parent_encap:
## QinX's parent encapsulation mismatch ## QinX's parent encapsulation mismatch
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
if vpp_iface.sub_number_of_tags > 0: if vpp_iface.sub_number_of_tags > 0:
@ -563,9 +551,7 @@ class Reconciler():
vpp_encap = self.__get_encapsulation(vpp_iface) vpp_encap = self.__get_encapsulation(vpp_iface)
if config_encap != vpp_encap: if config_encap != vpp_encap:
## Encapsulation mismatch ## Encapsulation mismatch
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
if vpp_iface.interface_dev_type=='Loopback': if vpp_iface.interface_dev_type=='Loopback':
@ -575,36 +561,31 @@ class Reconciler():
bond_iface = self.vpp.cache['interfaces'][vpp_iface.sup_sw_if_index] bond_iface = self.vpp.cache['interfaces'][vpp_iface.sup_sw_if_index]
if self.__bond_has_diff(bond_iface.interface_name): if self.__bond_has_diff(bond_iface.interface_name):
## If BondEthernet changed, it has to be re-created, so all LCPs must be removed. ## If BondEthernet changed, it has to be re-created, so all LCPs must be removed.
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
phy_lcp = lcps[vpp_iface.sup_sw_if_index] phy_lcp = lcps[vpp_iface.sup_sw_if_index]
config_phy_ifname, config_phy_iface = interface.get_by_lcp_name(self.cfg, phy_lcp.host_if_name) config_phy_ifname, config_phy_iface = interface.get_by_lcp_name(self.cfg, phy_lcp.host_if_name)
if not config_phy_iface: if not config_phy_iface:
## Phy doesn't exist in the config ## Phy doesn't exist in the config
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
if not 'lcp' in config_phy_iface: if not 'lcp' in config_phy_iface:
## Phy doesn't have an LCP ## Phy doesn't have an LCP
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
if phy_lcp.host_if_name != config_phy_iface['lcp']: if phy_lcp.host_if_name != config_phy_iface['lcp']:
## Phy LCP name mismatch ## Phy LCP name mismatch
cli="lcp delete %s" % (vpp_iface.interface_name) removed_lcps.append(lcp)
self.cli['prune'].append(cli);
removed_lcps.append(lcp.host_if_name)
continue continue
self.logger.debug("LCP OK: %s -> (vpp=%s, config=%s)" % (lcp.host_if_name, vpp_iface.interface_name, config_ifname)) self.logger.debug("LCP OK: %s -> (vpp=%s, config=%s)" % (lcp.host_if_name, vpp_iface.interface_name, config_ifname))
for lcpname in removed_lcps: for lcp in removed_lcps:
self.vpp.cache_remove_lcp(lcpname) vpp_ifname = self.vpp.cache['interfaces'][lcp.phy_sw_if_index].interface_name
cli="lcp delete %s" % (vpp_ifname)
self.cli['prune'].append(cli);
self.vpp.cache_remove_lcp(lcp.host_if_name)
return True return True
def prune_admin_state(self): def prune_admin_state(self):

View File

@ -70,6 +70,7 @@ class VPPApi():
def cache_remove_lcp(self, lcpname): def cache_remove_lcp(self, lcpname):
""" Removes the LCP and TAP interface, identified by lcpname, from the config. """ """ Removes the LCP and TAP interface, identified by lcpname, from the config. """
found=False
for idx, lcp in self.cache['lcps'].items(): for idx, lcp in self.cache['lcps'].items():
if lcp.host_if_name == lcpname: if lcp.host_if_name == lcpname:
found = True found = True