Merge "Open storperf testcase to huawei-pod2"
[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.common.constants 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.networks = {}
40         self.nfvi_node = []
41         super(StandaloneContext, self).__init__()
42
43     def read_config_file(self):
44         """Read from config file"""
45
46         with open(self.file_path) as stream:
47             LOG.info("Parsing pod file: %s", self.file_path)
48             cfg = yaml.load(stream)
49         return cfg
50
51     def init(self, attrs):
52         """initializes itself from the supplied arguments"""
53
54         self.name = attrs["name"]
55         self.file_path = attrs.get("file", "pod.yaml")
56         LOG.info("Parsing pod file: %s", self.file_path)
57
58         try:
59             cfg = self.read_config_file()
60         except IOError as ioerror:
61             if ioerror.errno == errno.ENOENT:
62                 self.file_path = YARDSTICK_ROOT_PATH + self.file_path
63                 cfg = self.read_config_file()
64             else:
65                 raise
66
67         self.nodes.extend(cfg["nodes"])
68         self.nfvi_node.extend([node for node in cfg["nodes"]
69                                if node["role"] == "nfvi_node"])
70         # add optional static network definition
71         self.networks.update(cfg.get("networks", {}))
72         LOG.debug("Nodes: %r", self.nodes)
73         LOG.debug("NFVi Node: %r", self.nfvi_node)
74         LOG.debug("Networks: %r", self.networks)
75
76     def deploy(self):
77         """don't need to deploy"""
78
79         # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
80         pass
81
82     def undeploy(self):
83         """don't need to undeploy"""
84
85         # Todo: NFVi undeploy (sriov, vswitch, ovs etc) based on the config.
86         super(StandaloneContext, self).undeploy()
87
88     def _get_server(self, attr_name):
89         """lookup server info by name from context
90
91         Keyword arguments:
92         attr_name -- A name for a server listed in nodes config file
93         """
94
95         if isinstance(attr_name, collections.Mapping):
96             return None
97
98         if self.name.split("-")[0] != attr_name.split(".")[1]:
99             return None
100
101         node_name = attr_name.split(".")[0]
102         matching_nodes = (n for n in self.nodes if n["name"] == node_name)
103
104         try:
105             # A clone is created in order to avoid affecting the
106             # original one.
107             node = dict(next(matching_nodes))
108         except StopIteration:
109             return None
110
111         try:
112             duplicate = next(matching_nodes)
113         except StopIteration:
114             pass
115         else:
116             raise ValueError("Duplicate nodes!!! Nodes: %s %s",
117                              (matching_nodes, duplicate))
118
119         node["name"] = attr_name
120         return node
121
122     def _get_network(self, attr_name):
123         if not isinstance(attr_name, collections.Mapping):
124             network = self.networks.get(attr_name)
125
126         else:
127             # Don't generalize too much  Just support vld_id
128             vld_id = attr_name.get('vld_id')
129             if vld_id is None:
130                 return None
131             try:
132                 network = next(n for n in self.networks.values() if
133                                n.get("vld_id") == vld_id)
134             except StopIteration:
135                 return None
136
137         if network is None:
138             return None
139
140         result = {
141             # name is required
142             "name": network["name"],
143             "vld_id": network.get("vld_id"),
144             "segmentation_id": network.get("segmentation_id"),
145             "network_type": network.get("network_type"),
146             "physical_network": network.get("physical_network"),
147         }
148         return result