Coverage for manila/network/linux/ovs_lib.py: 93%
27 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright 2014 Mirantis Inc.
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.
16import re
18from oslo_log import log
20from manila import utils
22LOG = log.getLogger(__name__)
25class OVSBridge(object):
26 def __init__(self, br_name):
27 self.br_name = br_name
28 self.re_id = self.re_compile_id()
30 def re_compile_id(self):
31 external = r'external_ids\s*'
32 mac = r'attached-mac="(?P<vif_mac>([a-fA-F\d]{2}:){5}([a-fA-F\d]{2}))"'
33 iface = r'iface-id="(?P<vif_id>[^"]+)"'
34 name = r'name\s*:\s"(?P<port_name>[^"]*)"'
35 port = r'ofport\s*:\s(?P<ofport>-?\d+)'
36 _re = (r'%(external)s:\s{ ( %(mac)s,? | %(iface)s,? | . )* }'
37 r' \s+ %(name)s \s+ %(port)s' % {'external': external,
38 'mac': mac,
39 'iface': iface, 'name': name,
40 'port': port})
41 return re.compile(_re, re.M | re.X)
43 def run_vsctl(self, args):
44 full_args = ["ovs-vsctl", "--timeout=2"] + args
45 try:
46 return utils.execute(*full_args, run_as_root=True)
47 except Exception:
48 LOG.exception("Unable to execute %(cmd)s.",
49 {'cmd': full_args})
51 def reset_bridge(self):
52 self.run_vsctl(["--", "--if-exists", "del-br", self.br_name])
53 self.run_vsctl(["add-br", self.br_name])
55 def delete_port(self, port_name):
56 self.run_vsctl(["--", "--if-exists", "del-port", self.br_name,
57 port_name])