def check_for_row_by_value_and_retry(self, table, column, match): try: idlutils.row_by_value(self.idl, table, column, match) except idlutils.RowNotFound: msg = (_("%(match)s does not exist in %(column)s of %(table)s") % { 'match': match, 'column': column, 'table': table }) raise RuntimeError(msg)
def run_idl(self, txn): # ovn-sbctl does a client-side check for duplicate entry, but since # there is an index on "name", it will fail if we try to insert a # duplicate, so I'm not doing the check unless may_exist is set if self.may_exist: chassis = idlutils.row_by_value(self.api.idl, self.table_name, 'name', self.chassis) if chassis: self.result = rowview.RowView(chassis) return chassis = txn.insert(self.api.tables[self.table_name]) chassis.name = self.chassis encaps = [] for encap_type in self.encap_types: encap = txn.insert(self.api.tables['Encap']) encap.type = encap_type encap.ip = self.encap_ip encap.options = {'csum': 'True'} # ovn-sbctl silently does this... # NOTE(twilson) addvalue seems like it should work, but fails with # Chassis table col encaps references nonexistent row error # chassis.addvalue('encaps', encap) encaps.append(encap) chassis.encaps = encaps for col, val in self.columns.items(): setattr(chassis, col, val) self.result = chassis.uuid
def run_idl(self, txn): if self.may_exist: br = idlutils.row_by_value(self.api.idl, 'Bridge', 'name', self.name, None) if br: if self.datapath_type: br.datapath_type = self.datapath_type self.result = br.uuid return row = txn.insert(self.api._tables['Bridge']) row.name = self.name if self.datapath_type: row.datapath_type = self.datapath_type try: self.api._ovs.addvalue('bridges', row) except AttributeError: # OVS < 2.6 self.api._ovs.verify('bridges') self.api._ovs.bridges = self.api._ovs.bridges + [row] # Add the internal bridge port cmd = AddPortCommand(self.api, self.name, self.name, self.may_exist) cmd.run_idl(txn) cmd = command.DbSetCommand(self.api, 'Interface', self.name, ('type', 'internal')) cmd.run_idl(txn) self.result = row.uuid
def run_idl(self, txn): try: lsp = idlutils.row_by_value(self.api.idl, 'Logical_Switch_Port', 'name', self.lport) except idlutils.RowNotFound: if self.if_exists: return msg = "Logical Switch Port %s does not exist" % self.lport raise RuntimeError(msg) options = lsp.options virtual_parents = options.get( ovn_const.LSP_OPTIONS_VIRTUAL_PARENTS_KEY, set()) if virtual_parents: virtual_parents = set(virtual_parents.split(',')) try: virtual_parents.remove(self.parent) except KeyError: pass # If virtual-parents is now empty, change the type and remove the # virtual-parents and virtual-ip options if not virtual_parents: options.pop(ovn_const.LSP_OPTIONS_VIRTUAL_PARENTS_KEY, None) options.pop(ovn_const.LSP_OPTIONS_VIRTUAL_IP_KEY, None) setattr(lsp, 'type', '') else: options[ovn_const.LSP_OPTIONS_VIRTUAL_PARENTS_KEY] = ','.join( virtual_parents) setattr(lsp, 'options', options)
def get_parent_port(self, lsp_name): try: lsp = idlutils.row_by_value(self.idl, 'Logical_Switch_Port', 'name', lsp_name) return lsp.parent_name except idlutils.RowNotFound: return ''
def get_gateway_chassis_binding(self, gateway_name): try: lrp = idlutils.row_by_value(self.idl, 'Logical_Router_Port', 'name', gateway_name) return self._get_logical_router_port_gateway_chassis(lrp) except idlutils.RowNotFound: return []
def _get_update_data_without_compare(self): lswitch_ovsdb_dict = {} for switch_name in self.lswitch_names: switch_name = utils.ovn_name(switch_name) lswitch = idlutils.row_by_value(self.api.idl, 'Logical_Switch', 'name', switch_name) lswitch_ovsdb_dict[switch_name] = lswitch if self.is_add_acl: acl_add_values_dict = {} for port in self.port_list: switch_name = utils.ovn_name(port['network_id']) if switch_name not in acl_add_values_dict: acl_add_values_dict[switch_name] = [] if port['id'] in self.acl_new_values_dict: acl_add_values_dict[switch_name].append( self.acl_new_values_dict[port['id']]) acl_del_objs_dict = {} else: acl_add_values_dict = {} acl_del_objs_dict = {} del_acl_extids = [] for acl_dict in self.acl_new_values_dict.values(): del_acl_extids.append( {acl_dict['match']: acl_dict['external_ids']}) for switch_name, lswitch in lswitch_ovsdb_dict.items(): if switch_name not in acl_del_objs_dict: acl_del_objs_dict[switch_name] = [] acls = getattr(lswitch, 'acls', []) for acl in acls: match = getattr(acl, 'match') acl_extids = {match: getattr(acl, 'external_ids')} if acl_extids in del_acl_extids: acl_del_objs_dict[switch_name].append(acl) return lswitch_ovsdb_dict, acl_del_objs_dict, acl_add_values_dict
def test_lsp_bind(self): chassis, switch, port = self._add_chassis_switch_port() self.api.lsp_bind(port.name, chassis.name).execute(check_error=True) binding = idlutils.row_by_value(self.api.idl, 'Port_Binding', 'logical_port', port.name) self.assertIn(chassis, binding.chassis) return chassis, switch, port
def get_address_set(self, addrset_id, ip_version='ip4'): addr_name = utils.ovn_addrset_name(addrset_id, ip_version) try: return idlutils.row_by_value(self.idl, 'Address_Set', 'name', addr_name) except idlutils.RowNotFound: return None
def _verify_dhcp_option_row_for_port(self, port_id, expected_lsp_dhcpv4_options, expected_lsp_dhcpv6_options=None): lsp = idlutils.row_by_value(self.monitor_nb_db_idl, 'Logical_Switch_Port', 'name', port_id, None) if lsp.dhcpv4_options: observed_lsp_dhcpv4_options = { 'cidr': lsp.dhcpv4_options[0].cidr, 'external_ids': lsp.dhcpv4_options[0].external_ids, 'options': lsp.dhcpv4_options[0].options } else: observed_lsp_dhcpv4_options = {} if lsp.dhcpv6_options: observed_lsp_dhcpv6_options = { 'cidr': lsp.dhcpv6_options[0].cidr, 'external_ids': lsp.dhcpv6_options[0].external_ids, 'options': lsp.dhcpv6_options[0].options } else: observed_lsp_dhcpv6_options = {} if expected_lsp_dhcpv6_options is None: expected_lsp_dhcpv6_options = {} self.assertEqual(expected_lsp_dhcpv4_options, observed_lsp_dhcpv4_options) self.assertEqual(expected_lsp_dhcpv6_options, observed_lsp_dhcpv6_options)
def run_idl(self, txn): #取此桥 br = idlutils.row_by_value(self.api.idl, 'Bridge', 'name', self.bridge) #在此桥上添加一个port port = txn.insert(self.api.idl.tables['Port']) port.name = self.port br.verify('ports') ports = getattr(br, 'ports', []) ports.append(port) br.ports = ports #在此桥上添加interface iface = txn.insert(self.api.idl.tables['Interface']) iface.name = self.port port.verify('interfaces') ifaces = getattr(port, 'interfaces', []) options_dict = getattr(iface, 'options', {}) external_ids = getattr(iface, 'external_ids', {}) #指明此interface的peer_port options_dict['peer'] = self.peer_port iface.options = options_dict iface.external_ids = external_ids #指明接口类型为patch iface.type = 'patch' ifaces.append(iface) port.interfaces = ifaces
def test_router_gateway_port_binding_host_id(self): # Test setting chassis on chassisredirect port in Port_Binding table, # will update host_id of corresponding router gateway port # with this chassis. chassis = idlutils.row_by_value(self.sb_api.idl, 'Chassis', 'name', self.chassis1) host_id = chassis.hostname ext = self._create_ext_network( 'ext1', 'vlan', 'physnet1', 1, "10.0.0.1", "10.0.0.0/24") gw_info = {'network_id': ext['network']['id']} router = self._create_router('router1', gw_info=gw_info) core_plugin = directory.get_plugin() gw_port_id = router.get('gw_port_id') # Set chassis on chassisredirect port in Port_Binding table logical_port = 'cr-lrp-%s' % gw_port_id self.sb_api.lsp_bind(logical_port, self.chassis1, may_exist=True).execute(check_error=True) def check_port_binding_host_id(port_id): port = core_plugin.get_ports( self.context, filters={'id': [port_id]})[0] return port[portbindings.HOST_ID] == host_id # Test if router gateway port updated with this chassis n_utils.wait_until_true(lambda: check_port_binding_host_id( gw_port_id))
def run_idl(self, txn): # TODO(lucasagomes): Remove this check after OVS 2.8.2 is tagged # (prior to that, the external_ids column didn't exist in this # table). if not self.api.is_col_present('Logical_Router_Static_Route', 'external_ids'): return try: lrouter = idlutils.row_by_value(self.api.idl, 'Logical_Router', 'name', self.lrouter) except idlutils.RowNotFound: if self.if_exists: return msg = _("Logical Router %s does not exist") % self.lrouter raise RuntimeError(msg) lrouter.verify('static_routes') static_routes = getattr(lrouter, 'static_routes', []) for route in static_routes: external_ids = getattr(route, 'external_ids', {}) if ovn_const.OVN_ROUTER_IS_EXT_GW in external_ids: _delvalue_from_list(lrouter, 'static_routes', route) route.delete() break lrouter.verify('nat') nats = getattr(lrouter, 'nat', []) for nat in nats: if nat.type != 'snat': continue _delvalue_from_list(lrouter, 'nat', nat) nat.delete() lrouter_ext_ids = getattr(lrouter, 'external_ids', {}) gw_port_id = lrouter_ext_ids.get(ovn_const.OVN_GW_PORT_EXT_ID_KEY) if not gw_port_id: return try: lrouter_port = idlutils.row_by_value( self.api.idl, 'Logical_Router_Port', 'name', utils.ovn_lrouter_port_name(gw_port_id)) except idlutils.RowNotFound: return _delvalue_from_list(lrouter, 'ports', lrouter_port)
def run_idl(self, txn): interface = idlutils.row_by_value(self.api.idl, 'Interface', 'name', self.name) ports = self.api._tables['Port'].rows.values() pname = next(port for port in ports if interface in port.interfaces) bridges = self.api._tables['Bridge'].rows.values() self.result = next(br.name for br in bridges if pname in br.ports)
def run_idl(self, txn): try: binding = idlutils.row_by_value(self.api.idl, 'Port_Binding', 'logical_port', self.port) except idlutils.RowNotFound: if self.if_exists: return raise binding.chassis = []
def run_idl(self, txn): try: lrouter_port = idlutils.row_by_value(self.api.idl, 'Logical_Router_Port', 'name', self.name) except idlutils.RowNotFound: if self.if_exists: return msg = _("Logical Router Port %s does not exist") % self.name raise RuntimeError(msg) try: lrouter = idlutils.row_by_value(self.api.idl, 'Logical_Router', 'name', self.lrouter) except idlutils.RowNotFound: msg = _("Logical Router %s does not exist") % self.lrouter raise RuntimeError(msg) _delvalue_from_list(lrouter, 'ports', lrouter_port)
def run_idl(self, txn): # TODO(twilson) This is expensive! # This traversal of all ports could be eliminated by caching the bridge # name on the Port's external_id field # In fact, if we did that, the only place that uses to_br functions # could just add the external_id field to the conditions passed to find port = idlutils.row_by_value(self.api.idl, 'Port', 'name', self.name) bridges = self.api._tables['Bridge'].rows.values() self.result = next(br.name for br in bridges if port in br.ports)
def run_idl(self, txn): br = idlutils.row_by_value(self.api.idl, 'Bridge', 'name', self.bridge) controllers = [] for target in self.targets: controller = txn.insert(self.api._tables['Controller']) controller.target = target controllers.append(controller) # Don't need to verify because we unconditionally overwrite br.controller = controllers
def get_chassis_data_for_ml2_bind_port(self, hostname): try: chassis = idlutils.row_by_value(self.idl, 'Chassis', 'hostname', hostname) except idlutils.RowNotFound: msg = _('Chassis with hostname %s does not exist') % hostname raise RuntimeError(msg) return (chassis.external_ids.get('datapath-type', ''), chassis.external_ids.get('iface-types', ''), self._get_chassis_physnets(chassis))
def run_idl(self, txn): if self.may_exist: addrset = idlutils.row_by_value(self.api.idl, 'Address_Set', 'name', self.name, None) if addrset: return row = txn.insert(self.api._tables['Address_Set']) row.name = self.name for col, val in self.columns.items(): setattr(row, col, val)
def test_lsp_bind_may_exist(self): chassis, switch, port = self.test_lsp_bind() other = self._chassis_add(['vxlan'], '192.0.2.2', chassis=utils.get_rand_device_name()) self.api.lsp_bind(port.name, other.name, may_exist=True).execute( check_error=True) binding = idlutils.row_by_value(self.api.idl, 'Port_Binding', 'logical_port', port.name) self.assertNotIn(other, binding.chassis) self.assertIn(chassis, binding.chassis)
def run_idl(self, txn): br = idlutils.row_by_value(self.api.idl, 'Bridge', 'name', self.bridge) for port in br.ports: if not any(self.is_deletable_port(iface) for iface in port.interfaces): continue br.delvalue('ports', port) for iface in port.interfaces: iface.delete() port.delete()
def run_idl(self, txn): chassis = self.api.lookup('Chassis', self.chassis) binding = idlutils.row_by_value(self.api.idl, 'Port_Binding', 'logical_port', self.port) if binding.chassis: if self.may_exist: return raise RuntimeError("Port %s already bound to %s" % (self.port, self.chassis)) binding.chassis = chassis
def run_idl(self, txn): try: lport = idlutils.row_by_value(self.api.idl, 'Logical_Switch_Port', 'name', self.lport) lswitch = idlutils.row_by_value(self.api.idl, 'Logical_Switch', 'name', self.lswitch) except idlutils.RowNotFound: if self.if_exists: return msg = _("Port %s does not exist") % self.lport raise RuntimeError(msg) # Delete DHCP_Options records no longer referred by this port. cur_port_dhcp_opts = get_lsp_dhcp_options_uuids(lport, self.lport) for uuid in cur_port_dhcp_opts: self.api._tables['DHCP_Options'].rows[uuid].delete() _delvalue_from_list(lswitch, 'ports', lport) self.api._tables['Logical_Switch_Port'].rows[lport.uuid].delete()
def _validate_ls_dns_records(self, lswitch_name, expected_dns_records): ls = idlutils.row_by_value(self.nb_api.idl, 'Logical_Switch', 'name', lswitch_name) observed_dns_records = [] for dns_row in ls.dns_records: observed_dns_records.append({ 'external_ids': dns_row.external_ids, 'records': dns_row.records }) self.assertItemsEqual(expected_dns_records, observed_dns_records)
def get_router_port_options(self, lsp_name): try: lsp = idlutils.row_by_value(self.idl, 'Logical_Switch_Port', 'name', lsp_name) options = getattr(lsp, 'options') for key in list(options.keys()): if key not in ovn_const.OVN_ROUTER_PORT_OPTION_KEYS: del (options[key]) return options except idlutils.RowNotFound: return {}
def get_chassis_metadata_networks(self, chassis_name): """Return a list with the metadata networks the chassis is hosting.""" try: chassis = idlutils.row_by_value(self.idl, 'Chassis', 'name', chassis_name) except idlutils.RowNotFound: msg = _('Chassis %s does not exist') % chassis_name raise RuntimeError(msg) proxy_networks = chassis.external_ids.get( 'neutron-metadata-proxy-networks', None) return proxy_networks.split(',') if proxy_networks else []
def run_idl(self, txn): if self.may_exist: lswitch = idlutils.row_by_value(self.api.idl, 'Logical_Switch', 'name', self.name, None) if lswitch: return row = txn.insert(self.api._tables['Logical_Switch']) row.name = self.name for col, val in self.columns.items(): setattr(row, col, val) self.result = row.uuid
def run_idl(self, txn): try: lrouter = idlutils.row_by_value(self.api.idl, 'Logical_Router', 'name', self.name) except idlutils.RowNotFound: if self.if_exists: return msg = _("Logical Router %s does not exist") % self.name raise RuntimeError(msg) self.api._tables['Logical_Router'].rows[lrouter.uuid].delete()
def run_idl(self, txn): if self.may_exist: lrouter = idlutils.row_by_value(self.api.idl, 'Logical_Router', 'name', self.name, None) if lrouter: return row = txn.insert(self.api._tables['Logical_Router']) row.name = self.name for col, val in self.columns.items(): setattr(row, col, val)