original_docstring: Deletes a router interface from the host.
  
          Disables SNAT, if it is set on the interface.
          Deletes a loopback interface from the host, this removes the BVI
          interface from the local bridge. Also, delete the default route and
          SNAT address for the external interface.
          
docstring: Deletes a router interface from the host.
  Disables SNAT, if it is set on the interface.
  Deletes a loopback interface from the host, this removes the BVI
  interface from the local bridge. Also, delete the default route and
  SNAT address for the external interface.
docstring_tokens: Deletes a router interface from the host . Disables SNAT if it is set on the interface . Deletes a loopback interface from the host this removes the BVI interface from the local bridge . Also delete the default route and SNAT address for the external interface .
code: def delete_router_interface_on_host(self, port_id):
          """Deletes a router interface from the host.
  
          Disables SNAT, if it is set on the interface.
          Deletes a loopback interface from the host, this removes the BVI
          interface from the local bridge. Also, delete the default route and
          SNAT address for the external interface.
          """
          is_external = 0
  
          if port_id in self.router_interfaces:
              router = self.router_interfaces[port_id]
          elif port_id in self.router_external_interfaces:
              router = self.router_external_interfaces[port_id]
              is_external = 1
              ext_intf_ip = '{}/{}'.format(router['gateway_ip'],
                                           router['prefixlen'])
              # Get all local IP addresses in the external VRF belonging
              # to the same external subnet as this router.
              # Check if atleast one local_ip matches a neutron assigned
              # external IP address of the router.
              # If there's no match, there are no valid local IPs within VPP.
              local_gw_ips = [r['gateway_ip'] for
                              r in self.router_external_interfaces.values()
                              if r['is_local']]
  
              # While, in theory, there may be multiple IPs on an interface,
              # in practice, we only program one (and program additional
              # IPs via a local route).
  
              # TODO(ijw): this is a somewhat unclean way of removing IP
              # addresses attached to VPP interfaces that are in the
              # subnet of ext_intf_ip, I think.  Unclear if this is the
              # right way to do that versus remembering the interface.
              local_ip: Optional[str]
              for ip in self.vpp.get_local_ip_address(ext_intf_ip,
                                                      router['is_ipv6'],
                                                      router['vrf_id']):
                  # Is the local_ip valid?
                  if ip in local_gw_ips:
                      LOG.debug('Found a router external local_ip in VPP: %s',
                                ip)
                      local_ip = ip
                      break
              # For-else would mean no breaks i.e. no valid local_ips
              else:
                  local_ip = None
          else:
              LOG.error("Router port:%s deletion error...port not found",
                        port_id)
              return False
  
          net_br_idx = router['bridge_domain_id']
          bvi_if_idx = self.vpp.get_bridge_bvi(net_br_idx)
  
          # If an external local route, we can safetly delete it from VPP
          # Don't delete any SNAT
          if is_external and router['is_local']:
              LOG.debug("delete_router_intf: Removing the local route: %s/32",
                        router['gateway_ip'])
              prefixlen = 128 if router['is_ipv6'] else 32
              self.vpp.delete_ip_route(vrf=router['vrf_id'],
                                       ip_address=self._pack_address(
                                           router['gateway_ip']),
                                       prefixlen=prefixlen,
                                       next_hop_address=None,
                                       next_hop_sw_if_index=None,
                                       is_ipv6=router['is_ipv6'],
                                       is_local=True)
          # External router is a loopback BVI. If a local route exists,
          # replace the BVI's IP address with its IP address.
          # Don't delete the SNAT.
          elif is_external and local_ip is not None:
              LOG.debug('delete_router_intf: replacing router loopback BVI IP '
                        'address %s with the local ip address %s',
                        router['gateway_ip'], local_ip)
              # Delete the IP address from the BVI.
              if bvi_if_idx is not None:
                  self.vpp.del_interface_ip(
                      bvi_if_idx,
                      ip_interface((router['gateway_ip'],
                                    router['prefixlen'],)))
              # Delete the local route
              prefixlen = 128 if router['is_ipv6'] else 32
              self.vpp.delete_ip_route(vrf=router['vrf_id'],
                                       ip_address=self._pack_address(local_ip),
                                       prefixlen=prefixlen,
                                       next_hop_address=None,
                                       next_hop_sw_if_index=None,
                                       is_ipv6=router['is_ipv6'],
                                       is_local=True)
              if bvi_if_idx is not None:
                  self.vpp.set_interface_ip(
                      bvi_if_idx,
                      ip_interface((local_ip, router['prefixlen'],)))
              # Set the router external interface corresponding to the local
              # route as non-local.
              for router in self.router_external_interfaces.values():
                  if ip_address(router['gateway_ip']) == \
                      ip_address(local_ip):
                      router['is_local'] = 0
                      LOG.debug('Router external %s is no longer a local '
                                'route but now assigned to the BVI', router)
          else:
              # At this point, we can safetly remove both the SNAT and BVI
              # loopback interfaces as no local routes exist.
              snat_interfaces = self.vpp.get_snat_interfaces()
              # Get SNAT out interfaces whose IP addrs are overloaded
              snat_out_interfaces = self.vpp.get_outside_snat_interface_indices()
              # delete SNAT if set on this interface
              if router['bvi_if_idx'] in snat_interfaces:
                  LOG.debug('Router: Deleting SNAT on interface '
                            'index: %s', router['bvi_if_idx'])
                  self.vpp.set_snat_on_interface(router['bvi_if_idx'],
                                                 is_inside=router['is_inside'],
                                                 is_add=False)
              # Delete the external 1:N SNAT and default routes in all VRFs
              # for external router interface deletion
              if not router['is_inside']:
                  LOG.debug('Router: Deleting external gateway port %s for '
                            'router: %s', port_id, router)
                  # Delete external snat addresses for the router
                  if router['bvi_if_idx'] in snat_out_interfaces:
                      LOG.debug('Router:Removing 1:N SNAT on external interface '
                                'index: %s', router['bvi_if_idx'])
                      self.vpp.snat_overload_on_interface_address(
                          router['bvi_if_idx'],
                          is_add=False)
                  # Delete all exported routes into tenant VRFs belonging to this
                  # external gateway
                  self.export_routes_from_tenant_vrfs(
                      ext_gw_ip=router['external_gateway_ip'], is_add=False)
                  # delete the default route in the default VRF
                  self.default_route_in_default_vrf(router, is_add=False)
              else:
                  # Delete all exported routes from this VRF
                  self.export_routes_from_tenant_vrfs(source_vrf=router[
                      'vrf_id'], is_add=False)
              # Delete the gateway IP address and the BVI interface if this is
              # the last IP address assigned on the BVI
              if bvi_if_idx is not None:
                  # Get all IP's assigned to the BVI interface
                  addresses = self.vpp.get_interface_ip_addresses(bvi_if_idx)
                  if len(addresses) > 1:
                      # Dont' delete the BVI, only remove one IP from it
                      self.vpp.del_interface_ip(
                          bvi_if_idx,
                          ip_interface((router['gateway_ip'],
                                        router['prefixlen'],)))
                  else:
                      # Last subnet assigned, delete the interface
                      self.vpp.delete_loopback(bvi_if_idx)
                      if cfg.CONF.ml2_vpp.enable_l3_ha:
                          self.router_interface_states.pop(bvi_if_idx, None)
          # Remove any local GPE mappings
          if router['net_type'] == TYPE_GPE and self.gpe is not None:
              LOG.debug('Removing local GPE mappings for router '
                        'interface: %s', port_id)
              self.gpe.delete_local_gpe_mapping(router['segmentation_id'],
                                                router['mac_address'])
          if not is_external:
              self.router_interfaces.pop(port_id)
          else:
              self.router_external_interfaces.pop(port_id)
