197e95c5e9fbce0cfad0c26b0228089c5e9fac19
[osm/SO.git] / rwlaunchpad / ra / pytest / multivm_vnf / test_trafgen_data.py
1 #!/usr/bin/env python
2 """
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 @file test_trafgen_data.py
20 @author Karun Ganesharatnam (karun.ganesharatnam@riftio.com)
21 @date 03/16/2016
22 @brief Scriptable load-balancer test with multi-vm VNFs
23 """
24
25 import ipaddress
26 import pytest
27 import re
28 import subprocess
29 import time
30
31 import rift.auto.session
32
33 from gi.repository import (
34 RwTrafgenYang,
35 RwTrafgenDataYang,
36 RwVnfBaseOpdataYang,
37 RwVnfBaseConfigYang,
38 RwTrafgenYang
39 )
40
41
42 @pytest.fixture(scope='session')
43 def trafgen_vnfr(request, rwvnfr_proxy, session_type):
44 vnfr = "/vnfr-catalog/vnfr"
45 vnfrs = rwvnfr_proxy.get(vnfr, list_obj=True)
46 for vnfr in vnfrs.vnfr:
47 if 'trafgen' in vnfr.short_name:
48 return vnfr
49 assert False, "Not found the VNFR with name 'trafgen'"
50
51 @pytest.fixture(scope='session')
52 def trafgen_session(request, trafgen_vnfr, session_type):
53 trafgen_host = trafgen_vnfr.vnf_configuration.config_access.mgmt_ip_address
54 if session_type == 'netconf':
55 tg_session = rift.auto.session.NetconfSession(host=trafgen_host)
56 elif session_type == 'restconf':
57 tg_session = rift.auto.session.RestconfSession(host=trafgen_host)
58
59 tg_session.connect()
60 rift.vcs.vcs.wait_until_system_started(tg_session, 900)
61 return tg_session
62
63 @pytest.fixture(scope='session')
64 def trafgen_ports(request, trafgen_vnfr, session_type):
65 return [cp.name for cp in trafgen_vnfr.connection_point]
66
67 @pytest.fixture(scope='module')
68 def tgdata_proxy(trafgen_session):
69 '''fixture that returns a proxy to RwTrafgenDataYang'''
70 return trafgen_session.proxy(RwTrafgenDataYang)
71
72
73 @pytest.fixture(scope='module')
74 def tgcfg_proxy(trafgen_session):
75 '''fixture that returns a proxy to RwTrafgenYang'''
76 return trafgen_session.proxy(RwTrafgenYang)
77
78
79 @pytest.fixture(scope='module')
80 def vnfdata_proxy(trafgen_session):
81 '''fixture that returns a proxy to RwVnfBaseOpdataYang'''
82 return trafgen_session.proxy(RwVnfBaseOpdataYang)
83
84
85 @pytest.fixture(scope='module')
86 def vnfcfg_proxy(trafgen_session):
87 '''fixture that returns a proxy to RwVnfBaseConfigYang'''
88 return trafgen_session.proxy(RwVnfBaseConfigYang)
89
90
91 def confirm_config(tgcfg_proxy, vnf_name):
92 '''To ensure the configuration is present for the given VNF
93
94 Arguments:
95 vnf_name - vnf name of configuration
96 '''
97 xpath = "/vnf-config/vnf[name='%s'][instance='0']" % vnf_name
98 for _ in range(24):
99 tg_config = tgcfg_proxy.get_config(xpath)
100 if tg_config is not None:
101 break
102 time.sleep(10)
103 else:
104 assert False, "Configuration check timeout"
105
106
107 def start_traffic(tgdata_proxy, tgcfg_proxy, port_name):
108 '''Start traffic on the port with the specified name.
109
110 Arguments:
111 port_name - name of port on which to start traffic
112 '''
113 confirm_config(tgcfg_proxy, 'trafgen')
114 rpc_input = RwTrafgenDataYang.RwStartTrafgenTraffic.from_dict({
115 'vnf_name':'trafgen',
116 'vnf_instance':0,
117 'port_name':port_name
118 })
119 rpc_output = RwVnfBaseOpdataYang.YangOutput_RwVnfBaseOpdata_Start_VnfOutput()
120 tgdata_proxy.rpc(rpc_input, rpc_name='start', output_obj=rpc_output)
121
122
123 def stop_traffic(tgdata_proxy, port_name):
124 '''Stop traffic on the port with the specified name.
125
126 Arguments:
127 port_name - name of port on which to stop traffic
128 '''
129 rpc_input = RwTrafgenDataYang.RwStopTrafgenTraffic.from_dict({
130 'vnf_name':'trafgen',
131 'vnf_instance':0,
132 'port_name':port_name
133 })
134 rpc_output = RwVnfBaseOpdataYang.YangOutput_RwVnfBaseOpdata_Stop_VnfOutput()
135 tgdata_proxy.rpc(rpc_input, rpc_name='stop', output_obj=rpc_output)
136
137
138 def wait_for_traffic_started(vnfdata_proxy, vnf_name, port_name, timeout=120, interval=2, threshold=60):
139 '''Wait for traffic to be started on the specified port
140
141 Traffic is determined to be started if the input/output packets on the port
142 increment during the specified interval
143
144 Arguments:
145 port_name - name of the port being monitored
146 timeout - time allowed for traffic to start
147 interval - interval at which the counters should be checked
148 threhsold - values under the threshold treated as 0
149 '''
150 def value_incremented(previous_sample, current_sample):
151 '''Comparison that returns True if the the sampled counter increased
152 beyond the specified threshold during the sampling interval
153 otherwise returns false
154 '''
155 return (int(current_sample) - int(previous_sample)) > threshold
156
157 xpath = "/vnf-opdata/vnf[name='{}'][instance='0']/port-state[portname='{}']/counters/{}"
158 vnfdata_proxy.wait_for_interval(xpath.format(vnf_name, port_name, 'input-packets'),
159 value_incremented, timeout=timeout, interval=interval)
160
161
162 def wait_for_traffic_stopped(vnfdata_proxy, vnf_name, port_name, timeout=60, interval=2, threshold=60):
163 '''Wait for traffic to be stopped on the specified port
164
165 Traffic is determined to be stopped if the input/output packets on the port
166 remain unchanged during the specified interval
167
168 Arguments:
169 port_name - name of the port being monitored
170 timeout - time allowed for traffic to start
171 interval - interval at which the counters should be checked
172 threshold - values under the threshold treated as 0
173 '''
174 def value_unchanged(previous_sample, current_sample):
175 '''Comparison that returns True if the the sampled counter increased
176 less than the specified threshold during the sampling interval
177 otherwise returns False
178 '''
179 return (int(current_sample) - int(previous_sample)) < threshold
180
181 xpath = "/vnf-opdata/vnf[name='{}'][instance='0']/port-state[portname='{}']/counters/{}"
182 vnfdata_proxy.wait_for_interval(xpath.format(vnf_name, port_name, 'input-packets'), value_unchanged, timeout=timeout, interval=interval)
183
184 @pytest.mark.depends('multivmvnf')
185 @pytest.mark.incremental
186 class TestMVVSlbDataFlow:
187
188 def test_start_stop_traffic(self, vnfdata_proxy, tgdata_proxy, tgcfg_proxy, trafgen_ports):
189 ''' This test verfies that traffic can be stopped and started on
190 all trafgen ports.
191
192 Arguments:
193 vnfdata_proxy - proxy to retrieve vnf operational data
194 tgdata_proxy - proxy to retrieve trafgen operational data
195 tgcfg_proxy - proxy to retrieve trafgen configuration
196 trafgen_ports - list of port names on which traffic can be started
197 '''
198 time.sleep(300)
199 for port in trafgen_ports:
200 start_traffic(tgdata_proxy, tgcfg_proxy, port)
201 wait_for_traffic_started(vnfdata_proxy, 'trafgen', port)
202 stop_traffic(tgdata_proxy, port)
203 wait_for_traffic_stopped(vnfdata_proxy, 'trafgen', port)
204
205
206 def test_start_traffic(self, vnfdata_proxy, tgdata_proxy, tgcfg_proxy, trafgen_ports):
207 ''' This test starts traffic on all trafgen ports in preperation for
208 subsequent tests
209
210 Arguments:
211 vnfdata_proxy - proxy to retrieve vnf operational data
212 tgdata_proxy - proxy to retrieve trafgen operational data
213 tgcfg_proxy - proxy to retrieve trafgen configuration
214 trafgen_ports - list of port names on which traffic can be started
215 '''
216 for port in trafgen_ports:
217 start_traffic(tgdata_proxy, tgcfg_proxy, port)
218 wait_for_traffic_started(vnfdata_proxy, 'trafgen', port)