Upgrade RF syntax for v3.2 compatibility
[integration/test.git] / csit / libraries / FlowLib.robot
index 265f790df2d04bf3e3f85b80abe057a64832bdf4..143b06f46d3235a8040b2cb8a19e4f655c1eb44e 100644 (file)
 Documentation     Keywords used to create/modify flow objects. The object is defined in the
 ...               corresponding FlowLib.py library and contains pertinent fields and methods (e.g.,
 ...               cookie and barrier fields, string formatted xml that can be used to push to
-...               controller)
-Library           ./FlowLib.py
+...               controller). TODO: Remove hard dependency on controller HTTP "session".
 Library           XML
+Library           String
 Library           RequestsLibrary
+Library           ScaleClient.py
+Library           FlowLib.py
+Library           XmlComparator.py
+Library           Common.py
 Variables         ../variables/Variables.py
 
-*** Variables ***
-
 *** Keywords ***
+Check No Switches In Inventory
+    [Arguments]    ${switches}
+    [Documentation]    Check no switch is in inventory
+    ${resp}    RequestsLibrary.Get Request    session    ${OPERATIONAL_NODES_API}
+    Log    ${resp.content}
+    FOR    ${switch}    IN RANGE    1    ${switches+1}
+        Should Not Contain    ${resp.content}    "openflow:${switch}"
+    END
+
+Check No Switches In Topology
+    [Arguments]    ${switches}
+    [Documentation]    Check no switch is in topology
+    ${resp}    RequestsLibrary.Get Request    session    ${OPERATIONAL_TOPO_API}
+    Log    ${resp.content}
+    Should Be Equal As Strings    ${resp.status_code}    200
+    FOR    ${switch}    IN RANGE    1    ${switches+1}
+        Should Not Contain    ${resp.content}    openflow:${switch}
+    END
+
+Check Switches In Inventory
+    [Arguments]    ${switches}
+    [Documentation]    Check all switches and stats in operational inventory
+    FOR    ${switch}    IN RANGE    1    ${switches+1}
+        ${resp}    RequestsLibrary.Get Request    session    ${OPERATIONAL_NODES_API}/node/openflow:${switch}
+        Log    ${resp.content}
+        Should Be Equal As Strings    ${resp.status_code}    200
+        Should Contain    ${resp.content}    flow-capable-node-connector-statistics
+        Should Contain    ${resp.content}    flow-table-statistics
+    END
+
+Check Switches In Topology
+    [Arguments]    ${switches}
+    [Documentation]    Check switches are in the topology.
+    ${resp}=    RequestsLibrary.Get Request    session    ${OPERATIONAL_TOPO_API}
+    Log    ${resp.content}
+    Should Be Equal As Strings    ${resp.status_code}    200
+    ${count}=    Get Count    ${resp.content}    "node-id":"openflow:
+    BuiltIn.Should Be Equal As Numbers    ${count}    ${switches}
+
+Check Number Of Links
+    [Arguments]    ${links}
+    [Documentation]    Check number of links in the topolgy.
+    ${resp}=    RequestsLibrary.Get Request    session    ${OPERATIONAL_TOPO_API}
+    Log    ${resp.content}
+    Should Be Equal As Strings    ${resp.status_code}    200
+    ${count}=    Get Count    ${resp.content}    "link-id":"openflow:
+    Should Be Equal As Integers    ${count}    ${links}
+
+Check Linear Topology
+    [Arguments]    ${switches}
+    [Documentation]    Check Linear topology.
+    ${resp}    RequestsLibrary.Get Request    session    ${OPERATIONAL_TOPO_API}
+    Log    ${resp.content}
+    Should Be Equal As Strings    ${resp.status_code}    200
+    FOR    ${switch}    IN RANGE    1    ${switches+1}
+        Should Contain    ${resp.content}    "node-id":"openflow:${switch}"
+        Should Contain    ${resp.content}    "tp-id":"openflow:${switch}:1"
+        Should Contain    ${resp.content}    "tp-id":"openflow:${switch}:2"
+        Should Contain    ${resp.content}    "source-tp":"openflow:${switch}:2"
+        Should Contain    ${resp.content}    "dest-tp":"openflow:${switch}:2"
+        ${edge}    Evaluate    ${switch}==1 or ${switch}==${switches}
+        Run Keyword Unless    ${edge}    Should Contain    ${resp.content}    "tp-id":"openflow:${switch}:3"
+        Run Keyword Unless    ${edge}    Should Contain    ${resp.content}    "source-tp":"openflow:${switch}:3"
+        Run Keyword Unless    ${edge}    Should Contain    ${resp.content}    "dest-tp":"openflow:${switch}:3"
+    END
+
+Check Flows Operational Datastore
+    [Arguments]    ${flow_count}    ${controller_ip}=${ODL_SYSTEM_IP}
+    [Documentation]    Check if number of Operational Flows on member of given index is equal to ${flow_count}.
+    ${sw}    ${reported_flow}    ${found_flow}=    ScaleClient.Flow Stats Collected    controller=${controller_ip}
+    Should_Be_Equal_As_Numbers    ${flow_count}    ${found_flow}
+
+Check Number Of Flows
+    [Arguments]    ${flows}
+    [Documentation]    Check number of flows in the inventory.
+    ${resp}=    RequestsLibrary.Get Request    session    ${OPERATIONAL_NODES_API}
+    Log    ${resp.content}
+    Should Be Equal As Strings    ${resp.status_code}    200
+    ${count}=    Get Count    ${resp.content}    "priority"
+    Should Be Equal As Integers    ${count}    ${flows}
+
+Check Number Of Groups
+    [Arguments]    ${groups}
+    [Documentation]    Check number of groups in the inventory.
+    ${resp}=    RequestsLibrary.Get Request    session    ${OPERATIONAL_NODES_API}
+    Log    ${resp.content}
+    Should Be Equal As Strings    ${resp.status_code}    200
+    ${group_count}=    Get Count    ${resp.content}    "group-type"
+    Should Be Equal As Integers    ${group_count}    ${groups}
+
 Check Flow Stats Are Available
     [Arguments]    ${node_id}    ${flows}
     [Documentation]    A GET on the /node/${node_id} inventory API is made and flow stats string is checked for existence.
