Merge "Ensure that at least one handler is available"
[yardstick.git] / yardstick / network_services / utils.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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 """
15
16 from __future__ import absolute_import
17 import logging
18 import os
19 import re
20
21 from oslo_config import cfg
22 from oslo_config.cfg import NoSuchOptError
23 from oslo_utils import encodeutils
24
25 NSB_ROOT = "/opt/nsb_bin"
26
27 CONF = cfg.CONF
28 OPTS = [
29     cfg.StrOpt('bin_path',
30                default=NSB_ROOT,
31                help='bin_path for VNFs location.'),
32     cfg.StrOpt('trex_path',
33                default=os.path.join(NSB_ROOT, 'trex/scripts'),
34                help='trex automation lib path.'),
35     cfg.StrOpt('trex_client_lib',
36                default=os.path.join(NSB_ROOT, 'trex_client/stl'),
37                help='trex python library path.'),
38 ]
39 CONF.register_opts(OPTS, group="nsb")
40
41
42 HEXADECIMAL = "[0-9a-zA-Z]"
43
44
45 class PciAddress(object):
46
47     PCI_PATTERN_STR = HEXADECIMAL.join([
48         "(",
49         "{4}):(",  # domain (4 bytes)
50         "{2}):(",  # bus (2 bytes)
51         "{2}).(",  # function (2 bytes)
52         ")",       # slot (1 byte)
53     ])
54
55     PCI_PATTERN = re.compile(PCI_PATTERN_STR)
56
57     @classmethod
58     def parse_address(cls, text, multi_line=False):
59         if multi_line:
60             text = text.replace(os.linesep, '')
61             match = cls.PCI_PATTERN.search(text)
62         return cls(match.group(0))
63
64     def __init__(self, address):
65         super(PciAddress, self).__init__()
66         match = self.PCI_PATTERN.match(address)
67         if not match:
68             raise ValueError('Invalid PCI address: {}'.format(address))
69         self.address = address
70         self.match = match
71
72     def __repr__(self):
73         return self.address
74
75     @property
76     def domain(self):
77         return self.match.group(1)
78
79     @property
80     def bus(self):
81         return self.match.group(2)
82
83     @property
84     def slot(self):
85         return self.match.group(3)
86
87     @property
88     def function(self):
89         return self.match.group(4)
90
91     def values(self):
92         return [self.match.group(n) for n in range(1, 5)]
93
94
95 def get_nsb_option(option, default=None):
96     """return requested option for yardstick.conf"""
97
98     try:
99         return CONF.nsb.__getitem__(option)
100     except NoSuchOptError:
101         logging.debug("Invalid key %s", option)
102     return default
103
104
105 def provision_tool(connection, tool_path, tool_file=None):
106     """
107     verify if the tool path exits on the node,
108     if not push the local binary to remote node
109
110     :return - Tool path
111     """
112     if not tool_path:
113         tool_path = get_nsb_option('tool_path')
114     if tool_file:
115         tool_path = os.path.join(tool_path, tool_file)
116     bin_path = get_nsb_option("bin_path")
117     exit_status = connection.execute("which %s > /dev/null 2>&1" % tool_path)[0]
118     if exit_status == 0:
119         return encodeutils.safe_decode(tool_path, incoming='utf-8').rstrip()
120
121     logging.warning("%s not found on %s, will try to copy from localhost",
122                     tool_path, connection.host)
123     bin_path = get_nsb_option("bin_path")
124     connection.execute('mkdir -p "%s"' % bin_path)
125     connection.put(tool_path, tool_path)
126     return tool_path