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 LOG = logging.getLogger(__name__)
22 class QueueFileWrapper(object):
23 """ Class providing file-like API for talking with SSH connection """
25 def __init__(self, q_in, q_out, prompt):
34 """ read chunk from input queue """
35 if self.q_in.qsize() > 0 and size:
36 in_data = self.q_in.get()
39 def write(self, chunk):
40 """ write chunk to output queue """
41 self.buf.append(chunk)
42 # flush on prompt or if we exceed bufsize
44 size = sum(len(c) for c in self.buf)
45 if self.prompt in chunk or size > self.bufsize:
46 out = ''.join(self.buf)
51 """ close multiprocessing queue """
56 while self.q_out.qsize() > 0:
60 class VnfdHelper(dict):
63 def mgmt_interface(self):
64 return self["mgmt-interface"]
76 return self.vdu0['external-interface']
80 return self['benchmark']['kpi']
82 def find_virtual_interface(self, **kwargs):
83 key, value = next(iter(kwargs.items()))
84 for interface in self.interfaces:
85 virtual_intf = interface["virtual-interface"]
86 if virtual_intf[key] == value:
89 def find_interface(self, **kwargs):
90 key, value = next(iter(kwargs.items()))
91 for interface in self.interfaces:
92 if interface[key] == value:
96 class VNFObject(object):
98 def __init__(self, name, vnfd):
99 super(VNFObject, self).__init__()
101 self.vnfd_helper = VnfdHelper(vnfd) # fixme: parse this into a structure
104 class GenericVNF(VNFObject):
106 """ Class providing file-like API for generic VNF implementation """
107 def __init__(self, name, vnfd):
108 super(GenericVNF, self).__init__(name, vnfd)
109 # List of statistics we can obtain from this VNF
110 # - ETSI MANO 6.3.1.1 monitoring_parameter
111 self.kpi = self._get_kpi_definition()
112 # Standard dictionary containing params like thread no, buffer size etc
114 self.runs_traffic = False
116 def _get_kpi_definition(self):
117 """ Get list of KPIs defined in VNFD
120 :return: list of KPIs, e.g. ['throughput', 'latency']
122 return self.vnfd_helper.kpi
124 def instantiate(self, scenario_cfg, context_cfg):
125 """ Prepare VNF for operation and start the VNF process/VM
131 raise NotImplementedError()
134 """ Kill all VNF processes
138 raise NotImplementedError()
140 def scale(self, flavor=""):
146 raise NotImplementedError()
148 def collect_kpi(self):
149 """This method should return a dictionary containing the
150 selected KPI at a given point of time.
152 :return: {"kpi": value, "kpi2": value}
154 raise NotImplementedError()
157 class GenericTrafficGen(GenericVNF):
158 """ Class providing file-like API for generic traffic generator """
160 def __init__(self, name, vnfd):
161 super(GenericTrafficGen, self).__init__(name, vnfd)
162 self.runs_traffic = True
163 self.traffic_finished = False
165 def run_traffic(self, traffic_profile):
166 """ Generate traffic on the wire according to the given params.
167 Method is non-blocking, returns immediately when traffic process
168 is running. Mandatory.
170 :param traffic_profile:
173 raise NotImplementedError()
175 def listen_traffic(self, traffic_profile):
176 """ Listen to traffic with the given parameters.
177 Method is non-blocking, returns immediately when traffic process
178 is running. Optional.
180 :param traffic_profile:
185 def verify_traffic(self, traffic_profile):
186 """ Verify captured traffic after it has ended. Optional.
188 :param traffic_profile:
194 """ After this method finishes, all traffic processes should stop. Mandatory.
198 raise NotImplementedError()