@@ -19,6 +111,38 @@ Check Flow Stats Are Available
     Should Be Equal As Strings    ${resp.status_code}    200
     Should Contain X Times    ${resp.content}    priority    ${flows}
 
+Check Number Of Hosts
+    [Arguments]    ${hosts}
+    [Documentation]    Check number of hosts in topology
+    ${resp}=    RequestsLibrary.Get Request    session    ${OPERATIONAL_TOPO_API}
+    Log    ${resp.content}
+    Should Be Equal As Strings    ${resp.status_code}    200
+    ${count}=    Get Count    ${resp.content}    "node-id":"host:
+    Should Be Equal As Integers    ${count}    ${hosts}
+
+Check No Hosts
+    [Documentation]    Check if all hosts are deleted from inventory
+    ${resp}=    RequestsLibrary.Get Request    session    ${OPERATIONAL_TOPO_API}
+    Should Be Equal As Strings    ${resp.status_code}    200
+    Should Not Contain    ${resp.content}    "node-id":"host:
+
+Add Table Miss Flows
+    [Arguments]    ${switches}
+    [Documentation]    Add table miss flows to switches.
+    ${switches}=    Convert To Integer    ${switches}
+    ${data}=    OperatingSystem.Get File    ${CURDIR}/../variables/openflowplugin/table_miss_flow.json
+    FOR    ${switch}    IN RANGE    1    ${switches+1}
+        TemplatedRequests.Put As Json To Uri    ${CONFIG_NODES_API}/node/openflow:${switch}/table/0/flow/default    ${data}    session
+    END
+
+Check Table Miss Flows
+    [Arguments]    ${switches}
+    [Documentation]    Check table miss flows in switches.
+    ${switches}=    Convert To Integer    ${switches}
+    FOR    ${switch}    IN RANGE    1    ${switches+1}
+        TemplatedRequests.Get As Json From Uri    ${OPERATIONAL_NODES_API}/node/openflow:${switch}/table/0/flow/default    session
+    END
+
 Create Inventory Flow
     [Documentation]    Calls FlowLib.Make_Inventory_Flow function and initializes and sanitizes
     ...    the basic flow elements that can be given to flow:inventory
@@ -147,44 +271,69 @@ Remove Flow XML Element
     Set Flow Field    ${flow}    xml    ${xml_string}
     [Return]    ${flow}
 
+Add Group To Controller And Verify
+    [Arguments]    ${group_body}    ${node_id}    ${group_id}
+    [Documentation]    Push group through REST-API and verify in data-store
+    ${resp}    RequestsLibrary.Put Request    session    ${CONFIG_NODES_API}/node/${node_id}/group/${group_id}    headers=${HEADERS_XML}    data=${group_body}
+    Log    ${resp.content}
+    BuiltIn.Should_Match    "${resp.status_code}"    "20?"
+    ${resp}    RequestsLibrary.Get Request    session    ${CONFIG_NODES_API}/node/${node_id}/group/${group_id}    headers=${ACCEPT_XML}
+    Log    ${resp.content}
+    Should Be Equal As Strings    ${resp.status_code}    200
+    Compare Xml    ${group_body}    ${resp.content}
+
 Add Flow To Controller And Verify
     [Arguments]    ${flow_body}    ${node_id}    ${table_id}    ${flow_id}
     [Documentation]    Push flow through REST-API and verify in data-store
