Python neutron_lib.callbacks.events.AFTER_CREATE Examples

The following are 22 code examples of neutron_lib.callbacks.events.AFTER_CREATE(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module neutron_lib.callbacks.events , or try the search function .
Example #1
Source File: bgp_plugin.py    From neutron-dynamic-routing with Apache License 2.0 6 votes vote down vote up
def _register_callbacks(self):
        registry.subscribe(self.floatingip_update_callback,
                           resources.FLOATING_IP,
                           events.AFTER_UPDATE)
        registry.subscribe(self.router_interface_callback,
                           resources.ROUTER_INTERFACE,
                           events.AFTER_CREATE)
        registry.subscribe(self.router_interface_callback,
                           resources.ROUTER_INTERFACE,
                           events.BEFORE_CREATE)
        registry.subscribe(self.router_interface_callback,
                           resources.ROUTER_INTERFACE,
                           events.AFTER_DELETE)
        registry.subscribe(self.router_gateway_callback,
                           resources.ROUTER_GATEWAY,
                           events.AFTER_CREATE)
        registry.subscribe(self.router_gateway_callback,
                           resources.ROUTER_GATEWAY,
                           events.AFTER_DELETE)
        registry.subscribe(self.port_callback,
                           resources.PORT,
                           events.AFTER_UPDATE) 
Example #2
Source File: test_registry.py    From neutron-lib with Apache License 2.0 6 votes vote down vote up
def test_decorated_inst_method_receives(self):
        i1 = ObjectWithDecoratedCallback()
        registry.notify(resources.PORT, events.BEFORE_CREATE, self)
        self.assertEqual(0, i1.counter)
        registry.notify(resources.PORT, events.AFTER_CREATE, self)
        self.assertEqual(1, i1.counter)
        registry.notify(resources.PORT, events.AFTER_UPDATE, self)
        self.assertEqual(2, i1.counter)
        registry.notify(resources.NETWORK, events.AFTER_UPDATE, self)
        self.assertEqual(2, i1.counter)
        registry.notify(resources.NETWORK, events.AFTER_DELETE, self)
        self.assertEqual(3, i1.counter)
        i2 = ObjectWithDecoratedCallback()
        self.assertEqual(0, i2.counter)
        registry.notify(resources.NETWORK, events.AFTER_DELETE, self)
        self.assertEqual(4, i1.counter)
        self.assertEqual(1, i2.counter) 
Example #3
Source File: bgp_plugin.py    From neutron-dynamic-routing with Apache License 2.0 6 votes vote down vote up
def router_gateway_callback(self, resource, event, trigger, payload=None):
        if event == events.AFTER_CREATE:
            self._handle_router_gateway_after_create(payload)
        if event == events.AFTER_DELETE:
            gw_network = payload.metadata.get('network_id')
            router_id = payload.resource_id
            next_hops = self._next_hops_from_gateway_ips(
                payload.metadata.get('gateway_ips'))
            ctx = context.get_admin_context()
            speakers = self._bgp_speakers_for_gateway_network(ctx, gw_network)
            for speaker in speakers:
                if speaker.ip_version in next_hops:
                    next_hop = next_hops[speaker.ip_version]
                    prefixes = self._tenant_prefixes_by_router(ctx,
                                                               router_id,
                                                               speaker.id)
                    routes = self._route_list_from_prefixes_and_next_hop(
                                                                     prefixes,
                                                                     next_hop)
                self._handle_router_interface_after_delete(gw_network, routes) 
Example #4
Source File: test_bgp_plugin.py    From neutron-dynamic-routing with Apache License 2.0 6 votes vote down vote up
def test__register_callbacks(self):
        with mock.patch.object(registry, 'subscribe') as subscribe:
            plugin = bgp_plugin.BgpPlugin()
            expected_calls = [
                mock.call(plugin.bgp_drscheduler.schedule_bgp_speaker_callback,
                          dr_resources.BGP_SPEAKER, events.AFTER_CREATE),
                mock.call(plugin.floatingip_update_callback,
                          resources.FLOATING_IP, events.AFTER_UPDATE),
                mock.call(plugin.router_interface_callback,
                          resources.ROUTER_INTERFACE, events.AFTER_CREATE),
                mock.call(plugin.router_interface_callback,
                          resources.ROUTER_INTERFACE, events.BEFORE_CREATE),
                mock.call(plugin.router_interface_callback,
                          resources.ROUTER_INTERFACE, events.AFTER_DELETE),
                mock.call(plugin.router_gateway_callback,
                          resources.ROUTER_GATEWAY, events.AFTER_CREATE),
                mock.call(plugin.router_gateway_callback,
                          resources.ROUTER_GATEWAY, events.AFTER_DELETE),
                mock.call(plugin.port_callback,
                          resources.PORT, events.AFTER_UPDATE),
            ]
            self.assertEqual(subscribe.call_args_list, expected_calls) 
Example #5
Source File: test_bgp_plugin.py    From neutron-dynamic-routing with Apache License 2.0 6 votes vote down vote up
def test_create_bgp_speaker(self):
        test_context = 'create_bgp_context'
        test_bgp_speaker = {'id': None}
        payload = self._create_test_payload(context=test_context)
        with mock.patch.object(bgp_db.BgpDbMixin,
                               'create_bgp_speaker') as create_bgp_sp:
            with mock.patch.object(registry, 'notify') as notify:
                create_bgp_sp.return_value = payload['bgp_speaker']
                self.assertEqual(self.plugin.create_bgp_speaker(
                    test_context, test_bgp_speaker),
                    payload['bgp_speaker'])
                create_bgp_sp.assert_called_once_with(test_context,
                                                      test_bgp_speaker)
                notify.assert_called_once_with(dr_resources.BGP_SPEAKER,
                                               events.AFTER_CREATE,
                                               self.plugin,
                                               payload=payload) 
Example #6
Source File: mech_driver.py    From dragonflow with Apache License 2.0 6 votes vote down vote up
def update_security_group(self, resource, event, trigger, **kwargs):
        sg = kwargs['security_group']
        sg_name = sg.get('name')
        rules = sg.get('security_group_rules', [])

        for rule in rules:
            try:
                rule['topic'] = rule.pop('project_id')
            except KeyError:
                rule['topic'] = rule.pop('tenant_id', None)
        sg_obj = neutron_secgroups.security_group_from_neutron_obj(sg)
        if event == events.AFTER_CREATE:
            self.nb_api.create(sg_obj)
            LOG.info("DFMechDriver: create security group %s", sg_name)
        elif event == events.AFTER_UPDATE:
            self.nb_api.update(sg_obj)
            LOG.info("DFMechDriver: update security group %s", sg_name)

        return sg_obj 
Example #7
Source File: mech_driver.py    From dragonflow with Apache License 2.0 6 votes vote down vote up
def subscribe_registries(self):
        registry.subscribe(self.post_fork_initialize,
                           resources.PROCESS,
                           events.AFTER_INIT)

        registry.subscribe(self.update_security_group,
                           resources.SECURITY_GROUP,
                           events.AFTER_CREATE)
        registry.subscribe(self.update_security_group,
                           resources.SECURITY_GROUP,
                           events.AFTER_UPDATE)
        registry.subscribe(self.delete_security_group,
                           resources.SECURITY_GROUP,
                           events.BEFORE_DELETE)
        registry.subscribe(self.create_security_group_rule,
                           resources.SECURITY_GROUP_RULE,
                           events.AFTER_CREATE)
        registry.subscribe(self.delete_security_group_rule,
                           resources.SECURITY_GROUP_RULE,
                           events.AFTER_DELETE) 
Example #8
Source File: bgp_plugin.py    From dragonflow with Apache License 2.0 6 votes vote down vote up
def _register_callbacks(self):
        registry.subscribe(self.floatingip_update_callback,
                           resources.FLOATING_IP,
                           events.AFTER_UPDATE)
        registry.subscribe(self.router_port_callback,
                           resources.ROUTER_INTERFACE,
                           events.AFTER_CREATE)
        registry.subscribe(self.router_port_callback,
                           resources.ROUTER_INTERFACE,
                           events.AFTER_DELETE)
        registry.subscribe(self.router_port_callback,
                           resources.ROUTER_GATEWAY,
                           events.AFTER_CREATE)
        registry.subscribe(self.router_port_callback,
                           resources.ROUTER_GATEWAY,
                           events.AFTER_DELETE) 
Example #9
Source File: test_trunk_driver_v2.py    From networking-odl with Apache License 2.0 6 votes vote down vote up
def test_trunk_subports_set_status_create_parent_active(
            self, mock_set_subport_status):
        resource = resources.SUBPORTS
        event_type = events.AFTER_CREATE
        fake_payload = self._fake_trunk_payload()
        core_plugin = directory.get_plugin()

        fake_payload.subports = [models.SubPort(port_id='fake_port_id',
                                                segmentation_id=101,
                                                segmentation_type='vlan',
                                                trunk_id='fake_id')]
        parent_port = FAKE_PARENT

        with mock.patch.object(core_plugin, '_get_port') as gp:
            gp.return_value = parent_port
            self.handler.trunk_subports_set_status(resource, event_type,
                                                   mock.ANY, fake_payload)
            mock_set_subport_status.assert_called_once_with(
                core_plugin, mock.ANY, 'fake_port_id',
                n_const.PORT_STATUS_ACTIVE) 
Example #10
Source File: test_trunk_driver_v2.py    From networking-odl with Apache License 2.0 6 votes vote down vote up
def test_trunk_subports_set_status_create_parent_down(
            self, mock_set_subport_status):
        resource = resources.SUBPORTS
        event_type = events.AFTER_CREATE
        fake_payload = self._fake_trunk_payload()
        core_plugin = directory.get_plugin()

        fake_payload.subports = [models.SubPort(port_id='fake_port_id',
                                                segmentation_id=101,
                                                segmentation_type='vlan',
                                                trunk_id='fake_id')]
        parent_port = FAKE_PARENT.copy()
        parent_port['status'] = n_const.PORT_STATUS_DOWN

        with mock.patch.object(core_plugin, '_get_port') as gp:
            gp.return_value = parent_port
            self.handler.trunk_subports_set_status(resource, event_type,
                                                   mock.ANY, fake_payload)
            mock_set_subport_status.assert_called_once_with(
                core_plugin, mock.ANY, 'fake_port_id',
                n_const.PORT_STATUS_DOWN) 
Example #11
Source File: test_registry.py    From neutron-lib with Apache License 2.0 5 votes vote down vote up
def test_no_strings_in_events_arg(self):
        with testtools.ExpectedException(AssertionError):
            registry.receives(resources.PORT, events.AFTER_CREATE) 
Example #12
Source File: test_manager.py    From neutron-lib with Apache License 2.0 5 votes vote down vote up
def test_clearing_subscribers(self):
        self.manager.subscribe(
            callback_1, resources.PORT, events.BEFORE_CREATE)
        self.manager.subscribe(
            callback_2, resources.PORT, events.AFTER_CREATE)
        self.assertEqual(2, len(self.manager._callbacks[resources.PORT]))
        self.assertEqual(2, len(self.manager._index))
        self.manager.clear()
        self.assertEqual(0, len(self.manager._callbacks))
        self.assertEqual(0, len(self.manager._index)) 
Example #13
Source File: driver.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def _register_subport_events(self):
        registry.subscribe(self._add_subports_handler,
                           resources.SUBPORTS, events.AFTER_CREATE)
        registry.subscribe(self._delete_subports_handler,
                           resources.SUBPORTS, events.AFTER_DELETE)
        registry.subscribe(self._update_port_handler,
                           resources.PORT, events.AFTER_UPDATE) 
Example #14
Source File: driver.py    From dragonflow with Apache License 2.0 5 votes vote down vote up
def _register_trunk_events(self):
        registry.subscribe(self._add_trunk_handler,
                           resources.TRUNK, events.AFTER_CREATE) 
Example #15
Source File: test_callback.py    From networking-odl with Apache License 2.0 5 votes vote down vote up
def test_callback_postcommit_sg_rules_create(self):
        rule = mock.Mock()
        rule_id = rule.get('id')
        self._test_callback_postcommit_for_sg_rules(
            events.AFTER_CREATE, odl_const.ODL_CREATE, rule, rule_id) 
Example #16
Source File: test_callback.py    From networking-odl with Apache License 2.0 5 votes vote down vote up
def test_callback_postcommit_sg_create(self):
        sg = mock.Mock()
        sg_id = sg.get('id')
        self._test_callback_postcommit_for_sg(
            events.AFTER_CREATE, odl_const.ODL_CREATE, sg, sg_id) 
Example #17
Source File: trunk_driver_v2.py    From networking-odl with Apache License 2.0 5 votes vote down vote up
def register(self, resource, event, trigger, payload=None):
        super(OpenDaylightTrunkDriverV2, self).register(
            resource, event, trigger, payload=payload)
        self._handler = OpenDaylightTrunkHandlerV2()
        registry.subscribe(self._handler.trunk_create_precommit,
                           resources.TRUNK, events.PRECOMMIT_CREATE)
        registry.subscribe(self._handler.trunk_create_postcommit,
                           resources.TRUNK, events.AFTER_CREATE)
        registry.subscribe(self._handler.trunk_update_precommit,
                           resources.TRUNK, events.PRECOMMIT_UPDATE)
        registry.subscribe(self._handler.trunk_update_postcommit,
                           resources.TRUNK, events.AFTER_UPDATE)
        registry.subscribe(self._handler.trunk_delete_precommit,
                           resources.TRUNK, events.PRECOMMIT_DELETE)
        registry.subscribe(self._handler.trunk_delete_postcommit,
                           resources.TRUNK, events.AFTER_DELETE)
        for event_ in (events.PRECOMMIT_CREATE, events.PRECOMMIT_DELETE):
            registry.subscribe(self._handler.trunk_update_precommit,
                               resources.SUBPORTS, event_)
        for event_ in (events.AFTER_CREATE, events.AFTER_DELETE):
            registry.subscribe(self._handler.trunk_update_postcommit,
                               resources.SUBPORTS, event_)
            # Upon subport creation/deletion we need to set the right port
            # status:
            # 1. Set it to parent status when it is attached to the trunk
            # 2. Set it to down when is removed from the trunk
            registry.subscribe(self._handler.trunk_subports_set_status,
                               resources.SUBPORTS, event_)
        # NOTE(ltomasbo): if the status of the parent port changes, the
        # subports need to update their status too
        registry.subscribe(self._handler.trunk_subports_update_status,
                           resources.PORT, events.AFTER_UPDATE) 
Example #18
Source File: test_bgp_dragent_scheduler.py    From neutron-dynamic-routing with Apache License 2.0 5 votes vote down vote up
def test_schedule_bgp_speaker_callback_with_valid_event(self):
        payload = self._create_test_payload()
        with mock.patch.object(self.plugin,
                               'schedule_bgp_speaker') as sched_bgp:
            self.scheduler.schedule_bgp_speaker_callback(
                dr_resources.BGP_SPEAKER,
                events.AFTER_CREATE,
                self.scheduler, payload)
            sched_bgp.assert_called_once_with(mock.ANY,
                                              payload['bgp_speaker']) 
Example #19
Source File: test_bgp_dragent_scheduler.py    From neutron-dynamic-routing with Apache License 2.0 5 votes vote down vote up
def test__register_callbacks(self):
        with mock.patch.object(registry, 'subscribe') as subscribe:
            scheduler = bgp_dras.ChanceScheduler()
            expected_calls = [
                mock.call(scheduler.schedule_bgp_speaker_callback,
                          dr_resources.BGP_SPEAKER, events.AFTER_CREATE),
            ]
            self.assertEqual(subscribe.call_args_list, expected_calls)
        with mock.patch.object(registry, 'subscribe') as subscribe:
            scheduler = bgp_dras.WeightScheduler()
            expected_calls = [
                mock.call(scheduler.schedule_bgp_speaker_callback,
                          dr_resources.BGP_SPEAKER, events.AFTER_CREATE),
            ]
            self.assertEqual(subscribe.call_args_list, expected_calls) 
Example #20
Source File: bgp_dragent_scheduler.py    From neutron-dynamic-routing with Apache License 2.0 5 votes vote down vote up
def _register_callbacks(self):
        registry.subscribe(self.schedule_bgp_speaker_callback,
                           dr_resources.BGP_SPEAKER,
                           events.AFTER_CREATE) 
Example #21
Source File: bgp_plugin.py    From neutron-dynamic-routing with Apache License 2.0 5 votes vote down vote up
def router_interface_callback(self, resource, event, trigger, **kwargs):
        if event == events.AFTER_CREATE:
            self._handle_router_interface_after_create(**kwargs)
        if event == events.AFTER_DELETE:
            gw_network = kwargs['network_id']
            next_hops = self._next_hops_from_gateway_ips(
                                                        kwargs['gateway_ips'])
            ctx = context.get_admin_context()
            speakers = self._bgp_speakers_for_gateway_network(ctx, gw_network)
            for speaker in speakers:
                routes = self._route_list_from_prefixes_and_next_hop(
                                                kwargs['cidrs'],
                                                next_hops[speaker.ip_version])
                self._handle_router_interface_after_delete(gw_network, routes) 
Example #22
Source File: bgp_plugin.py    From neutron-dynamic-routing with Apache License 2.0 5 votes vote down vote up
def create_bgp_speaker(self, context, bgp_speaker):
        bgp_speaker = super(BgpPlugin, self).create_bgp_speaker(context,
                                                                bgp_speaker)
        payload = {'plugin': self, 'context': context,
                   'bgp_speaker': bgp_speaker}
        registry.notify(dr_resources.BGP_SPEAKER, events.AFTER_CREATE,
                        self, payload=payload)
        return bgp_speaker