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