-    ${resp}    RequestsLibrary.Put Request    session    ${REST_CON}/node/${node_id}/table/${table_id}/flow/${flow_id}    headers=${HEADERS_XML}    data=${flow_body}
+    ${resp}    RequestsLibrary.Put Request    session    ${CONFIG_NODES_API}/node/${node_id}/table/${table_id}/flow/${flow_id}    headers=${HEADERS_XML}    data=${flow_body}
     Log    ${resp.content}
-    Should Be Equal As Strings    ${resp.status_code}    200
-    ${resp}    RequestsLibrary.Get Request    session    ${REST_CON}/node/${node_id}/table/${table_id}/flow/${flow_id}    headers=${ACCEPT_XML}
+    BuiltIn.Should_Match    "${resp.status_code}"    "20?"
+    ${resp}    RequestsLibrary.Get Request    session    ${CONFIG_NODES_API}/node/${node_id}/table/${table_id}/flow/${flow_id}    headers=${ACCEPT_XML}
     Log    ${resp.content}
     Should Be Equal As Strings    ${resp.status_code}    200
-    compare xml    ${flow_body}    ${resp.content}
+    Compare Xml    ${flow_body}    ${resp.content}
 
 Verify Flow On Mininet Switch
     [Arguments]    ${flow_elements}
     [Documentation]    Checking flow on switch
-    sleep    1
-    write    dpctl dump-flows -O OpenFlow13
+    Sleep    1
+    Write    dpctl dump-flows -O OpenFlow13
     ${switchoutput}    Read Until    >
-    : FOR    ${flowElement}    IN    @{flow_elements}
-    \    should Contain    ${switchoutput}    ${flowElement}
+    FOR    ${flowElement}    IN    @{flow_elements}
+        Should Contain    ${switchoutput}    ${flowElement}
+    END
+
+Remove Group From Controller And Verify
+    [Arguments]    ${node_id}    ${group_id}
+    [Documentation]    Remove group and verify
+    ${resp}    RequestsLibrary.Delete Request    session    ${CONFIG_NODES_API}/node/${node_id}/group/${group_id}
+    Log    ${resp.content}
+    Should Be Equal As Strings    ${resp.status_code}    200
+    ${resp}    RequestsLibrary.Get Request    session    ${CONFIG_NODES_API}/node/${node_id}/group/${group_id}
+    Builtin.Return_From_Keyword_If    ${resp.status_code} == 404
+    Builtin.Log    ${resp.text}
+    Builtin.Fail    The request failed with code ${resp.status_code}
 
 Remove Flow From Controller And Verify
-    [Arguments]    ${flow_body}    ${node_id}    ${table_id}    ${flow_id}
-    [Documentation]    Remove flow
-    ${resp}    RequestsLibrary.Delete Request    session    ${REST_CON}/node/${node_id}/table/${table_id}/flow/${flow_id}
+    [Arguments]    ${node_id}    ${table_id}    ${flow_id}
+    [Documentation]    Remove flow and verify
+    ${resp}    RequestsLibrary.Delete Request    session    ${CONFIG_NODES_API}/node/${node_id}/table/${table_id}/flow/${flow_id}
     Log    ${resp.content}
     Should Be Equal As Strings    ${resp.status_code}    200
-    ${resp}    RequestsLibrary.Get Request    session    ${REST_CON}/node/${node_id}/table/${table_id}
-    Log    ${resp.content}
-    Should Not Contain    ${resp.content}    ${flow_id}
+    ${resp}    RequestsLibrary.Get Request    session    ${CONFIG_NODES_API}/node/${node_id}/table/${table_id}/flow/${flow_id}
+    Builtin.Return_From_Keyword_If    ${resp.status_code} == 404
+    Builtin.Log    ${resp.text}
+    Builtin.Fail    The request failed with code ${resp.status_code}
 
 Verify Flow Does Not Exist On Mininet Switch
     [Arguments]    ${flow_elements}
     [Documentation]    Checking flow on switch is removed
-    sleep    1
-    write    dpctl dump-flows -O OpenFlow13
+    Sleep    1
+    Write    dpctl dump-flows -O OpenFlow13
     ${switchoutput}    Read Until    >
