3 # Copyright (c) 2016-2017 Intel Corporation
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 # Unittest for yardstick.network_services.vnf_generic.vnfdgen
20 from __future__ import absolute_import
22 from six.moves import range
24 from yardstick.network_services.vnf_generic import vnfdgen
26 TREX_VNFD_TEMPLATE = """
29 - id: TrexTrafficGen # ISB class mapping
32 description: TRex stateless traffic generator for RFC2544
34 vdu-id: trexgen-baremetal
35 user: {{user}} # Value filled by vnfdgen
36 password: {{password}} # Value filled by vnfdgen
37 ip: {{ip}} # Value filled by vnfdgen
44 - id: trexgen-baremetal
45 name: trexgen-baremetal
46 description: TRex stateless traffic generator for RFC2544
51 vpci: '{{ interfaces.xe0.vpci}}'
52 local_ip: '{{ interfaces.xe0.local_ip }}'
53 dst_ip: '{{ interfaces.xe0.dst_ip }}'
54 local_mac: '{{ interfaces.xe0.local_mac }}'
55 dst_mac: '{{ interfaces.xe0.dst_mac }}'
57 vnfd-connection-point-ref: xe0
61 vpci: '{{ interfaces.xe1.vpci }}'
62 local_ip: '{{ interfaces.xe1.local_ip }}'
63 dst_ip: '{{ interfaces.xe1.dst_ip }}'
64 local_mac: '{{ interfaces.xe1.local_mac }}'
65 dst_mac: '{{ interfaces.xe1.dst_mac }}'
67 vnfd-connection-point-ref: xe1
75 - tx_throughput_pc_linerate
76 - rx_throughput_pc_linerate
82 COMPLETE_TREX_VNFD = \
91 'tx_throughput_pc_linerate',
92 'rx_throughput_pc_linerate',
96 'connection-point': [{'name': 'xe0',
100 'description': 'TRex stateless traffic generator for RFC2544',
101 'id': 'TrexTrafficGen',
102 'mgmt-interface': {'ip': '1.1.1.1',
105 'vdu-id': 'trexgen-baremetal'},
107 'short-name': 'trexgen',
108 'vdu': [{'description': 'TRex stateless traffic generator for RFC2544',
109 'external-interface':
111 'virtual-interface': {'bandwidth': '10 Gbps',
113 'dst_mac': '00:01:02:03:04:05',
114 'local_ip': '1.1.1.2',
115 'local_mac': '00:01:02:03:05:05',
116 'type': 'PCI-PASSTHROUGH',
117 'vpci': '0000:00:10.2'},
118 'vnfd-connection-point-ref': 'xe0'},
120 'virtual-interface': {'bandwidth': '10 Gbps',
122 'dst_mac': '00:01:02:03:04:06',
123 'local_ip': '2.1.1.2',
124 'local_mac': '00:01:02:03:05:06',
125 'type': 'PCI-PASSTHROUGH',
126 'vpci': '0000:00:10.1'},
127 'vnfd-connection-point-ref': 'xe1'}],
128 'id': 'trexgen-baremetal',
129 'name': 'trexgen-baremetal'}]}]}}
131 NODE_CFG = {'ip': '1.1.1.1',
134 'role': 'TrafficGen',
136 'interfaces': {'xe0': {'dpdk_port_num': 1,
138 'dst_mac': '00:01:02:03:04:05',
139 'local_ip': '1.1.1.2',
140 'local_mac': '00:01:02:03:05:05',
141 'vpci': '0000:00:10.2'},
142 'xe1': {'dpdk_port_num': 0,
144 'dst_mac': '00:01:02:03:04:06',
145 'local_ip': '2.1.1.2',
146 'local_mac': '00:01:02:03:05:06',
147 'vpci': '0000:00:10.1'}}}
150 TRAFFIC_PROFILE_TPL = """
155 64B: "{{ get(imix, 'private.imix_small', 10) }}"
156 128B: "{{ get(imix, 'private.imix_128B', 10) }}"
157 256B: "{{ get(imix, 'private.imix_256B', 10) }}"
158 373B: "{{ get(imix, 'private.imix_373B', 10) }}"
159 570B: "{{get(imix, 'private.imix_570B', 10) }}"
160 1400B: "{{get(imix, 'private.imix_1400B', 10) }}"
161 1518B: "{{get(imix, 'private.imix_1500B', 40) }}"
165 "private": [{"ipv4": {"outer_l2":
166 {"framesize": {"64B": '10', "128B": '10',
167 "256B": '10', "373B": '10',
168 "570B": '10', "1400B": '10',
172 class TestVnfdGen(unittest.TestCase):
173 """ Class to verify VNFS testcases """
175 def test_generate_vnfd(self):
176 """ Function to verify vnfd generation based on template """
178 generated_vnfd = vnfdgen.generate_vnfd(TREX_VNFD_TEMPLATE, NODE_CFG)
179 self.assertDictEqual(COMPLETE_TREX_VNFD, generated_vnfd)
181 def test_generate_tp_no_vars(self):
182 """ Function to verify traffic profile generation without imix """
185 generated_tp = vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL, {"imix": {}})
186 self.assertDictEqual(TRAFFIC_PROFILE, generated_tp)
188 def test_dict_flatten_empty_dict(self):
189 self.assertEqual(vnfdgen.dict_key_flatten({}), {})
191 def test_dict_flatten_int(self):
193 self.assertEqual(vnfdgen.dict_key_flatten(d), d)
195 def test_dict_flatten_str(self):
196 d = {'a': "1", 'b': '2'}
197 self.assertEqual(vnfdgen.dict_key_flatten(d), d)
199 def test_dict_flatten_list(self):
200 d = {'a': 1, 'b': list(range(2))}
201 self.assertEqual(vnfdgen.dict_key_flatten(d),
202 {'a': 1, 'b0': 0, 'b1': 1})
204 def test_dict_flatten_dict(self):
205 d = {'a': 1, 'b': {x: x for x in list(range(2))}}
206 self.assertEqual(vnfdgen.dict_key_flatten(d),
207 {'a': 1, 'b.0': 0, 'b.1': 1})
209 def test_generate_tp_single_var(self):
210 """ Function to verify traffic profile generation with imix """
213 vnfdgen.generate_vnfd(TRAFFIC_PROFILE_TPL,
214 {"imix": {"private": {"imix_small": '20'}}})
216 tp2 = dict(TRAFFIC_PROFILE)
217 tp2["private"][0]["ipv4"]["outer_l2"]["framesize"]["64B"] = '20'
218 self.assertDictEqual(tp2, generated_tp)