Merge "Yardstick TC082: move sample test case perf.yaml"
[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 errno
16 import logging
17
18
19 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxDpdkVnfSetupEnvHelper
20 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxResourceHelper
21 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF
22
23 LOG = logging.getLogger(__name__)
24
25
26 class ProxApproxVnf(SampleVNF):
27
28     APP_NAME = 'PROX'
29     APP_WORD = 'PROX'
30     PROX_MODE = "Workload"
31     VNF_PROMPT = "PROX started"
32     LUA_PARAMETER_NAME = "sut"
33
34     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
35         if setup_env_helper_type is None:
36             setup_env_helper_type = ProxDpdkVnfSetupEnvHelper
37
38         if resource_helper_type is None:
39             resource_helper_type = ProxResourceHelper
40
41         super(ProxApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
42                                             resource_helper_type)
43
44     def _vnf_up_post(self):
45         self.resource_helper.up_post()
46
47     def vnf_execute(self, cmd, *args, **kwargs):
48         # try to execute with socket commands
49         # ignore socket errors, e.g. when using force_quit
50         ignore_errors = kwargs.pop("_ignore_errors", False)
51         try:
52             return self.resource_helper.execute(cmd, *args, **kwargs)
53         except OSError as e:
54             if ignore_errors and e.errno in {errno.EPIPE, errno.ESHUTDOWN}:
55                 pass
56             else:
57                 raise
58
59     def collect_kpi(self):
60         if self.resource_helper is None:
61             result = {
62                 "packets_in": 0,
63                 "packets_dropped": 0,
64                 "packets_fwd": 0,
65                 "collect_stats": {"core": {}},
66             }
67             return result
68
69         if len(self.vnfd_helper.interfaces) not in {1, 2, 4}:
70             raise RuntimeError("Failed ..Invalid no of ports .. "
71                                "1, 2 or 4 ports only supported at this time")
72
73         port_stats = self.vnf_execute('port_stats', range(len(self.vnfd_helper.interfaces)))
74         try:
75             rx_total = port_stats[6]
76             tx_total = port_stats[7]
77         except IndexError:
78             LOG.error("port_stats parse fail %s", port_stats)
79             # return empty dict so we don't mess up existing KPIs
80             return {}
81
82         result = {
83             "packets_in": tx_total,
84             "packets_dropped": (tx_total - rx_total),
85             "packets_fwd": rx_total,
86             "collect_stats": self.resource_helper.collect_kpi(),
87         }
88         LOG.debug("%s collect KPIs %s", self.APP_NAME, result)
89         return result
90
91     def _tear_down(self):
92         # this should be standardized for all VNFs or removed
93         self.setup_helper.rebind_drivers()
94
95     def terminate(self):
96         # try to quit with socket commands
97         self.vnf_execute("stop_all")
98         self.vnf_execute("quit")
99         # hopefully quit succeeds and socket closes, so ignore force_quit socket errors
100         self.vnf_execute("force_quit", _ignore_errors=True)
101         if self._vnf_process:
102             self._vnf_process.terminate()
103         self.setup_helper.kill_vnf()
104         self._tear_down()
105         self.resource_helper.stop_collect()
106
107     def instantiate(self, scenario_cfg, context_cfg):
108         # build config in parent process so we can access
109         # config from TG subprocesses
110         self.scenario_helper.scenario_cfg = scenario_cfg
111         self.setup_helper.build_config_file()
112         super(ProxApproxVnf, self).instantiate(scenario_cfg, context_cfg)