Adding trex trafficgen example.
[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
19 from oslo_config import cfg
20 from oslo_config.cfg import NoSuchOptError
21 from oslo_utils import encodeutils
22
23 CONF = cfg.CONF
24 OPTS = [
25     cfg.StrOpt('bin_path',
26                default='/opt/nsb_bin',
27                help='bin_path for VNFs location.'),
28     cfg.StrOpt('trex_path',
29                default='/opt/nsb_bin/trex/scripts',
30                help='trex automation lib pathh.'),
31 ]
32 CONF.register_opts(OPTS, group="nsb")
33
34
35 def get_nsb_option(option, default=None):
36     """return requested option for yardstick.conf"""
37
38     try:
39         return CONF.nsb.__getitem__(option)
40     except NoSuchOptError:
41         logging.debug("Invalid key %s", option)
42     else:
43         return default
44
45
46 def provision_tool(connection, tool_path):
47     """
48     verify if the tool path exits on the node,
49     if not push the local binary to remote node
50
51     :return - Tool path
52     """
53     bin_path = get_nsb_option("bin_path")
54     exit_status, stdout = connection.execute("which %s" % tool_path)[:2]
55     if exit_status == 0:
56         return encodeutils.safe_encode(stdout, incoming='utf-8').rstrip()
57
58     logging.warning("%s not found on %s, will try to copy from localhost",
59                     tool_path, connection.host)
60     connection.execute('mkdir -p "%s"' % bin_path)
61     connection.put(tool_path, tool_path)
62     return tool_path