Merge "pylint fixes: remove redundant parens, fix comparison order"
[yardstick.git] / yardstick / network_services / traffic_profile / fixed.py
1 # Copyright (c) 2016-2017 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 """ Fixed traffic profile definitions """
15
16 from __future__ import absolute_import
17
18 from yardstick.network_services.traffic_profile.base import TrafficProfile
19 from stl.trex_stl_lib.trex_stl_streams import STLTXCont
20 from stl.trex_stl_lib.trex_stl_client import STLStream
21 from stl.trex_stl_lib.trex_stl_packet_builder_scapy import STLPktBuilder
22 from stl.trex_stl_lib import api as Pkt
23
24
25 class FixedProfile(TrafficProfile):
26     """
27     This profile adds a single stream at the beginning of the traffic session
28     """
29     def __init__(self, tp_config):
30         super(FixedProfile, self).__init__(tp_config)
31         self.first_run = True
32
33     def execute(self, traffic_generator):
34         if self.first_run:
35             for index, ports in enumerate(traffic_generator.my_ports):
36                 ext_intf = \
37                     traffic_generator.vnfd["vdu"][0]["external-interface"]
38                 virtual_interface = ext_intf[index]["virtual-interface"]
39                 src_ip = virtual_interface["local_ip"]
40                 dst_ip = virtual_interface["dst_ip"]
41
42                 traffic_generator.client.add_streams(
43                     self._create_stream(src_ip, dst_ip),
44                     ports=[ports])
45
46             traffic_generator.client.start(ports=traffic_generator.my_ports)
47             self.first_run = False
48
49     def _create_stream(self, src_ip, dst_ip):
50         base_frame = \
51             Pkt.Ether() / Pkt.IP(src=src_ip, dst=dst_ip) / Pkt.UDP(dport=12,
52                                                                    sport=1025)
53
54         frame_size = self.params["traffic_profile"]["frame_size"]
55         pad_size = max(0, frame_size - len(base_frame))
56         frame = base_frame / ("x" * max(0, pad_size))
57
58         frame_rate = self.params["traffic_profile"]["frame_rate"]
59         return STLStream(packet=STLPktBuilder(pkt=frame),
60                          mode=STLTXCont(pps=frame_rate))