Merge "Bug Fix: force the network to attacht to trex to start the traffic"
[yardstick.git] / yardstick / benchmark / contexts / standalone.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """This module handle non managed standalone virtualization node."""
15
16 from __future__ import absolute_import
17 import logging
18 import errno
19 import collections
20 import yaml
21
22 from yardstick.benchmark.contexts.base import Context
23 from yardstick.definitions import YARDSTICK_ROOT_PATH
24
25 LOG = logging.getLogger(__name__)
26
27
28 class StandaloneContext(Context):
29     """ This class handles standalone nodes - VM running on Non-Managed NFVi
30     Configuration: vswitch, ovs, ovs-dpdk, sr-iov, linuxbridge
31     """
32
33     __context_type__ = "Standalone"
34
35     def __init__(self):
36         self.name = None
37         self.file_path = None
38         self.nodes = []
39         self.nfvi_node = []
40         super(StandaloneContext, self).__init__()
41
42     def read_config_file(self):
43         """Read from config file"""
44
45         with open(self.file_path) as stream:
46             LOG.info("Parsing pod file: %s", self.file_path)
47             cfg = yaml.load(stream)
48         return cfg
49
50     def init(self, attrs):
51         """initializes itself from the supplied arguments"""
52
53         self.name = attrs["name"]
54         self.file_path = attrs.get("file", "pod.yaml")
55         LOG.info("Parsing pod file: %s", self.file_path)
56
57         try:
58             cfg = self.read_config_file()
59         except IOError as ioerror:
60             if ioerror.errno == errno.ENOENT:
61                 self.file_path = YARDSTICK_ROOT_PATH + self.file_path
62                 cfg = self.read_config_file()
63             else:
64                 raise
65
66         self.nodes.extend(cfg["nodes"])
67         self.nfvi_node.extend([node for node in cfg["nodes"]
68                                if node["role"] == "nfvi_node"])
69         LOG.debug("Nodes: %r", self.nodes)
70         LOG.debug("NFVi Node: %r", self.nfvi_node)
71
72     def deploy(self):
73         """don't need to deploy"""
74
75         # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
76         pass
77
78     def undeploy(self):
79         """don't need to undeploy"""
80
81         # Todo: NFVi undeploy (sriov, vswitch, ovs etc) based on the config.
82         super(StandaloneContext, self).undeploy()
83
84     def _get_server(self, attr_name):
85         """lookup server info by name from context
86
87         Keyword arguments:
88         attr_name -- A name for a server listed in nodes config file
89         """
90
91         if isinstance(attr_name, collections.Mapping):
92             return None
93
94         if self.name.split("-")[0] != attr_name.split(".")[1]:
95             return None
96
97         node_name = attr_name.split(".")[0]
98         matching_nodes = (n for n in self.nodes if n["name"] == node_name)
99
100         try:
101             # A clone is created in order to avoid affecting the
102             # original one.
103             node = dict(next(matching_nodes))
104         except StopIteration:
105             return None
106
107         try:
108             duplicate = next(matching_nodes)
109         except StopIteration:
110             pass
111         else:
112             raise ValueError("Duplicate nodes!!! Nodes: %s %s",
113                              (matching_nodes, duplicate))
114
115         node["name"] = attr_name
116         return node