1 # Copyright (c) 2016-2019 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.
20 from yardstick.network_services.helpers.samplevnf_helper import PortPairs
23 LOG = logging.getLogger(__name__)
26 class QueueFileWrapper(object):
27 """ Class providing file-like API for talking with SSH connection """
29 def __init__(self, q_in, q_out, prompt):
38 """ read chunk from input queue """
39 if self.q_in.qsize() > 0 and size:
40 in_data = self.q_in.get()
43 def write(self, chunk):
44 """ write chunk to output queue """
45 self.buf.append(chunk)
46 # flush on prompt or if we exceed bufsize
48 size = sum(len(c) for c in self.buf)
49 if self.prompt in chunk or size > self.bufsize:
50 out = ''.join(self.buf)
55 """ close multiprocessing queue """
60 while self.q_out.qsize() > 0:
64 class VnfdHelper(dict):
66 def __init__(self, *args, **kwargs):
67 super(VnfdHelper, self).__init__(*args, **kwargs)
68 self.port_pairs = PortPairs(self['vdu'][0]['external-interface'])
69 # port num is not present until binding so we have to memoize
70 self._port_num_map = {}
73 def mgmt_interface(self):
74 return self["mgmt-interface"]
86 return self.vdu0['external-interface']
90 return self['benchmark']['kpi']
92 def find_virtual_interface(self, **kwargs):
93 key, value = next(iter(kwargs.items()))
94 for interface in self.interfaces:
95 virtual_intf = interface["virtual-interface"]
96 if virtual_intf[key] == value:
100 def find_interface(self, **kwargs):
101 key, value = next(iter(kwargs.items()))
102 for interface in self.interfaces:
103 if interface[key] == value:
107 # hide dpdk_port_num key so we can abstract
108 def find_interface_by_port(self, port):
109 for interface in self.interfaces:
110 virtual_intf = interface["virtual-interface"]
111 # we have to convert to int to compare
112 if int(virtual_intf['dpdk_port_num']) == port:
116 def port_num(self, port):
117 # we need interface name -> DPDK port num (PMD ID) -> LINK ID
118 # LINK ID -> PMD ID is governed by the port mask
124 if isinstance(port, dict):
127 intf = self.find_interface(name=port)
128 return self._port_num_map.setdefault(intf["name"],
129 int(intf["virtual-interface"]["dpdk_port_num"]))
131 def port_nums(self, intfs):
132 return [self.port_num(i) for i in intfs]
134 def ports_iter(self):
135 for port_name in self.port_pairs.all_ports:
136 port_num = self.port_num(port_name)
137 yield port_name, port_num
140 @six.add_metaclass(abc.ABCMeta)
141 class GenericVNF(object):
142 """Class providing file-like API for generic VNF implementation
144 Currently the only class implementing this interface is
145 yardstick/network_services/vnf_generic/vnf/sample_vnf:SampleVNF.
148 # centralize network naming convention
149 UPLINK = PortPairs.UPLINK
150 DOWNLINK = PortPairs.DOWNLINK
152 def __init__(self, name, vnfd):
154 self.vnfd_helper = VnfdHelper(vnfd)
155 # List of statistics we can obtain from this VNF
156 # - ETSI MANO 6.3.1.1 monitoring_parameter
157 self.kpi = self.vnfd_helper.kpi
158 # Standard dictionary containing params like thread no, buffer size etc
160 self.runs_traffic = False
163 def instantiate(self, scenario_cfg, context_cfg):
164 """Prepare VNF for operation and start the VNF process/VM
166 :param scenario_cfg: Scenario config
167 :param context_cfg: Context config
172 def wait_for_instantiate(self):
173 """Wait for VNF to start
180 """Kill all VNF processes"""
183 def scale(self, flavor=""):
186 :param flavor: Name of the flavor.
191 def collect_kpi(self):
192 """Return a dict containing the selected KPI at a given point of time
194 :return: {"kpi": value, "kpi2": value}
198 def start_collect(self):
199 """Start KPI collection
204 def stop_collect(self):
205 """Stop KPI collection
210 @six.add_metaclass(abc.ABCMeta)
211 class GenericTrafficGen(GenericVNF):
212 """Class providing file-like API for generic traffic generator"""
214 def __init__(self, name, vnfd):
215 super(GenericTrafficGen, self).__init__(name, vnfd)
216 self.runs_traffic = True
217 self.traffic_finished = False
220 def run_traffic(self, traffic_profile):
221 """Generate traffic on the wire according to the given params.
223 This method is non-blocking, returns immediately when traffic process
224 is running. Mandatory.
226 :param traffic_profile:
232 """After this method finishes, all traffic processes should stop.
239 def listen_traffic(self, traffic_profile):
240 """Listen to traffic with the given parameters.
242 Method is non-blocking, returns immediately when traffic process
243 is running. Optional.
245 :param traffic_profile:
250 def verify_traffic(self, traffic_profile):
251 """Verify captured traffic after it has ended.
255 :param traffic_profile:
260 def wait_for_instantiate(self):
261 """Wait for an instance to load.
269 def start_collect(self):
270 """Start KPI collection.
272 Traffic measurements are always collected during injection.
280 def stop_collect(self):
281 """Stop KPI collection.