Setup OVS-DPDK Standalone Context
[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 import time
22
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
27 LOG = logging.getLogger(__name__)
28
29
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
33     """
34
35     __context_type__ = "Standalone"
36
37     def __init__(self):
38         self.name = None
39         self.file_path = None
40         self.nodes = []
41         self.networks = {}
42         self.nfvi_node = []
43         self.nfvi_obj = None
44         super(self.__class__, self).__init__()
45
46     def read_config_file(self):
47         """Read from config file"""
48
49         with open(self.file_path) as stream:
50             LOG.info("Parsing pod file: %s", self.file_path)
51             cfg = yaml.load(stream)
52         return cfg
53
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()
58         nfvi_obj.__init__()
59         nfvi_obj.parse_pod_and_get_data(self.file_path)
60         return nfvi_obj
61
62     def init(self, attrs):
63         """initializes itself from the supplied arguments"""
64
65         self.name = attrs["name"]
66         self.file_path = attrs.get("file", "pod.yaml")
67         LOG.info("Parsing pod file: %s", self.file_path)
68
69         try:
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()
75             else:
76                 raise
77
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"]))
90             else:
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)
99
100     def deploy(self):
101         """don't need to deploy"""
102
103         # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
104         if not self.vm_deploy:
105             return
106
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()
111
112         nic_details = self.nfvi_obj.get_nic_details()
113         print("{0}".format(nic_details))
114
115         if self.nfvi_node[0]["role"] == "Sriov":
116             self.nfvi_obj.setup_sriov_context(
117                 self.nfvi_obj.sriov[0]['phy_ports'],
118                 nic_details,
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()
123             time.sleep(5)
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'],
128                 nic_details,
129                 self.nfvi_obj.ovs[0]['phy_driver'])
130             pass
131
132     def undeploy(self):
133         """don't need to undeploy"""
134
135         if not self.vm_deploy:
136             return
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()
141         pass
142
143     def _get_server(self, attr_name):
144         """lookup server info by name from context
145
146         Keyword arguments:
147         attr_name -- A name for a server listed in nodes config file
148         """
149         if isinstance(attr_name, collections.Mapping):
150             return None
151         if self.name != attr_name.split(".")[1]:
152             return None
153         node_name = attr_name.split(".")[0]
154         matching_nodes = (n for n in self.nodes if n["name"] == node_name)
155         try:
156             # A clone is created in order to avoid affecting the
157             # original one.
158             node = dict(next(matching_nodes))
159         except StopIteration:
160             return None
161
162         try:
163             duplicate = next(matching_nodes)
164         except StopIteration:
165             pass
166         else:
167             raise ValueError("Duplicate nodes!!! Nodes: %s %s",
168                              (matching_nodes, duplicate))
169         node["name"] = attr_name
170         return node
171
172     def _get_network(self, attr_name):
173         if not isinstance(attr_name, collections.Mapping):
174             network = self.networks.get(attr_name)
175
176         else:
177             # Don't generalize too much  Just support vld_id
178             vld_id = attr_name.get('vld_id')
179             if vld_id is None:
180                 return None
181             try:
182                 network = next(n for n in self.networks.values() if
183                                n.get("vld_id") == vld_id)
184             except StopIteration:
185                 return None
186
187         if network is None:
188             return None
189
190         result = {
191             # name is required
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"),
197         }
198         return result
199
200     def get_context_impl(self, nfvi_type):
201         """ Find the implementing class from vnf_model["vnf"]["name"] field
202
203         :param vnf_model: dictionary containing a parsed vnfd
204         :return: subclass of GenericVNF
205         """
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]
211         try:
212             return next(iter(impl))
213         except StopIteration:
214             raise ValueError("No implementation for %s", expected_name)