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 """
21 from yardstick.network_services.helpers.samplevnf_helper import PortPairs
24 LOG = logging.getLogger(__name__)
27 class QueueFileWrapper(object):
28 """ Class providing file-like API for talking with SSH connection """
30 def __init__(self, q_in, q_out, prompt):
39 """ read chunk from input queue """
40 if self.q_in.qsize() > 0 and size:
41 in_data = self.q_in.get()
44 def write(self, chunk):
45 """ write chunk to output queue """
46 self.buf.append(chunk)
47 # flush on prompt or if we exceed bufsize
49 size = sum(len(c) for c in self.buf)
50 if self.prompt in chunk or size > self.bufsize:
51 out = ''.join(self.buf)
56 """ close multiprocessing queue """
61 while self.q_out.qsize() > 0:
65 class VnfdHelper(dict):
67 def __init__(self, *args, **kwargs):
68 super(VnfdHelper, self).__init__(*args, **kwargs)
69 self.port_pairs = PortPairs(self['vdu'][0]['external-interface'])
70 # port num is not present until binding so we have to memoize
71 self._port_num_map = {}
74 def mgmt_interface(self):
75 return self["mgmt-interface"]
87 return self.vdu0['external-interface']
91 return self['benchmark']['kpi']
93 def find_virtual_interface(self, **kwargs):
94 key, value = next(iter(kwargs.items()))
95 for interface in self.interfaces:
96 virtual_intf = interface["virtual-interface"]
97 if virtual_intf[key] == value:
101 def find_interface(self, **kwargs):
102 key, value = next(iter(kwargs.items()))
103 for interface in self.interfaces:
104 if interface[key] == value:
108 # hide dpdk_port_num key so we can abstract
109 def find_interface_by_port(self, port):
110 for interface in self.interfaces:
111 virtual_intf = interface["virtual-interface"]
112 # we have to convert to int to compare
113 if int(virtual_intf['dpdk_port_num']) == port:
117 def port_num(self, port):
118 # we need interface name -> DPDK port num (PMD ID) -> LINK ID
119 # LINK ID -> PMD ID is governed by the port mask
125 if isinstance(port, dict):
128 intf = self.find_interface(name=port)
129 return self._port_num_map.setdefault(intf["name"],
130 int(intf["virtual-interface"]["dpdk_port_num"]))
132 def port_nums(self, intfs):
133 return [self.port_num(i) for i in intfs]
135 def ports_iter(self):
136 for port_name in self.port_pairs.all_ports:
137 port_num = self.port_num(port_name)
138 yield port_name, port_num
141 @six.add_metaclass(abc.ABCMeta)
142 class GenericVNF(object):
143 """Class providing file-like API for generic VNF implementation
145 Currently the only class implementing this interface is
146 yardstick/network_services/vnf_generic/vnf/sample_vnf:SampleVNF.
149 # centralize network naming convention
150 UPLINK = PortPairs.UPLINK
151 DOWNLINK = PortPairs.DOWNLINK
153 def __init__(self, name, vnfd):
155 self.vnfd_helper = VnfdHelper(vnfd)
156 # List of statistics we can obtain from this VNF
157 # - ETSI MANO 6.3.1.1 monitoring_parameter
158 self.kpi = self.vnfd_helper.kpi
159 # Standard dictionary containing params like thread no, buffer size etc
161 self.runs_traffic = False
164 def instantiate(self, scenario_cfg, context_cfg):
165 """Prepare VNF for operation and start the VNF process/VM
167 :param scenario_cfg: Scenario config
168 :param context_cfg: Context config
173 def wait_for_instantiate(self):
174 """Wait for VNF to start
181 """Kill all VNF processes"""
184 def scale(self, flavor=""):
187 :param flavor: Name of the flavor.
192 def collect_kpi(self):
193 """Return a dict containing the selected KPI at a given point of time
195 :return: {"kpi": value, "kpi2": value}
199 def start_collect(self):
200 """Start KPI collection
205 def stop_collect(self):
206 """Stop KPI collection
211 @six.add_metaclass(abc.ABCMeta)
212 class GenericTrafficGen(GenericVNF):
213 """ Class providing file-like API for generic traffic generator """
215 def __init__(self, name, vnfd):
216 super(GenericTrafficGen, self).__init__(name, vnfd)
217 self.runs_traffic = True
218 self.traffic_finished = False
221 def run_traffic(self, traffic_profile):
222 """Generate traffic on the wire according to the given params.
224 This method is non-blocking, returns immediately when traffic process
225 is running. Mandatory.
227 :param traffic_profile:
233 """After this method finishes, all traffic processes should stop.
240 def listen_traffic(self, traffic_profile):
241 """Listen to traffic with the given parameters.
243 Method is non-blocking, returns immediately when traffic process
244 is running. Optional.
246 :param traffic_profile:
251 def verify_traffic(self, traffic_profile):
252 """Verify captured traffic after it has ended.
256 :param traffic_profile:
261 def wait_for_instantiate(self):
262 """Wait for an instance to load.
270 def start_collect(self):
271 """Start KPI collection.
273 Traffic measurements are always collected during injection.
281 def stop_collect(self):
282 """Stop KPI collection.