1 # Copyright (c) 2016-2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """ Helper function to get Network Service testing configuration """
16 from __future__ import absolute_import
21 from oslo_config import cfg
22 from oslo_config.cfg import NoSuchOptError
23 from oslo_utils import encodeutils
26 NSB_ROOT = "/opt/nsb_bin"
30 cfg.StrOpt('bin_path',
32 help='bin_path for VNFs location.'),
33 cfg.StrOpt('trex_path',
34 default=os.path.join(NSB_ROOT, 'trex/scripts'),
35 help='trex automation lib path.'),
36 cfg.StrOpt('trex_client_lib',
37 default=os.path.join(NSB_ROOT, 'trex_client/stl'),
38 help='trex python library path.'),
40 CONF.register_opts(OPTS, group="nsb")
43 HEXADECIMAL = "[0-9a-zA-Z]"
46 class PciAddress(object):
47 """Class to handle PCI addresses.
49 A PCI address could be written in two ways:
50 - Simple BDF notation:
51 00:00.0 # bus:device.function
52 - With domain extension.
53 0000:00:00.0 # domain:bus:device.function
55 Note: in Libvirt, 'device' is called 'slot'.
57 Reference: https://wiki.xenproject.org/wiki/
58 Bus:Device.Function_(BDF)_Notation
61 r"((?P<domain>[0-9a-zA-Z]{4}):)?(?P<bus>[0-9a-zA-Z]{2}):"
62 r"(?P<slot>[0-9a-zA-Z]{2})\.(?P<function>[0-9a-zA-Z]{1})")
64 def __init__(self, address):
65 pci_pattern = re.compile(self.PCI_PATTERN_STR)
66 match = pci_pattern.search(address)
68 raise ValueError('Invalid PCI address: {}'.format(address))
70 self._domain = (match.group('domain') or '0000').lower()
71 self._bus = match.group('bus').lower()
72 self._slot = match.group('slot').lower()
73 self._function = match.group('function').lower()
74 self.address = '{:0>4}:{:0>2}:{:0>2}.{:1}'.format(self.domain,
100 return [self._domain, self._bus, self._slot, self._function]
103 def get_nsb_option(option, default=None):
104 """return requested option for yardstick.conf"""
107 return CONF.nsb.__getitem__(option)
108 except NoSuchOptError:
109 logging.debug("Invalid key %s", option)
113 def provision_tool(connection, tool_path, tool_file=None):
115 verify if the tool path exits on the node,
116 if not push the local binary to remote node
121 tool_path = get_nsb_option('tool_path')
123 tool_path = os.path.join(tool_path, tool_file)
124 bin_path = get_nsb_option("bin_path")
125 exit_status = connection.execute("which %s > /dev/null 2>&1" % tool_path)[0]
127 return encodeutils.safe_decode(tool_path, incoming='utf-8').rstrip()
129 logging.warning("%s not found on %s, will try to copy from localhost",
130 tool_path, connection.host)
131 bin_path = get_nsb_option("bin_path")
132 connection.execute('mkdir -p "%s"' % bin_path)
133 connection.put(tool_path, tool_path)