1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
13 from yardstick.common import constants
14 from yardstick.common import utils
18 """Class to represent the status of the flags in a context"""
20 _FLAGS = {'no_setup': False,
22 'os_cloud_config': constants.OS_CLOUD_DEFAULT_CONFIG}
24 def __init__(self, **kwargs):
25 for name, value in self._FLAGS.items():
26 setattr(self, name, value)
28 for name, value in ((name, value) for (name, value) in kwargs.items()
29 if name in self._FLAGS):
30 setattr(self, name, value)
32 def parse(self, **kwargs):
33 """Read in values matching the flags stored in this object"""
37 for name, value in ((name, value) for (name, value) in kwargs.items()
38 if name in self._FLAGS):
39 setattr(self, name, value)
42 @six.add_metaclass(abc.ABCMeta)
43 class Context(object):
44 """Class that represents a context in the logical model"""
49 def split_name(name, sep='.'):
51 name_iter = iter(name.split(sep))
52 except AttributeError:
53 # name is not a string
55 return next(name_iter), next(name_iter, None)
58 Context.list.append(self)
63 def init(self, attrs):
64 """Initiate context"""
65 self._name = attrs['name']
66 self._task_id = attrs['task_id']
67 self._flags.parse(**attrs.get('flags', {}))
68 self._name_task_id = '{}-{}'.format(
69 self._name, self._task_id[:self.SHORT_TASK_ID_LEN])
73 if self._flags.no_setup or self._flags.no_teardown:
76 return self._name_task_id
79 def assigned_name(self):
83 def get_cls(context_type):
84 """Return class of specified type."""
85 for context in utils.itersubclasses(Context):
86 if context_type == context.__context_type__:
88 raise RuntimeError("No such context_type %s" % context_type)
91 def get(context_type):
92 """Returns instance of a context for context type.
94 return Context.get_cls(context_type)()
102 """Undeploy context."""
103 self._delete_context()
105 def _delete_context(self):
106 Context.list.remove(self)
109 def _get_server(self, attr_name):
110 """get server info by name from context
114 def _get_network(self, attr_name):
115 """get network info by name from context
119 def get_server(attr_name):
120 """lookup server info by name from context
121 attr_name: either a name for a server created by yardstick or a dict
122 with attribute name mapping when using external heat templates
124 servers = (context._get_server(attr_name) for context in Context.list)
126 return next(s for s in servers if s)
127 except StopIteration:
128 raise ValueError("context not found for server %r" %
132 def get_context_from_server(attr_name):
133 """lookup context info by name from node config
134 attr_name: either a name of the node created by yardstick or a dict
135 with attribute name mapping when using external templates
137 :returns Context instance
139 servers = ((context._get_server(attr_name), context)
140 for context in Context.list)
142 return next(con for s, con in servers if s)
143 except StopIteration:
144 raise ValueError("context not found for name %r" %
148 def get_network(attr_name):
149 """lookup server info by name from context
150 attr_name: either a name for a server created by yardstick or a dict
151 with attribute name mapping when using external heat templates
154 networks = (context._get_network(attr_name) for context in Context.list)
156 return next(n for n in networks if n)
157 except StopIteration:
158 raise ValueError("context not found for server %r" %