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