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
27 LOG = logging.getLogger(__name__)
30 class StandaloneContext(Context):
31 """ This class handles standalone nodes - VM running on Non-Managed NFVi
32 Configuration: vswitch, ovs, ovs-dpdk, sr-iov, linuxbridge
35 __context_type__ = "Standalone"
44 super(self.__class__, self).__init__()
46 def read_config_file(self):
47 """Read from config file"""
49 with open(self.file_path) as stream:
50 LOG.info("Parsing pod file: %s", self.file_path)
51 cfg = yaml.load(stream)
54 def get_nfvi_obj(self):
55 print("{0}".format(self.nfvi_node[0]['role']))
56 context_type = self.get_context_impl(self.nfvi_node[0]['role'])
57 nfvi_obj = context_type()
59 nfvi_obj.parse_pod_and_get_data(self.file_path)
62 def init(self, attrs):
63 """initializes itself from the supplied arguments"""
65 self.name = attrs["name"]
66 self.file_path = attrs.get("file", "pod.yaml")
67 LOG.info("Parsing pod file: %s", self.file_path)
70 cfg = self.read_config_file()
71 except IOError as ioerror:
72 if ioerror.errno == errno.ENOENT:
73 self.file_path = YARDSTICK_ROOT_PATH + self.file_path
74 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()
93 # add optional static network definition
94 self.networks.update(cfg.get("networks", {}))
95 self.nfvi_obj = self.get_nfvi_obj()
96 LOG.debug("Nodes: %r", self.nodes)
97 LOG.debug("NFVi Node: %r", self.nfvi_node)
98 LOG.debug("Networks: %r", self.networks)
101 """don't need to deploy"""
103 # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
104 if not self.vm_deploy:
107 # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
108 self.nfvi_obj.ssh_remote_machine()
109 if self.nfvi_obj.first_run is True:
110 self.nfvi_obj.install_req_libs()
112 nic_details = self.nfvi_obj.get_nic_details()
113 print("{0}".format(nic_details))
115 if self.nfvi_node[0]["role"] == "Sriov":
116 self.nfvi_obj.setup_sriov_context(
117 self.nfvi_obj.sriov[0]['phy_ports'],
119 self.nfvi_obj.sriov[0]['phy_driver'])
120 if self.nfvi_node[0]["role"] == "Ovsdpdk":
121 self.nfvi_obj.setup_ovs(self.nfvi_obj.ovs[0]["phy_ports"])
122 self.nfvi_obj.start_ovs_serverswitch()
124 self.nfvi_obj.setup_ovs_bridge()
125 self.nfvi_obj.add_oflows()
126 self.nfvi_obj.setup_ovs_context(
127 self.nfvi_obj.ovs[0]['phy_ports'],
129 self.nfvi_obj.ovs[0]['phy_driver'])
133 """don't need to undeploy"""
135 if not self.vm_deploy:
137 # Todo: NFVi undeploy (sriov, vswitch, ovs etc) based on the config.
138 # self.nfvi_obj = self.get_nfvi_obj()
139 self.nfvi_obj.ssh_remote_machine()
140 self.nfvi_obj.destroy_vm()
143 def _get_server(self, attr_name):
144 """lookup server info by name from context
147 attr_name -- A name for a server listed in nodes config file
149 if isinstance(attr_name, collections.Mapping):
151 if self.name != attr_name.split(".")[1]:
153 node_name = attr_name.split(".")[0]
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",
168 (matching_nodes, duplicate))
169 node["name"] = attr_name
172 def _get_network(self, attr_name):
173 if not isinstance(attr_name, collections.Mapping):
174 network = self.networks.get(attr_name)
177 # Don't generalize too much Just support vld_id
178 vld_id = attr_name.get('vld_id')
182 network = next(n for n in self.networks.values() if
183 n.get("vld_id") == vld_id)
184 except StopIteration:
192 "name": network["name"],
193 "vld_id": network.get("vld_id"),
194 "segmentation_id": network.get("segmentation_id"),
195 "network_type": network.get("network_type"),
196 "physical_network": network.get("physical_network"),
200 def get_context_impl(self, nfvi_type):
201 """ Find the implementing class from vnf_model["vnf"]["name"] field
203 :param vnf_model: dictionary containing a parsed vnfd
204 :return: subclass of GenericVNF
206 import_modules_from_package(
207 "yardstick.benchmark.contexts")
208 expected_name = nfvi_type
209 impl = [c for c in itersubclasses(StandaloneContext)
210 if c.__name__ == expected_name]
212 return next(iter(impl))
213 except StopIteration:
214 raise ValueError("No implementation for %s", expected_name)