update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[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 gi
26 import ipaddress
27 import pytest
28 import re
29 import subprocess
30 import time
31
32 import rift.auto.session
33
34 from gi.repository import (
35 RwTrafgenYang,
36 RwTrafgenDataYang,
37 RwVnfBaseOpdataYang,
38 RwVnfBaseConfigYang,
39 RwTrafgenYang
40 )
41 gi.require_version('RwKeyspec', '1.0')
42 from gi.repository.RwKeyspec import quoted_key
43
44
45 @pytest.fixture(scope='session')
46 def trafgen_vnfr(request, rwvnfr_proxy, session_type):
47 vnfr = "/rw-project:project[rw-project:name='default']/vnfr-catalog/vnfr"
48 vnfrs = rwvnfr_proxy.get(vnfr, list_obj=True)
49 for vnfr in vnfrs.vnfr:
50 if 'trafgen' in vnfr.short_name:
51 return vnfr
52 assert False, "Not found the VNFR with name 'trafgen'"
53
54 @pytest.fixture(scope='session')
55 def trafgen_session(request, trafgen_vnfr, session_type):
56 trafgen_host = trafgen_vnfr.vnf_configuration.config_access.mgmt_ip_address
57 if session_type == 'netconf':
58 tg_session = rift.auto.session.NetconfSession(host=trafgen_host)
59 elif session_type == 'restconf':
60 tg_session = rift.auto.session.RestconfSession(host=trafgen_host)
61
62 tg_session.connect()
63 rift.vcs.vcs.wait_until_system_started(tg_session, 900)
64 return tg_session
65
66 @pytest.fixture(scope='session')
67 def trafgen_ports(request, trafgen_vnfr, session_type):
68 return [cp.name for cp in trafgen_vnfr.connection_point]
69
70 @pytest.fixture(scope='module')
71 def tgdata_proxy(trafgen_session):
72 '''fixture that returns a proxy to RwTrafgenDataYang'''
73 return trafgen_session.proxy(RwTrafgenDataYang)
74
75
76 @pytest.fixture(scope='module')
77 def tgcfg_proxy(trafgen_session):
78 '''fixture that returns a proxy to RwTrafgenYang'''
79 return trafgen_session.proxy(RwTrafgenYang)
80
81
82 @pytest.fixture(scope='module')
83 def vnfdata_proxy(trafgen_session):
84 '''fixture that returns a proxy to RwVnfBaseOpdataYang'''
85 return trafgen_session.proxy(RwVnfBaseOpdataYang)
86
87
88 @pytest.fixture(scope='module')
89 def vnfcfg_proxy(trafgen_session):
90 '''fixture that returns a proxy to RwVnfBaseConfigYang'''
91 return trafgen_session.proxy(RwVnfBaseConfigYang)
92
93
94 def confirm_config(tgcfg_proxy, vnf_name):
95 '''To ensure the configuration is present for the given VNF
96
97 Arguments:
98 vnf_name - vnf name of configuration
99 '''
100 xpath = "/rw-project:project[rw-project:name='default']/vnf-config/vnf[name=%s][instance='0']" % quoted_key(vnf_name)
101 for _ in range(24):
102 tg_config = tgcfg_proxy.get_config(xpath)
103 if tg_config is not None:
104 break
105 time.sleep(10)
106 else:
107 assert False, "Configuration check timeout"
108
109
110 def start_traffic(tgdata_proxy, tgcfg_proxy, port_name):
111 '''Start traffic on the port with the specified name.
112
113 Arguments:
114 port_name - name of port on which to start traffic
115 '''
116 confirm_config(tgcfg_proxy, 'trafgen')
117 rpc_input = RwTrafgenDataYang.RwStartTrafgenTraffic.from_dict({
118 'vnf_name':'trafgen',
119 'vnf_instance':0,
120 'port_name':port_name
121 })
122 rpc_output = RwVnfBaseOpdataYang.YangOutput_RwVnfBaseOpdata_Start_VnfOutput()
123 tgdata_proxy.rpc(rpc_input, rpc_name='start', output_obj=rpc_output)
124
125
126 def stop_traffic(tgdata_proxy, port_name):
127 '''Stop traffic on the port with the specified name.
128
129 Arguments:
130 port_name - name of port on which to stop traffic
131 '''
132 rpc_input = RwTrafgenDataYang.RwStopTrafgenTraffic.from_dict({
133 'vnf_name':'trafgen',
134 'vnf_instance':0,
135 'port_name':port_name
136 })
137 rpc_output = RwVnfBaseOpdataYang.YangOutput_RwVnfBaseOpdata_Stop_VnfOutput()
138 tgdata_proxy.rpc(rpc_input, rpc_name='stop', output_obj=rpc_output)
139
140
141 def wait_for_traffic_started(vnfdata_proxy, vnf_name, port_name, timeout=120, interval=2, threshold=60):
142 '''Wait for traffic to be started on the specified port
143
144 Traffic is determined to be started if the input/output packets on the port
145 increment during the specified interval
146
147 Arguments:
148 port_name - name of the port being monitored
149 timeout - time allowed for traffic to start
150 interval - interval at which the counters should be checked
151 threhsold - values under the threshold treated as 0
152 '''
153 def value_incremented(previous_sample, current_sample):
154 '''Comparison that returns True if the the sampled counter increased
155 beyond the specified threshold during the sampling interval
156 otherwise returns false
157 '''
158 return (int(current_sample) - int(previous_sample)) > threshold
159
160 xpath = "/rw-project:project[rw-project:name='default']/vnf-opdata/vnf[name={}][instance='0']/port-state[portname={}]/counters/{}"
161 vnfdata_proxy.wait_for_interval(xpath.format(quoted_key(vnf_name), quoted_key(port_name), quoted_key('input-packets')),
162 value_incremented, timeout=timeout, interval=interval)
163
164
165 def wait_for_traffic_stopped(vnfdata_proxy, vnf_name, port_name, timeout=60, interval=2, threshold=60):
166 '''Wait for traffic to be stopped on the specified port
167
168 Traffic is determined to be stopped if the input/output packets on the port
169 remain unchanged during the specified interval
170
171 Arguments:
172 port_name - name of the port being monitored
173 timeout - time allowed for traffic to start
174 interval - interval at which the counters should be checked
175 threshold - values under the threshold treated as 0
176 '''
177 def value_unchanged(previous_sample, current_sample):
178 '''Comparison that returns True if the the sampled counter increased
179 less than the specified threshold during the sampling interval
180 otherwise returns False
181 '''
182 return (int(current_sample) - int(previous_sample)) < threshold
183
184 xpath = "/rw-project:project[rw-project:name='default']/vnf-opdata/vnf[name={}][instance='0']/port-state[portname={}]/counters/{}"
185 vnfdata_proxy.wait_for_interval(xpath.format(quoted_key(vnf_name), quoted_key(port_name), quoted_key('input-packets')), value_unchanged, timeout=timeout, interval=interval)
186
187 @pytest.mark.depends('multivmvnf')
188 @pytest.mark.incremental
189 class TestMVVSlbDataFlow:
190
191 def test_start_stop_traffic(self, vnfdata_proxy, tgdata_proxy, tgcfg_proxy, trafgen_ports):
192 ''' This test verfies that traffic can be stopped and started on
193 all trafgen ports.
194
195 Arguments:
196 vnfdata_proxy - proxy to retrieve vnf operational data
197 tgdata_proxy - proxy to retrieve trafgen operational data
198 tgcfg_proxy - proxy to retrieve trafgen configuration
199 trafgen_ports - list of port names on which traffic can be started
200 '''
201 time.sleep(300)
202 for port in trafgen_ports:
203 start_traffic(tgdata_proxy, tgcfg_proxy, port)
204 wait_for_traffic_started(vnfdata_proxy, 'trafgen', port)
205 stop_traffic(tgdata_proxy, port)
206 wait_for_traffic_stopped(vnfdata_proxy, 'trafgen', port)
207
208
209 def test_start_traffic(self, vnfdata_proxy, tgdata_proxy, tgcfg_proxy, trafgen_ports):
210 ''' This test starts traffic on all trafgen ports in preperation for
211 subsequent tests
212
213 Arguments:
214 vnfdata_proxy - proxy to retrieve vnf operational data
215 tgdata_proxy - proxy to retrieve trafgen operational data
216 tgcfg_proxy - proxy to retrieve trafgen configuration
217 trafgen_ports - list of port names on which traffic can be started
218 '''
219 for port in trafgen_ports:
220 start_traffic(tgdata_proxy, tgcfg_proxy, port)
221 wait_for_traffic_started(vnfdata_proxy, 'trafgen', port)