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