[vyos_vrouter] Support https endpoints
[functest-xtesting.git] / functest / opnfv_tests / vnf / router / vnf_controller / vm_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 """vm controll module"""
11
12 import logging
13 import os
14 import time
15 import yaml
16
17 from functest.opnfv_tests.vnf.router.utilvnf import Utilvnf
18 from functest.opnfv_tests.vnf.router.vnf_controller.command_generator import (
19     CommandGenerator)
20 from functest.opnfv_tests.vnf.router.vnf_controller.ssh_client import (
21     SshClient)
22
23
24 class VmController(object):
25     """vm controll class"""
26
27     logger = logging.getLogger(__name__)
28
29     def __init__(self, util_info):
30         self.logger.debug("initialize vm controller")
31         self.command_gen = CommandGenerator()
32         credentials = util_info["credentials"]
33
34         self.util = Utilvnf()
35         self.util.set_credentials(credentials["snaps_creds"])
36
37         with open(self.util.test_env_config_yaml) as file_fd:
38             test_env_config_yaml = yaml.safe_load(file_fd)
39         file_fd.close()
40
41         self.reboot_wait = test_env_config_yaml.get("general").get(
42             "reboot_wait")
43         self.command_wait = test_env_config_yaml.get("general").get(
44             "command_wait")
45         self.ssh_connect_timeout = test_env_config_yaml.get("general").get(
46             "ssh_connect_timeout")
47         self.ssh_connect_retry_count = test_env_config_yaml.get("general").get(
48             "ssh_connect_retry_count")
49
50     def command_gen_from_template(self, command_file_path, cmd_input_param):
51         (command_file_dir, command_file_name) = os.path.split(
52             command_file_path)
53         template = self.command_gen.load_template(command_file_dir,
54                                                   command_file_name)
55         return self.command_gen.command_create(template,
56                                                cmd_input_param)
57
58     def config_vm(self, vm_info, test_cmd_file_path,
59                   cmd_input_param, prompt_file_path):
60         ssh = self.connect_ssh_and_config_vm(vm_info,
61                                              test_cmd_file_path,
62                                              cmd_input_param,
63                                              prompt_file_path)
64         if ssh is None:
65             return False
66
67         ssh.close()
68
69         return True
70
71     def connect_ssh_and_config_vm(self, vm_info, test_cmd_file_path,
72                                   cmd_input_param, prompt_file_path):
73
74         key_filename = None
75         if "key_path" in vm_info:
76             key_filename = vm_info["key_path"]
77
78         ssh = SshClient(ip_address=vm_info["floating_ip"],
79                         user=vm_info["user"],
80                         password=vm_info["pass"],
81                         key_filename=key_filename)
82
83         result = ssh.connect(self.ssh_connect_timeout,
84                              self.ssh_connect_retry_count)
85         if not result:
86             self.logger.debug("try to vm reboot.")
87             self.util.reboot_vm(vm_info["vnf_name"])
88             time.sleep(self.reboot_wait)
89             result = ssh.connect(self.ssh_connect_timeout,
90                                  self.ssh_connect_retry_count)
91             if not result:
92                 return None
93
94         (result, _) = self.command_create_and_execute(
95             ssh,
96             test_cmd_file_path,
97             cmd_input_param,
98             prompt_file_path)
99         if not result:
100             ssh.close()
101             return None
102
103         return ssh
104
105     def command_create_and_execute(self, ssh, test_cmd_file_path,
106                                    cmd_input_param, prompt_file_path):
107         prompt_file = open(prompt_file_path,
108                            'r')
109         prompt = yaml.safe_load(prompt_file)
110         prompt_file.close()
111         config_mode_prompt = prompt["config_mode"]
112
113         commands = self.command_gen_from_template(test_cmd_file_path,
114                                                   cmd_input_param)
115         return self.command_list_execute(ssh,
116                                          commands,
117                                          config_mode_prompt)
118
119     def command_list_execute(self, ssh, command_list, prompt):
120         res_data_list = []
121         for command in command_list:
122             self.logger.debug("Command : " + command)
123             (res, res_data) = self.command_execute(ssh,
124                                                    command,
125                                                    prompt)
126             self.logger.debug("Response : " + res_data)
127             res_data_list.append(res_data)
128             if not res:
129                 return res, res_data_list
130
131             time.sleep(self.command_wait)
132
133         return True, res_data_list
134
135     def command_execute(self, ssh, command, prompt):
136         res_data = ssh.send(command, prompt)
137         if res_data is None:
138             self.logger.info("retry send command : " + command)
139             res_data = ssh.send(command,
140                                 prompt)
141             if not ssh.error_check(res_data):
142                 return False, res_data
143
144         return True, res_data