Creating a generic opertion
[yardstick.git] / yardstick / benchmark / scenarios / availability / operation / baseoperation.py
1 ##############################################################################
2 # Copyright (c) 2016 Juan Qiu and others
3 # juan_ qiu@tongji.edu.cn
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 operation_conf_path = pkg_resources.resource_filename(
19     "yardstick.benchmark.scenarios.availability",
20     "operation_conf.yaml")
21
22
23 class OperationMgr(object):
24
25     def __init__(self):
26         self._operation_list = []
27
28     def init_operations(self, operation_cfgs, context):
29         LOG.debug("operationMgr confg: %s" % operation_cfgs)
30         for cfg in operation_cfgs:
31             operation_type = cfg['operation_type']
32             operation_cls = BaseOperation.get_operation_cls(operation_type)
33             operation_ins = operation_cls(cfg, context)
34             operation_ins.key = cfg['key']
35             operation_ins.setup()
36             self._operation_list.append(operation_ins)
37
38     def __getitem__(self, item):
39         for obj in self._operation_list:
40             if(obj.key == item):
41                 return obj
42         raise KeyError("No such operation instance of key - %s" % item)
43
44     def rollback(self):
45         for _instance in self._operation_list:
46             _instance.rollback()
47
48
49 class BaseOperation(object):
50
51     operation_cfgs = {}
52
53     def __init__(self, config, context):
54         if not BaseOperation.operation_cfgs:
55             with open(operation_conf_path) as stream:
56                 BaseOperation.operation_cfgs = yaml.load(stream)
57         self.key = ''
58         self._config = config
59         self._context = context
60
61     @staticmethod
62     def get_operation_cls(type):
63         '''return operation instance of specified type'''
64         operation_type = type
65         for operation_cls in utils.itersubclasses(BaseOperation):
66             if operation_type == operation_cls.__operation__type__:
67                 return operation_cls
68         raise RuntimeError("No such runner_type %s" % operation_type)
69
70     def get_script_fullpath(self, path):
71         base_path = os.path.dirname(operation_conf_path)
72         return os.path.join(base_path, path)
73
74     def setup(self):
75         pass
76
77     def run(self):
78         pass
79
80     def rollback(self):
81         pass