Merge "vsperf: Enhanced vswitchperf configuration"
[yardstick.git] / yardstick / benchmark / scenarios / availability / attacker / baseattacker.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 import pkg_resources
10 import yaml
11 import logging
12 import os
13
14 import yardstick.common.utils as utils
15
16 LOG = logging.getLogger(__name__)
17
18 attacker_conf_path = pkg_resources.resource_filename(
19     "yardstick.benchmark.scenarios.availability",
20     "attacker_conf.yaml")
21
22
23 class AttackerMgr(object):
24
25     def __init__(self):
26         self._attacker_list = []
27
28     def init_attackers(self, attacker_cfgs, context):
29         LOG.debug("attackerMgr confg: %s", attacker_cfgs)
30
31         for cfg in attacker_cfgs:
32             attacker_cls = BaseAttacker.get_attacker_cls(cfg)
33             attacker_ins = attacker_cls(cfg, context)
34             attacker_ins.key = cfg['key']
35             attacker_ins.setup()
36             self._attacker_list.append(attacker_ins)
37
38     def __getitem__(self, item):
39         for obj in self._attacker_list:
40             if(obj.key == item):
41                 return obj
42
43     def recover(self):
44         for _instance in self._attacker_list:
45             _instance.recover()
46
47
48 class BaseAttacker(object):
49
50     attacker_cfgs = {}
51
52     def __init__(self, config, context):
53         if not BaseAttacker.attacker_cfgs:
54             with open(attacker_conf_path) as stream:
55                 BaseAttacker.attacker_cfgs = yaml.load(stream)
56
57         self._config = config
58         self._context = context
59         self.setup_done = False
60
61     @staticmethod
62     def get_attacker_cls(attacker_cfg):
63         '''return attacker instance of specified type'''
64         attacker_type = attacker_cfg['fault_type']
65         for attacker_cls in utils.itersubclasses(BaseAttacker):
66             if attacker_type == attacker_cls.__attacker_type__:
67                 return attacker_cls
68         raise RuntimeError("No such runner_type %s" % attacker_type)
69
70     def get_script_fullpath(self, path):
71         base_path = os.path.dirname(attacker_conf_path)
72         return os.path.join(base_path, path)
73
74     def recover(self):
75         pass