Add support for Python 3
[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 from __future__ import absolute_import
10 import pkg_resources
11 import yaml
12 import logging
13 import os
14
15 import yardstick.common.utils as utils
16
17 LOG = logging.getLogger(__name__)
18
19 operation_conf_path = pkg_resources.resource_filename(
20     "yardstick.benchmark.scenarios.availability",
21     "operation_conf.yaml")
22
23
24 class OperationMgr(object):
25
26     def __init__(self):
27         self._operation_list = []
28
29     def init_operations(self, operation_cfgs, context):
30         LOG.debug("operationMgr confg: %s", operation_cfgs)
31         for cfg in operation_cfgs:
32             operation_type = cfg['operation_type']
33             operation_cls = BaseOperation.get_operation_cls(operation_type)
34             operation_ins = operation_cls(cfg, context)
35             operation_ins.key = cfg['key']
36             operation_ins.setup()
37             self._operation_list.append(operation_ins)
38
39     def __getitem__(self, item):
40         for obj in self._operation_list:
41             if(obj.key == item):
42                 return obj
43         raise KeyError("No such operation instance of key - %s" % item)
44
45     def rollback(self):
46         for _instance in self._operation_list:
47             _instance.rollback()
48
49
50 class BaseOperation(object):
51
52     operation_cfgs = {}
53
54     def __init__(self, config, context):
55         if not BaseOperation.operation_cfgs:
56             with open(operation_conf_path) as stream:
57                 BaseOperation.operation_cfgs = yaml.load(stream)
58         self.key = ''
59         self._config = config
60         self._context = context
61
62     @staticmethod
63     def get_operation_cls(type):
64         '''return operation instance of specified type'''
65         operation_type = type
66         for operation_cls in utils.itersubclasses(BaseOperation):
67             if operation_type == operation_cls.__operation__type__:
68                 return operation_cls
69         raise RuntimeError("No such runner_type %s" % operation_type)
70
71     def get_script_fullpath(self, path):
72         base_path = os.path.dirname(operation_conf_path)
73         return os.path.join(base_path, path)
74
75     def setup(self):
76         pass
77
78     def run(self):
79         pass
80
81     def rollback(self):
82         pass