Setup IPSEC tunnel mode for VPP Crypto testing
[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     cfg.StrOpt('jre_path_i386',
40                default='',
41                help='path to installation of 32-bit Java 1.7+.'),
42 ]
43 CONF.register_opts(OPTS, group="nsb")
44
45
46 HEXADECIMAL = "[0-9a-zA-Z]"
47
48
49 class PciAddress(object):
50     """Class to handle PCI addresses.
51
52     A PCI address could be written in two ways:
53     - Simple BDF notation:
54         00:00.0 # bus:device.function
55     - With domain extension.
56         0000:00:00.0 # domain:bus:device.function
57
58     Note: in Libvirt, 'device' is called 'slot'.
59
60     Reference: https://wiki.xenproject.org/wiki/
61                Bus:Device.Function_(BDF)_Notation
62     """
63     PCI_PATTERN_STR = (
64         r"((?P<domain>[0-9a-zA-Z]{4}):)?(?P<bus>[0-9a-zA-Z]{2}):"
65         r"(?P<slot>[0-9a-zA-Z]{2})\.(?P<function>[0-9a-zA-Z]{1})")
66
67     def __init__(self, address):
68         pci_pattern = re.compile(self.PCI_PATTERN_STR)
69         match = pci_pattern.search(address)
70         if not match:
71             raise ValueError('Invalid PCI address: {}'.format(address))
72
73         self._domain = (match.group('domain') or '0000').lower()
74         self._bus = match.group('bus').lower()
75         self._slot = match.group('slot').lower()
76         self._function = match.group('function').lower()
77         self.address = '{:0>4}:{:0>2}:{:0>2}.{:1}'.format(self.domain,
78                                                           self.bus,
79                                                           self.slot,
80                                                           self.function)
81         self.match = match
82
83     def __repr__(self):
84         return self.address
85
86     @property
87     def domain(self):
88         return self._domain
89
90     @property
91     def bus(self):
92         return self._bus
93
94     @property
95     def slot(self):
96         return self._slot
97
98     @property
99     def function(self):
100         return self._function
101
102     def values(self):
103         return [self._domain, self._bus, self._slot, self._function]
104
105
106 def get_nsb_option(option, default=None):
107     """return requested option for yardstick.conf"""
108
109     try:
110         return CONF.nsb.__getitem__(option)
111     except NoSuchOptError:
112         logging.debug("Invalid key %s", option)
113     return default
114
115
116 def provision_tool(connection, tool_path, tool_file=None):
117     """
118     verify if the tool path exits on the node,
119     if not push the local binary to remote node
120
121     :return - Tool path
122     """
123     if not tool_path:
124         tool_path = get_nsb_option('tool_path')
125     if tool_file:
126         tool_path = os.path.join(tool_path, tool_file)
127     exit_status = connection.execute("which %s > /dev/null 2>&1" % tool_path)[0]
128     if exit_status == 0:
129         return encodeutils.safe_decode(tool_path, incoming='utf-8').rstrip()
130
131     logging.warning("%s not found on %s, will try to copy from localhost",
132                     tool_path, connection.host)
133     bin_path = get_nsb_option("bin_path")
134     connection.execute('mkdir -p "%s"' % bin_path)
135     connection.put(tool_path, tool_path)
136     return tool_path