Merge "test_pktgen_dpdk_throughput: speedup unittest, mock time.sleep()"
[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 from __future__ import absolute_import
10 import abc
11 import six
12
13 import yardstick.common.utils as utils
14
15
16 @six.add_metaclass(abc.ABCMeta)
17 class Context(object):
18     """Class that represents a context in the logical model"""
19     list = []
20
21     def __init__(self):
22         Context.list.append(self)
23
24     @abc.abstractmethod
25     def init(self, attrs):
26         """Initiate context."""
27
28     @staticmethod
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__:
33                 return context
34         raise RuntimeError("No such context_type %s" % context_type)
35
36     @staticmethod
37     def get(context_type):
38         """Returns instance of a context for context type.
39         """
40         return Context.get_cls(context_type)()
41
42     @abc.abstractmethod
43     def deploy(self):
44         """Deploy context."""
45
46     @abc.abstractmethod
47     def undeploy(self):
48         """Undeploy context."""
49         self._delete_context()
50
51     def _delete_context(self):
52         Context.list.remove(self)
53
54     @abc.abstractmethod
55     def _get_server(self, attr_name):
56         """get server info by name from context
57         """
58
59     @abc.abstractmethod
60     def _get_network(self, attr_name):
61         """get network info by name from context
62         """
63
64     @staticmethod
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
69         """
70         servers = (context._get_server(attr_name) for context in Context.list)
71         try:
72             return next(s for s in servers if s)
73         except StopIteration:
74             raise ValueError("context not found for server '%r'" %
75                              attr_name)
76
77     @staticmethod
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
82         """
83
84         networks = (context._get_network(attr_name) for context in Context.list)
85         try:
86             return next(n for n in networks if n)
87         except StopIteration:
88             raise ValueError("context not found for server '%r'" %
89                              attr_name)