Upgrade RF syntax for v3.2 compatibility
[integration/test.git] / csit / libraries / TsdrUtils.robot
1 *** Settings ***
2 Library           RequestsLibrary
3 Library           XML
4 Library           OperatingSystem
5 Library           String
6 Library           Collections
7 Library           DateTime
8 Resource          Utils.robot
9 Variables         ../variables/Variables.py
10 Library           json
11 Library           HttpLibrary.HTTP
12 Library           Process
13
14 *** Variables ***
15 ${HBASE_CLIENT}    /tmp/Hbase/hbase-0.94.27/bin
16 ${CASSANDRA_CLIENT}    /tmp/cassandra/apache-cassandra-2.1.16/bin
17 ${final}          ${EMPTY}
18 ${prompt_timeout}    ${EMPTY}
19 ${CASSANDRA_DB_PATH}    /tmp/cassandra/apache-cassandra-2.1.16/
20 ${metric_path}    metricpath
21 ${metric_val}     metricval
22 ${metric_log}     metriclog
23 ${temp_metric_val}    temp_metric_val
24 ${NETFLOW_PORT}    2055
25 ${KARAF_PATH}     ${WORKSPACE}/${BUNDLEFOLDER}
26 ${TSDR_PATH}      ${KARAF_PATH}/tsdr
27 ${PURGE_PATH}     ${KARAF_PATH}/etc/tsdr.data.purge.cfg
28 ${SNMP_PATH}      ${KARAF_PATH}/etc/tsdr.snmp.cfg
29 ${SNMP_COMMUNITY}    mib2dev\/if-mib
30 &{HEADERS_QUERY}    Content-Type=application/json    Content-Type=application/json
31 &{OPER_STATUS}    up=1    down=2    testing=3    unknown=4    dormant=5    notPresent=6    lowerLayerDown=7
32 &{syslog_facility}    kern=0
33 ${MESSAGE}        Oct 29 18:10:31: ODL: %STKUNIT0-M:CP %IFMGR-5-ASTATE_UP: Changed interface Admin state to up: Te 0/0
34 ${MESSAGE_PATTERN}    Changed interface
35
36 *** Keywords ***
37 Start Tsdr Suite
38     [Arguments]    ${switch}=ovsk    ${switch_count}=3
39     [Documentation]    TSDR specific setup/cleanup work that can be done safely before any system.
40     ...    is run.
41     Clean Mininet System
42     ${mininet_conn_id1}=    Open Connection    ${TOOLS_SYSTEM_IP}    prompt=${DEFAULT_LINUX_PROMPT}    timeout=120s
43     Set Suite Variable    ${mininet_conn_id1}
44     Login With Public Key    ${TOOLS_SYSTEM_USER}    ${USER_HOME}/.ssh/${SSH_KEY}    any
45     Execute Command    sudo ovs-vsctl set-manager ptcp:6644
46     ${start}=    Set Variable    sudo mn --controller=remote,ip=${ODL_SYSTEM_IP} --topo=linear,${switch_count} --switch ${switch},protocols=OpenFlow13
47     Log    ${start}
48     Write    ${start}
49     Read Until    mininet>
50
51 Configure Netflow
52     [Arguments]    ${user}=${TOOLS_SYSTEM_USER}
53     [Documentation]    Configure Netflow
54     ${output}=    Run Command On Controller    ${TOOLS_SYSTEM_IP}    sudo ovs-vsctl -- set Bridge s1 netflow=@nf -- --id=@nf create NetFlow target=\\"${ODL_SYSTEM_IP}:${NETFLOW_PORT}\\" active-timeout=10    ${user}
55
56 Ping All Hosts
57     [Arguments]    ${switch}=ovsk
58     [Documentation]    Ping between all hosts in mininet topology.
59     Switch Connection    ${mininet_conn_id1}
60     Write    pingall
61     Read Until    mininet>
62
63 Ping Pair Hosts
64     [Arguments]    ${host1}    ${host2}
65     [Documentation]    Ping between ${host1} and ${host2}
66     Switch Connection    ${mininet_conn_id1}
67     Write    pingpair ${host1} ${host2}
68     Read Until    mininet>
69
70 Ping Pair Hosts Hbase
71     [Arguments]    ${pattern}
72     [Documentation]    Ping between h1 and h2 and check Hbase
73     Ping Pair Hosts    h1    h2
74     ${query_output}=    Query the Data from HBaseClient    count 'NETFLOW'
75     Should Match Regexp    ${query_output}    ${pattern}
76
77 Ping Pair Hosts Cassandra
78     [Arguments]    ${pattern}
79     [Documentation]    Ping between h1 and h2 and check Cassandra
80     Ping Pair Hosts    h1    h2
81     ${query_output}=    Count Cassandra rows    select count(*) from tsdr.metriclog;
82     Should Match Regexp    ${query_output}    ${pattern}
83
84 Ping Pair Hosts HSQLDB
85     [Arguments]    ${pattern}
86     [Documentation]    Iperf between h1 and h2 and check Cassandra
87     Ping Pair Hosts    h1    h2
88     ${query_output}=    Issue Command On Karaf Console    tsdr:list NETFLOW | wc -l
89     Should Match Regexp    ${query_output}    ${pattern}
90
91 Stop Tsdr Suite
92     [Documentation]    Cleanup/Shutdown work that should be done at the completion of all
93     ...    tests
94     Switch Connection    ${mininet_conn_id1}
95     Read
96     Write    exit
97     Read Until    ${DEFAULT_LINUX_PROMPT}
98     Close Connection
99
100 Purge Data
101     [Arguments]    ${HOST}=127.0.0.1    ${purge_enabled}=true    ${purge_time}=00:00:00    ${purge_interval}=1400    ${retention}=0
102     [Documentation]    Write Purge file and copy it to directory.127.0.0.1 refers local controller
103     Create File    purge.cfg    \#TSDR Project Configuration file
104     Append To File    purge.cfg    \n
105     Append To File    purge.cfg    host=${HOST}
106     Append To File    purge.cfg    \n
107     Append To File    purge.cfg    data_purge_enabled=${purge_enabled}
108     Append To File    purge.cfg    \n
109     Append To File    purge.cfg    data_purge_time=${purge_time}
110     Append To File    purge.cfg    \n
111     Append To File    purge.cfg    data_purge_interval_in_minutes=${purge_interval}
112     Append To File    purge.cfg    \n
113     Append To File    purge.cfg    retention_time_in_hours=${retention}
114     Append To File    purge.cfg    \n
115     Move File    purge.cfg    ${PURGE_PATH}
116
117 Initialize the HBase for TSDR
118     [Documentation]    Install and initialize the tsdr tables on HBase Server
119     ${hbase_server}=    Run Command On Remote System    ${ODL_SYSTEM_IP}    export JAVA_HOME=/usr && ${HBASE_CLIENT}/start-hbase.sh    ${TOOLS_SYSTEM_USER}    ${prompt_timeout}=120
120     Log    ${hbase_server}
121     ${hbase_process}=    Run Command On Remote System    ${ODL_SYSTEM_IP}    ps -ef | grep HMaster
122     Log    ${hbase_process}
123
124 Stop the HBase Server
125     [Documentation]    Stop the HBase server
126     ${hbase_server}=    Run Command On Remote System    ${ODL_SYSTEM_IP}    export JAVA_HOME=/usr && ${HBASE_CLIENT}/stop-hbase.sh    ${TOOLS_SYSTEM_USER}    ${prompt_timeout}=90
127     Log    ${hbase_server}
128
129 Configure the Queue on Switch
130     [Arguments]    ${queue_interface}    ${user}=${TOOLS_SYSTEM_USER}
131     [Documentation]    Configure the 2 queues on specified openvswitch interface
132     Log    Configure the queue on ${queue_interface}
133     ${output}=    Run Command On Remote System    ${TOOLS_SYSTEM_IP}    sudo ovs-vsctl set port ${queue_interface} qos=@newqos -- --id=@newqos create qos type=linux-htb other-config:max-rate=200000000 queues=0=@q0,1=@q1,2=@q2 -- --id=@q0 create queue other-config:min-rate=100000 other-config:max-rate=200000 -- --id=@q1 create queue other-config:min-rate=10001 other-config:max-rate=300000 -- --id=@q2 create queue other-config:min-rate=300001 other-config:max-rate=200000000
134     Log    ${output}
135
136 Query the Data from HBaseClient
137     [Arguments]    ${query}    ${remote}=${ODL_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${prompt_timeout}=120s
138     [Documentation]    Execute the HBase Query and return the result
139     Log    Attempting to execute ${query} on ${remote} via HbaseClient
140     ${conn_id}=    Open Connection    ${remote}    prompt=${DEFAULT_LINUX_PROMPT}    timeout=${prompt_timeout}
141     Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    any
142     Write    export JAVA_HOME=/usr
143     Write    ${HBASE_CLIENT}/hbase shell
144     Read Until    hbase(main):001:0>
145     Write    ${query}
146     ${output}=    Read Until    hbase(main):
147     Write    exit
148     LOG    ${output}
149     Comment    ${output}=    Read Until    ${DEFAULT_LINUX_PROMPT}
150     Close Connection
151     [Return]    ${output}
152
153 Verify the Metric is Collected?
154     [Arguments]    ${tsdr_cmd}    ${metric}    ${remote}=${ODL_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${prompt_timeout}=120s
155     [Documentation]    Verify the ${tsdr_cmd} output contains ${metric}
156     ${output}=    Issue Command On Karaf Console    ${tsdr_cmd}    ${remote}    ${KARAF_SHELL_PORT}    ${prompt_timeout}
157     Should Contain    ${output}    ${metric}
158
159 Prepare HBase Filter
160     [Arguments]    ${left_str}    ${right_str}    ${connector}
161     [Documentation]    Prepare the Hbase Filter from Tsdr List output
162     ${left_str}=    Remove Space on String    ${left_str}
163     ${right_str}=    Remove Space on String    ${right_str}    1
164     ${x}=    Run Keyword If    '${left_str}' == 'MetricID'    Catenate    ${SPACE}    _
165     ...    ELSE IF    '${left_str}' == 'ObjectKeys'    Catenate    ${right_str}    _
166     ...    ELSE IF    '${left_str}' == 'TimeStamp'    Get Epoch Time    ${right_str}
167     ...    ELSE    Catenate    ${SPACE}
168     [Return]    ${x}
169
170 Create the Hbase table row
171     [Arguments]    ${tsdr_line}    ${metrics}
172     [Documentation]    Create the Hbase table row from tsdr:list
173     @{words}=    Split String    ${tsdr_line}    |
174     FOR    ${li}    IN    @{words}
175         ${key}=    Fetch From Left    ${li}    =
176         ${value}=    Fetch From Right    ${li}    =
177         ${each_value}=    Prepare HBase Filter    ${key}    ${value}    _
178         ${final}=    Concatenate the String    ${final}    ${each_value}
179     END
180     ${query}=    Concatenate the String    ${metrics}    ${final}
181     ${query}=    Remove Space on String    ${query}
182     [Return]    ${query}
183
184 Initialize Cassandra Tables
185     [Arguments]    ${remote}=${ODL_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${prompt_timeout}=120s    ${key_table}=metricpath    ${val_table}=metricval
186     [Documentation]    Truncate Existing tables in Cassandra to Start it fresh.
187     Log    Attempting to truncate tables in Cassandra
188     Run Command On Remote System    ${ODL_SYSTEM_IP}    sudo rm -rf ${CASSANDRA_DB_PATH}${key_table}
189     Run Command On Remote System    ${ODL_SYSTEM_IP}    sudo rm -rf ${CASSANDRA_DB_PATH}${val_table}
190     ${conn_id}=    Open Connection    ${remote}    prompt=${DEFAULT_LINUX_PROMPT}    timeout=${prompt_timeout}
191     Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    any
192     Write    export JAVA_HOME=/usr
193     Write    ${CASSANDRA_CLIENT}/cqlsh
194     Read Until    cqlsh>
195     Write    TRUNCATE tsdr.${key_table} ;
196     Read Until    cqlsh>
197     Write    TRUNCATE tsdr.${val_table} ;
198     Read Until    cqlsh>
199     Write    exit
200     Close Connection
201
202 Initialize Cassandra Tables Metricval
203     [Arguments]    ${remote}=${ODL_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${prompt_timeout}=120s    ${key_table}=metricpath    ${val_table}=metricval
204     [Documentation]    Truncate Existing tables in Cassandra to Start it fresh
205     Log    Attempting to truncate tables in Cassandra
206     Run Command On Remote System    ${ODL_SYSTEM_IP}    sudo rm -rf ${CASSANDRA_DB_PATH}${key_table}
207     Run Command On Remote System    ${ODL_SYSTEM_IP}    sudo rm -rf ${CASSANDRA_DB_PATH}${val_table}
208     ${conn_id}=    Open Connection    ${remote}    prompt=${DEFAULT_LINUX_PROMPT}    timeout=${prompt_timeout}
209     Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    any
210     Write    export JAVA_HOME=/usr
211     Write    ${CASSANDRA_CLIENT}/cqlsh
212     Read Until    cqlsh>
213     Write    TRUNCATE tsdr.${val_table} ;
214     Read Until    cqlsh>
215     Write    exit
216     Close Connection
217
218 Query Metrics on H2 Datastore
219     [Arguments]    ${category}    ${attribute}    ${nodeid}=openflow:1
220     [Documentation]    Generate the JDBC query for H2 Datastore
221     ${h2_query}=    Concatenate the String    jdbc:query metric "select * from Metric where MetricCategory=    '${category}' and
222     ${h2_query}=    Concatenate the String    ${h2_query}    MetricName = '${attribute}' and NODEID = '${nodeid}' order by ID desc limit 5"
223     ${output}=    Issue Command On Karaf Console    ${h2_query}    ${ODL_SYSTEM_IP}    ${KARAF_SHELL_PORT}    30
224     [Return]    ${output}
225
226 Generate HBase Query
227     [Arguments]    ${table}    ${filter}    ${metric}
228     [Documentation]    Scan the Hbase Table with Filter
229     ${hbase_query}=    Concatenate the String    scan    '${table}'
230     ${hbase_query}=    Concatenate the String    ${hbase_query}    ,{ FILTER =>\"(RowFilter(=,
231     ${hbase_query}=    Concatenate the String    ${hbase_query}    'regexstring:${filter}*\')) AND (RowFilter(=,
232     ${hbase_query}=    Concatenate the String    ${hbase_query}    'regexstring:${metric}*\'))\",LIMIT=>10}
233     [Return]    ${hbase_query}
234
235 Get Metrics Value
236     [Arguments]    ${tsdr_line}
237     [Documentation]    Get Metric Value from tsdr:list
238     ${value}=    Fetch From Right    ${tsdr_line}    |
239     ${value}=    Replace String    ${value}    MetricValue    value
240     ${value}=    Replace String    ${value}    [m    ${EMPTY}
241     ${value}=    Replace String    ${value}    =    \=
242     ${value}=    Remove Space on String    ${value}
243     ${value}=    Convert to String    ${value}
244     [Return]    ${value}
245
246 Verify the Metrics Attributes on Hbase Client
247     [Arguments]    ${attribute}    ${rowfilter}    ${table}=PortStats
248     [Documentation]    Verification on Metrics attributes on Hbase Client
249     ${query}=    Generate HBase Query    ${table}    ${rowfilter}    ${attribute}
250     ${out}=    Query the Data from HBaseClient    ${query}
251     Should Match Regexp    ${out}    (?mui)value
252
253 Verify the Metrics Attributes on Cassandra Client
254     [Arguments]    ${pattern}
255     [Documentation]    Verification on Metrics attributes on Cassandra Client
256     @{metric_row}=    Find Metricval Keys    ${pattern}    metricpath
257     ${keya}=    Get From List    ${metric_row}    1
258     ${keyb}=    Get From List    ${metric_row}    2
259     ${keya_bool}=    Evaluate    ${keya} < 0
260     ${keya}=    Run Keyword If    '${keya_bool}' == 'True'    Catenate    SEPARATOR=    \\    ${keya}
261     ...    ELSE    Catenate    ${keya}
262     ${metricval}=    Create Temporary Key Info    ${keya} ${keyb}
263     @{lines}=    Split to lines    ${metricval}
264     ${mv_len}=    Get Length    ${lines}
265     ${mv_len}=    Evaluate    ${mv_len} - 1
266     ${found_line}=    Get From List    ${lines}    ${mv_len}
267     @{split_line}=    Split String    ${found_line}    ${SPACE}
268     ${metric_count}=    Get From List    ${split_line}    3
269     [Return]    ${metric_count}
270
271 Form Portstats Query Pattern
272     [Arguments]    ${metric}    ${node}    ${port}    ${attribute}
273     [Documentation]    Used for geneating openflow metrics Queries for Cassandra.
274     ${pattern}=    Concatenate the String    ${attribute}    .
275     ${pattern}=    Concatenate the String    ${pattern}    ${metric}
276     ${pattern}=    Concatenate the String    ${pattern}    .
277     ${pattern}=    Concatenate the String    ${pattern}    ${node}
278     ${pattern}=    Concatenate the String    ${pattern}    .
279     ${pattern}=    Concatenate the String    ${pattern}    Node_${node}
280     ${pattern}=    Concatenate the String    ${pattern}    .
281     ${pattern}=    Concatenate the String    ${pattern}    NodeConnector_${node}:${port}
282     ${pattern}=    Remove Space on String    ${pattern}
283     [Return]    ${pattern}
284
285 Create Temporary Key Info
286     [Arguments]    ${pattern}    ${remote}=${ODL_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${prompt_timeout}=120s    ${val_table}=metricval
287     [Documentation]    Return rows matching keya and keyb
288     ${output}=    Run Command On Remote System    ${ODL_SYSTEM_IP}    cat ${CASSANDRA_DB_PATH}${val_table}|grep "${pattern}"
289     [Return]    ${output}
290
291 Verify Metric Val File
292     [Documentation]    Returns Value for metric matching particular keya,keyb
293     @{metricval}=    Read File and Return Split Lines    ${CASSANDRA_DB_PATH}${temp_metric_val}
294     ${mv_len}=    Get Length    ${metricval}
295     ${mv_len}=    Evaluate    ${mv_len} - 1
296     ${found_line}=    Get From List    ${metricval}    ${mv_len}
297     @{split_line}=    Split String    ${found_line}    ${SPACE}
298     ${metric_count}=    Get From List    ${split_line}    3
299     [Return]    ${metric_count}
300
301 Verify Metric log File
302     [Arguments]    ${metric_log}    ${pattern}
303     [Documentation]    Returns Value for lines in Metriclog matching the pattern
304     ${contents}=    Grep From File    ${CASSANDRA_DB_PATH}${temp_metric_val}    ${pattern}
305     [Return]    ${contents}
306
307 Grep From File
308     [Arguments]    ${file}    ${pattern}
309     [Documentation]    Use cat to grep from the file and return the output
310     ${output}=    Run Command On Remote System    ${ODL_SYSTEM_IP}    cat ${file} | ${pattern}
311     [Return]    ${output}
312
313 Find Metricval Keys
314     [Arguments]    ${pattern}    ${file}
315     [Documentation]    Return list element which has the particular pattern.
316     ${metric_grep}=    Grep From File    ${TSDR_PATH}/tsdrKeyCache.txt    ${pattern}
317     @{split_line}=    Split String    ${metric_grep}    |
318     ${keypath}=    Get From List    ${split_line}    0
319     [Return]    @{split_line}
320
321 Copy TSDR tables
322     [Arguments]    ${remote}=${ODL_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${prompt_timeout}=120s    ${key_table}=metricpath    ${val_table}=metricval
323     [Documentation]    Copy TSDR files to external File system for text processing.
324     Log    Attempting to truncate tables in Cassandra
325     ${conn_id}=    Open Connection    ${remote}    prompt=${DEFAULT_LINUX_PROMPT}    timeout=${prompt_timeout}
326     Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    any
327     Write    export JAVA_HOME=/usr
328     Write    sudo ${CASSANDRA_CLIENT}/cqlsh
329     Read Until    cqlsh>
330     Write    COPY tsdr.${key_table} TO '${CASSANDRA_DB_PATH}${key_table}' WITH DELIMITER = ' ';
331     Read Until    cqlsh>
332     Write    COPY tsdr.${val_table} TO '${CASSANDRA_DB_PATH}${val_table}' WITH DELIMITER = ' ';
333     Read Until    cqlsh>
334     Write    exit
335     Close Connection
336
337 Issue Cassandra Query
338     [Arguments]    ${query}    ${output}    ${remote}=${ODL_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${prompt_timeout}=120s
339     [Documentation]    Issue query in cqlsh and match it with output which is passed as a argument
340     ${conn_id}=    Open Connection    ${remote}    prompt=${DEFAULT_LINUX_PROMPT}    timeout=${prompt_timeout}
341     Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    any
342     Write    export JAVA_HOME=/usr
343     Write    ${CASSANDRA_CLIENT}/cqlsh
344     Read Until    cqlsh>
345     ${query_output}=    Write    ${query}
346     ${query_output}=    Read Until    cqlsh>
347     ${str_output}=    Convert To String    ${output}
348     Should Contain    ${query_output}    ${str_output}
349     Write    exit
350     Close Connection
351
352 Count Cassandra rows
353     [Arguments]    ${query}    ${remote}=${ODL_SYSTEM_IP}    ${user}=${TOOLS_SYSTEM_USER}    ${prompt_timeout}=120s
354     [Documentation]    Issue query in cqlsh and match it with output which is passed as a argument
355     ${conn_id}=    Open Connection    ${remote}    prompt=${DEFAULT_LINUX_PROMPT}    timeout=${prompt_timeout}
356     Login With Public Key    ${user}    ${USER_HOME}/.ssh/${SSH_KEY}    any
357     Write    export JAVA_HOME=/usr
358     Write    ${CASSANDRA_CLIENT}/cqlsh
359     Read Until    cqlsh>
360     ${query_output}=    Write    ${query}
361     ${query_output}=    Read Until    cqlsh>
362     Write    exit
363     Close Connection
364     [Return]    ${query_output}
365
366 Read File and Return Split Lines
367     [Arguments]    ${filename}
368     [Documentation]    Reads the file and returns each line as list
369     ${contents}=    Run Command On Remote System    ${ODL_SYSTEM_IP}    cat ${filename}
370     @{lines}=    Split to lines    ${contents}
371     [Return]    @{lines}
372
373 Get Stats XML
374     [Arguments]    ${query}    ${xpath}
375     [Documentation]    Parse the xml output and returns it.
376     ${sid}=    RequestsLibrary.Create_Session    session    http://${ODL_SYSTEM_IP}:${RESTCONFPORT}    headers=${SEND_ACCEPT_XML_HEADERS}    auth=${AUTH}
377     ${resp}=    RequestsLibrary.Get Request    session    ${query}    headers=${SEND_ACCEPT_XML_HEADERS}
378     ${resp_xml}=    Parse XML    ${resp.content}
379     ${id1}=    Get Element Text    ${resp_xml}    ${xpath}
380     Delete All Sessions
381     [Return]    ${id1}
382
383 Return all XML matches
384     [Arguments]    ${query}    ${xpath}
385     [Documentation]    Returns all the values from xpath
386     ${sid}=    RequestsLibrary.Create_Session    session    http://${ODL_SYSTEM_IP}:${RESTCONFPORT}    headers=${SEND_ACCEPT_XML_HEADERS}    auth=${AUTH}
387     ${resp}=    RequestsLibrary.Get Request    session    ${query}    headers=${SEND_ACCEPT_XML_HEADERS}
388     ${resp_xml}=    Parse XML    ${resp.content}
389     @{id1}=    Get Elements Texts    ${resp_xml}    ${xpath}
390     Delete All Sessions
391     [Return]    @{id1}
392
393 Compare Tsdr XML Metrics
394     [Arguments]    ${xml}    ${tsdr}    ${deviation}=10
395     [Documentation]    Compares xml metrics from openflow plugin with TSDR metric values
396     ${val_max}=    Evaluate    ${xml}*${deviation}/100
397     ${val_diff}=    Evaluate    ${tsdr} - ${xml}
398     ${find_negative}=    Evaluate    ${val_diff} < 0
399     ${val_diff}=    Run Keyword If    '${find_negative}' == 'True'    Evaluate    ${val_diff}*-1
400     ...    ELSE    Evaluate    ${val_diff}*1
401     Should Be True    ${val_diff} <= ${val_max}
402
403 Generate Syslog
404     [Arguments]    ${facility}
405     [Documentation]    Uses netcat to generate logs and send it to port ${ODL_SYSTEM_IP}:1514
406     Run    echo "<${facility}>${MESSAGE}" | nc -w 4 -u ${ODL_SYSTEM_IP} 1514
407
408 Verify Metric Val File For Syslog
409     [Documentation]    Returns Value for metric matching particular keya,keyb
410     @{metricval}=    Read File and Return Split Lines    ${CASSANDRA_DB_PATH}${temp_metric_val}
411     [Return]    ${metricval}
412
413 Verify the Metrics Syslog on Cassandra Client
414     [Arguments]    ${pattern}
415     [Documentation]    Getting the keya and keyb for a particular syslog agent and create a temporary file from metriclog
416     @{metric_row}=    Find Metricval Keys    ${pattern}    metricpath
417     ${keya}=    Get From List    ${metric_row}    1
418     ${keyb}=    Get From List    ${metric_row}    2
419     ${keya_bool}=    Evaluate    ${keya} < 0
420     ${keya}=    Run Keyword If    '${keya_bool}' == 'True'    Catenate    SEPARATOR=    \\    ${keya}
421     ...    ELSE    Catenate    ${keya}
422     ${metric_log}=    Create Temporary Key Info    ${keya} ${keyb}    val_table=metriclog
423     [Return]    ${metric_log}
424
425 Iterating over metricpath
426     [Documentation]    Used to traverse over metricpath file and traverse over metricpath file and get the keys
427     @{mp_lines}=    Read File and Return Split Lines    ${CASSANDRA_DB_PATH}${metric_path}
428     FOR    ${line}    IN    @{mp_lines}
429         @{split_line}=    Split String    ${line}    ${SPACE}
430         ${keya}=    Get From List    ${split_line}    1
431         ${keyb}=    Get From List    ${split_line}    2
432         Iterating over metricval    ${keya}    ${keyb}
433     END
434
435 Iterating over metricval
436     [Arguments]    ${keya}    ${keyb}
437     [Documentation]    Used to traverse over metricval file and check if keya and keyb are present.
438     ${mv_contents}=    OperatingSystem.Get File    ${CASSANDRA_DB_PATH}${metric_val}
439     Should Contain    ${mv_contents}    ${keya}    ${keyb}
440
441 Check Metric path
442     [Arguments]    ${pattern}
443     [Documentation]    Count the number of rows in metricpath and compare with the pattern.
444     ${query_output}=    Count Cassandra rows    select count(*) from tsdr.metricpath;
445     Should Match Regexp    ${query_output}    ${pattern}
446
447 Check HSQLDB
448     [Arguments]    ${pattern}    ${TYPE}
449     [Documentation]    Count the number of rows in HSQLDB with Metric ${TYPE}
450     ${output}=    Issue Command On Karaf Console    tsdr:list ${TYPE}
451     Should Match Regexp    ${output}    ${pattern}
452
453 Check Metric Val
454     [Arguments]    ${pattern}
455     [Documentation]    Count the number of rows in metricval table and compare with the pattern.
456     ${query_output}=    Count Cassandra rows    select count(*) from tsdr.metricval;
457     Should Match Regexp    ${query_output}    ${pattern}
458
459 Check Metric Log
460     [Arguments]    ${pattern}
461     [Documentation]    Count the number of rows in metriclog and compare with the pattern.
462     ${query_output}=    Count Cassandra rows    select count(*) from tsdr.metriclog;
463     Should Match Regexp    ${query_output}    ${pattern}
464
465 Generate TSDR Query
466     [Arguments]    ${DC}=    ${MN}=    ${NID}=    ${RK}=    ${from}=0    ${until}=now
467     [Documentation]    Issues TSDR Query and returns the list
468     Create Session    session    http://${ODL_SYSTEM_IP}:${RESTCONFPORT}    auth=${AUTH}    headers=${HEADERS_QUERY}
469     ${resp}=    RequestsLibrary.Get Request    session    /tsdr/metrics/query?tsdrkey="[NID=${NID}][DC=${DC}][MN=${MN}][RK=${RK}]"&from=${from}&until=${until}    headers=${HEADERS_QUERY}
470     @{convert}=    Parse Json    ${resp.content}
471     Delete All Sessions
472     [Return]    @{convert}
473
474 Generate TSDR NBI
475     [Arguments]    ${DC}=    ${MN}=    ${NID}=    ${RK}=    ${from}=0    ${until}=now
476     ...    ${datapts}=1000000
477     [Documentation]    Issues TSDR Query and returns the list
478     Create Session    session    http://${ODL_SYSTEM_IP}:${RESTCONFPORT}    auth=${AUTH}    headers=${HEADERS_QUERY}
479     ${resp}=    RequestsLibrary.Get Request    session    /tsdr/nbi/render?target="[NID=${NID}][DC=${DC}][MN=${MN}][RK=${RK}]"&from=${from}&until=${until}&maxDataPoints=${datapts}    headers=${HEADERS_QUERY}
480     @{convert}=    Parse Json    ${resp.content}
481     ${dict_convert}=    Convert To Dictionary    @{convert}
482     @{dict}=    Get Dictionary Values    ${dict_convert}
483     @{datapoints_list}=    Convert to List    @{dict}[0]
484     Delete All Sessions
485     [Return]    @{datapoints_list}
486
487 Extract PORTSTATS RecordKeys
488     [Arguments]    ${Record_keys}
489     [Documentation]    Dissect Record keys for Portstats
490     ${node_dict}=    Get From List    ${Record_keys}    0
491     ${nc_dict}=    Get From List    ${Record_keys}    1
492     ${NODE}=    Get From Dictionary    ${node_dict}    keyValue
493     ${NC}=    Get From Dictionary    ${nc_dict}    keyValue
494     ${rk_val}=    Set Variable    Node:${NODE},NodeConnector:${NC}
495     [Return]    ${rk_val}
496
497 Extract QUEUESTATS RecordKeys
498     [Arguments]    ${Record_keys}
499     [Documentation]    Dissect Record keys for Queuestats
500     ${node_dict}=    Get From List    ${Record_keys}    0
501     ${connect_dict}=    Get From List    ${Record_keys}    1
502     ${queue_dict}=    Get From List    ${Record_keys}    2
503     ${NODE}=    Get From Dictionary    ${node_dict}    keyValue
504     ${CONNECT}=    Get From Dictionary    ${connect_dict}    keyValue
505     ${QUEUE}=    Get From Dictionary    ${queue_dict}    keyValue
506     ${rk_val}=    Set Variable    Node:${NODE},NodeConnector:${CONNECT},Queue:${QUEUE}
507     [Return]    ${rk_val}
508
509 Extract FLOWSTATS RecordKeys
510     [Arguments]    ${Record_keys}
511     [Documentation]    Dissect Record keys for Flowstats
512     ${node_dict}=    Get From List    ${Record_keys}    0
513     ${table_dict}=    Get From List    ${Record_keys}    1
514     ${flow_dict}=    Get From List    ${Record_keys}    2
515     ${NODE}=    Get From Dictionary    ${node_dict}    keyValue
516     ${TABLE}=    Get From Dictionary    ${table_dict}    keyValue
517     ${FLOW}=    Get From Dictionary    ${flow_dict}    keyValue
518     ${rk_val}=    Set Variable    Node:${NODE},Table:${TABLE},Flow:${FLOW}
519     [Return]    ${rk_val}
520
521 Extract FLOWTABLESTATS RecordKeys
522     [Arguments]    ${Record_keys}
523     [Documentation]    Dissect Record keys for Flowtablestats
524     ${node_dict}=    Get From List    ${Record_keys}    0
525     ${table_dict}=    Get From List    ${Record_keys}    1
526     ${NODE}=    Get From Dictionary    ${node_dict}    keyValue
527     ${TABLE}=    Get From Dictionary    ${table_dict}    keyValue
528     ${rk_val}=    Set Variable    Node:${NODE},Table:${TABLE}
529     [Return]    ${rk_val}
530
531 Extract Row Values from TSDR Query
532     [Arguments]    ${row_dict}    ${tsdr_row}    ${nbi_row}    ${DATA_CATEGORY}
533     [Documentation]    Extract the row values from query and generate it in DB format
534     ${nbi_value}=    Get From List    ${nbi_row}    0
535     ${nbi_time}=    Get From List    ${nbi_row}    1
536     ${MN}=    Get From Dictionary    ${row_dict}    metricName
537     ${MV}=    Get From Dictionary    ${row_dict}    metricValue
538     ${NID}=    Get From Dictionary    ${row_dict}    nodeID
539     ${RK}=    Get From Dictionary    ${row_dict}    recordKeys
540     ${time}=    Get From Dictionary    ${row_dict}    timeStamp
541     ${DC}=    Get From Dictionary    ${row_dict}    tsdrDataCategory
542     ${RK_VAL}=    Run Keyword If    '${DATA_CATEGORY}'=='PORTSTATS'    Extract PORTSTATS RecordKeys    ${RK}
543     ...    ELSE IF    '${DATA_CATEGORY}'=='FLOWSTATS'    Extract FLOWSTATS RecordKeys    ${RK}
544     ...    ELSE IF    '${DATA_CATEGORY}'=='FLOWTABLESTATS'    Extract FLOWTABLESTATS RecordKeys    ${RK}
545     ...    ELSE IF    '${DATA_CATEGORY}'=='QUEUESTATS'    Extract QUEUESTATS RecordKeys    ${RK}
546     ${epoch_time}=    Convert Date    ${time}    epoch    date_format=%a %b %d %H:%M:%S %Z %Y
547     ${epoch_time_int}=    Convert To Integer    ${epoch_time}
548     Should Match    ${tsdr_row}    *${NID}*
549     Should Match    ${tsdr_row}    *${DC}*
550     Should Match    ${tsdr_row}    *${MN}*
551     Should Match    ${tsdr_row}    *${RK_VAL}*
552     Should Match    ${tsdr_row}    *[${MV}]*
553     Should Match    ${tsdr_row}    *${epoch_time_int}*
554     Should Be Equal As Numbers    ${nbi_time}    ${epoch_time_int}
555     Should Be Equal As Numbers    ${nbi_value}    ${MV}
556
557 Evaluate Datasets Length
558     [Arguments]    ${tsdr_lines}    ${query_output}    ${nbi_output}
559     [Documentation]    Compare the outputs returned from all Data Stores
560     ${query_count}=    Get Length    ${query_output}
561     ${tsdr_count}=    Get Length    ${tsdr_lines}
562     ${nbi_count}=    Get Length    ${nbi_output}
563     Should Be Equal As Numbers    ${query_count}    ${tsdr_count}
564     Should Be Equal As Numbers    ${nbi_count}    ${tsdr_count}
565
566 Evaluate Datasets Value
567     [Arguments]    ${tsdr_lines}    ${query_output}    ${nbi_output}    ${TYPE}
568     [Documentation]    Compare the outputs returned from all Data Stores
569     FOR    ${q_item}    ${t_item}    ${n_item}    IN ZIP    ${query_output}    ${tsdr_lines}
570     ...    ${nbi_output}
571         ${query_row}=    Extract Row Values from TSDR Query    ${q_item}    ${t_item}    ${n_item}    ${TYPE}
572     END
573     FOR    ${found_line}    IN    @{matching_list}
574         @{split_line}=    Split String    ${found_line}    |
575         ${hex_name}=    Get From List    ${split_line}    2
576         ${if_desc}=    Decode Bytes To String    ${hex_name}    HEX
577         Append To List    ${ifdesc_list}    ${if_desc}
578     END
579     [Return]    @{ifdesc_list}
580
581 Write SNMP config
582     [Arguments]    ${HOST}=127.0.0.1    ${community}=${SNMP_COMMUNITY}
583     [Documentation]    Write SNMP Config File
584     Create File    snmp.cfg    credentials=[${HOST},${community}]
585     Append To File    snmp.cfg    \n
586     Move File    snmp.cfg    ${SNMP_PATH}
587
588 Bringup Netflow
589     [Documentation]    Brings up basic netflow setup .
590     Verify Feature Is Installed    odl-tsdr-netflow-statistics-collector
591     Wait Until Keyword Succeeds    24x    10 sec    Check Karaf Log Has Messages    NetFlow Data Colletor Initialized
592     Start Tsdr Suite
593     Ping All Hosts
594     Configure Netflow
595
596 Collect Data from SNMP Agent
597     [Arguments]    ${SNMP_IP}=127.0.0.1    ${SNMP_AGENT_COMM}=${SNMP_COMMUNITY}
598     [Documentation]    Poll for SNMP Agent OID
599     ${snmpagentinfo}    Create Dictionary    ip-address=${SNMP_IP}    community=${SNMP_AGENT_COMM}
600     ${snmpagentcreate}    Create Dictionary    input=${snmpagentinfo}
601     ${snmpagentcreate_json}=    json.dumps    ${snmpagentcreate}
602     Create Session    session    http://${ODL_SYSTEM_IP}:${RESTCONFPORT}    auth=${AUTH}    headers=${HEADERS_QUERY}
603     ${resp}=    RequestsLibrary.Post Request    session    /restconf/operations/snmp:get-interfaces    data=${snmpagentcreate_json}
604     ${convert}=    To Json    ${resp.content}
605     @{dict1}=    Get Dictionary Keys    ${convert}
606     ${dict1_0}=    Get From List    ${dict1}    0
607     ${dict1_val}=    Get From Dictionary    ${convert}    ${dict1_0}
608     @{ifEntry}=    Get From Dictionary    ${dict1_val}    ifEntry
609     @{SNMP_ENTRY}=    Create List
610     @{SNMP_VALUES}=    Create List
611     FOR    ${int}    IN    @{ifEntry}
612         ${ifindex}=    Get From Dictionary    ${int}    ifIndex
613         ${ifOutDiscards}=    Get From Dictionary    ${int}    ifOutDiscards
614         ${ifInDiscards}=    Get From Dictionary    ${int}    ifInDiscards
615         ${ifType}=    Get From Dictionary    ${int}    ifType
616         ${ifInOctets}=    Get From Dictionary    ${int}    ifInOctets
617         ${ifSpeed}=    Get From Dictionary    ${int}    ifSpeed
618         ${ifOutQLen}=    Get From Dictionary    ${int}    ifOutQLen
619         ${ifOutErrors}=    Get From Dictionary    ${int}    ifOutErrors
620         ${ifPhysAddress}=    Get From Dictionary    ${int}    ifPhysAddress
621         ${ifInUcastPkts}=    Get From Dictionary    ${int}    ifInUcastPkts
622         ${ifOutNUcastPkts}=    Get From Dictionary    ${int}    ifOutNUcastPkts
623         ${ifInErrors}=    Get From Dictionary    ${int}    ifInErrors
624         ${ifOutOctets}=    Get From Dictionary    ${int}    ifOutOctets
625         ${ifAdminStatus1}=    Get From Dictionary    ${int}    ifAdminStatus
626         ${ifAdminStatus}=    Get From Dictionary    ${OPER_STATUS}    ${ifAdminStatus1}
627         ${ifInUnknownProtos}=    Get From Dictionary    ${int}    ifInUnknownProtos
628         ${ifOutUcastPkts}=    Get From Dictionary    ${int}    ifOutUcastPkts
629         ${ifInNUcastPkts}=    Get From Dictionary    ${int}    ifInNUcastPkts
630         ${ifMtu}=    Get From Dictionary    ${int}    ifMtu
631         ${ifOperStatus1}=    Get From Dictionary    ${int}    ifOperStatus
632         ${ifOperStatus}=    Get From Dictionary    ${OPER_STATUS}    ${ifOperStatus1}
633         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfOutDiscards | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfOutDiscards
634         Append To List    ${SNMP_VALUES}    ${ifOutDiscards}
635         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfInDiscards | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfInDiscards
636         Append To List    ${SNMP_VALUES}    ${ifInDiscards}
637         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfInOctets | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfInOctets
638         Append To List    ${SNMP_VALUES}    ${ifInOctets}
639         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfOutQLen | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfOutQLen
640         Append To List    ${SNMP_VALUES}    ${ifOutQLen}
641         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfOutErrors | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfOutErrors
642         Append To List    ${SNMP_VALUES}    ${ifOutErrors}
643         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfInUcastPkts | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfInUcastPkts
644         Append To List    ${SNMP_VALUES}    ${ifInUcastPkts}
645         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfOutNUcastPkts | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfOutNUcastPkts
646         Append To List    ${SNMP_VALUES}    ${ifOutNUcastPkts}
647         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfInErrors | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfInErrors
648         Append To List    ${SNMP_VALUES}    ${ifInErrors}
649         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfOutOctets | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfOutOctets
650         Append To List    ${SNMP_VALUES}    ${ifOutOctets}
651         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfAdminStatus | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfAdminStatus
652         Append To List    ${SNMP_VALUES}    ${ifAdminStatus}
653         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfInUnknownProtos | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfInUnknownProtos
654         Append To List    ${SNMP_VALUES}    ${ifInUnknownProtos}
655         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfOutUcastPkts | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfOutUcastPkts
656         Append To List    ${SNMP_VALUES}    ${ifOutUcastPkts}
657         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfInNUcastPkts | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfInNUcastPkts
658         Append To List    ${SNMP_VALUES}    ${ifInNUcastPkts}
659         Append To List    ${SNMP_ENTRY}    grep NID=${SNMP_IP} | grep DC=SNMPINTERFACES | grep MN=IfOperStatus | grep RK=ifIndex:${ifindex},ifName:Iso88023Csmacd,SnmpMetric:IfOperStatus
660         Append To List    ${SNMP_VALUES}    ${ifOperStatus}
661     END
662     [Return]    ${SNMP_ENTRY}    ${SNMP_VALUES}
663
664 Retrieve Value From Elasticsearch
665     [Arguments]    ${data_category}    ${metric_name}    ${node_id}    ${rk_node_id}
666     [Documentation]    Retrieve the last record of Elastic Search from index TSDR. Query is done by data category, metricname and node ID
667     Create Session    session    http://${ODL_SYSTEM_IP}:${ELASTICPORT}    auth=${AUTH}    headers=${HEADERS_QUERY}
668     ${els_query}=    create_query_string_search    ${data_category}    ${metric_name}    ${node_id}    ${rk_node_id}
669     ${els_JSON_request}=    build_elastic_search_JSON_request    ${els_query}
670     ${resp}=    RequestsLibrary.Post_Request    session    _search?pretty    data=${els_JSON_request}
671     Should Be Equal As Strings    ${resp.status_code}    200
672     @{convert}=    Parse Json    ${resp.content}
673     ${json}=    RequestsLibrary.To Json    ${resp.content}
674     ${result}=    extract_metric_value_search    ${json}
675     [Teardown]    Delete All Sessions
676     [Return]    ${result}
677
678 Check Available values from Elasticsearch
679     [Arguments]    ${data_category}    ${number_items}
680     [Documentation]    Check whether data were sent to Elastic Search. We retrieve all data by data category and then compare its count
681     Create Session    session    http://${ODL_SYSTEM_IP}:${ELASTICPORT}    auth=${AUTH}    headers=${HEADERS_QUERY}
682     ${els_query}=    create_query_string_count    ${data_category}
683     ${els_JSON_request}=    build_elastic_search_JSON_request    ${els_query}
684     ${resp}=    RequestsLibrary.Post_Request    session    _search?pretty    data=${els_JSON_request}
685     Should Be Equal As Strings    ${resp.status_code}    200
686     @{convert}=    Parse Json    ${resp.content}
687     ${json}=    RequestsLibrary.To Json    ${resp.content}
688     ${result}=    extract_metric_value_count    ${json}
689     Log To Console    Elasticsearch: Check number of elements
690     Should Be True    ${result} > ${number_items}
691     [Teardown]    Delete All Sessions
692
693 Clear Elasticsearch Datastore
694     [Documentation]    Clear Elastic Search Datastore
695     Create Session    session    http://${ODL_SYSTEM_IP}:${ELASTICPORT}    auth=${AUTH}    headers=${HEADERS_QUERY}
696     ${resp}=    RequestsLibrary.Delete_Request    session    tsdr
697     Should Be Equal As Strings    ${resp.status_code}    200
698     [Teardown]    Delete All Sessions
699     [Return]    ${resp.status_code}