code_tokens: def delete_router_interface_on_host ( self , port_id ) : is_external = 0 if port_id in self . router_interfaces : router = self . router_interfaces [ port_id ] elif port_id in self . router_external_interfaces : router = self . router_external_interfaces [ port_id ] is_external = 1 ext_intf_ip = '{}/{}' . format ( router [ 'gateway_ip' ] , router [ 'prefixlen' ] ) local_gw_ips = [ r [ 'gateway_ip' ] for r in self . router_external_interfaces . values ( ) if r [ 'is_local' ] ] local_ip : Optional [ str ] for ip in self . vpp . get_local_ip_address ( ext_intf_ip , router [ 'is_ipv6' ] , router [ 'vrf_id' ] ) : if ip in local_gw_ips : LOG . debug ( 'Found a router external local_ip in VPP: %s' , ip ) local_ip = ip break else : local_ip = None else : LOG . error ( "Router port:%s deletion error...port not found" , port_id ) return False net_br_idx = router [ 'bridge_domain_id' ] bvi_if_idx = self . vpp . get_bridge_bvi ( net_br_idx ) if is_external and router [ 'is_local' ] : LOG . debug ( "delete_router_intf: Removing the local route: %s/32" , router [ 'gateway_ip' ] ) prefixlen = 128 if router [ 'is_ipv6' ] else 32 self . vpp . delete_ip_route ( vrf = router [ 'vrf_id' ] , ip_address = self . _pack_address ( router [ 'gateway_ip' ] ) , prefixlen = prefixlen , next_hop_address = None , next_hop_sw_if_index = None , is_ipv6 = router [ 'is_ipv6' ] , is_local = True ) elif is_external and local_ip is not None : LOG . debug ( 'delete_router_intf: replacing router loopback BVI IP ' 'address %s with the local ip address %s' , router [ 'gateway_ip' ] , local_ip ) if bvi_if_idx is not None : self . vpp . del_interface_ip ( bvi_if_idx , ip_interface ( ( router [ 'gateway_ip' ] , router [ 'prefixlen' ] , ) ) ) prefixlen = 128 if router [ 'is_ipv6' ] else 32 self . vpp . delete_ip_route ( vrf = router [ 'vrf_id' ] , ip_address = self . _pack_address ( local_ip ) , prefixlen = prefixlen , next_hop_address = None , next_hop_sw_if_index = None , is_ipv6 = router [ 'is_ipv6' ] , is_local = True ) if bvi_if_idx is not None : self . vpp . set_interface_ip ( bvi_if_idx , ip_interface ( ( local_ip , router [ 'prefixlen' ] , ) ) ) for router in self . router_external_interfaces . values ( ) : if ip_address ( router [ 'gateway_ip' ] ) == ip_address ( local_ip ) : router [ 'is_local' ] = 0 LOG . debug ( 'Router external %s is no longer a local ' 'route but now assigned to the BVI' , router ) else : snat_interfaces = self . vpp . get_snat_interfaces ( ) snat_out_interfaces = self . vpp . get_outside_snat_interface_indices ( ) if router [ 'bvi_if_idx' ] in snat_interfaces : LOG . debug ( 'Router: Deleting SNAT on interface ' 'index: %s' , router [ 'bvi_if_idx' ] ) self . vpp . set_snat_on_interface ( router [ 'bvi_if_idx' ] , is_inside = router [ 'is_inside' ] , is_add = False ) if not router [ 'is_inside' ] : LOG . debug ( 'Router: Deleting external gateway port %s for ' 'router: %s' , port_id , router ) if router [ 'bvi_if_idx' ] in snat_out_interfaces : LOG . debug ( 'Router:Removing 1:N SNAT on external interface ' 'index: %s' , router [ 'bvi_if_idx' ] ) self . vpp . snat_overload_on_interface_address ( router [ 'bvi_if_idx' ] , is_add = False ) self . export_routes_from_tenant_vrfs ( ext_gw_ip = router [ 'external_gateway_ip' ] , is_add = False ) self . default_route_in_default_vrf ( router , is_add = False ) else : self . export_routes_from_tenant_vrfs ( source_vrf = router [ 'vrf_id' ] , is_add = False ) if bvi_if_idx is not None : addresses = self . vpp . get_interface_ip_addresses ( bvi_if_idx ) if len ( addresses ) > 1 : self . vpp . del_interface_ip ( bvi_if_idx , ip_interface ( ( router [ 'gateway_ip' ] , router [ 'prefixlen' ] , ) ) ) else : self . vpp . delete_loopback ( bvi_if_idx ) if cfg . CONF . ml2_vpp . enable_l3_ha : self . router_interface_states . pop ( bvi_if_idx , None ) if router [ 'net_type' ] == TYPE_GPE and self . gpe is not None : LOG . debug ( 'Removing local GPE mappings for router ' 'interface: %s' , port_id ) self . gpe . delete_local_gpe_mapping ( router [ 'segmentation_id' ] , router [ 'mac_address' ] ) if not is_external : self . router_interfaces . pop ( port_id ) else : self . router_external_interfaces . pop ( port_id )
short_docstring: Deletes a router interface from the host.
short_docstring_tokens: Deletes a router interface from the host .
comment: """Deletes a router interface from the host.
  
          Disables SNAT, if it is set on the interface.
          Deletes a loopback interface from the host, this removes the BVI
          interface from the local bridge. Also, delete the default route and
          SNAT address for the external interface.
          """$SEP$# Get all local IP addresses in the external VRF belonging$SEP$# to the same external subnet as this router.$SEP$# Check if atleast one local_ip matches a neutron assigned$SEP$# external IP address of the router.$SEP$# If there's no match, there are no valid local IPs within VPP.$SEP$# While, in theory, there may be multiple IPs on an interface,$SEP$# in practice, we only program one (and program additional$SEP$# IPs via a local route).$SEP$# TODO(ijw): this is a somewhat unclean way of removing IP$SEP$# addresses attached to VPP interfaces that are in the$SEP$# subnet of ext_intf_ip, I think.  Unclear if this is the$SEP$# right way to do that versus remembering the interface.$SEP$# Is the local_ip valid?$SEP$# For-else would mean no breaks i.e. no valid local_ips$SEP$# If an external local route, we can safetly delete it from VPP$SEP$# Don't delete any SNAT$SEP$# External router is a loopback BVI. If a local route exists,$SEP$# replace the BVI's IP address with its IP address.$SEP$# Don't delete the SNAT.$SEP$# Delete the IP address from the BVI.$SEP$# Delete the local route$SEP$# Set the router external interface corresponding to the local$SEP$# route as non-local.$SEP$# At this point, we can safetly remove both the SNAT and BVI$SEP$# loopback interfaces as no local routes exist.$SEP$# Get SNAT out interfaces whose IP addrs are overloaded$SEP$# delete SNAT if set on this interface$SEP$# Delete the external 1:N SNAT and default routes in all VRFs$SEP$# for external router interface deletion$SEP$# Delete external snat addresses for the router$SEP$# Delete all exported routes into tenant VRFs belonging to this$SEP$# external gateway$SEP$# delete the default route in the default VRF$SEP$# Delete all exported routes from this VRF$SEP$# Delete the gateway IP address and the BVI interface if this is$SEP$# the last IP address assigned on the BVI$SEP$# Get all IP's assigned to the BVI interface$SEP$# Dont' delete the BVI, only remove one IP from it$SEP$# Last subnet assigned, delete the interface$SEP$# Remove any local GPE mappings