Upgrade RF syntax for v3.2 compatibility
[integration/test.git] / csit / libraries / SFC / SfcKeywords.robot
1 *** Settings ***
2 Documentation     Common Keywords for the SFC Test suites.
3 Library           Collections
4 Resource          ../CompareStream.robot
5 Resource          ../Utils.robot
6
7 *** Variables ***
8
9 *** Keywords ***
10 Get JSON Elements From URI
11     [Arguments]    ${uri}
12     ${resp} =    RequestsLibrary.Get Request    session    ${uri}
13     [Return]    ${resp.json()}
14
15 Check Classifier Flows
16     ${flowList} =    DockerSfc.Get Flows In Docker Containers
17     BuiltIn.log    ${flowList}
18     ${expected_nsh_pop}=    Set_Variable_If_At_Most    oxygen    *actions=pop_nsh*    *actions=decap(),decap(),*
19     ${expected_nsh_push}=    Set_Variable_If_At_Most    oxygen    *actions=push_nsh*    *actions=encap(nsh*
20     Collections.Should Contain Match    ${flowList}    ${expected_nsh_pop}
21     Collections.Should Contain Match    ${flowList}    ${expected_nsh_push}
22
23 Check Service Function Types Added
24     [Arguments]    ${elements}
25     [Documentation]    Check that the service function types are updated with the service function names
26     Utils.Check For Elements At URI    ${SERVICE_FUNCTION_TYPES_URI}    ${elements}
27
28 Check Service Function Types Removed
29     [Arguments]    ${elements}
30     [Documentation]    Check that the service function names are removed from the service function types
31     Utils.Check For Elements Not At URI    ${SERVICE_FUNCTION_TYPES_URI}    ${elements}
32
33 Check Empty Service Function Paths State
34     [Documentation]    Check that the service function paths state is empty after deleting SFPs
35     Utils.No Content From URI    session    ${SERVICE_FUNCTION_PATH_STATE_URI}
36
37 Check Rendered Service Path Created
38     [Arguments]    ${sfp_name}
39     [Documentation]    Check that the Rendered Service Path is created
40     ${sfp_name_elements_list} =    BuiltIn.Create List    ${sfp_name}
41     Utils.Check For Elements At URI    ${SERVICE_FUNCTION_PATHS_STATE_URI}    ${sfp_name_elements_list}
42
43 Check Rendered Service Path Deleted
44     [Arguments]    ${sfp_name}
45     [Documentation]    Check that the Rendered Service Path is deleted
46     Utils.No Content From URI    session    ${SERVICE_FUNCTION_PATH_STATE_URI}/${sfp_name}
47
48 Get Rendered Service Path Name
49     [Arguments]    ${sfp_name}    ${get_reverse}=False
50     [Documentation]    Given an SFP name, do a get on ${SERVICE_FUNCTION_PATH_STATE_URI} to get the RSP name
51     ${resp} =    RequestsLibrary.Get Request    session    ${SERVICE_FUNCTION_PATH_STATE_URI}/${sfp_name}
52     BuiltIn.Should Contain    ${ALLOWED_STATUS_CODES}    ${resp.status_code}
53     # should be like this: {"service-function-path-state":[{"name":"SFC1-100","sfp-rendered-service-path":[{"name":"SFC1-100-Path-183"}]}]}
54     ${sfp_rendered_service_path_list} =    Collections.Get_From_Dictionary    ${resp.json()}    service-function-path-state
55     ${sfp_rendered_service_path_dict} =    Collections.Get_From_List    ${sfp_rendered_service_path_list}    0
56     ${sfp_state_list} =    Collections.Get_From_Dictionary    ${sfp_rendered_service_path_dict}    sfp-rendered-service-path
57     ${list_length} =    BuiltIn.Get Length    ${sfp_state_list}
58     # The "sfp-rendered-service-path" will only have 1 or 2 entries, depending on chain symmetry config.
59     # The RSP name will be "<SfpName>-Path-<PathId>" and the optional symmetric name will be <SfpName>-Path-<PathId>-Reverse"
60     ${value} =    BuiltIn.Set Variable    None
61     FOR    ${i}    IN RANGE    ${list_length}
62         ${rsp_name_dict} =    Collections.Get From List    ${sfp_state_list}    ${i}
63         ${name} =    Collections.Get_From_Dictionary    ${rsp_name_dict}    name
64         @{matches} =    String.Get Regexp Matches    ${name}    .*Reverse$
65         ${matches_length} =    BuiltIn.Get Length    ${matches}
66         ${value} =    BuiltIn.Set Variable If    "${get_reverse}" == "False" and 0 == ${matches_length}    ${name}    "${get_reverse}" == "True" and 0 < ${matches_length}    ${name}
67         ...    "${value}" != "None"    ${value}
68     END
69     [Return]    ${value}
70
71 Create Sfp And Wait For Rsp Creation
72     [Arguments]    ${sfp_file_name}
73     [Documentation]    Given an SFP name, create it and wait for the associated RSPs to be created
74     Utils.Add Elements To URI From File And Verify    ${SERVICE_FUNCTION_PATHS_URI}    ${sfp_file_name}
75     ${sfp_dict} =    Utils.Json Parse From File    ${sfp_file_name}
76     ${sfps} =    Collections.Get_From_Dictionary    ${sfp_dict}    service-function-paths
77     ${sfp_json_list} =    Collections.Get_From_Dictionary    ${sfps}    service-function-path
78     ${list_length} =    BuiltIn.Get Length    ${sfp_json_list}
79     # Each SFP state entry returned from ODL will have either 2 or 3 name elements, 2 for non-symmetric 3 for symmetric RSP
80     FOR    ${i}    IN RANGE    ${list_length}
81         ${sfp_entry_dict} =    Collections.Get From List    ${sfp_json_list}    ${i}
82         ${sfp_name} =    Collections.Get_From_Dictionary    ${sfp_entry_dict}    name
83         ${status}    ${symmetric} =    BuiltIn.Run Keyword And Ignore Error    Collections.Get_From_Dictionary    ${sfp_entry_dict}    symmetric
84         ${symmetric} =    BuiltIn.Set Variable If    "${status}" == "FAIL"    False    ${symmetric}
85         ${num_names} =    BuiltIn.Set Variable If    "${symmetric}" == "False"    2    3
86         BuiltIn.Wait Until Keyword Succeeds    60s    2s    Utils.Check For Specific Number Of Elements At URI    ${SERVICE_FUNCTION_PATH_STATE_URI}/${sfp_name}    name
87         ...    ${num_names}
88     END
89
90 Delete Sfp And Wait For Rsps Deletion
91     [Arguments]    ${sfp_name}
92     [Documentation]    Given an SFP name, delete it and wait for the associated SFP state and RSPs to be deleted
93     Utils.Remove All Elements At URI And Verify    ${SERVICE_FUNCTION_PATH_URI}/${sfp_name}
94     BuiltIn.Wait Until Keyword Succeeds    60s    2s    Check Rendered Service Path Deleted    ${sfp_name}
95
96 Delete All Sfps And Wait For Rsps Deletion
97     [Documentation]    Delete all SFPs and wait for the RSPs to be deleted
98     Utils.Remove All Elements At URI And Verify    ${SERVICE_FUNCTION_PATHS_URI}
99     BuiltIn.Wait Until Keyword Succeeds    60s    2s    Check Empty Service Function Paths State
100
101 Switch Ips In Json Files
102     [Arguments]    ${json_dir}    ${container_names}
103     ${normalized_dir}=    OperatingSystem.Normalize Path    ${json_dir}/*.json
104     FOR    ${cont_name}    IN    @{container_names}
105         ${cont_ip} =    Get Docker IP    ${cont_name}
106         OperatingSystem.Run    sudo sed -i 's/${cont_name}/${cont_ip}/g' ${normalized_dir}
107     END