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