def test_no_reassociate_on_update(self): fip = l3.FloatingIp(id='fake_id', lport=local_lport1) old_fip = l3.FloatingIp(id='fake_id', lport=local_lport1) fip.emit_updated(old_fip) self.dnat_app._install_local_floatingip.assert_not_called() self.dnat_app._uninstall_local_floatingip.assert_not_called()
def test_disassociate_on_update(self): old_fip = l3.FloatingIp(id='fake_id', lport=local_lport1) fip = l3.FloatingIp(id='fake_id') with self._mock_assoc_disassoc() as (a, d): fip.emit_updated(old_fip) a.assert_not_called() d.assert_called_once_with(old_fip)
def test_reassociate_on_lport_change_non_local(self): old_fip = l3.FloatingIp(id='fake_id', lport=local_lport1, floating_lport=floating_lport) fip = l3.FloatingIp(id='fake_id', lport=remote_lport) fip.emit_updated(old_fip) self.dnat_app._uninstall_local_floatingip.assert_called_once_with( old_fip) self.dnat_app._install_remote_floatingip.assert_called_once_with(fip)
def test_uninstall_on_update(self): old_fip = l3.FloatingIp(id='fake_id', topic='topic', version=4, lport=local_lport1, floating_lport=floating_lport) fip = l3.FloatingIp(id='fake_id') fip.emit_updated(old_fip) self.dnat_app._uninstall_local_floatingip.assert_called_once_with( old_fip)
def update_floatingip(self, context, id, floatingip): floatingip_dict = super( DFL3AgentlessRouterPlugin, self, ).update_floatingip(context, id, floatingip) # Check is status update is required new_status = self._port_to_floatingip_status( context, floatingip_dict.get('port_id'), ) if new_status != floatingip_dict['status']: self.update_floatingip_status( context, floatingip_dict['id'], new_status, ) self.nb_api.update( l3.FloatingIp( id=floatingip_dict['id'], topic=floatingip_dict['tenant_id'], name=floatingip_dict.get('name', df_const.DF_FIP_DEFAULT_NAME), version=floatingip_dict['revision_number'], lrouter=floatingip_dict['router_id'], lport=floatingip_dict['port_id'], fixed_ip_address=floatingip_dict['fixed_ip_address'], ), ) return floatingip_dict
def create_floatingip(self, context, floatingip): initial_status = self._port_to_floatingip_status( context, floatingip['floatingip'].get('port_id'), ) floatingip_dict = super( DFL3AgentlessRouterPlugin, self, ).create_floatingip(context, floatingip, initial_status=initial_status) floatingip_port = self._get_floatingip_port(context, floatingip_dict['id']) self.nb_api.create( l3.FloatingIp( id=floatingip_dict['id'], topic=floatingip_dict['tenant_id'], name=floatingip_dict.get('name', df_const.DF_FIP_DEFAULT_NAME), version=floatingip_dict['revision_number'], floating_ip_address=floatingip_dict['floating_ip_address'], fixed_ip_address=floatingip_dict['fixed_ip_address'], lrouter=floatingip_dict['router_id'], lport=floatingip_dict['port_id'], floating_lport=floatingip_port['id'], ), skip_send_event=('port_id' not in floatingip_dict), ) return floatingip_dict
def test_remove_local_lport(self): fip = l3.FloatingIp(id='fake_id', lport=local_lport1) self.dnat_app.local_floatingips[fip.id] = fip with self._mock_assoc_disassoc() as (_, d): local_lport1.emit_unbind_local() d.assert_called_once_with(fip)
def _handle_egress_invalid_ttl(self, event): LOG.debug("Get an invalid TTL packet at table %s", const.EGRESS_DNAT_TABLE) if self.egress_ttl_invalid_handler_rate_limit(): LOG.warning("Get more than %(rate)s TTL invalid " "packets per second at table %(table)s", {'rate': self.conf.dnat_ttl_invalid_max_rate, 'table': const.EGRESS_DNAT_TABLE}) return msg = event.msg pkt = packet.Packet(msg.data) e_pkt = pkt.get_protocol(ethernet.ethernet) port_key = msg.match.get('reg6') lport = self.db_store.get_one( l2.LogicalPort(unique_key=port_key), index=l2.LogicalPort.get_index('unique_key'), ) floatingip = self.db_store.get_one( l3.FloatingIp(lport=lport.id), index=l3.FloatingIp.get_index('lport'), ) if floatingip is None: LOG.warning("The invalid TTL packet's destination mac %s " "can't be recognized.", e_pkt.src) return icmp_ttl_pkt = icmp_error_generator.generate( icmp.ICMP_TIME_EXCEEDED, icmp.ICMP_TTL_EXPIRED_CODE, msg.data, floatingip.floating_ip_address, pkt) self.dispatch_packet(icmp_ttl_pkt, port_key)
def delete_floatingip(self, context, fip_id): floatingip = self.get_floatingip(context, fip_id) super(DFL3RouterPlugin, self).delete_floatingip(context, fip_id) try: self.nb_api.delete( l3.FloatingIp(id=fip_id, topic=floatingip['tenant_id']), ) except df_exceptions.DBKeyNotFound: LOG.exception("floatingip %s is not found in DF DB", fip_id)
def test_floating_port_deleted_before_fip_delete(self): fip = l3.FloatingIp( id='fake_id', lport=local_lport1, floating_lport=floating_lport, ) fip.emit_deleted() self.dnat_app._uninstall_local_floatingip.assert_not_called()
def test_uninstall_remote_floatingip_called_on_delete(self): fip = l3.FloatingIp( id='fake_id', lport=remote_lport, floating_lport=floating_lport, ) fip.emit_deleted() self.dnat_app._uninstall_remote_floatingip.assert_called_once_with(fip)
def _create_local_port(self, lport): fips = self.db_store.get_all( l3.FloatingIp(lport=lport), index=l3.FloatingIp.get_index('lport'), ) for fip in fips: self._associate_floatingip(fip)
def build_floating_ip_from_neutron_floating_ip(floating_ip): return l3.FloatingIp( id=floating_ip['id'], topic=utils.get_obj_topic(floating_ip), version=floating_ip['revision_number'], lrouter=floating_ip.get('router_id', None), lport=floating_ip.get('port_id', None), fixed_ip_address=floating_ip.get('fixed_ip_address', None), )
def delete_floatingip(self, context, fip_id): floatingip = self.get_floatingip(context, fip_id) super(DFL3AgentlessRouterPlugin, self).delete_floatingip(context, fip_id) try: topic = df_utils.get_obj_topic(floatingip) self.nb_api.delete(l3.FloatingIp(id=fip_id, topic=topic), ) except df_exceptions.DBKeyNotFound: LOG.exception("floatingip %s is not found in DF DB", fip_id)
def update_fip_status(self, context, fip_id, status): self.nb_api.update( l3.FloatingIp( id=fip_id, status=status, ), skip_send_event=True, ) self.update_floatingip_status(context, fip_id, status)
def test_remove_remote_lport(self): fip = l3.FloatingIp( id='fake_id', topic='topic1', version=1, lport=remote_lport, ) self.dnat_app.db_store.update(fip) remote_lport.emit_unbind_remote() self.dnat_app._uninstall_remote_floatingip.assert_called_once_with(fip)
def build_floating_ip_from_neutron_floating_ip(floating_ip): return l3.FloatingIp( id=floating_ip['id'], topic=floating_ip['tenant_id'], name=floating_ip.get('name', df_const.DF_FIP_DEFAULT_NAME), version=floating_ip['revision_number'], lrouter=floating_ip.get('router_id', None), lport=floating_ip.get('port_id', None), fixed_ip_address=floating_ip.get('fixed_ip_address', None), )
def test_add_local_lport(self): fip = l3.FloatingIp( id='fake_id', topic='topic1', version=1, lport=local_lport1, ) self.dnat_app.db_store.update(fip) local_lport1.emit_bind_local() self.dnat_app._install_local_floatingip.assert_called_once_with(fip)
def test_install_remote_floatingip_called_on_create(self): fip = l3.FloatingIp( id='fake_id', topic='topic1', version=4, lport=remote_lport, floating_lport=floating_lport, ) fip.emit_created() self.dnat_app._install_remote_floatingip.assert_called_once_with(fip)
def _update_local_port(self, lport, orig_lport): if orig_lport.is_local: # Associate only when lport becomes local return fips = self.db_store.get_all( l3.FloatingIp(lport=lport), index=l3.FloatingIp.get_index('lport'), ) for fip in fips: self._associate_floatingip(fip)
def update_floatingip(self, context, id, floatingip): floatingip_dict = super(DFL3RouterPlugin, self).update_floatingip( context, id, floatingip) self.nb_api.update( l3.FloatingIp( id=floatingip_dict['id'], topic=floatingip_dict['tenant_id'], name=floatingip_dict.get('name', df_const.DF_FIP_DEFAULT_NAME), version=floatingip_dict['revision_number'], lrouter=floatingip_dict['router_id'], lport=floatingip_dict['port_id'], fixed_ip_address=floatingip_dict['fixed_ip_address'], ), ) return floatingip_dict
def create_floatingip(self, context, floatingip): floatingip_port = None try: floatingip_dict = super(DFL3RouterPlugin, self).create_floatingip( context, floatingip, initial_status=const.FLOATINGIP_STATUS_DOWN) # Note: Here the context is elevated, because the floatingip port # will not have tenant and floatingip subnet might be in other # tenant. admin_context = context.elevated() floatingip_port = self._get_floatingip_port( admin_context, floatingip_dict['id']) if not floatingip_port: raise n_common_exc.DeviceNotFoundError( device_name=floatingip_dict['id']) except Exception: with excutils.save_and_reraise_exception() as ctxt: ctxt.reraise = True # delete the stale floatingip port try: if floatingip_port: self.nb_api.delete( l2.LogicalPort(id=floatingip_port['id'], topic=floatingip_port['tenant_id'])) except df_exceptions.DBKeyNotFound: pass self.nb_api.create( l3.FloatingIp( id=floatingip_dict['id'], topic=floatingip_dict['tenant_id'], name=floatingip_dict.get('name', df_const.DF_FIP_DEFAULT_NAME), version=floatingip_dict['revision_number'], status=floatingip_dict['status'], floating_ip_address=floatingip_dict['floating_ip_address'], fixed_ip_address=floatingip_dict['fixed_ip_address'], lrouter=floatingip_dict['router_id'], lport=floatingip_dict['port_id'], floating_lport=floatingip_port['id'], ), skip_send_event=('port_id' not in floatingip_dict), ) return floatingip_dict
def test_remote_floating_lport_deleted(self): fip = l3.FloatingIp( id='fake_id', topic='topic1', version=1, lport=remote_lport, floating_lport=floating_lport, ) self.dnat_app.db_store.update(fip) m = mock.Mock() def cb(lport): if lport.id == floating_lport.id: m() l2.LogicalPort.register_unbind_remote(cb) floating_lport.emit_bind_remote() self.controller.delete(floating_lport) m.assert_called_once_with() self.dnat_app._uninstall_remote_floatingip.assert_called_once_with( fip, emit_unbind=False)
fake_remote_port1 = make_fake_remote_port( id='fake_remote_port', macs=['fa:16:3e:8c:2e:af'], name='fake_remote_port', ips=['10.0.0.8'], binding=chassis_binding('fake_host2'), unique_key=5, subnets=['fake_subnet1']) fake_floatingip1 = l3.FloatingIp( id='fake_floatingip_id1', topic='fake_tenant1', version=7, floating_ip_address='172.24.4.2', fixed_ip_address='10.0.0.6', lport='fake_port1', floating_lport='fake_floatingip_port_id', lrouter='fake_router_id', ) fake_security_group = secgroups.SecurityGroup( name="fake_security_group", topic="fake_tenant1", version=5, unique_key=1, id="fake_security_group_id1", rules=[ secgroups.SecurityGroupRule( direction="egress", security_group_id="fake_security_group_id1",
version=1, binding=test_app_base.local_binding, ) remote_lport = l2.LogicalPort( id='lport3', topic='topic1', unique_key=4, version=1, binding=test_app_base.remote_binding, ) floatingip1 = l3.FloatingIp( id='floatingip1', topic='tenant1', version=7, floating_ip_address='172.24.4.2', floating_lport='floating_lport1', lrouter='fake_router_id', ) class TestDNATApp(test_app_base.DFAppTestBase): apps_list = ["dnat"] def setUp(self): super(TestDNATApp, self).setUp(enable_selective_topo_dist=True) self.dnat_app = self.open_flow_app.dispatcher.apps['dnat'] self.dnat_app.external_ofport = 99 self._mock_dnat_method('_install_local_floatingip') self._mock_dnat_method('_uninstall_local_floatingip') self._mock_dnat_method('_install_remote_floatingip')
def test_install_floatingip_not_called_on_create(self): l3.FloatingIp(id='fake_id').emit_created() self.dnat_app._install_local_floatingip.assert_not_called() self.dnat_app._install_remote_floatingip.assert_not_called()
def test_disassociate_floatingip_not_called_on_delete(self): fip = l3.FloatingIp(id='fake_id') with self._mock_assoc_disassoc() as (_, d): fip.emit_deleted() d.assert_not_called()
def test_disassociate_floatingip_called_on_delete(self): fip = l3.FloatingIp(id='fake_id', lport=local_lport1) with self._mock_assoc_disassoc() as (_, d): fip.emit_deleted() d.assert_called_once_with(fip)
def test_install_on_update(self): old_fip = l3.FloatingIp(id='fake_id') fip = l3.FloatingIp(id='fake_id', lport=local_lport1) fip.emit_updated(old_fip) self.dnat_app._install_local_floatingip.assert_called_once_with(fip)
def _get_floatingips_by_floating_lport(self, lport): return self.db_store.get_all( l3.FloatingIp(floating_lport=lport.id), index=l3.FloatingIp.get_index('floating_lport'), )