def get_port_dhcpv4_options(self, port): lsp_dhcp_disabled, lsp_dhcpv4_opts = utils.get_lsp_dhcpv4_opts(port) if lsp_dhcp_disabled: return # If the port has multiple IPv4 addresses, DHCPv4 options are set # for the first address in port['fixed_ips'] subnet_dhcp_options = None subnet_id = None for fixed_ip in port['fixed_ips']: if netaddr.IPAddress(fixed_ip['ip_address']).version == 4: subnet_dhcp_options = self._nb_ovn.get_subnet_dhcp_options( fixed_ip['subnet_id']) subnet_id = fixed_ip['subnet_id'] if subnet_dhcp_options: break if not subnet_dhcp_options: # Ideally this should not happen. # May be a sync is required in such cases ? return if not lsp_dhcpv4_opts: return subnet_dhcp_options # This port has extra DHCP options defined. # So we need to create a new row in DHCP_Options table for this # port. # # TODO(numans) In cases where the below transaction is successful # but the Logical_Switch_Port create or update transaction fails # we need to delete the DHCP_Options row created else it will be # an orphan row. # # NOTE(lizk) In cases where the below transaction is successful, but # the Logical_Switch_Port get deleted before setting port dhcp options # to it, we will delete the DHCP_Options row created to make sure # no orphan left behind. subnet_dhcp_options['options'].update(lsp_dhcpv4_opts) subnet_dhcp_options['external_ids'].update({'port_id': port['id']}) LOG.debug('Creating port dhcp options for port %s in OVN NB DB', port['id']) with self._nb_ovn.transaction(check_error=True) as txn: txn.add( self._nb_ovn.add_dhcp_options( subnet_id, port_id=port['id'], cidr=subnet_dhcp_options['cidr'], options=subnet_dhcp_options['options'], external_ids=subnet_dhcp_options['external_ids'])) return self._nb_ovn.get_port_dhcp_options(subnet_id, port['id'])
def get_port_dhcpv4_options(self, port): lsp_dhcp_disabled, lsp_dhcpv4_opts = utils.get_lsp_dhcpv4_opts(port) if lsp_dhcp_disabled: return # If the port has multiple IPv4 addresses, DHCPv4 options are set # for the first address in port['fixed_ips'] subnet_dhcp_options = None subnet_id = None for fixed_ip in port['fixed_ips']: if netaddr.IPAddress(fixed_ip['ip_address']).version == 4: subnet_dhcp_options = self._nb_ovn.get_subnet_dhcp_options( fixed_ip['subnet_id']) subnet_id = fixed_ip['subnet_id'] if subnet_dhcp_options: break if not subnet_dhcp_options: # Ideally this should not happen. # May be a sync is required in such cases ? return if not lsp_dhcpv4_opts: return subnet_dhcp_options # This port has extra DHCP options defined. # So we need to create a new row in DHCP_Options table for this # port. # # TODO(numans) In cases where the below transaction is successful # but the Logical_Switch_Port create or update transaction fails # we need to delete the DHCP_Options row created else it will be # an orphan row. # # NOTE(lizk) In cases where the below transaction is successful, but # the Logical_Switch_Port get deleted before setting port dhcp options # to it, we will delete the DHCP_Options row created to make sure # no orphan left behind. subnet_dhcp_options['options'].update(lsp_dhcpv4_opts) subnet_dhcp_options['external_ids'].update( {'port_id': port['id']}) LOG.debug('Creating port dhcp options for port %s in OVN NB DB', port['id']) with self._nb_ovn.transaction(check_error=True) as txn: txn.add(self._nb_ovn.add_dhcp_options( subnet_id, port_id=port['id'], cidr=subnet_dhcp_options['cidr'], options=subnet_dhcp_options['options'], external_ids=subnet_dhcp_options['external_ids'])) return self._nb_ovn.get_port_dhcp_options(subnet_id, port['id'])
def sync_networks_ports_and_dhcp_opts(self, ctx): LOG.debug('OVN-NB Sync networks, ports and DHCP options started') db_networks = {} for net in self.core_plugin.get_networks(ctx): db_networks[utils.ovn_name(net['id'])] = net db_ports = {} for port in self.core_plugin.get_ports(ctx): db_ports[port['id']] = port ovn_all_dhcp_options = self.ovn_api.get_all_dhcp_options() db_network_cache = dict(db_networks) ports_need_sync_dhcp_opts = [] lswitches = self.ovn_api.get_all_logical_switches_with_ports() del_lswitchs_list = [] del_lports_list = [] for lswitch in lswitches: if lswitch['name'] in db_networks: for lport in lswitch['ports']: if lport in db_ports: ports_need_sync_dhcp_opts.append(db_ports.pop(lport)) else: del_lports_list.append({ 'port': lport, 'lswitch': lswitch['name'] }) del db_networks[lswitch['name']] else: del_lswitchs_list.append(lswitch) for net_id, network in db_networks.items(): LOG.warning( _LW("Network found in Neutron but not in " "OVN DB, network_id=%s"), network['id']) if self.mode == SYNC_MODE_REPAIR: try: LOG.debug('Creating the network %s in OVN NB DB', network['id']) self._create_network_in_ovn(network) except RuntimeError: LOG.warning( _LW("Create network in OVN NB failed for" " network %s"), network['id']) self._sync_subnet_dhcp_options(ctx, db_network_cache, ovn_all_dhcp_options['subnets']) for port_id, port in db_ports.items(): LOG.warning( _LW("Port found in Neutron but not in OVN " "DB, port_id=%s"), port['id']) if self.mode == SYNC_MODE_REPAIR: try: LOG.debug('Creating the port %s in OVN NB DB', port['id']) self._create_port_in_ovn(ctx, port) if port_id in ovn_all_dhcp_options['ports']: _, lsp_opts = utils.get_lsp_dhcpv4_opts(port) if lsp_opts: ovn_all_dhcp_options['ports'].pop(port_id) except RuntimeError: LOG.warning( _LW("Create port in OVN NB failed for" " port %s"), port['id']) with self.ovn_api.transaction(check_error=True) as txn: for lswitch in del_lswitchs_list: LOG.warning( _LW("Network found in OVN but not in " "Neutron, network_id=%s"), lswitch['name']) if self.mode == SYNC_MODE_REPAIR: LOG.debug('Deleting the network %s from OVN NB DB', lswitch['name']) txn.add( self.ovn_api.delete_lswitch( lswitch_name=lswitch['name'])) for lport_info in del_lports_list: LOG.warning( _LW("Port found in OVN but not in " "Neutron, port_id=%s"), lport_info['port']) if self.mode == SYNC_MODE_REPAIR: LOG.debug('Deleting the port %s from OVN NB DB', lport_info['port']) txn.add( self.ovn_api.delete_lswitch_port( lport_name=lport_info['port'], lswitch_name=lport_info['lswitch'])) if lport_info['port'] in ovn_all_dhcp_options['ports']: LOG.debug('Deleting port DHCP options for (port %s)', lport_info['port']) txn.add( self.ovn_api.delete_dhcp_options( ovn_all_dhcp_options['ports'].pop( lport_info['port'])['uuid'])) self._sync_port_dhcp_options(ctx, ports_need_sync_dhcp_opts, ovn_all_dhcp_options['ports']) LOG.debug('OVN-NB Sync networks, ports and DHCP options finished')
def sync_networks_ports_and_dhcp_opts(self, ctx): LOG.debug('OVN-NB Sync networks, ports and DHCP options started') db_networks = {} for net in self.core_plugin.get_networks(ctx): db_networks[utils.ovn_name(net['id'])] = net db_ports = {} for port in self.core_plugin.get_ports(ctx): db_ports[port['id']] = port ovn_all_dhcp_options = self.ovn_api.get_all_dhcp_options() db_network_cache = dict(db_networks) ports_need_sync_dhcp_opts = [] lswitches = self.ovn_api.get_all_logical_switches_with_ports() del_lswitchs_list = [] del_lports_list = [] for lswitch in lswitches: if lswitch['name'] in db_networks: for lport in lswitch['ports']: if lport in db_ports: ports_need_sync_dhcp_opts.append(db_ports.pop(lport)) else: del_lports_list.append({'port': lport, 'lswitch': lswitch['name']}) del db_networks[lswitch['name']] else: del_lswitchs_list.append(lswitch) for net_id, network in db_networks.items(): LOG.warning(_LW("Network found in Neutron but not in " "OVN DB, network_id=%s"), network['id']) if self.mode == SYNC_MODE_REPAIR: try: LOG.debug('Creating the network %s in OVN NB DB', network['id']) self._create_network_in_ovn(network) except RuntimeError: LOG.warning(_LW("Create network in OVN NB failed for" " network %s"), network['id']) self._sync_subnet_dhcp_options( ctx, db_network_cache, ovn_all_dhcp_options['subnets']) for port_id, port in db_ports.items(): LOG.warning(_LW("Port found in Neutron but not in OVN " "DB, port_id=%s"), port['id']) if self.mode == SYNC_MODE_REPAIR: try: LOG.debug('Creating the port %s in OVN NB DB', port['id']) self._create_port_in_ovn(ctx, port) if port_id in ovn_all_dhcp_options['ports']: _, lsp_opts = utils.get_lsp_dhcpv4_opts(port) if lsp_opts: ovn_all_dhcp_options['ports'].pop(port_id) except RuntimeError: LOG.warning(_LW("Create port in OVN NB failed for" " port %s"), port['id']) with self.ovn_api.transaction(check_error=True) as txn: for lswitch in del_lswitchs_list: LOG.warning(_LW("Network found in OVN but not in " "Neutron, network_id=%s"), lswitch['name']) if self.mode == SYNC_MODE_REPAIR: LOG.debug('Deleting the network %s from OVN NB DB', lswitch['name']) txn.add(self.ovn_api.delete_lswitch( lswitch_name=lswitch['name'])) for lport_info in del_lports_list: LOG.warning(_LW("Port found in OVN but not in " "Neutron, port_id=%s"), lport_info['port']) if self.mode == SYNC_MODE_REPAIR: LOG.debug('Deleting the port %s from OVN NB DB', lport_info['port']) txn.add(self.ovn_api.delete_lswitch_port( lport_name=lport_info['port'], lswitch_name=lport_info['lswitch'])) if lport_info['port'] in ovn_all_dhcp_options['ports']: LOG.debug('Deleting port DHCP options for (port %s)', lport_info['port']) txn.add(self.ovn_api.delete_dhcp_options( ovn_all_dhcp_options['ports'].pop( lport_info['port'])['uuid'])) self._sync_port_dhcp_options(ctx, ports_need_sync_dhcp_opts, ovn_all_dhcp_options['ports']) LOG.debug('OVN-NB Sync networks, ports and DHCP options finished')