Merge "Module to manage pip packages"
[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 import datetime
18 import time
19
20
21 from yardstick.common.process import check_if_process_failed
22 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxDpdkVnfSetupEnvHelper
23 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxResourceHelper
24 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF, PROCESS_JOIN_TIMEOUT
25
26 LOG = logging.getLogger(__name__)
27
28
29 class ProxApproxVnf(SampleVNF):
30
31     APP_NAME = 'PROX'
32     APP_WORD = 'PROX'
33     PROX_MODE = "Workload"
34     VNF_PROMPT = "PROX started"
35     LUA_PARAMETER_NAME = "sut"
36
37     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
38         if setup_env_helper_type is None:
39             setup_env_helper_type = ProxDpdkVnfSetupEnvHelper
40
41         if resource_helper_type is None:
42             resource_helper_type = ProxResourceHelper
43
44         self.prev_packets_in = 0
45         self.prev_packets_sent = 0
46         self.prev_time = time.time()
47         super(ProxApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
48                                             resource_helper_type)
49
50     def _vnf_up_post(self):
51         self.resource_helper.up_post()
52
53     def vnf_execute(self, cmd, *args, **kwargs):
54         # try to execute with socket commands
55         # ignore socket errors, e.g. when using force_quit
56         ignore_errors = kwargs.pop("_ignore_errors", False)
57         try:
58             return self.resource_helper.execute(cmd, *args, **kwargs)
59         except OSError as e:
60             if e.errno in {errno.EPIPE, errno.ESHUTDOWN, errno.ECONNRESET}:
61                 if ignore_errors:
62                     LOG.debug("ignoring vnf_execute exception %s for command %s", e, cmd)
63                 else:
64                     raise
65             else:
66                 raise
67
68     def collect_kpi(self):
69         # we can't get KPIs if the VNF is down
70         check_if_process_failed(self._vnf_process)
71
72         if self.resource_helper is None:
73             result = {
74                 "packets_in": 0,
75                 "packets_dropped": 0,
76                 "packets_fwd": 0,
77                 "collect_stats": {"core": {}},
78             }
79             return result
80
81         # use all_ports so we only use ports matched in topology
82         port_count = len(self.vnfd_helper.port_pairs.all_ports)
83         if port_count not in {1, 2, 4}:
84             raise RuntimeError("Failed ..Invalid no of ports .. "
85                                "1, 2 or 4 ports only supported at this time")
86
87         self.port_stats = self.vnf_execute('port_stats', range(port_count))
88         curr_time = time.time()
89         try:
90             rx_total = self.port_stats[6]
91             tx_total = self.port_stats[7]
92         except IndexError:
93             LOG.debug("port_stats parse fail ")
94             # return empty dict so we don't mess up existing KPIs
95             return {}
96
97         result = {
98             "packets_in": rx_total,
99             "packets_dropped": max((tx_total - rx_total), 0),
100             "packets_fwd": tx_total,
101             # we share ProxResourceHelper with TG, but we want to collect
102             # collectd KPIs here and not TG KPIs, so use a different method name
103             "collect_stats": self.resource_helper.collect_collectd_kpi(),
104         }
105         curr_packets_in = int((rx_total - self.prev_packets_in) / (curr_time - self.prev_time))
106         curr_packets_fwd = int((tx_total - self.prev_packets_sent) / (curr_time - self.prev_time))
107
108         result["curr_packets_in"] = curr_packets_in
109         result["curr_packets_fwd"] = curr_packets_fwd
110
111         self.prev_packets_in = rx_total
112         self.prev_packets_sent = tx_total
113         self.prev_time = curr_time
114
115         LOG.debug("%s collect KPIs %s %s", self.APP_NAME, datetime.datetime.now(), result)
116         return result
117
118     def _tear_down(self):
119         # this should be standardized for all VNFs or removed
120         self.setup_helper.tear_down()
121
122     def terminate(self):
123         # stop collectd first or we get pika errors?
124         self.resource_helper.stop_collect()
125         # try to quit with socket commands
126         # pkill is not matching, debug with pgrep
127         self.ssh_helper.execute("sudo pgrep -lax  %s" % self.setup_helper.APP_NAME)
128         self.ssh_helper.execute("sudo ps aux | grep -i %s" % self.setup_helper.APP_NAME)
129         if self._vnf_process.is_alive():
130             self.vnf_execute("stop_all")
131             self.vnf_execute("quit")
132             # hopefully quit succeeds and socket closes, so ignore force_quit socket errors
133             self.vnf_execute("force_quit", _ignore_errors=True)
134         self.setup_helper.kill_vnf()
135         self._tear_down()
136         if self._vnf_process is not None:
137             LOG.debug("joining before terminate %s", self._vnf_process.name)
138             self._vnf_process.join(PROCESS_JOIN_TIMEOUT)
139             self._vnf_process.terminate()