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 Context.list.append(self)
25 def init(self, attrs):
26 """Initiate context."""
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__:
34 raise RuntimeError("No such context_type %s" % context_type)
37 def get(context_type):
38 """Returns instance of a context for context type.
40 return Context.get_cls(context_type)()
48 """Undeploy context."""
49 self._delete_context()
51 def _delete_context(self):
52 Context.list.remove(self)
55 def _get_server(self, attr_name):
56 """get server info by name from context
60 def _get_network(self, attr_name):
61 """get network info by name from context
65 def get_server(attr_name):
66 """lookup server info by name from context
67 attr_name: either a name for a server created by yardstick or a dict
68 with attribute name mapping when using external heat templates
70 servers = (context._get_server(attr_name) for context in Context.list)
72 return next(s for s in servers if s)
74 raise ValueError("context not found for server '%r'" %
78 def get_network(attr_name):
79 """lookup server info by name from context
80 attr_name: either a name for a server created by yardstick or a dict
81 with attribute name mapping when using external heat templates
84 networks = (context._get_network(attr_name) for context in Context.list)
86 return next(n for n in networks if n)
88 raise ValueError("context not found for server '%r'" %