samples: Add generic L3 forwarder tests
[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
26 NSB_ROOT = "/opt/nsb_bin"
27
28 CONF = cfg.CONF
29 OPTS = [
30     cfg.StrOpt('bin_path',
31                default=NSB_ROOT,
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.'),
39 ]
40 CONF.register_opts(OPTS, group="nsb")
41
42
43 HEXADECIMAL = "[0-9a-zA-Z]"
44
45
46 class PciAddress(object):
47     """Class to handle PCI addresses.
48
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
54
55     Note: in Libvirt, 'device' is called 'slot'.
56
57     Reference: https://wiki.xenproject.org/wiki/
58                Bus:Device.Function_(BDF)_Notation
59     """
60     PCI_PATTERN_STR = (
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})")
63
64     def __init__(self, address):
65         pci_pattern = re.compile(self.PCI_PATTERN_STR)
66         match = pci_pattern.search(address)
67         if not match:
68             raise ValueError('Invalid PCI address: {}'.format(address))
69
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,
75                                                           self.bus,
76                                                           self.slot,
77                                                           self.function)
78         self.match = match
79
80     def __repr__(self):
81         return self.address
82
83     @property
84     def domain(self):
85         return self._domain
86
87     @property
88     def bus(self):
89         return self._bus
90
91     @property
92     def slot(self):
93         return self._slot
94
95     @property
96     def function(self):
97         return self._function
98
99     def values(self):
100         return [self._domain, self._bus, self._slot, self._function]
101
102
103 def get_nsb_option(option, default=None):
104     """return requested option for yardstick.conf"""
105
106     try:
107         return CONF.nsb.__getitem__(option)
108     except NoSuchOptError:
109         logging.debug("Invalid key %s", option)
110     return default
111
112
113 def provision_tool(connection, tool_path, tool_file=None):
114     """
115     verify if the tool path exits on the node,
116     if not push the local binary to remote node
117
118     :return - Tool path
119     """
120     if not tool_path:
121         tool_path = get_nsb_option('tool_path')
122     if tool_file:
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]
126     if exit_status == 0:
127         return encodeutils.safe_decode(tool_path, incoming='utf-8').rstrip()
128
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)
134     return tool_path