-    : FOR    ${flowElement}    IN    @{flow_elements}
-    \    should Not Contain    ${switchoutput}    ${flowElement}
+    FOR    ${flowElement}    IN    @{flow_elements}
+        Should Not Contain    ${switchoutput}    ${flowElement}
+    END
 
 Remove Default Flows
     [Arguments]    ${node_id}
@@ -226,14 +375,14 @@ Create Flow Variables For Suite From XML File
     Set Suite Variable    ${xmlroot}
 
 Check Datastore Presence
-    [Arguments]    ${fname}    ${reqconfpres}    ${reqoperpres}    ${upd}
+    [Arguments]    ${fname}    ${reqconfpres}    ${reqoperpres}    ${upd}    ${check_id}=${False}
     [Documentation]    Checks if flow is properly existing or not existing in the config and operational
     ...    datastores, based on the variables ${reqconfpres} and ${reqoperpres}
     Create Flow Variables For Suite From XML File    ${XmlsDir}/${fname}
     # Note:    ${upddata} and ${data} are suite variables set by the keyword above.
     ${det}=    Set Variable If    ${upd}==${True}    ${upddata}    ${data}
     Check Config Flow    ${reqconfpres}    ${det}
-    Check Operational Flow    ${reqoperpres}    ${det}
+    Check Operational Flow    ${reqoperpres}    ${det}    ${check_id}
 
 Flow Presence In Config Store
     [Arguments]    ${expvalue}
@@ -249,7 +398,7 @@ Flow Presence In Config Store
     Return From Keyword    ${pres}    ${msg}
 
 Flow Presence In Operational Store
-    [Arguments]    ${expvalue}
+    [Arguments]    ${expvalue}    ${check_id}=${False}
     [Documentation]    Checks the operational store for given flow. Returns True if present, otherwise returns False
     ...    This keyword assumes that the global/suite variables are available (${table_id}, ${flow_id} and ${switch_idx}
     ${headers}=    Create Dictionary    Accept=application/xml
@@ -257,7 +406,7 @@ Flow Presence In Operational Store
     Log    ${resp}
     Log    ${resp.content}
     Return From Keyword If    ${resp.status_code}!=200    ${False}    ${EMPTY}
-    ${pres}    ${msg}=    Is Flow Operational2    ${expvalue}    ${resp.content}
+    ${pres}    ${msg}=    Is Flow Operational2    ${expvalue}    ${resp.content}    ${check_id}
     Run Keyword If    '''${msg}'''!='${EMPTY}'    Log    ${msg}
     Return From Keyword    ${pres}    ${msg}
 
@@ -278,10 +427,10 @@ Check Config Flow
     Should Be Equal    ${expected}    ${presence_flow}    msg=${msgf}
 
 Check Operational Flow
-    [Arguments]    ${expected}    ${expvalue}
+    [Arguments]    ${expected}    ${expvalue}    ${check_id}=${False}
     [Documentation]    Wrapper keyword that calls "Flow Presence In Operational Store" and "Get Presence Failure Message" from this library
-    ...    to verify that the ${expvalue} flow is or is not found in the config store, depending on whether or not it was ${expected}
-    ${presence_table}    ${msg}=    Flow Presence In Operational Store    ${expvalue}
+    ...    to verify that the ${expvalue} flow is or is not found in the operational store, depending on whether or not it was ${expected}
+    ${presence_table}    ${msg}=    Flow Presence In Operational Store    ${expvalue}    ${check_id}
     ${msgf}=    Get Presence Failure Message    operational    ${expected}    ${presence_table}    ${msg}
     Should Be Equal    ${expected}    ${presence_table}    msg=${msgf}
 
@@ -372,3 +521,11 @@ Delete Flow Via Restconf
     Log    ${resp.content}
     ${msg}=    Set Variable    Delete flow for ${CONFIG_NODES_API}/node/openflow:${node_id}/table/${table_id}/flow/${flow_id} failed, http response ${resp.status_code} received.
     Should Be Equal As Strings    ${resp.status_code}    200    msg=${msg}
+
+Get Flow Id
+    [Arguments]    ${dpnid}    ${table_id}    ${flow_element}
+    [Documentation]    This verifies specific flow-id for particular table-id matching from the flow element
+    ${resp} =    RequestsLibrary.Get Request    session    ${CONFIG_NODES_API}/node/openflow:${dpnid}/table/${table_id}
+    BuiltIn.Log    ${resp.content}
+    @{flow_id} =    String.Get Regexp Matches    ${resp.content}    id\":\"(\\d+${flow_element})    1
+    [Return]    @{flow_id}[0]