def add_router_interface(self, context, router_id, interface_info): with context.session.begin(subtransactions=True): result = super(DFL3RouterPlugin, self).add_router_interface( context, router_id, interface_info) router_version = version_db._update_db_version_row( context.session, router_id) self.core_plugin = self._get_core_plugin() port = self.core_plugin.get_port(context, result['port_id']) subnet = self.core_plugin.get_subnet(context, result['subnet_id']) cidr = netaddr.IPNetwork(subnet['cidr']) network = "%s/%s" % (port['fixed_ips'][0]['ip_address'], str(cidr.prefixlen)) logical_port = self.nb_api.get_logical_port(port['id'], port['tenant_id']) self.nb_api.add_lrouter_port(result['port_id'], result['id'], result['network_id'], result['tenant_id'], router_version=router_version, mac=port['mac_address'], network=network, tunnel_key=logical_port.get_tunnel_key()) return result
def add_router_interface(self, context, router_id, interface_info): add_by_port, add_by_sub = self._validate_interface_info(interface_info) if add_by_sub: subnet = self.get_subnet(context, interface_info['subnet_id']) port = { 'port': { 'tenant_id': subnet['tenant_id'], 'network_id': subnet['network_id'], 'name': '', 'admin_state_up': True, 'device_id': '', 'device_owner': l3_db.DEVICE_OWNER_ROUTER_INTF, 'mac_address': attr.ATTR_NOT_SPECIFIED, 'fixed_ips': [{ 'subnet_id': subnet['id'], 'ip_address': subnet['gateway_ip'] }] } } port = self.create_port(context, port) elif add_by_port: port = self.get_port(context, interface_info['port_id']) subnet_id = port['fixed_ips'][0]['subnet_id'] subnet = self.get_subnet(context, subnet_id) lswitch_id = subnet['network_id'] cidr = netaddr.IPNetwork(subnet['cidr']) network = "%s/%s" % (port['fixed_ips'][0]['ip_address'], str(cidr.prefixlen)) logical_port = self.nb_api.get_logical_port(port['id'], port['tenant_id']) interface_info['port_id'] = port['id'] if 'subnet_id' in interface_info: del interface_info['subnet_id'] with context.session.begin(subtransactions=True): result = super(DFPlugin, self).add_router_interface(context, router_id, interface_info) router_version = version_db._update_db_version_row( context.session, router_id) self.nb_api.add_lrouter_port(port['id'], router_id, lswitch_id, port['tenant_id'], router_version=router_version, mac=port['mac_address'], network=network, tunnel_key=logical_port.get_tunnel_key()) return result
def update_router(self, context, router_id, router): with context.session.begin(subtransactions=True): router = super(DFL3RouterPlugin, self).update_router( context, router_id, router) router_version = version_db._update_db_version_row( context.session, router['id']) try: gw_info = router.get('external_gateway_info', {}) if gw_info: gw_info.update({'port_id': router.get('gw_port_id')}) is_distributed = router.get('distributed', False) self.nb_api.update_lrouter( router_id, topic=router['tenant_id'], name=router['name'], is_distributed=is_distributed, version=router_version, routes=router.get('routes', []), admin_state_up=router['admin_state_up'], description=router['description'], gateway=(gw_info if gw_info else {}) ) except df_exceptions.DBKeyNotFound: LOG.debug("router %s is not found in DF DB, might have " "been deleted concurrently" % router_id) return router
def delete_security_group_rule(self, context, id): with context.session.begin(subtransactions=True): security_group_rule = self.get_security_group_rule(context, id) sg_id = security_group_rule['security_group_id'] sg_group = self.get_security_group(context, sg_id) super(DFPlugin, self).delete_security_group_rule(context, id) sg_version_id = version_db._update_db_version_row( context.session, sg_id) self.nb_api.delete_security_group_rule(sg_id, id, sg_group['tenant_id'], sg_version=sg_version_id)
def create_security_group_rule(self, context, security_group_rule): with context.session.begin(subtransactions=True): sg_rule = super(DFPlugin, self).create_security_group_rule( context, security_group_rule) sg_id = sg_rule['security_group_id'] sg_version_id = version_db._update_db_version_row( context.session, sg_id) sg_group = self.get_security_group(context, sg_id) self.nb_api.add_security_group_rules(sg_id, sg_group['tenant_id'], sg_rules=[sg_rule], sg_version=sg_version_id) return sg_rule
def update_router(self, context, id, router): with context.session.begin(subtransactions=True): router = super(DFPlugin, self).update_router( context, id, router) router_version = version_db._update_db_version_row( context.session, id) tenant_id = router['tenant_id'] router_name = router.get('name', df_const.DF_ROUTER_DEFAULT_NAME) self.nb_api.update_lrouter(id, topic=tenant_id, name=router_name, version=router_version) return router
def update_security_group(self, context, sg_id, security_group): with context.session.begin(subtransactions=True): sg_db = super(DFPlugin, self).update_security_group(context, sg_id, security_group) sg_version = version_db._update_db_version_row( context.session, sg_id) sg_name = sg_db.get('name', df_const.DF_SG_DEFAULT_NAME) tenant_id = sg_db['tenant_id'] rules = sg_db.get('security_group_rules') self.nb_api.update_security_group(id=sg_id, topic=tenant_id, name=sg_name, rules=rules, version=sg_version) return sg_db
def delete_security_group_rule(self, resource, event, trigger, **kwargs): context = kwargs['context'] tenant_id = context.tenant_id sgr_id = kwargs['security_group_rule_id'] core_plugin = manager.NeutronManager.get_plugin() sgr = core_plugin.get_security_group_rule(context, sgr_id) sg_id = sgr['security_group_id'] with context.session.begin(subtransactions=True): sg_version_id = version_db._update_db_version_row(context.session, sg_id) self.nb_api.delete_security_group_rule(sg_id, sgr_id, tenant_id, sg_version=sg_version_id) LOG.info(_LI("DFMechDriver: delete security group rule %s"), sgr_id)
def add_router_interface(self, context, router_id, interface_info): add_by_port, add_by_sub = self._validate_interface_info( interface_info) if add_by_sub: subnet = self.get_subnet(context, interface_info['subnet_id']) port = {'port': {'tenant_id': subnet['tenant_id'], 'network_id': subnet['network_id'], 'name': '', 'admin_state_up': True, 'device_id': '', 'device_owner': l3_db.DEVICE_OWNER_ROUTER_INTF, 'mac_address': attr.ATTR_NOT_SPECIFIED, 'fixed_ips': [{'subnet_id': subnet['id'], 'ip_address': subnet['gateway_ip']}]}} port = self.create_port(context, port) elif add_by_port: port = self.get_port(context, interface_info['port_id']) subnet_id = port['fixed_ips'][0]['subnet_id'] subnet = self.get_subnet(context, subnet_id) lswitch_id = subnet['network_id'] cidr = netaddr.IPNetwork(subnet['cidr']) network = "%s/%s" % (port['fixed_ips'][0]['ip_address'], str(cidr.prefixlen)) logical_port = self.nb_api.get_logical_port(port['id'], port['tenant_id']) interface_info['port_id'] = port['id'] if 'subnet_id' in interface_info: del interface_info['subnet_id'] with context.session.begin(subtransactions=True): result = super(DFPlugin, self).add_router_interface( context, router_id, interface_info) router_version = version_db._update_db_version_row( context.session, router_id) self.nb_api.add_lrouter_port(port['id'], router_id, lswitch_id, port['tenant_id'], router_version=router_version, mac=port['mac_address'], network=network, tunnel_key=logical_port.get_tunnel_key()) return result
def create_security_group_rule(self, resource, event, trigger, **kwargs): sg_rule = kwargs['security_group_rule'] sg_id = sg_rule['security_group_id'] tenant_id = sg_rule['tenant_id'] context = kwargs['context'] with context.session.begin(subtransactions=True): sg_version_id = version_db._update_db_version_row(context.session, sg_id) sg_rule['topic'] = tenant_id del sg_rule['tenant_id'] self.nb_api.add_security_group_rules(sg_id, tenant_id, sg_rules=[sg_rule], sg_version=sg_version_id) LOG.info(_LI("DFMechDriver: create security group rule in group %s"), sg_id) return sg_rule
def update_floatingip(self, context, id, floatingip): with context.session.begin(subtransactions=True): floatingip_dict = super(DFPlugin, self).update_floatingip( context, id, floatingip) fip_version = version_db._update_db_version_row( context.session, id) self.nb_api.update_floatingip( id=floatingip_dict['id'], topic=floatingip_dict['tenant_id'], notify=True, name=floatingip_dict.get('name', df_const.DF_FIP_DEFAULT_NAME), router_id=floatingip_dict['router_id'], port_id=floatingip_dict['port_id'], version=fip_version, fixed_ip_address=floatingip_dict['fixed_ip_address'], status=floatingip_dict['status']) return floatingip_dict
def delete_subnet(self, context, id): orig_subnet = super(DFPlugin, self).get_subnet(context, id) net_id = orig_subnet['network_id'] with context.session.begin(subtransactions=True): # delete subnet in DB super(DFPlugin, self).delete_subnet(context, id) network_version = version_db._update_db_version_row( context.session, net_id) # update df controller with subnet delete if net_id: try: self.nb_api.delete_subnet(id, net_id, orig_subnet['tenant_id'], nw_version=network_version) except df_exceptions.DBKeyNotFound: LOG.debug("network %s is not found in DB, might have " "been deleted concurrently" % net_id)
def update_subnet(self, context, id, subnet): dhcp_port = None new_subnet = None net_id = None network_version = None try: with context.session.begin(subtransactions=True): # update subnet in DB original_subnet = super(DFPlugin, self).get_subnet(context, id) new_subnet = super(DFPlugin, self).update_subnet(context, id, subnet) net_id = new_subnet['network_id'] dhcp_port = self._update_subnet_dhcp( context, original_subnet, new_subnet) network_version = version_db._update_db_version_row( context.session, net_id) except Exception: with excutils.save_and_reraise_exception() as ctxt: ctxt.reraise = True # delete the stale dhcp port try: if dhcp_port: self.nb_api.delete_lport(dhcp_port['id'], dhcp_port['tenant_id']) except df_exceptions.DBKeyNotFound: pass if new_subnet and net_id: # update df controller with subnet dhcp_address = self._get_ip_from_port(dhcp_port) self.nb_api.update_subnet( new_subnet['id'], net_id, new_subnet['tenant_id'], name=new_subnet.get('name', df_const.DF_SUBNET_DEFAULT_NAME), nw_version=network_version, enable_dhcp=new_subnet['enable_dhcp'], cidr=new_subnet['cidr'], dhcp_ip=dhcp_address, gateway_ip=new_subnet['gateway_ip'], dns_nameservers=new_subnet.get('dns_nameservers', []), host_routes=new_subnet.get('host_routes', [])) return new_subnet
def update_subnet(self, context, id, subnet): dhcp_port = None new_subnet = None net_id = None network_version = None try: with context.session.begin(subtransactions=True): # update subnet in DB original_subnet = super(DFPlugin, self).get_subnet(context, id) new_subnet = super(DFPlugin, self).update_subnet(context, id, subnet) net_id = new_subnet['network_id'] dhcp_port = self._update_subnet_dhcp( context, original_subnet, new_subnet) network_version = version_db._update_db_version_row( context.session, net_id) except Exception: with excutils.save_and_reraise_exception() as ctxt: ctxt.reraise = True # delete the stale dhcp port try: if dhcp_port: self.nb_api.delete_lport(dhcp_port['id'], dhcp_port['tenant_id']) except df_exceptions.DBKeyNotFound: pass if new_subnet and net_id: # update df controller with subnet dhcp_address = self._get_ip_from_port(dhcp_port) self.nb_api.update_subnet( new_subnet['id'], net_id, new_subnet['tenant_id'], name=new_subnet.get('name', df_const.DF_SUBNET_DEFAULT_NAME), nw_version=network_version, enable_dhcp=new_subnet['enable_dhcp'], cidr=new_subnet['cidr'], dhcp_ip=dhcp_address, gateway_ip=new_subnet['gateway_ip'], dns_nameservers=new_subnet.get('dns_nameservers', [])) return new_subnet
def update_network(self, context, network_id, network): pnet._raise_if_updates_provider_attributes(network['network']) with context.session.begin(subtransactions=True): result = super(DFPlugin, self).update_network(context, network_id, network) if psec.PORTSECURITY in network['network']: self._process_network_port_security_update( context, network['network'], result) self._process_l3_update(context, result, network['network']) network_version = version_db._update_db_version_row( context.session, network_id) self.nb_api.update_lswitch(id=network_id, topic=result['tenant_id'], name=result.get( 'name', df_const.DF_NETWORK_DEFAULT_NAME), router_external=result['router:external'], version=network_version) return result
def remove_router_interface(self, context, router_id, interface_info): with context.session.begin(subtransactions=True): new_router = super(DFPlugin, self).remove_router_interface( context, router_id, interface_info) router_version = version_db._update_db_version_row( context.session, router_id) subnet = self.get_subnet(context, new_router['subnet_id']) network_id = subnet['network_id'] try: self.nb_api.delete_lrouter_port(router_id, network_id, subnet['tenant_id'], router_version=router_version) except df_exceptions.DBKeyNotFound: LOG.debug("logical router %s is not found in DF DB, " "suppressing delete_lrouter_port " "exception" % router_id) return new_router
def update_network(self, context, network_id, network): pnet._raise_if_updates_provider_attributes(network['network']) with context.session.begin(subtransactions=True): result = super(DFPlugin, self).update_network(context, network_id, network) if psec.PORTSECURITY in network['network']: self._process_network_port_security_update(context, network['network'], result) self._process_l3_update(context, result, network['network']) network_version = version_db._update_db_version_row( context.session, network_id) self.nb_api.update_lswitch(id=network_id, topic=result['tenant_id'], name=result.get( 'name', df_const.DF_NETWORK_DEFAULT_NAME), router_external=result['router:external'], version=network_version) return result
def delete_subnet_precommit(self, context): network_version = version_db._update_db_version_row( context._plugin_context.session, context.current['network_id']) context.current['db_version'] = network_version
def update_port(self, context, id, port): with context.session.begin(subtransactions=True): parent_name, tag = self._get_data_from_binding_profile( context, port['port']) original_port = self.get_port(context, id) updated_port = super(DFPlugin, self).update_port(context, id, port) # TODO(yuanwei): in ML2 plugin, security_groups and # allow_address_pairs configuration depend on portsec switch is # enabled. if psec.PORTSECURITY in port['port']: self._process_port_port_security_update( context, port['port'], updated_port) else: original_port_security = original_port.get(psec.PORTSECURITY) if original_port_security is not None: updated_port[psec.PORTSECURITY] = original_port_security else: # if the port-security-enabled field was not set in the # original port, we should remain this field of the # logical port in the DF DB unchanged. lport = self.nb_api.get_logical_port( port_id=id, topic=updated_port['tenant_id']) updated_port[psec.PORTSECURITY] = \ lport.get_port_security_enable() self._process_portbindings_create_and_update(context, port['port'], updated_port) self.update_security_group_on_port( context, id, port, original_port, updated_port) address_pairs_updated = False if addr_pair.ADDRESS_PAIRS in port['port']: address_pairs_updated = self.update_address_pairs_on_port( context, id, port, original_port, updated_port) if not address_pairs_updated: updated_port[addr_pair.ADDRESS_PAIRS] = original_port.get( addr_pair.ADDRESS_PAIRS, []) self._update_extra_dhcp_opts_on_port( context, id, port, updated_port=updated_port) port_version = version_db._update_db_version_row( context.session, id) ips = [] if 'fixed_ips' in updated_port: ips = [ip['ip_address'] for ip in updated_port['fixed_ips']] chassis = None if 'binding:host_id' in updated_port: chassis = updated_port['binding:host_id'] # Router GW ports are not needed by dragonflow controller and # they currently cause error as they couldnt be mapped to # a valid ofport (or location) if updated_port.get('device_owner') == const.DEVICE_OWNER_ROUTER_GW: chassis = None updated_security_groups = updated_port.get('security_groups') if updated_security_groups == []: security_groups = None else: security_groups = updated_security_groups port_name = updated_port.get('name', df_const.DF_PORT_DEFAULT_NAME) self.nb_api.update_lport(id=updated_port['id'], topic=updated_port['tenant_id'], macs=[updated_port['mac_address']], ips=ips, name=port_name, parent_name=parent_name, tag=tag, enabled=updated_port['admin_' 'state_up'], chassis=chassis, device_owner=updated_port.get( 'device_owner', None), security_groups=security_groups, port_security_enabled=updated_port[ psec.PORTSECURITY], allowed_address_pairs=updated_port[ addr_pair.ADDRESS_PAIRS], version=port_version) return updated_port
def update_port_precommit(self, context): port_version = version_db._update_db_version_row( context._plugin_context.session, context.current['id']) context.current['db_version'] = port_version