Merge "Add common openstack opertation scenarios: network"
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / prox_vnf.py
1 # Copyright (c) 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
15 import logging
16 import multiprocessing
17 import os
18 import time
19
20 from yardstick.network_services.vnf_generic.vnf.base import QueueFileWrapper
21 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxResourceHelper
22 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxDpdkVnfSetupEnvHelper
23 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF
24
25 LOG = logging.getLogger(__name__)
26
27
28 class ProxApproxVnf(SampleVNF):
29
30     APP_NAME = 'PROX'
31     APP_WORD = 'PROX'
32     PROX_MODE = "Workload"
33     VNF_PROMPT = "PROX started"
34     LUA_PARAMETER_NAME = "sut"
35
36     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
37         if setup_env_helper_type is None:
38             setup_env_helper_type = ProxDpdkVnfSetupEnvHelper
39
40         if resource_helper_type is None:
41             resource_helper_type = ProxResourceHelper
42
43         super(ProxApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
44                                             resource_helper_type)
45         self._result = {}
46         self._terminated = multiprocessing.Value('i', 0)
47         self._queue = multiprocessing.Value('i', 0)
48
49     def instantiate(self, scenario_cfg, context_cfg):
50         LOG.info("printing .........prox instantiate ")
51
52         self.scenario_helper.scenario_cfg = scenario_cfg
53
54         # this won't work we need 1GB hugepages at boot
55         self.setup_helper.setup_vnf_environment()
56
57         # self.connection.run("cat /proc/cpuinfo")
58
59         prox_args, prox_path, remote_path = self.resource_helper.get_process_args()
60
61         self.q_in = multiprocessing.Queue()
62         self.q_out = multiprocessing.Queue()
63         self.queue_wrapper = QueueFileWrapper(self.q_in, self.q_out, "PROX started")
64         self._vnf_process = multiprocessing.Process(target=self._run_prox,
65                                                     args=(remote_path, prox_path, prox_args))
66         self._vnf_process.start()
67
68     def _vnf_up_post(self):
69         self.resource_helper.up_post()
70
71     def _run_prox(self, file_wrapper, config_path, prox_path, prox_args):
72         # This runs in a different process and should not share an SSH connection
73         # with the rest of the object
74         self.ssh_helper.drop_connection()
75
76         time.sleep(self.WAIT_TIME)
77
78         args = " ".join(" ".join([k, v if v else ""]) for k, v in prox_args.items())
79
80         cmd_template = "sudo bash -c 'cd {}; {} -o cli {} -f {} '"
81         prox_cmd = cmd_template.format(os.path.dirname(prox_path), prox_path, args, config_path)
82
83         LOG.debug(prox_cmd)
84         self.ssh_helper.run(prox_cmd, stdin=file_wrapper, stdout=file_wrapper,
85                             keep_stdin_open=True, pty=False)
86
87     def vnf_execute(self, cmd, wait_time=2):
88         # try to execute with socket commands
89         self.resource_helper.execute(cmd)
90
91     def collect_kpi(self):
92         if self.resource_helper is None:
93             result = {
94                 "packets_in": 0,
95                 "packets_dropped": 0,
96                 "packets_fwd": 0,
97                 "collect_stats": {"core": {}},
98             }
99             return result
100
101         if len(self.vnfd_helper.interfaces) not in {2, 4}:
102             raise RuntimeError("Failed ..Invalid no of ports .. "
103                                "2 or 4 ports only supported at this time")
104
105         port_stats = self.resource_helper.execute('port_stats', self.vnfd_helper.interfaces)
106         rx_total = port_stats[6]
107         tx_total = port_stats[7]
108         result = {
109             "packets_in": tx_total,
110             "packets_dropped": (tx_total - rx_total),
111             "packets_fwd": rx_total,
112             "collect_stats": self.resource_helper.collect_kpi(),
113         }
114         return result
115
116     def _tear_down(self):
117         self.setup_helper.rebind_drivers()