JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / agent / env / plugins / libvirt_plugin.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import logging
11
12 from vstf.common.utils import ping, my_sleep
13 from vstf.agent.env.plugins.model import EnvBuilderPlugin
14 from vstf.agent.env.basic.source_manager import SourceCodeManager
15 from vstf.agent.env.basic.vm_manager import VMControlOperation
16 from vstf.agent.env.vswitch_plugins.manager import VswitchPluginManager
17 from vstf.agent.env.driver_plugins.manager import DriverPluginManager
18
19 LOG = logging.getLogger(__name__)
20
21
22 class Plugin(EnvBuilderPlugin):
23     def __init__(self):
24         super(Plugin, self).__init__()
25         self.vm_mgr = VMControlOperation()
26         self.vs_mgr = VswitchPluginManager()
27         self.dr_mgr = DriverPluginManager()
28
29     def clean(self):
30         self.vm_mgr.clean_all_vms()
31         self.vs_mgr.clean()
32         self.dr_mgr.clean()
33
34     def load_drivers(self):
35         drivers = self.host_cfg['drivers']
36         self.dr_mgr.load(drivers)
37
38     def create_brs(self):
39         for br_cfg in self.host_cfg['bridges']:
40             plugin = self.vs_mgr.get_vs_plugin(br_cfg['type'])
41             plugin.create_br(br_cfg)
42
43     def config_br_ports(self):
44         for vm_cfg in self.host_cfg['vms']:
45             for tap_cfg in vm_cfg['taps']:
46                 plugin = self.vs_mgr.get_vs_plugin(tap_cfg['br_type'])
47                 plugin.set_tap_vid(tap_cfg)
48         for br_cfg in self.host_cfg['bridges']:
49             plugin = self.vs_mgr.get_vs_plugin(br_cfg['type'])
50             plugin.set_fastlink(br_cfg)
51
52     def create_vms(self):
53         for vm_cfg in self.host_cfg['vms']:
54             self.vm_mgr.create_vm(vm_cfg)
55
56     def wait_vms(self):
57         for vm_cfg in self.host_cfg['vms']:
58             self.vm_mgr.wait_vm(vm_cfg['vm_name'])
59             self.vm_mgr.init_config_vm(vm_cfg['vm_name'])
60
61     def check_vm_connectivity(self):
62         for vm_cfg in self.host_cfg['vms']:
63             vm_ip = vm_cfg['init_config']['ctrl_ip_setting'].split('/')[0]
64             for _ in range(3):
65                 ret = ping(vm_ip)
66                 if ret:
67                     break
68                 my_sleep(3)
69             else:
70                 raise Exception("ping ip:%s failed." % vm_ip)