Merge "[docs] Remove the VTC chapter in the userguide"
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / tg_pktgen.py
1 # Copyright (c) 2018 Intel Corporation
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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
15 import logging
16 import multiprocessing
17 import time
18 import uuid
19
20 from yardstick.common import constants
21 from yardstick.common import exceptions
22 from yardstick.common import utils
23 from yardstick.network_services.vnf_generic.vnf import base as vnf_base
24
25
26 LOG = logging.getLogger(__name__)
27
28
29 class PktgenTrafficGen(vnf_base.GenericTrafficGen,
30                        vnf_base.GenericVNFEndpoint):
31     """DPDK Pktgen traffic generator
32
33     Website: http://pktgen-dpdk.readthedocs.io/en/latest/index.html
34     """
35
36     TIMEOUT = 30
37
38     def __init__(self, name, vnfd, task_id):
39         vnf_base.GenericTrafficGen.__init__(self, name, vnfd, task_id)
40         self.queue = multiprocessing.Queue()
41         self._id = uuid.uuid1().int
42         self._mq_producer = self._setup_mq_producer(self._id)
43         vnf_base.GenericVNFEndpoint.__init__(self, self._id, [task_id],
44                                              self.queue)
45         self._consumer = vnf_base.GenericVNFConsumer([task_id], self)
46         self._consumer.start_rpc_server()
47         self._traffic_profile = None
48         self._node_ip = vnfd['mgmt-interface'].get('ip')
49         self._lua_node_port = self._get_lua_node_port(
50             vnfd['mgmt-interface'].get('service_ports', []))
51         self._rate = 1
52
53     def instantiate(self, scenario_cfg, context_cfg):  # pragma: no cover
54         pass
55
56     def run_traffic(self, traffic_profile):
57         self._traffic_profile = traffic_profile
58         self._traffic_profile.init(self._node_ip, self._lua_node_port)
59         utils.wait_until_true(self._is_running, timeout=self.TIMEOUT,
60                               sleep=2)
61
62     def terminate(self):  # pragma: no cover
63         pass
64
65     def collect_kpi(self):  # pragma: no cover
66         pass
67
68     def scale(self, flavor=''):  # pragma: no cover
69         pass
70
71     def wait_for_instantiate(self):  # pragma: no cover
72         pass
73
74     def runner_method_start_iteration(self, ctxt, **kwargs):
75         # pragma: no cover
76         LOG.debug('Start method')
77         # NOTE(ralonsoh): 'rate' should be modified between iterations. The
78         # current implementation is just for testing.
79         self._rate += 1
80         self._traffic_profile.start()
81         self._traffic_profile.rate(self._rate)
82         time.sleep(4)
83         self._traffic_profile.stop()
84         self._mq_producer.tg_method_iteration(1, 1, {})
85
86     def runner_method_stop_iteration(self, ctxt, **kwargs):  # pragma: no cover
87         # pragma: no cover
88         LOG.debug('Stop method')
89
90     @staticmethod
91     def _get_lua_node_port(service_ports):
92         for port in (port for port in service_ports if
93                      int(port['port']) == constants.LUA_PORT):
94             return int(port['node_port'])
95         # NOTE(ralonsoh): in case LUA port is not present, an exception should
96         # be raised.
97
98     def _is_running(self):
99         try:
100             self._traffic_profile.help()
101             return True
102         except exceptions.PktgenActionError:
103             return False