aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/availability/operation/baseoperation.py
blob: be286b8fd8ed5f15551e45778ff3fa1a48fcfe06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
##############################################################################
# Copyright (c) 2016 Juan Qiu and others
# juan_ qiu@tongji.edu.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from __future__ import absolute_import
import pkg_resources
import yaml
import logging
import os

import yardstick.common.utils as utils

LOG = logging.getLogger(__name__)

operation_conf_path = pkg_resources.resource_filename(
    "yardstick.benchmark.scenarios.availability",
    "operation_conf.yaml")


class OperationMgr(object):

    def __init__(self):
        self._operation_list = []

    def init_operations(self, operation_cfgs, context):
        LOG.debug("operationMgr confg: %s", operation_cfgs)
        for cfg in operation_cfgs:
            operation_type = cfg['operation_type']
            operation_cls = BaseOperation.get_operation_cls(operation_type)
            operation_ins = operation_cls(cfg, context)
            operation_ins.key = cfg['key']
            operation_ins.setup()
            self._operation_list.append(operation_ins)

    def __getitem__(self, item):
        for obj in self._operation_list:
            if(obj.key == item):
                return obj
        raise KeyError("No such operation instance of key - %s" % item)

    def rollback(self):
        for _instance in self._operation_list:
            _instance.rollback()


class BaseOperation(object):

    operation_cfgs = {}

    def __init__(self, config, context):
        if not BaseOperation.operation_cfgs:
            with open(operation_conf_path) as stream:
                BaseOperation.operation_cfgs = yaml.load(stream)
        self.key = ''
        self._config = config
        self._context = context

    @staticmethod
    def get_operation_cls(type):
        """return operation instance of specified type"""
        operation_type = type
        for operation_cls in utils.itersubclasses(BaseOperation):
            if operation_type == operation_cls.__operation__type__:
                return operation_cls
        raise RuntimeError("No such runner_type %s" % operation_type)

    def get_script_fullpath(self, path):
        base_path = os.path.dirname(operation_conf_path)
        return os.path.join(base_path, path)

    def setup(self):
        pass

    def run(self):
        pass

    def rollback(self):
        pass