Debian Codegraph @

neutron_fwaas/services/logapi/common/fwg_callback.py

raw symbols SCIP
1 # Copyright (c) 2018 Fujitsu Limited
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 from neutron.objects import ports as port_objects
17 from neutron.services.logapi.drivers import manager
18 from neutron_lib.callbacks import events
19 from neutron_lib import constants as nl_const
20 from neutron_lib.services.logapi import constants as log_const
21
22 from neutron_fwaas.services.logapi.common import log_db_api
23
24
25 class FirewallGroupCallBack(manager.ResourceCallBackBase):
26
27 def handle_event(self, resource, event, trigger, **kwargs):
28 payload = kwargs.get('payload')
29 context = payload.context
30 ports_delta = []
31 if event == events.AFTER_CREATE:
32 # Update log when a new firewall group is created with ports
33 ports_delta = payload.latest_state['ports']
34
35 elif event == events.AFTER_UPDATE:
36 old_ports = payload.states[0]['ports']
37 new_ports = payload.states[1]['ports']
38
39 # Check whether port is updated from firewall group or not
40 ports_delta = \
41 set(new_ports).symmetric_difference(set(old_ports))
42
43 if self.need_to_notify(context, ports_delta):
44 self.trigger_logging(context, payload.resource_id, ports_delta)
45
46 def trigger_logging(self, context, fwg_id, ports_delta):
47 log_resources = log_db_api.get_logs_for_fwg(
48 context, fwg_id, ports_delta)
49 if log_resources:
50 self.resource_push_api(
51 log_const.RESOURCE_UPDATE, context, log_resources)
52
53 def need_to_notify(self, context, ports):
54 notify = False
55 for port_id in ports:
56 port = port_objects.Port.get_object(context, id=port_id)
57 if not port:
58 continue
59 device_owner = port.get('device_owner', '')
60 if device_owner in nl_const.ROUTER_INTERFACE_OWNERS:
61 notify = True
62 break
63 return notify