Merge "Log each test case status in a task"
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / cgnapt_vnf.py
1 # Copyright (c) 2016-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 from __future__ import absolute_import
16 import logging
17
18 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNF, DpdkVnfSetupEnvHelper
19
20 LOG = logging.getLogger(__name__)
21
22 # CGNAPT should work the same on all systems, we can provide the binary
23 CGNAPT_PIPELINE_COMMAND = 'sudo {tool_path} -p {port_mask_hex} -f {cfg_file} -s {script}'
24 WAIT_FOR_STATIC_NAPT = 4
25
26 CGNAPT_COLLECT_KPI = """\
27 CG-NAPT(.*\n)*\
28 Received\s(\d+),\
29 Missed\s(\d+),\
30 Dropped\s(\d+),\
31 Translated\s(\d+),\
32 ingress\
33 """
34
35
36 class CgnaptApproxSetupEnvHelper(DpdkVnfSetupEnvHelper):
37
38     APP_NAME = "vCGNAPT"
39     CFG_CONFIG = "/tmp/cgnapt_config"
40     CFG_SCRIPT = "/tmp/cgnapt_script"
41     DEFAULT_CONFIG_TPL_CFG = "cgnat.cfg"
42     PIPELINE_COMMAND = CGNAPT_PIPELINE_COMMAND
43     SW_DEFAULT_CORE = 6
44     HW_DEFAULT_CORE = 3
45     VNF_TYPE = "CGNAPT"
46
47     @staticmethod
48     def _generate_ip_from_pool(ip):
49         ip_parts = ip.split('.')
50         assert len(ip_parts) == 4
51         iter1 = (str(n) for n in range(int(ip_parts[2]), 256))
52         for ip_parts[2] in iter1:
53             yield '.'.join(ip_parts)
54
55     @staticmethod
56     def _update_cgnat_script_file(ip_pipeline_cfg, mcpi):
57         pipeline_config_str = str(ip_pipeline_cfg)
58         input_cmds = '\n'.join(mcpi)
59         icmp_flag = 'link 0 down' in input_cmds
60         if icmp_flag:
61             pipeline_config_str = ''
62         return '\n'.join([pipeline_config_str, input_cmds])
63
64     def scale(self, flavor=""):
65         raise NotImplementedError
66
67     def _get_cgnapt_config(self, interfaces=None):
68         # TODO: static CGNAPT is broken, don't use it
69         if interfaces is None:
70             interfaces = self.vnfd_helper.interfaces
71
72         # fixme: Get private port and gateway from port list
73         uplink_ports = self.vnfd_helper.port_pairs.uplink_ports
74         return [self._get_ports_gateway(intf["name"]) for intf in uplink_ports]
75
76
77 class CgnaptApproxVnf(SampleVNF):
78
79     APP_NAME = "vCGNAPT"
80     APP_WORD = 'cgnapt'
81     COLLECT_KPI = CGNAPT_COLLECT_KPI
82
83     COLLECT_MAP = {
84         "packets_in": 2,
85         "packets_fwd": 5,
86         "packets_dropped": 4,
87     }
88
89     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
90         if setup_env_helper_type is None:
91             setup_env_helper_type = CgnaptApproxSetupEnvHelper
92
93         super(CgnaptApproxVnf, self).__init__(name, vnfd, setup_env_helper_type,
94                                               resource_helper_type)
95
96     def _vnf_up_post(self):
97         super(CgnaptApproxVnf, self)._vnf_up_post()
98         if self.scenario_helper.options.get('napt', 'static') != 'static':
99             return
100
101         # ip_iter = self.setup_helper._generate_ip_from_pool("152.16.40.10")
102         # gw_ips = self.setup_helper._get_cgnapt_config()
103         # if self.scenario_helper.vnf_cfg.get("lb_config", "SW") == 'HW':
104         #     pipeline = self.setup_helper.HW_DEFAULT_CORE
105         #     offset = 3
106         # else:
107         #     pipeline = self.setup_helper.SW_DEFAULT_CORE - 1
108         #     offset = 0
109         #
110         # worker_threads = int(self.scenario_helper.vnf_cfg["worker_threads"])
111         # # p <pipeline id> entry addm <prv_ipv4/6> prvport> <pub_ip> <pub_port> <phy_port> <ttl>
112         # # <no_of_entries> <end_prv_port> <end_pub_port>
113         # cmd_template = "p {0} entry addm {1} 1 {2} 1 0 32 65535 65535 65535"
114         # for gw, ip in zip(gw_ips, ip_iter):
115         #     cmd = cmd_template.format(pipeline, gw, ip)
116         #     pipeline += worker_threads
117         #     pipeline += offset
118         #     self.vnf_execute(cmd)
119         #
120         # time.sleep(WAIT_FOR_STATIC_NAPT)