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