1 # Copyright (c) 2016-2017 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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."""
16 from __future__ import absolute_import
23 from yardstick.benchmark.contexts.base import Context
24 from yardstick.common.constants import YARDSTICK_ROOT_PATH
25 from yardstick.common.utils import import_modules_from_package, itersubclasses
26 from yardstick.common.yaml_loader import yaml_load
28 LOG = logging.getLogger(__name__)
31 class StandaloneContext(Context):
32 """ This class handles standalone nodes - VM running on Non-Managed NFVi
33 Configuration: vswitch, ovs, ovs-dpdk, sr-iov, linuxbridge
36 __context_type__ = "Standalone"
46 super(StandaloneContext, self).__init__()
48 def read_config_file(self):
49 """Read from config file"""
51 with open(self.file_path) as stream:
52 LOG.info("Parsing pod file: %s", self.file_path)
53 cfg = yaml_load(stream)
56 def get_nfvi_obj(self):
57 print("{0}".format(self.nfvi_node[0]['role']))
58 context_type = self.get_context_impl(self.nfvi_node[0]['role'])
59 nfvi_obj = context_type()
61 nfvi_obj.parse_pod_and_get_data(self.file_path)
64 def init(self, attrs):
65 """initializes itself from the supplied arguments"""
67 self.name = attrs["name"]
68 self.file_path = file_path = attrs.get("file", "pod.yaml")
71 cfg = self.read_config_file()
72 except IOError as io_error:
73 if io_error.errno != errno.ENOENT:
75 self.file_path = os.path.join(YARDSTICK_ROOT_PATH, file_path)
76 cfg = self.read_config_file()
78 self.vm_deploy = attrs.get("vm_deploy", True)
79 self.nodes.extend([node for node in cfg["nodes"]
80 if str(node["role"]) != "Sriov" and
81 str(node["role"]) != "Ovsdpdk"])
82 for node in cfg["nodes"]:
83 if str(node["role"]) == "Sriov":
84 self.nfvi_node.extend([node for node in cfg["nodes"]
85 if str(node["role"]) == "Sriov"])
86 if str(node["role"]) == "Ovsdpdk":
87 self.nfvi_node.extend([node for node in cfg["nodes"]
88 if str(node["role"]) == "Ovsdpdk"])
89 LOG.info("{0}".format(node["role"]))
91 LOG.debug("Node role is other than SRIOV and OVS")
92 self.nfvi_obj = self.get_nfvi_obj()
94 # add optional static network definition
95 self.networks.update(cfg.get("networks", {}))
96 self.nfvi_obj = self.get_nfvi_obj()
97 LOG.debug("Nodes: %r", self.nodes)
98 LOG.debug("NFVi Node: %r", self.nfvi_node)
99 LOG.debug("Networks: %r", self.networks)
102 """don't need to deploy"""
104 # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
105 if not self.vm_deploy:
108 # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
109 self.nfvi_obj.ssh_remote_machine()
110 if self.nfvi_obj.first_run is True:
111 self.nfvi_obj.install_req_libs()
113 nic_details = self.nfvi_obj.get_nic_details()
114 print("{0}".format(nic_details))
116 if self.nfvi_node[0]["role"] == "Sriov":
117 self.nfvi_obj.setup_sriov_context(
118 self.nfvi_obj.sriov[0]['phy_ports'],
120 self.nfvi_obj.sriov[0]['phy_driver'])
121 if self.nfvi_node[0]["role"] == "Ovsdpdk":
122 self.nfvi_obj.setup_ovs(self.nfvi_obj.ovs[0]["phy_ports"])
123 self.nfvi_obj.start_ovs_serverswitch()
125 self.nfvi_obj.setup_ovs_bridge()
126 self.nfvi_obj.add_oflows()
127 self.nfvi_obj.setup_ovs_context(
128 self.nfvi_obj.ovs[0]['phy_ports'],
130 self.nfvi_obj.ovs[0]['phy_driver'])
134 """don't need to undeploy"""
136 if not self.vm_deploy:
138 # Todo: NFVi undeploy (sriov, vswitch, ovs etc) based on the config.
139 # self.nfvi_obj = self.get_nfvi_obj()
140 self.nfvi_obj.ssh_remote_machine()
141 self.nfvi_obj.destroy_vm()
144 def _get_server(self, attr_name):
145 """lookup server info by name from context
148 attr_name -- A name for a server listed in nodes config file
150 node_name, name = self.split_name(attr_name)
151 if name is None or self.name != name:
154 matching_nodes = (n for n in self.nodes if n["name"] == node_name)
156 # A clone is created in order to avoid affecting the
158 node = dict(next(matching_nodes))
159 except StopIteration:
163 duplicate = next(matching_nodes)
164 except StopIteration:
167 raise ValueError("Duplicate nodes!!! Nodes: %s %s",
170 node["name"] = attr_name
173 def _get_network(self, attr_name):
174 if not isinstance(attr_name, collections.Mapping):
175 network = self.networks.get(attr_name)
178 # Don't generalize too much Just support vld_id
179 vld_id = attr_name.get('vld_id', {})
180 # for standalone context networks are dicts
181 iter1 = (n for n in self.networks.values() if n.get('vld_id') == vld_id)
182 network = next(iter1, None)
189 "name": network["name"],
190 "vld_id": network.get("vld_id"),
191 "segmentation_id": network.get("segmentation_id"),
192 "network_type": network.get("network_type"),
193 "physical_network": network.get("physical_network"),
197 def get_context_impl(self, nfvi_type):
198 """ Find the implementing class from vnf_model["vnf"]["name"] field
200 :param vnf_model: dictionary containing a parsed vnfd
201 :return: subclass of GenericVNF
203 import_modules_from_package(
204 "yardstick.benchmark.contexts")
205 expected_name = nfvi_type
206 impl = [c for c in itersubclasses(StandaloneContext)
207 if c.__name__ == expected_name]
209 return next(iter(impl))
210 except StopIteration:
211 raise ValueError("No implementation for %s", expected_name)