aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/contexts/base.py
blob: 76a8288118bfc8c0d12fcb087825084a84d9d753 (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
##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
#
# 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
##############################################################################
import abc
import six

import yardstick.common.utils as utils


@six.add_metaclass(abc.ABCMeta)
class Context(object):
    '''Class that represents a context in the logical model'''
    list = []

    def __init__(self):
        Context.list.append(self)

    @abc.abstractmethod
    def init(self, attrs):
        "Initiate context."

    @staticmethod
    def get_cls(context_type):
        '''Return class of specified type.'''
        for context in utils.itersubclasses(Context):
            if context_type == context.__context_type__:
                return context
        raise RuntimeError("No such context_type %s" % context_type)

    @staticmethod
    def get(context_type):
        """Returns instance of a context for context type.
        """
        return Context.get_cls(context_type)()

    @abc.abstractmethod
    def deploy(self):
        '''Deploy context.'''

    @abc.abstractmethod
    def undeploy(self):
        '''Undeploy context.'''

    @abc.abstractmethod
    def _get_server(self, attr_name):
        '''get server info by name from context
        '''

    @staticmethod
    def get_server(attr_name):
        '''lookup server info by name from context
        attr_name: either a name for a server created by yardstick or a dict
        with attribute name mapping when using external heat templates
        '''
        server = None
        for context in Context.list:
            server = context._get_server(attr_name)
            if server is not None:
                break

        if server is None:
            raise ValueError("context not found for server '%r'" %
                             attr_name)

        return server