Upgrade RF syntax for v3.2 compatibility
[integration/test.git] / csit / libraries / GbpSxp.robot
1 *** Settings ***
2 Documentation     GbpSxp library covering common tasks of gbp-sxp test cases.
3 Library           OperatingSystem    WITH NAME    os
4 Library           SSHLibrary
5 Library           RequestsLibrary
6 Library           DateTime
7 Library           ./GbpSxp.py    WITH NAME    gbpsxp
8 Resource          ../variables/Variables.robot
9 Resource          ./Utils.robot
10 Resource          ./SSHKeywords.robot
11
12 *** Variables ***
13 ${ISE_API_DIR}    ${CURDIR}/../variables/gbp/ise-mock-server-api
14 ${ISE_REST_PORT}    9060
15 ${GBP_ENDPOINTS_URI}    /restconf/operational/base-endpoint:endpoints
16 ${GBP_EP_TEMPLATES_CONFIG_URI}    /restconf/config/sxp-ep-provider-model:sxp-ep-mapper
17 ${GBP_RENDERER_CONFIG_URI}    /restconf/config/renderer:renderers/renderer/ios-xe-renderer
18 ${GBP_RENDERER_POLICY_OPERATIONAL_URI}    /restconf/operational/renderer:renderers/renderer/ios-xe-renderer/renderer-policy
19 ${GBP_RENDERER_POLICY_STATUS_OPERATIONAL_URI}    ${GBP_RENDERER_POLICY_OPERATIONAL_URI}/status
20 ${GBP_RPC_UNREGISTER_ENDPOINT_URI}    /restconf/operations/base-endpoint:unregister-endpoint
21 ${GBP_TENANT_CONFIG_URI}    /restconf/config/policy:tenants/tenant/tenant-red
22 ${MOUNTPOINT_IOSXE_SUFFIX}    yang-ext:mount/ned:native
23 ${NETCONF_CONFIG_URI}    /restconf/config/network-topology:network-topology/topology/topology-netconf
24 ${NETCONF_OPERATIONAL_URI}    /restconf/operational/network-topology:network-topology/topology/topology-netconf
25 ${SXP_EP_PROVIDER_CONFIG_URI}    /restconf/config/sxp-ep-provider-model:sxp-ep-mapper
26 ${SXP_ISE_ADAPTER_CONFIG_URI}    /restconf/config/sxp-ise-adapter-model:gbp-sxp-ise-adapter
27 ${SXP_ISE_ADAPTER_OPERATIONAL_URI}    /restconf/operational/sxp-ise-adapter-model:gbp-sxp-ise-adapter
28 ${SXP_NODE_RPC_ADD_ENTRY_URI}    /restconf/operations/sxp-controller:add-entry
29 ${SXP_TOPOLOGY_NODE_CONFIG_URI}    /restconf/config/network-topology:network-topology/topology/sxp
30 ${SXP_TOPOLOGY_NODE_OPERATIONAL_URI}    /restconf/operational/network-topology:network-topology/topology/sxp
31
32 *** Keywords ***
33 Prepare_Ssh_Tooling
34     [Arguments]    ${ip_address}=${TOOLS_SYSTEM_IP}
35     [Documentation]    Setup ssh session to tools system
36     ${tools_connection}    SSHKeywords.Open_Connection_To_Tools_System    ${ip_address}
37     SSHKeywords.Virtual_Env_Create
38     SSHKeywords.Virtual_Env_Install_Package    mock-server tornado==4.2
39     ${sed_output}    SSHKeywords.Execute_Command_At_Path_Should_Pass    sed -r -i 's/^(DEFAULT_FORMAT = ).+$/\\1XML/' lib/python2.7/site-packages/mock_server/data.py    path=${SSHKeywords__current_venv_path}
40     SSHKeywords.Virtual_Env_Freeze
41     [Return]    ${tools_connection}
42
43 Deploy_Ise_Mock_Server
44     [Arguments]    ${ise_mock_server_api_folder}    ${ise_rest_port}
45     [Documentation]    Deploy and start ise mock-server
46     # deploy ise mock-server
47     SSHLibrary.Put_Directory    ${ISE_API_DIR}/${ise_mock_server_api_folder}    destination=.    recursive=True
48     # start ise mock-server
49     SSHKeywords.Virtual_Env_Activate_On_Current_Session
50     SSHLibrary.Write    mock-server --dir=${ise_mock_server_api_folder} --port=${ise_rest_port} --address=${TOOLS_SYSTEM_IP} --debug
51     BuiltIn.Wait_Until_Keyword_Succeeds    5    1    gbpsxp.Check_Ise_Mock_Server_Is_Online    ${ise_rest_port}
52     ${mock_server_pid}    SSHKeywords.Execute_Command_Should_Pass    pgrep -f mock-server
53     SSHKeywords.Execute_Command_Should_Pass    lsof -p ${mock_server_pid}
54
55 Teardown_Ise_Mock_Server
56     [Arguments]    ${ise_mock_server_api_folder}
57     [Documentation]    Stop and wipe clean ise mock-server
58     # stop ise mock-server
59     SSHLibrary.Execute_command    pkill -f mock-server    return_stderr=True    return_rc=False
60     SSHKeywords.Execute_Command_Should_Pass    ls -la ${ise_mock_server_api_folder}; cat ${ise_mock_server_api_folder}/access-*.log
61     # wipe ise mock-server deployment
62     SSHLibrary.Execute_command    rm -rf ${ise_mock_server_api_folder}    return_stderr=True    return_rc=False
63
64 Teardown_Ssh_Tooling
65     [Arguments]    ${session_list}=@{EMPTY}
66     [Documentation]    Deactivate virtualenv and close ssh session on tools system
67     FOR    ${ssh_session}    IN    @{session_list}
68         BuiltIn.Log    ${ssh_session}
69         SSHKeywords.Restore_Current_Ssh_Connection_From_Index    ${ssh_session}
70         SSHKeywords.Virtual_Env_Deactivate_On_Current_Session
71         SSHKeywords.Virtual_Env_Delete
72         # fallback to single session
73     END
74     ${session_list_size}    get length    ${session_list}
75     BuiltIn.Log    ${session_list_size}
76     BuiltIn.Run_Keyword_If    ${session_list_size} == 0    SSHKeywords.Virtual_Env_Deactivate_On_Current_Session
77     BuiltIn.Run_Keyword_If    ${session_list_size} == 0    SSHKeywords.Virtual_Env_Delete
78     SSHLibrary.Close_All_Connections
79
80 Configure_Ise_Source_And_Gain_Harvest_Status
81     [Arguments]    ${session_arg}    ${configure_ise_source_file}    ${ise_rest_uri}
82     [Documentation]    Post ise-source configuration and wait for ise harvest status
83     # post ise-source configuration and wait for status
84     ${sxp_source_config_json}    Utils.Json_Parse_From_File    ${configure_ise_source_file}
85     gbpsxp.Replace_Ise_Source_Address    ${sxp_source_config_json}    ${ise_rest_uri}
86     BuiltIn.Log    ${sxp_source_config_json}
87     ${now}    DateTime.Get_Current_Date    result_format=%Y-%m-%dT%H:%M:%S.%f
88     Utils.Post_Elements_To_URI    ${SXP_ISE_ADAPTER_CONFIG_URI}    ${sxp_source_config_json}
89     ${ise_harvest_status_json}    BuiltIn.Wait_Until_Keyword_Succeeds    20    1    Gain_Uptodate_Harvest_Status    ${session_arg}    ${now}
90     [Return]    ${ise_harvest_status_json}
91
92 Gain_Uptodate_Harvest_Status
93     [Arguments]    ${session_arg}    ${now_timestamp}
94     [Documentation]    Read ise source harvest status and check if timestamp not older then limit
95     ${ise_harvest_status}    Utils.Get_Data_From_URI    ${session_arg}    ${SXP_ISE_ADAPTER_OPERATIONAL_URI}
96     ${ise_harvest_status_json}    Utils.Json_Parse_From_String    ${ise_harvest_status}
97     ${status_timestamp}    BuiltIn.Set_Variable    ${ise_harvest_status_json['gbp-sxp-ise-adapter']['ise-harvest-status']['timestamp']}
98     gbpsxp.Check_iso8601_datetime_younger_then_limit    ${status_timestamp}    ${now_timestamp}
99     [Return]    ${ise_harvest_status_json}
100
101 Check_Ise_Mock_Server_Is_Online
102     [Arguments]    ${ise_port}
103     [Documentation]    Checks if the port ${ise_port} is occupied on tools system
104     ${ise_port_occupied}    SSHLibrary.Execute_command    netstat -lnp | grep '${ise_port}'    return_stdout=False    return_rc=True
105     BuiltIn.Should_Be_Equal_As_Integers    0    ${ise_port_occupied}
106
107 Check_Ise_Harvest_Status
108     [Arguments]    ${ise_harvest_status_json}    ${expected_templates_amount}
109     [Documentation]    Check ise harvest status by templatest written amount
110     ${templates_written}    BuiltIn.Set_Variable    ${ise_harvest_status_json['gbp-sxp-ise-adapter']['ise-harvest-status']['templates-written']}
111     BuiltIn.Should_Be_Equal_As_Integers    ${expected_templates_amount}    ${templates_written}
112
113 Clean_ise_source_config
114     [Documentation]    Wipe clean ise source configuration
115     BuiltIn.Run_Keyword_And_Ignore_Error    Utils.Remove_All_Elements_If_Exist    ${SXP_ISE_ADAPTER_CONFIG_URI}