Merge "prox: pass prox_config_dict between Processes using queue"
[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 not ignore_errors or e.errno not in {errno.EPIPE, errno.ESHUTDOWN}:
55                 raise
56
57     def collect_kpi(self):
58         if self.resource_helper is None:
59             result = {
60                 "packets_in": 0,
61                 "packets_dropped": 0,
62                 "packets_fwd": 0,
63                 "collect_stats": {"core": {}},
64             }
65             return result
66
67         intf_count = len(self.vnfd_helper.interfaces)
68         if intf_count not in {1, 2, 4}:
69             raise RuntimeError("Failed ..Invalid no of ports .. "
70                                "1, 2 or 4 ports only supported at this time")
71
72         port_stats = self.vnf_execute('port_stats', range(intf_count))
73         try:
74             rx_total = port_stats[6]
75             tx_total = port_stats[7]
76         except IndexError:
77             LOG.error("port_stats parse fail %s", port_stats)
78             # return empty dict so we don't mess up existing KPIs
79             return {}
80
81         result = {
82             "packets_in": tx_total,
83             "packets_dropped": (tx_total - rx_total),
84             "packets_fwd": rx_total,
85             "collect_stats": self.resource_helper.collect_kpi(),
86         }
87         LOG.debug("%s collect KPIs %s", self.APP_NAME, result)
88         return result
89
90     def _tear_down(self):
91         # this should be standardized for all VNFs or removed
92         self.setup_helper.tear_down()
93
94     def terminate(self):
95         # try to quit with socket commands
96         self.vnf_execute("stop_all")
97         self.vnf_execute("quit")
98         # hopefully quit succeeds and socket closes, so ignore force_quit socket errors
99         self.vnf_execute("force_quit", _ignore_errors=True)
100         if self._vnf_process:
101             self._vnf_process.terminate()
102         self.setup_helper.kill_vnf()
103         self._tear_down()
104         self.resource_helper.stop_collect()