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