Upgrade RF syntax for v3.2 compatibility
[integration/test.git] / csit / libraries / SwitchUtils.robot
1 *** Settings ***
2 Library           SSHLibrary
3 Library           Telnet
4
5 *** Variables ***
6
7 *** Keywords ***
8 Get Switch Datapath ID
9     [Arguments]    ${switch}
10     [Documentation]    Using the connection index for the given switch, will execute the command string
11     ...    "datapath_id_output_command" which will store the output in switch.datapath_id_output_string.
12     ...    The switch object method "update_datapath_id" is called which is assumed to place the ODL
13     ...    friendly (decimal) version of the datapath id in to switch.datapath_id and the value is also
14     ...    returned from this keyword.
15     Configure Connection Index And Prompt Wrapper    ${switch}
16     Read Wrapper    ${switch}
17     ${switch.datapath_id_output_string}=    Execute Command Wrapper    ${switch}    ${switch.datapath_id_output_command}
18     Log    ${switch.datapath_id_output_string}
19     Call Method    ${switch}    update_datapath_id
20     [Return]    ${switch.datapath_id}
21
22 Verify Switch In Operational Data Store
23     [Arguments]    ${switch}
24     [Documentation]    Verifies the existence of the switch.datapath_id in the operational datastore.
25     ${resp}    RequestsLibrary.Get Request    session    ${REST_CONTEXT}
26     Log    ${resp.content}
27     Should Match Regexp    ${resp.content}    openflow:${switch.datapath_id}
28
29 Verify Switch Not In Operational Data Store
30     [Arguments]    ${switch}
31     [Documentation]    Verifies that the given switch.datapath_id is not in the operational datastore.
32     ${resp}    RequestsLibrary.Get Request    session    ${REST_CONTEXT}
33     Log    ${resp.content}
34     Should Not Match Regexp    ${resp.content}    openflow:${switch.datapath_id}
35
36 Iterate Switch Commands From List
37     [Arguments]    ${switch}    ${cmd_list}
38     [Documentation]    Each string in the @{cmd_list} argument is executed on the switch.connection_index.
39     Configure Connection Index And Prompt Wrapper    ${switch}
40     FOR    ${cmd}    IN    @{cmd_list}
41         Log    ${cmd}
42         Read Wrapper    ${switch}
43         Execute Command Wrapper    ${switch}    ${cmd}
44     END
45
46 Configure OpenFlow
47     [Arguments]    ${switch}
48     [Documentation]    The commands neccessary to configure openflow on the given switch object should exist in the switch.base_openflow_config attribute. \ Also, the commands/logic to verify that openflow is working are checked in this keyword and come
49     ...    from switch.openflow_validation_cmd output where the validation strings are
50     ...    stored in switch.openflow_enable_validations
51     Log    Applying configs to configure openflow on the given switch.
52     Configure Connection Index And Prompt Wrapper    ${switch}
53     Iterate Switch Commands From List    ${switch}    ${switch.base_openflow_config}
54     Read Wrapper    ${switch}
55     Wait Until Keyword Succeeds    10s    1s    Validate Switch Output    ${switch}    ${switch.openflow_validation_cmd}    ${switch.openflow_enable_validations}
56     Read Wrapper    ${switch}
57
58 Validate Switch Output
59     [Arguments]    ${switch}    ${cmd}    ${validations}    ${should_exist}=true
60     [Documentation]    A generic keyword that will execute one command on the switch, and check for each string in the @{validations} argument. \ There is a boolean flag ${should_exist} that can be used to check that the validations are or are NOT in the output of the command executed.
61     Configure Connection Index And Prompt Wrapper    ${switch}
62     Read Wrapper    ${switch}
63     ${tmp}=    Execute Command Wrapper    ${switch}    /sbin/ifconfig
64     Log    ${tmp}
65     ${output}=    Execute Command Wrapper    ${switch}    ${cmd}
66     Log    ${output}
67     FOR    ${str}    IN    @{validations}
68         Run Keyword If    "${should_exist}" == "true"    Should Match Regexp    ${output}    ${str}
69         Run Keyword If    "${should_exist}" == "false"    Should Not Match Regexp    ${output}    ${str}
70     END
71
72 Enable OpenFlow
73     [Arguments]    ${switch}
74     [Documentation]    executes the switch.openflow_enable_config on the given switch and validates that openflow is operational with the switch.openflow_validation_command against all the strings in the switch.openflow_enable_validations list.
75     Log    Will toggle openflow to be ON
76     Iterate Switch Commands From List    ${switch}    ${switch.openflow_enable_config}
77     Read Wrapper    ${switch}
78     Wait Until Keyword Succeeds    10s    1s    Validate Switch Output    ${switch}    ${switch.openflow_validation_cmd}    ${switch.openflow_enable_validations}
79
80 Disable OpenFlow
81     [Arguments]    ${switch}
82     [Documentation]    executes the switch.openflow_disable_config on the given switch and validates that openflow is NOT operational with the switch.openflow_validation_command against all the strings in the switch.openflow_disable_validations list.
83     Log    Will toggle openflow to be OFF
84     Iterate Switch Commands From List    ${switch}    ${switch.openflow_disable_config}
85     Read Wrapper    ${switch}
86     Wait Until Keyword Succeeds    10s    1s    Validate Switch Output    ${switch}    ${switch.openflow_validation_cmd}    ${switch.openflow_disable_validations}
87
88 Open Connection Wrapper
89     [Arguments]    ${switch}
90     [Documentation]    Some switches require telnet access and others require ssh access. \ Based on the
91     ...    switch.mgmt_protocol, the connection open will be handled by the right robot
92     ...    library (Telnet or SSHLibrary). \ The connection_index is returned.
93     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    Call Method    ${switch}    set_ssh_key    ${USER_HOME}/.ssh/${SSH_KEY}
94     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    Call Method    ${switch}    set_mgmt_user    ${TOOLS_SYSTEM_USER}
95     ${connection_index}=    Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Open Connection    ${switch.mgmt_ip}    prompt=${switch.mgmt_prompt}    timeout=30s
96     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    Login With Public Key    ${switch.mgmt_user}    ${switch.ssh_key}    any
97     ${connection_index}=    Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Open Connection    ${switch.mgmt_ip}
98     ...    ELSE    Set Variable    ${connection_index}
99     [Return]    ${connection_index}
100
101 Configure Connection Index And Prompt Wrapper
102     [Arguments]    ${switch}
103     [Documentation]    when using multiple switch connections (e.g. more than one switch device) this keyword will switch the current connection index and prompt so that the following
104     ...    Read or Write actions happen on the correct device.
105     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Switch Connection    ${switch.connection_index}
106     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Set Client Configuration    prompt=${switch.mgmt_prompt}    timeout=5s
107     Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Switch Connection    ${switch.connection_index}
108     Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Set Prompt    ${switch.mgmt_prompt}    True
109
110 Read Wrapper
111     [Arguments]    ${switch}
112     [Documentation]    Wraps the Read command so that depending on the switch.mgmt_protocol the right
113     ...    library (Telnet or SSHLibrary) is used.
114     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Read
115     Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Read
116
117 Write Bare Wrapper
118     [Arguments]    ${switch}    ${cmd}
119     [Documentation]    Wraps the Write Bare command so that depending on the switch.mgmt_protocol the right
120     ...    library (Telnet or SSHLibrary) is used.
121     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Write Bare    ${cmd}
122     Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Write Bare    ${cmd}
123
124 Execute Command Wrapper
125     [Arguments]    ${switch}    ${cmd}
126     [Documentation]    Wraps the Execute Command keyword so that depending on the switch.mgmt_protocol the right
127     ...    library (Telnet or SSHLibrary) is used.
128     ${output}=    Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Execute Command    ${cmd}
129     ${output}=    Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Execute Command    ${cmd}
130     ...    ELSE    Set Variable    ${output}
131     [Return]    ${output}
132
133 Connect To Switch
134     [Arguments]    ${switch}
135     [Documentation]    Will Open a connection to the switch, which will set the switch.connection_index.
136     ...    For each switch.connection_configs string, a write bare will be executed on the
137     ...    switch connection. \ The write bare is done becuase some switch consoles require
138     ...    extra input (CR/LF, etc.) that are needed. \ The connection_configs strings should
139     ...    be sufficient to put the switch console in to a usuable state so that further
140     ...    interactions with the switch can be used with the robot keyword "Execute
141     ...    Command"
142     ${connection_index}=    Open Connection Wrapper    ${switch}
143     Call Method    ${switch}    set_connection_index    ${connection_index}
144     Configure Connection Index And Prompt Wrapper    ${switch}
145     FOR    ${cmd}    IN    @{switch.connection_configs}
146         Write Bare Wrapper    ${switch}    ${cmd}
147         Sleep    1
148         Read Wrapper    ${switch}
149     END
150
151 Cleanup Switch
152     [Arguments]    ${switch}
153     [Documentation]    will execute and command strings stored in switch.cleanup_cmds
154     Iterate Switch Commands From List    ${switch}    ${switch.cleanup_cmds}
155
156 Initialize Switch
157     [Arguments]    ${switch}
158     [Documentation]    Will connect and execute all switch.initialization_cmds on the given switch.
159     ...    In some cases, this may be a reboot. \ If so, the switch.initialization_type can
160     ...    be set to "reboot" and further logic is invoked to wait for the reboot to complete
161     ...    and a reconnect to the switch is made.
162     Connect To Switch    ${switch}
163     Configure Connection Index And Prompt Wrapper    ${switch}
164     FOR    ${cmd}    IN    @{switch.initialization_cmds}
165         Write Bare Wrapper    ${switch}    ${cmd}
166         Sleep    1
167         Run Keyword And Ignore Error    Read Wrapper    ${switch}
168     END
169     Run Keyword If    "${switch.initialization_type}" == "reboot"    Wait For Switch Reboot    ${switch}
170     Run Keyword If    "${switch.initialization_type}" == "reboot"    Connect To Switch    ${switch}
171
172 Wait For Switch Reboot
173     [Arguments]    ${switch}
174     [Documentation]    If a switch has been set to reboot, it may take some time. \ This keyword will first
175     ...    make sure the switch has gone down (10 pings over 10 seconds should not see
176     ...    a 100% success, although it may respond for a short time after the reload is
177     ...    issued). \ Then a poll is done with a single ping request every 5s until a success
178     ...    is found, at which point it is assumed the switch is up and ready.
179     ${output}=    Run    ping ${switch.mgmt_ip} -c 10 -W 1
180     Should Not Contain    ${output}    10 packets transmitted, 10 received, 0% packet loss    Does not appear that switch has rebooted
181     Wait Until Keyword Succeeds    240s    5s    Ping    ${switch.mgmt_ip}
182
183 Ping
184     [Arguments]    ${ip}
185     ${output}=    Run    ping ${ip} -c 1 -W 1
186     Should Contain    ${output}    1 packets transmitted, 1 received