aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/network_services/vnf_generic/vnf/base.py
blob: 8ef96b74465fe765904c7e69ba12580f8d058c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright (c) 2016-2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import abc

import logging
import six

from yardstick.network_services.helpers.samplevnf_helper import PortPairs


LOG = logging.getLogger(__name__)


class QueueFileWrapper(object):
    """ Class providing file-like API for talking with SSH connection """

    def __init__(self, q_in, q_out, prompt):
        self.q_in = q_in
        self.q_out = q_out
        self.closed = False
        self.buf = []
        self.bufsize = 20
        self.prompt = prompt

    def read(self, size):
        """ read chunk from input queue """
        if self.q_in.qsize() > 0 and size:
            in_data = self.q_in.get()
            return in_data

    def write(self, chunk):
        """ write chunk to output queue """
        self.buf.append(chunk)
        # flush on prompt or if we exceed bufsize

        size = sum(len(c) for c in self.buf)
        if self.prompt in chunk or size > self.bufsize:
            out = ''.join(self.buf)
            self.buf = []
            self.q_out.put(out)

    def close(self):
        """ close multiprocessing queue """
        pass

    def clear(self):
        """ clear queue """
        while self.q_out.qsize() > 0:
            self.q_out.get()


class VnfdHelper(dict):

    def __init__(self, *args, **kwargs):
        super(VnfdHelper, self).__init__(*args, **kwargs)
        self.port_pairs = PortPairs(self['vdu'][0]['external-interface'])
        # port num is not present until binding so we have to memoize
        self._port_num_map = {}

    @property
    def mgmt_interface(self):
        return self["mgmt-interface"]

    @property
    def vdu(self):
        return self['vdu']

    @property
    def vdu0(self):
        return self.vdu[0]

    @property
    def interfaces(self):
        return self.vdu0['external-interface']

    @property
    def kpi(self):
        return self['benchmark']['kpi']

    def find_virtual_interface(self, **kwargs):
        key, value = next(iter(kwargs.items()))
        for interface in self.interfaces:
            virtual_intf = interface["virtual-interface"]
            if virtual_intf[key] == value:
                return virtual_intf
        raise KeyError()

    def find_interface(self, **kwargs):
        key, value = next(iter(kwargs.items()))
        for interface in self.interfaces:
            if interface[key] == value:
                return interface
        raise KeyError()

    # hide dpdk_port_num key so we can abstract
    def find_interface_by_port(self, port):
        for interface in self.interfaces:
            virtual_intf = interface["virtual-interface"]
            # we have to convert to int to compare
            if int(virtual_intf['dpdk_port_num']) == port:
                return interface
        raise KeyError()

    def port_num(self, port):
        # we need interface name -> DPDK port num (PMD ID) -> LINK ID
        # LINK ID -> PMD ID is governed by the port mask
        """

        :rtype: int
        :type port: str
        """
        if isinstance(port, dict):
            intf = port
        else:
            intf = self.find_interface(name=port)
        return self._port_num_map.setdefault(intf["name"],
                                             int(intf["virtual-interface"]["dpdk_port_num"]))

    def port_nums(self, intfs):
        return [self.port_num(i) for i in intfs]

    def ports_iter(self):
        for port_name in self.port_pairs.all_ports:
            port_num = self.port_num(port_name)
            yield port_name, port_num


@six.add_metaclass(abc.ABCMeta)
class GenericVNF(object):
    """Class providing file-like API for generic VNF implementation

    Currently the only class implementing this interface is
    yardstick/network_services/vnf_generic/vnf/sample_vnf:SampleVNF.
    """

    # centralize network naming convention
    UPLINK = PortPairs.UPLINK
    DOWNLINK = PortPairs.DOWNLINK

    def __init__(self, name, vnfd):
        self.name = name
        self.vnfd_helper = VnfdHelper(vnfd)
        # List of statistics we can obtain from this VNF
        # - ETSI MANO 6.3.1.1 monitoring_parameter
        self.kpi = self.vnfd_helper.kpi
        # Standard dictionary containing params like thread no, buffer size etc
        self.config = {}
        self.runs_traffic = False

    @abc.abstractmethod
    def instantiate(self, scenario_cfg, context_cfg):
        """Prepare VNF for operation and start the VNF process/VM

        :param scenario_cfg: Scenario config
        :param context_cfg: Context config
        :return: True/False
        """

    @abc.abstractmethod
    def wait_for_instantiate(self):
        """Wait for VNF to start

        :return: True/False
        """

    @abc.abstractmethod
    def terminate(self):
        """Kill all VNF processes"""

    @abc.abstractmethod
    def scale(self, flavor=""):
        """rest

        :param flavor: Name of the flavor.
        :return:
        """

    @abc.abstractmethod
    def collect_kpi(self):
        """Return a dict containing the selected KPI at a given point of time

        :return: {"kpi": value, "kpi2": value}
        """

    @abc.abstractmethod
    def start_collect(self):
        """Start KPI collection
        :return: None
        """

    @abc.abstractmethod
    def stop_collect(self):
        """Stop KPI collection
        :return: None
        """


@six.add_metaclass(abc.ABCMeta)
class GenericTrafficGen(GenericVNF):
    """Class providing file-like API for generic traffic generator"""

    def __init__(self, name, vnfd):
        super(GenericTrafficGen, self).__init__(name, vnfd)
        self.runs_traffic = True
        self.traffic_finished = False

    @abc.abstractmethod
    def run_traffic(self, traffic_profile):
        """Generate traffic on the wire according to the given params.

        This method is non-blocking, returns immediately when traffic process
        is running. Mandatory.

        :param traffic_profile:
        :return: True/False
        """

    @abc.abstractmethod
    def terminate(self):
        """After this method finishes, all traffic processes should stop.

        Mandatory.

        :return: True/False
        """

    def listen_traffic(self, traffic_profile):
        """Listen to traffic with the given parameters.

        Method is non-blocking, returns immediately when traffic process
        is running. Optional.

        :param traffic_profile:
        :return: True/False
        """
        pass

    def verify_traffic(self, traffic_profile):
        """Verify captured traffic after it has ended.

        Optional.

        :param traffic_profile:
        :return: dict
        """
        pass

    def wait_for_instantiate(self):
        """Wait for an instance to load.

        Optional.

        :return: True/False
        """
        pass

    def start_collect(self):
        """Start KPI collection.

        Traffic measurements are always collected during injection.

        Optional.

        :return: True/False
        """
        pass

    def stop_collect(self):
        """Stop KPI collection.

        Optional.

        :return: True/False
        """
        pass