Update linters and fix all new issues
[functest.git] / functest / opnfv_tests / vnf / router / vnf_controller / vnf_controller.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Okinawa Open Laboratory and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # pylint: disable=missing-docstring
11
12 """vrouter controll module"""
13
14 import logging
15 import os
16 import time
17 import yaml
18
19 import prettytable
20
21 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
22 from functest.opnfv_tests.vnf.router.vnf_controller.checker import Checker
23 from functest.opnfv_tests.vnf.router.vnf_controller.ssh_client import (
24     SshClient)
25 from functest.opnfv_tests.vnf.router.vnf_controller.vm_controller import (
26     VmController)
27
28
29 class VnfController():
30     """vrouter controll class"""
31
32     logger = logging.getLogger(__name__)
33
34     def __init__(self, util_info):
35         self.logger.debug("init vnf controller")
36         self.util = Utilvnf()
37         self.vm_controller = VmController(util_info)
38
39         with open(self.util.test_env_config_yaml, encoding='utf-8') as file_fd:
40             test_env_config_yaml = yaml.safe_load(file_fd)
41         file_fd.close()
42
43         self.cmd_wait = test_env_config_yaml.get("general").get("command_wait")
44         self.ssh_connect_timeout = test_env_config_yaml.get("general").get(
45             "ssh_connect_timeout")
46         self.ssh_connect_retry_count = test_env_config_yaml.get("general").get(
47             "ssh_connect_retry_count")
48
49     def config_vnf(self, source_vnf, destination_vnf, test_cmd_file_path,
50                    parameter_file_path, prompt_file_path):
51         # pylint: disable=too-many-arguments
52         with open(
53                 parameter_file_path, 'r', encoding='utf-8') as parameter_file:
54             cmd_input_param = yaml.safe_load(parameter_file)
55
56         cmd_input_param["macaddress"] = source_vnf["data_plane_network_mac"]
57         cmd_input_param["source_ip"] = source_vnf["data_plane_network_ip"]
58         cmd_input_param["destination_ip"] = destination_vnf[
59             "data_plane_network_ip"]
60
61         return self.vm_controller.config_vm(source_vnf,
62                                             test_cmd_file_path,
63                                             cmd_input_param,
64                                             prompt_file_path)
65
66     def result_check(self, target_vnf, reference_vnf,
67                      check_rule_file_path_list, parameter_file_path,
68                      prompt_file_path):
69         # pylint: disable=too-many-arguments,too-many-locals
70
71         res_dict_data_list = []
72
73         with open(
74                 parameter_file_path, 'r', encoding='utf-8') as parameter_file:
75             cmd_input_param = yaml.safe_load(parameter_file)
76
77         cmd_input_param["source_ip"] = target_vnf["data_plane_network_ip"]
78         cmd_input_param["destination_ip"] = reference_vnf[
79             "data_plane_network_ip"]
80
81         with open(prompt_file_path, 'r', encoding='utf-8') as prompt_file:
82             prompt = yaml.safe_load(prompt_file)
83         terminal_mode_prompt = prompt["terminal_mode"]
84
85         ssh = SshClient(target_vnf["floating_ip"],
86                         target_vnf["user"],
87                         target_vnf["pass"])
88
89         result = ssh.connect(self.ssh_connect_timeout,
90                              self.ssh_connect_retry_count)
91         if not result:
92             return False, res_dict_data_list
93
94         checker = Checker()
95
96         res_table = prettytable.PrettyTable(
97             header_style='upper', padding_width=5,
98             field_names=['test item', 'result'])
99
100         status = True
101         res_data_list = []
102         for check_rule_file_path in check_rule_file_path_list:
103             (check_rule_dir, check_rule_file) = os.path.split(
104                 check_rule_file_path)
105             check_rules = checker.load_check_rule(check_rule_dir,
106                                                   check_rule_file,
107                                                   cmd_input_param)
108             (res, res_data) = self.vm_controller.command_execute(
109                 ssh,
110                 check_rules["command"],
111                 terminal_mode_prompt)
112             res_data_list.append(res_data)
113             if not res:
114                 status = False
115                 break
116
117             (res, res_dict_data) = checker.regexp_information(res_data,
118                                                               check_rules)
119             res_dict_data_list.append(res_dict_data)
120             res_table.add_row([res_dict_data["test_name"],
121                                res_dict_data["result"]])
122             if not res:
123                 status = False
124
125             time.sleep(self.cmd_wait)
126
127         ssh.close()
128
129         self.logger.info("Test result:\n\n%s\n", res_table.get_string())
130
131         self.output_check_result_detail_data(res_data_list)
132
133         return status, res_dict_data_list
134
135     def output_check_result_detail_data(self, res_data_list):
136         for res_data in res_data_list:
137             self.logger.debug(res_data)