samples: Add generic L3 forwarder tests
[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     @staticmethod
22     def split_name(name, sep='.'):
23         try:
24             name_iter = iter(name.split(sep))
25         except AttributeError:
26             # name is not a string
27             return None, None
28         return next(name_iter), next(name_iter, None)
29
30     def __init__(self):
31         Context.list.append(self)
32
33     @abc.abstractmethod
34     def init(self, attrs):
35         """Initiate context."""
36
37     @staticmethod
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__:
42                 return context
43         raise RuntimeError("No such context_type %s" % context_type)
44
45     @staticmethod
46     def get(context_type):
47         """Returns instance of a context for context type.
48         """
49         return Context.get_cls(context_type)()
50
51     @abc.abstractmethod
52     def deploy(self):
53         """Deploy context."""
54
55     @abc.abstractmethod
56     def undeploy(self):
57         """Undeploy context."""
58         self._delete_context()
59
60     def _delete_context(self):
61         Context.list.remove(self)
62
63     @abc.abstractmethod
64     def _get_server(self, attr_name):
65         """get server info by name from context
66         """
67
68     @abc.abstractmethod
69     def _get_network(self, attr_name):
70         """get network info by name from context
71         """
72
73     @staticmethod
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
78         """
79         servers = (context._get_server(attr_name) for context in Context.list)
80         try:
81             return next(s for s in servers if s)
82         except StopIteration:
83             raise ValueError("context not found for server %r" %
84                              attr_name)
85
86     @staticmethod
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
91
92         :returns Context instance
93         """
94         servers = ((context._get_server(attr_name), context)
95                    for context in Context.list)
96         try:
97             return next(con for s, con in servers if s)
98         except StopIteration:
99             raise ValueError("context not found for name %r" %
100                              attr_name)
101
102     @staticmethod
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
107         """
108
109         networks = (context._get_network(attr_name) for context in Context.list)
110         try:
111             return next(n for n in networks if n)
112         except StopIteration:
113             raise ValueError("context not found for server %r" %
114                              attr_name)