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