From 231067cabadb91eb40e44202b4f55785d5c7afe1 Mon Sep 17 00:00:00 2001 From: Slawek Kaplonski Date: Wed, 27 Apr 2022 16:49:26 +0200 Subject: [PATCH] Rehome ovsfw constants and utils modules Constants module contains definition of the OF registry numbers used by ovs firewall driver. After [1] some other registries are also used to store information related to QoS bw limit and min bw rules. So it would be good to have list of all used registers in one place and reuse them where it's needed. That's why this patch moves those constants to the neutron_lib. Additionally it also adds utils module with some helper functions which are using those constants and are actually used not only in neutron but also in neutron-fwaas. [1] https://review.opendev.org/c/openstack/neutron/+/832662 Related-Bug: #1959567 Change-Id: I89cc83699770e97bba369f50dd4d9e0ed6563aa4 --- neutron_lib/agent/common/__init__.py | 0 neutron_lib/agent/common/constants.py | 28 +++++++++++ neutron_lib/agent/common/utils.py | 50 +++++++++++++++++++ neutron_lib/tests/unit/agent/__init__.py | 0 .../tests/unit/agent/common/__init__.py | 0 .../tests/unit/agent/common/test_utils.py | 41 +++++++++++++++ .../tests/unit/agent/linux/__init__.py | 0 ...s-firewall-constants-522a307ff8ef4a78.yaml | 8 +++ 8 files changed, 127 insertions(+) create mode 100644 neutron_lib/agent/common/__init__.py create mode 100644 neutron_lib/agent/common/constants.py create mode 100644 neutron_lib/agent/common/utils.py create mode 100644 neutron_lib/tests/unit/agent/__init__.py create mode 100644 neutron_lib/tests/unit/agent/common/__init__.py create mode 100644 neutron_lib/tests/unit/agent/common/test_utils.py create mode 100644 neutron_lib/tests/unit/agent/linux/__init__.py create mode 100644 releasenotes/notes/rehome-ovs-firewall-constants-522a307ff8ef4a78.yaml diff --git a/neutron_lib/agent/common/__init__.py b/neutron_lib/agent/common/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/neutron_lib/agent/common/constants.py b/neutron_lib/agent/common/constants.py new file mode 100644 index 000000000..9433cc54d --- /dev/null +++ b/neutron_lib/agent/common/constants.py @@ -0,0 +1,28 @@ +# Copyright 2022 +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +REG_INGRESS_BW_LIMIT = 3 +REG_MIN_BW = 4 +REG_PORT = 5 +REG_NET = 6 +# for logging remote group rule +REG_REMOTE_GROUP = 7 + +INGRESS_BW_LIMIT_REG_NAME = "reg_ingress_bw_limit" +MIN_BW_REG_NAME = "reg_min_bw" +PORT_REG_NAME = "reg_port" +NET_REG_NAME = "reg_net" +REMOTE_GROUP_REG_NAME = "reg_remote_group" diff --git a/neutron_lib/agent/common/utils.py b/neutron_lib/agent/common/utils.py new file mode 100644 index 000000000..2462d17ea --- /dev/null +++ b/neutron_lib/agent/common/utils.py @@ -0,0 +1,50 @@ +# Copyright 2022 +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from neutron_lib.agent.common import constants + + +def _replace_register(flow_params, register_number, register_value): + """Replace value from flows to given register number + + 'register_value' key in dictionary will be replaced by register number + given by 'register_number' + + :param flow_params: Dictionary containing defined flows + :param register_number: The number of register where value will be stored + :param register_value: Key to be replaced by register number + + """ + try: + reg_port = flow_params[register_value] + del flow_params[register_value] + flow_params['reg{:d}'.format(register_number)] = reg_port + except KeyError: + pass + + +def create_reg_numbers(flow_params): + """Replace reg_(port|net) values with defined register numbers""" + _replace_register(flow_params, constants.REG_PORT, constants.PORT_REG_NAME) + _replace_register(flow_params, constants.REG_NET, constants.NET_REG_NAME) + _replace_register(flow_params, + constants.REG_REMOTE_GROUP, + constants.REMOTE_GROUP_REG_NAME) + _replace_register(flow_params, + constants.REG_MIN_BW, + constants.MIN_BW_REG_NAME) + _replace_register(flow_params, + constants.REG_INGRESS_BW_LIMIT, + constants.INGRESS_BW_LIMIT_REG_NAME) diff --git a/neutron_lib/tests/unit/agent/__init__.py b/neutron_lib/tests/unit/agent/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/neutron_lib/tests/unit/agent/common/__init__.py b/neutron_lib/tests/unit/agent/common/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/neutron_lib/tests/unit/agent/common/test_utils.py b/neutron_lib/tests/unit/agent/common/test_utils.py new file mode 100644 index 000000000..3403ef0c8 --- /dev/null +++ b/neutron_lib/tests/unit/agent/common/test_utils.py @@ -0,0 +1,41 @@ +# Copyright 2022 +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from neutron_lib.agent.common import constants +from neutron_lib.agent.common import utils +from neutron_lib.tests import _base + + +class TestCreateRegNumbers(_base.BaseTestCase): + def test_no_registers_defined(self): + flow = {'foo': 'bar'} + utils.create_reg_numbers(flow) + self.assertEqual({'foo': 'bar'}, flow) + + def test_all_registers_defined(self): + flow = {'foo': 'bar', + constants.PORT_REG_NAME: 1, + constants.NET_REG_NAME: 2, + constants.REMOTE_GROUP_REG_NAME: 3, + constants.INGRESS_BW_LIMIT_REG_NAME: 4, + constants.MIN_BW_REG_NAME: 5} + expected_flow = {'foo': 'bar', + 'reg{:d}'.format(constants.REG_PORT): 1, + 'reg{:d}'.format(constants.REG_NET): 2, + 'reg{:d}'.format(constants.REG_REMOTE_GROUP): 3, + 'reg{:d}'.format(constants.REG_INGRESS_BW_LIMIT): 4, + 'reg{:d}'.format(constants.REG_MIN_BW): 5} + utils.create_reg_numbers(flow) + self.assertEqual(expected_flow, flow) diff --git a/neutron_lib/tests/unit/agent/linux/__init__.py b/neutron_lib/tests/unit/agent/linux/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/releasenotes/notes/rehome-ovs-firewall-constants-522a307ff8ef4a78.yaml b/releasenotes/notes/rehome-ovs-firewall-constants-522a307ff8ef4a78.yaml new file mode 100644 index 000000000..94e85713b --- /dev/null +++ b/releasenotes/notes/rehome-ovs-firewall-constants-522a307ff8ef4a78.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + The neutron ovs_lib related constantswhich represents used + OpenFlow registries are now available in the + ``neutron_lib.agent.common.constants`` module. + The helper function ``create_reg_numbers`` is now available in the + ``neutron_lib.agent.common.utils`` module.