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 ##############################################################################
9 from __future__ import absolute_import
13 import yardstick.common.utils as utils
16 @six.add_metaclass(abc.ABCMeta)
17 class Context(object):
18 """Class that represents a context in the logical model"""
22 def split_name(name, sep='.'):
24 name_iter = iter(name.split(sep))
25 except AttributeError:
26 # name is not a string
28 return next(name_iter), next(name_iter, None)
31 Context.list.append(self)
34 def init(self, attrs):
35 """Initiate context."""
38 def get_cls(context_type):
39 """Return class of specified type."""
40 for context in utils.itersubclasses(Context):
41 if context_type == context.__context_type__:
43 raise RuntimeError("No such context_type %s" % context_type)
46 def get(context_type):
47 """Returns instance of a context for context type.
49 return Context.get_cls(context_type)()
57 """Undeploy context."""
58 self._delete_context()
60 def _delete_context(self):
61 Context.list.remove(self)
64 def _get_server(self, attr_name):
65 """get server info by name from context
69 def _get_network(self, attr_name):
70 """get network info by name from context
74 def get_server(attr_name):
75 """lookup server info by name from context
76 attr_name: either a name for a server created by yardstick or a dict
77 with attribute name mapping when using external heat templates
79 servers = (context._get_server(attr_name) for context in Context.list)
81 return next(s for s in servers if s)
83 raise ValueError("context not found for server %r" %
87 def get_context_from_server(attr_name):
88 """lookup context info by name from node config
89 attr_name: either a name of the node created by yardstick or a dict
90 with attribute name mapping when using external templates
92 :returns Context instance
94 servers = ((context._get_server(attr_name), context)
95 for context in Context.list)
97 return next(con for s, con in servers if s)
99 raise ValueError("context not found for name %r" %
103 def get_network(attr_name):
104 """lookup server info by name from context
105 attr_name: either a name for a server created by yardstick or a dict
106 with attribute name mapping when using external heat templates
109 networks = (context._get_network(attr_name) for context in Context.list)
111 return next(n for n in networks if n)
112 except StopIteration:
113 raise ValueError("context not found for server %r" %