aboutsummaryrefslogtreecommitdiffstats
path: root/functest/opnfv_tests/vnf/router/vnf_controller/vnf_controller.py
blob: a5b1ad8568419df3297964254c7eb30bf8fac34e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python

# Copyright (c) 2017 Okinawa Open Laboratory and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

# pylint: disable=missing-docstring

"""vrouter controll module"""

import logging
import os
import time
import yaml

import prettytable

from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
from functest.opnfv_tests.vnf.router.vnf_controller.checker import Checker
from functest.opnfv_tests.vnf.router.vnf_controller.ssh_client import (
    SshClient)
from functest.opnfv_tests.vnf.router.vnf_controller.vm_controller import (
    VmController)


class VnfController(object):
    """vrouter controll class"""

    logger = logging.getLogger(__name__)

    def __init__(self, util_info):
        self.logger.debug("init vnf controller")
        self.util = Utilvnf()
        self.vm_controller = VmController(util_info)

        with open(self.util.test_env_config_yaml) as file_fd:
            test_env_config_yaml = yaml.safe_load(file_fd)
        file_fd.close()

        self.cmd_wait = test_env_config_yaml.get("general").get("command_wait")
        self.ssh_connect_timeout = test_env_config_yaml.get("general").get(
            "ssh_connect_timeout")
        self.ssh_connect_retry_count = test_env_config_yaml.get("general").get(
            "ssh_connect_retry_count")

    def config_vnf(self, source_vnf, destination_vnf, test_cmd_file_path,
                   parameter_file_path, prompt_file_path):
        # pylint: disable=too-many-arguments
        parameter_file = open(parameter_file_path,
                              'r')
        cmd_input_param = yaml.safe_load(parameter_file)
        parameter_file.close()

        cmd_input_param["macaddress"] = source_vnf["data_plane_network_mac"]
        cmd_input_param["source_ip"] = source_vnf["data_plane_network_ip"]
        cmd_input_param["destination_ip"] = destination_vnf[
            "data_plane_network_ip"]

        return self.vm_controller.config_vm(source_vnf,
                                            test_cmd_file_path,
                                            cmd_input_param,
                                            prompt_file_path)

    def result_check(self, target_vnf, reference_vnf,
                     check_rule_file_path_list, parameter_file_path,
                     prompt_file_path):
        # pylint: disable=too-many-arguments,too-many-locals

        res_dict_data_list = []

        parameter_file = open(parameter_file_path,
                              'r')
        cmd_input_param = yaml.safe_load(parameter_file)
        parameter_file.close()

        cmd_input_param["source_ip"] = target_vnf["data_plane_network_ip"]
        cmd_input_param["destination_ip"] = reference_vnf[
            "data_plane_network_ip"]

        prompt_file = open(prompt_file_path,
                           'r')
        prompt = yaml.safe_load(prompt_file)
        prompt_file.close()
        terminal_mode_prompt = prompt["terminal_mode"]

        ssh = SshClient(target_vnf["floating_ip"],
                        target_vnf["user"],
                        target_vnf["pass"])

        result = ssh.connect(self.ssh_connect_timeout,
                             self.ssh_connect_retry_count)
        if not result:
            return False, res_dict_data_list

        checker = Checker()

        res_table = prettytable.PrettyTable(
            header_style='upper', padding_width=5,
            field_names=['test item', 'result'])

        status = True
        res_data_list = []
        for check_rule_file_path in check_rule_file_path_list:
            (check_rule_dir, check_rule_file) = os.path.split(
                check_rule_file_path)
            check_rules = checker.load_check_rule(check_rule_dir,
                                                  check_rule_file,
                                                  cmd_input_param)
            (res, res_data) = self.vm_controller.command_execute(
                ssh,
                check_rules["command"],
                terminal_mode_prompt)
            res_data_list.append(res_data)
            if not res:
                status = False
                break

            (res, res_dict_data) = checker.regexp_information(res_data,
                                                              check_rules)
            res_dict_data_list.append(res_dict_data)
            res_table.add_row([res_dict_data["test_name"],
                               res_dict_data["result"]])
            if not res:
                status = False

            time.sleep(self.cmd_wait)

        ssh.close()

        self.logger.info("Test result:\n\n%s\n", res_table.get_string())

        self.output_check_result_detail_data(res_data_list)

        return status, res_dict_data_list

    def output_check_result_detail_data(self, res_data_list):
        for res_data in res_data_list:
            self.logger.debug(res_data)