Debian Codegraph @

neutron_fwaas/services/logapi/fwg_validate.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
17 from neutron.services.logapi.common import exceptions as log_exc
18 from neutron.services.logapi.common import validators
19 from neutron_lib import constants as nl_const
20 from neutron_lib.plugins import directory
21 from sqlalchemy.orm import exc as orm_exc
22
23 from neutron_fwaas.common import fwaas_constants
24 from neutron_fwaas.services.logapi import constants as log_const
25 from neutron_fwaas.services.logapi import exceptions as fwg_log_exc
26
27 fwg_plugin = None
28
29
30 def _check_fwg(context, fwg_id):
31 try:
32 fwg = fwg_plugin.get_firewall_group(context, id=fwg_id)
33 except orm_exc.NoResultFound:
34 raise log_exc.ResourceNotFound(resource_id=fwg_id)
35
36 if fwg['status'] != nl_const.ACTIVE:
37 raise fwg_log_exc.FWGIsNotReadyForLogging(
38 fwg_id=fwg_id, fwg_status=fwg['status'])
39
40
41 def _check_fwg_port(context, port_id):
42
43 # Checking port exists
44 port = ports.Port.get_object(context, id=port_id)
45 if not port:
46 raise log_exc.TargetResourceNotFound(target_id=port_id)
47
48 device_owner = port.get('device_owner', '')
49 # Checking supported firewall group logging for vm port
50 if device_owner.startswith(nl_const.DEVICE_OWNER_COMPUTE_PREFIX):
51 if not validators.validate_log_type_for_port(
52 log_const.FIREWALL_GROUP, port):
53 raise log_exc.LoggingTypeNotSupported(
54 log_type=log_const.FIREWALL_GROUP,
55 port_id=port_id)
56 # Checking supported firewall group for router interface, DVR interface,
57 # and HA replicated interface
58 elif device_owner not in nl_const.ROUTER_INTERFACE_OWNERS:
59 raise log_exc.LoggingTypeNotSupported(
60 log_type=log_const.FIREWALL_GROUP, port_id=port_id)
61
62 # Checking port status
63 port_status = port.get('status')
64 if port_status != nl_const.PORT_STATUS_ACTIVE:
65 raise fwg_log_exc.PortIsNotReadyForLogging(target_id=port_id,
66 port_status=port_status)
67
68 # Checking whether router port or vm port binding with any firewall group
69 fwg_id = fwg_plugin.driver.firewall_db.get_fwg_attached_to_port(
70 context, port_id=port_id)
71
72 if not fwg_id:
73 raise fwg_log_exc.TargetResourceNotAssociated(target_id=port_id)
74
75 fwg = fwg_plugin.get_firewall_group(context, id=fwg_id)
76
77 if fwg['status'] != nl_const.ACTIVE:
78 raise fwg_log_exc.FWGIsNotReadyForLogging(fwg_id=fwg_id,
79 fwg_status=fwg['status'])
80
81
82 def _check_target_resource_bound_fwg(context, fwg_id, target_id):
83 ports = fwg_plugin.driver.firewall_db.get_ports_in_firewall_group(
84 context=context, firewall_group_id=fwg_id)
85 if target_id not in ports:
86 raise log_exc.InvalidResourceConstraint(
87 resource=log_const.FIREWALL_GROUP,
88 resource_id=fwg_id,
89 target_resource=log_const.TARGET_RESOURCE,
90 target_id=target_id)
91
92
93 @validators.ResourceValidateRequest.register(log_const.FIREWALL_GROUP)
94 def validate_firewall_group_request(context, log_data):
95 """Validate a log request
96
97 This method validates log request is satisfied or not.
98
99 A ResourceNotFound will be raised if resource_id in log_data not exists or
100 a TargetResourceNotFound will be raised if target_id in log_data not
101 exists. Beside, FWGIsNotReadyForLogging will be raised in the case of
102 queried firewall group is not in ACTIVE state. PortIsNotReadyForLogging
103 exception will be raised if port is not in ACTIVE status. Besides,
104 TargetResourceNotAssociated exception will be raised if a given port does
105 not have any firewall group attach to. This method will also raise a
106 LoggingTypeNotSupported, if there is no log_driver supporting for
107 resource_type in log_data.
108
109 In addition, if log_data specify both resource_id and target_id. A
110 InvalidResourceConstraint will be raised if there is no constraint between
111 resource_id and target_id.
112
113 """
114
115 global fwg_plugin
116 if not fwg_plugin:
117 fwg_plugin = directory.get_plugin(fwaas_constants.FIREWALL_V2)
118 resource_id = log_data.get('resource_id')
119 target_id = log_data.get('target_id')
120 if resource_id and target_id:
121 _check_target_resource_bound_fwg(context, resource_id, target_id)
122 if resource_id:
123 _check_fwg(context, resource_id)
124 if target_id:
125 _check_fwg_port(context, target_id)