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 ##############################################################################
12 import yardstick.common.utils as utils
16 """Class to represent the status of the flags in a context"""
18 _FLAGS = {'no_setup': False,
21 def __init__(self, **kwargs):
22 for name, value in self._FLAGS.items():
23 setattr(self, name, value)
25 for name, value in ((name, value) for (name, value) in kwargs.items()
26 if name in self._FLAGS):
27 setattr(self, name, value)
29 def parse(self, **kwargs):
30 """Read in values matching the flags stored in this object"""
34 for name, value in ((name, value) for (name, value) in kwargs.items()
35 if name in self._FLAGS):
36 setattr(self, name, value)
39 @six.add_metaclass(abc.ABCMeta)
40 class Context(object):
41 """Class that represents a context in the logical model"""
45 def split_name(name, sep='.'):
47 name_iter = iter(name.split(sep))
48 except AttributeError:
49 # name is not a string
51 return next(name_iter), next(name_iter, None)
54 Context.list.append(self)
57 def init(self, attrs):
58 """Initiate context."""
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__:
66 raise RuntimeError("No such context_type %s" % context_type)
69 def get(context_type):
70 """Returns instance of a context for context type.
72 return Context.get_cls(context_type)()
80 """Undeploy context."""
81 self._delete_context()
83 def _delete_context(self):
84 Context.list.remove(self)
87 def _get_server(self, attr_name):
88 """get server info by name from context
92 def _get_network(self, attr_name):
93 """get network info by name from context
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
102 servers = (context._get_server(attr_name) for context in Context.list)
104 return next(s for s in servers if s)
105 except StopIteration:
106 raise ValueError("context not found for server %r" %
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
115 :returns Context instance
117 servers = ((context._get_server(attr_name), context)
118 for context in Context.list)
120 return next(con for s, con in servers if s)
121 except StopIteration:
122 raise ValueError("context not found for name %r" %
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
132 networks = (context._get_network(attr_name) for context in Context.list)
134 return next(n for n in networks if n)
135 except StopIteration:
136 raise ValueError("context not found for server %r" %