ae860accd6c5c560f45a83c4d86e35276df09d66
[yardstick.git] / yardstick / benchmark / contexts / base.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 import abc
10 import six
11
12 import yardstick.common.utils as utils
13
14
15 @six.add_metaclass(abc.ABCMeta)
16 class Context(object):
17     '''Class that represents a context in the logical model'''
18     list = []
19
20     def __init__(self):
21         Context.list.append(self)
22
23     @abc.abstractmethod
24     def init(self, attrs):
25         "Initiate context."
26
27     @staticmethod
28     def get_cls(context_type):
29         '''Return class of specified type.'''
30         for context in utils.itersubclasses(Context):
31             if context_type == context.__context_type__:
32                 return context
33         raise RuntimeError("No such context_type %s" % context_type)
34
35     @staticmethod
36     def get(context_type):
37         """Returns instance of a context for context type.
38         """
39         return Context.get_cls(context_type)()
40
41     @abc.abstractmethod
42     def deploy(self):
43         '''Deploy context.'''
44
45     @abc.abstractmethod
46     def undeploy(self):
47         '''Undeploy context.'''
48
49     @abc.abstractmethod
50     def _get_server(self, attr_name):
51         '''get server object by name from context
52         '''
53
54     @staticmethod
55     def get_server(attr_name):
56         '''lookup server object by name from context
57         attr_name: either a name for a server created by yardstick or a dict
58         with attribute name mapping when using external heat templates
59         '''
60         server = None
61         for context in Context.list:
62             server = context._get_server(attr_name)
63             if server is not None:
64                 break
65
66         if server is None:
67             raise ValueError("context not found for server '%s'" %
68                              attr_name["name"])
69
70         return server