Merge "Add Flags class to base.Context"
[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 class Flags(object):
16     """Class to represent the status of the flags in a context"""
17
18     _FLAGS = {'no_setup': False,
19               'no_teardown': False}
20
21     def __init__(self, **kwargs):
22         for name, value in self._FLAGS.items():
23             setattr(self, name, value)
24
25         for name, value in ((name, value) for (name, value) in kwargs.items()
26                             if name in self._FLAGS):
27             setattr(self, name, value)
28
29     def parse(self, **kwargs):
30         """Read in values matching the flags stored in this object"""
31         if not kwargs:
32             return
33
34         for name, value in ((name, value) for (name, value) in kwargs.items()
35                             if name in self._FLAGS):
36             setattr(self, name, value)
37
38
39 @six.add_metaclass(abc.ABCMeta)
40 class Context(object):
41     """Class that represents a context in the logical model"""
42     list = []
43
44     @staticmethod
45     def split_name(name, sep='.'):
46         try:
47             name_iter = iter(name.split(sep))
48         except AttributeError:
49             # name is not a string
50             return None, None
51         return next(name_iter), next(name_iter, None)
52
53     def __init__(self):
54         Context.list.append(self)
55
56     @abc.abstractmethod
57     def init(self, attrs):
58         """Initiate context."""
59
60     @staticmethod
61     def get_cls(context_type):
62         """Return class of specified type."""
63         for context in utils.itersubclasses(Context):
64             if context_type == context.__context_type__:
65                 return context
66         raise RuntimeError("No such context_type %s" % context_type)
67
68     @staticmethod
69     def get(context_type):
70         """Returns instance of a context for context type.
71         """
72         return Context.get_cls(context_type)()
73
74     @abc.abstractmethod
75     def deploy(self):
76         """Deploy context."""
77
78     @abc.abstractmethod
79     def undeploy(self):
80         """Undeploy context."""
81         self._delete_context()
82
83     def _delete_context(self):
84         Context.list.remove(self)
85
86     @abc.abstractmethod
87     def _get_server(self, attr_name):
88         """get server info by name from context
89         """
90
91     @abc.abstractmethod
92     def _get_network(self, attr_name):
93         """get network info by name from context
94         """
95
96     @staticmethod
97     def get_server(attr_name):
98         """lookup server info by name from context
99         attr_name: either a name for a server created by yardstick or a dict
100         with attribute name mapping when using external heat templates
101         """
102         servers = (context._get_server(attr_name) for context in Context.list)
103         try:
104             return next(s for s in servers if s)
105         except StopIteration:
106             raise ValueError("context not found for server %r" %
107                              attr_name)
108
109     @staticmethod
110     def get_context_from_server(attr_name):
111         """lookup context info by name from node config
112         attr_name: either a name of the node created by yardstick or a dict
113         with attribute name mapping when using external templates
114
115         :returns Context instance
116         """
117         servers = ((context._get_server(attr_name), context)
118                    for context in Context.list)
119         try:
120             return next(con for s, con in servers if s)
121         except StopIteration:
122             raise ValueError("context not found for name %r" %
123                              attr_name)
124
125     @staticmethod
126     def get_network(attr_name):
127         """lookup server info by name from context
128         attr_name: either a name for a server created by yardstick or a dict
129         with attribute name mapping when using external heat templates
130         """
131
132         networks = (context._get_network(attr_name) for context in Context.list)
133         try:
134             return next(n for n in networks if n)
135         except StopIteration:
136             raise ValueError("context not found for server %r" %
137                              attr_name)