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