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 """ Base class implementation for generic vnf implementation """
16 from __future__ import absolute_import
19 from yardstick.network_services.helpers.samplevnf_helper import PortPairs
21 LOG = logging.getLogger(__name__)
24 class QueueFileWrapper(object):
25 """ Class providing file-like API for talking with SSH connection """
27 def __init__(self, q_in, q_out, prompt):
36 """ read chunk from input queue """
37 if self.q_in.qsize() > 0 and size:
38 in_data = self.q_in.get()
41 def write(self, chunk):
42 """ write chunk to output queue """
43 self.buf.append(chunk)
44 # flush on prompt or if we exceed bufsize
46 size = sum(len(c) for c in self.buf)
47 if self.prompt in chunk or size > self.bufsize:
48 out = ''.join(self.buf)
53 """ close multiprocessing queue """
58 while self.q_out.qsize() > 0:
62 class VnfdHelper(dict):
64 def __init__(self, *args, **kwargs):
65 super(VnfdHelper, self).__init__(*args, **kwargs)
66 self.port_pairs = PortPairs(self['vdu'][0]['external-interface'])
69 def mgmt_interface(self):
70 return self["mgmt-interface"]
82 return self.vdu0['external-interface']
86 return self['benchmark']['kpi']
88 def find_virtual_interface(self, **kwargs):
89 key, value = next(iter(kwargs.items()))
90 for interface in self.interfaces:
91 virtual_intf = interface["virtual-interface"]
92 if virtual_intf[key] == value:
95 def find_interface(self, **kwargs):
96 key, value = next(iter(kwargs.items()))
97 for interface in self.interfaces:
98 if interface[key] == value:
101 # hide dpdk_port_num key so we can abstract
102 def find_interface_by_port(self, port):
103 for interface in self.interfaces:
104 virtual_intf = interface["virtual-interface"]
105 # we have to convert to int to compare
106 if int(virtual_intf['dpdk_port_num']) == port:
109 def port_num(self, port):
110 # we need interface name -> DPDK port num (PMD ID) -> LINK ID
111 # LINK ID -> PMD ID is governed by the port mask
117 if isinstance(port, dict):
120 intf = self.find_interface(name=port)
121 return int(intf["virtual-interface"]["dpdk_port_num"])
123 def port_nums(self, intfs):
124 return [self.port_num(i) for i in intfs]
126 def ports_iter(self):
127 for port_name in self.port_pairs.all_ports:
128 port_num = self.port_num(port_name)
129 yield port_name, port_num
132 class VNFObject(object):
134 # centralize network naming convention
135 UPLINK = PortPairs.UPLINK
136 DOWNLINK = PortPairs.DOWNLINK
138 def __init__(self, name, vnfd):
139 super(VNFObject, self).__init__()
141 self.vnfd_helper = VnfdHelper(vnfd) # fixme: parse this into a structure
144 class GenericVNF(VNFObject):
146 """ Class providing file-like API for generic VNF implementation """
147 def __init__(self, name, vnfd):
148 super(GenericVNF, self).__init__(name, vnfd)
149 # List of statistics we can obtain from this VNF
150 # - ETSI MANO 6.3.1.1 monitoring_parameter
151 self.kpi = self._get_kpi_definition()
152 # Standard dictionary containing params like thread no, buffer size etc
154 self.runs_traffic = False
156 def _get_kpi_definition(self):
157 """ Get list of KPIs defined in VNFD
160 :return: list of KPIs, e.g. ['throughput', 'latency']
162 return self.vnfd_helper.kpi
164 def instantiate(self, scenario_cfg, context_cfg):
165 """ Prepare VNF for operation and start the VNF process/VM
171 raise NotImplementedError()
174 """ Kill all VNF processes
178 raise NotImplementedError()
180 def scale(self, flavor=""):
186 raise NotImplementedError()
188 def collect_kpi(self):
189 """This method should return a dictionary containing the
190 selected KPI at a given point of time.
192 :return: {"kpi": value, "kpi2": value}
194 raise NotImplementedError()
197 class GenericTrafficGen(GenericVNF):
198 """ Class providing file-like API for generic traffic generator """
200 def __init__(self, name, vnfd):
201 super(GenericTrafficGen, self).__init__(name, vnfd)
202 self.runs_traffic = True
203 self.traffic_finished = False
205 def run_traffic(self, traffic_profile):
206 """ Generate traffic on the wire according to the given params.
207 Method is non-blocking, returns immediately when traffic process
208 is running. Mandatory.
210 :param traffic_profile:
213 raise NotImplementedError()
215 def listen_traffic(self, traffic_profile):
216 """ Listen to traffic with the given parameters.
217 Method is non-blocking, returns immediately when traffic process
218 is running. Optional.
220 :param traffic_profile:
225 def verify_traffic(self, traffic_profile):
226 """ Verify captured traffic after it has ended. Optional.
228 :param traffic_profile:
234 """ After this method finishes, all traffic processes should stop. Mandatory.
238 raise NotImplementedError()