cd228fe267f474d6fe2f664cb78ebd22561294d3
[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["username"],
36                                   credentials["password"],
37                                   credentials["auth_url"],
38                                   credentials["tenant_name"],
39                                   credentials["region_name"])
40
41         with open(self.util.test_env_config_yaml) as file_fd:
42             test_env_config_yaml = yaml.safe_load(file_fd)
43         file_fd.close()
44
45         self.reboot_wait = test_env_config_yaml.get("general").get(
46             "reboot_wait")
47         self.command_wait = test_env_config_yaml.get("general").get(
48             "command_wait")
49         self.ssh_connect_timeout = test_env_config_yaml.get("general").get(
50             "ssh_connect_timeout")
51         self.ssh_connect_retry_count = test_env_config_yaml.get("general").get(
52             "ssh_connect_retry_count")
53
54     def command_gen_from_template(self, command_file_path, cmd_input_param):
55         (command_file_dir, command_file_name) = os.path.split(
56             command_file_path)
57         template = self.command_gen.load_template(command_file_dir,
58                                                   command_file_name)
59         return self.command_gen.command_create(template,
60                                                cmd_input_param)
61
62     def config_vm(self, vm_info, test_cmd_file_path,
63                   cmd_input_param, prompt_file_path):
64         ssh = self.connect_ssh_and_config_vm(vm_info,
65                                              test_cmd_file_path,
66                                              cmd_input_param,
67                                              prompt_file_path)
68         if ssh is None:
69             return False
70
71         ssh.close()
72
73         return True
74
75     def connect_ssh_and_config_vm(self, vm_info, test_cmd_file_path,
76                                   cmd_input_param, prompt_file_path):
77
78         key_filename = None
79         if "key_path" in vm_info:
80             key_filename = vm_info["key_path"]
81
82         ssh = SshClient(ip_address=vm_info["floating_ip"],
83                         user=vm_info["user"],
84                         password=vm_info["pass"],
85                         key_filename=key_filename)
86
87         result = ssh.connect(self.ssh_connect_timeout,
88                              self.ssh_connect_retry_count)
89         if not result:
90             self.logger.debug("try to vm reboot.")
91             self.util.reboot_vm(vm_info["vnf_name"])
92             time.sleep(self.reboot_wait)
93             result = ssh.connect(self.ssh_connect_timeout,
94                                  self.ssh_connect_retry_count)
95             if not result:
96                 return None
97
98         (result, _) = self.command_create_and_execute(
99             ssh,
100             test_cmd_file_path,
101             cmd_input_param,
102             prompt_file_path)
103         if not result:
104             ssh.close()
105             return None
106
107         return ssh
108
109     def command_create_and_execute(self, ssh, test_cmd_file_path,
110                                    cmd_input_param, prompt_file_path):
111         prompt_file = open(prompt_file_path,
112                            'r')
113         prompt = yaml.safe_load(prompt_file)
114         prompt_file.close()
115         config_mode_prompt = prompt["config_mode"]
116
117         commands = self.command_gen_from_template(test_cmd_file_path,
118                                                   cmd_input_param)
119         return self.command_list_execute(ssh,
120                                          commands,
121                                          config_mode_prompt)
122
123     def command_list_execute(self, ssh, command_list, prompt):
124         res_data_list = []
125         for command in command_list:
126             self.logger.debug("Command : " + command)
127             (res, res_data) = self.command_execute(ssh,
128                                                    command,
129                                                    prompt)
130             self.logger.debug("Response : " + res_data)
131             res_data_list.append(res_data)
132             if not res:
133                 return res, res_data_list
134
135             time.sleep(self.command_wait)
136
137         return True, res_data_list
138
139     def command_execute(self, ssh, command, prompt):
140         res_data = ssh.send(command, prompt)
141         if res_data is None:
142             self.logger.info("retry send command : " + command)
143             res_data = ssh.send(command,
144                                 prompt)
145             if not ssh.error_check(res_data):
146                 return False, res_data
147
148         return True, res_data