Step 1: Move vm scripts to the right place
[integration/test.git] / test / 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   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    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
45 Configure OpenFlow
46     [Arguments]    ${switch}
47     [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
48     ...    from switch.openflow_validation_cmd output where the validation strings are
49     ...    stored in switch.openflow_enable_validations
50     Log    Applying configs to configure openflow on the given switch.
51     Configure Connection Index And Prompt Wrapper    ${switch}
52     Iterate Switch Commands From List    ${switch}    ${switch.base_openflow_config}
53     Read Wrapper    ${switch}
54     Wait Until Keyword Succeeds    10s    1s    Validate Switch Output    ${switch}    ${switch.openflow_validation_cmd}    ${switch.openflow_enable_validations}
55     Read Wrapper    ${switch}
56
57 Validate Switch Output
58     [Arguments]    ${switch}    ${cmd}    ${validations}    ${should_exist}=true
59     [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.
60     Configure Connection Index And Prompt Wrapper    ${switch}
61     Read Wrapper    ${switch}
62     ${tmp}=    Execute Command Wrapper    ${switch}    /sbin/ifconfig
63     Log    ${tmp}
64     ${output}=    Execute Command Wrapper    ${switch}    ${cmd}
65     Log    ${output}
66     : FOR    ${str}    IN    @{validations}
67     \    Run Keyword If    "${should_exist}" == "true"    Should Match Regexp    ${output}    ${str}
68     \    Run Keyword If    "${should_exist}" == "false"    Should Not Match Regexp    ${output}    ${str}
69
70 Enable OpenFlow
71     [Arguments]    ${switch}
72     [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.
73     Log    Will toggle openflow to be ON
74     Iterate Switch Commands From List    ${switch}    ${switch.openflow_enable_config}
75     Read Wrapper    ${switch}
76     Wait Until Keyword Succeeds    10s    1s    Validate Switch Output    ${switch}    ${switch.openflow_validation_cmd}    ${switch.openflow_enable_validations}
77
78 Disable OpenFlow
79     [Arguments]    ${switch}
80     [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.
81     Log    Will toggle openflow to be OFF
82     Iterate Switch Commands From List    ${switch}    ${switch.openflow_disable_config}
83     Read Wrapper    ${switch}
84     Wait Until Keyword Succeeds    10s    1s    Validate Switch Output    ${switch}    ${switch.openflow_validation_cmd}    ${switch.openflow_disable_validations}
85
86 Open Connection Wrapper
87     [Arguments]    ${switch}
88     [Documentation]    Some switches require telnet access and others require ssh access. \ Based on the
89     ...    switch.mgmt_protocol, the connection open will be handled by the right robot
90     ...    library (Telnet or SSHLibrary). \ The connection_index is returned.
91     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    Call Method    ${switch}    set_ssh_key    ${USER_HOME}/.ssh/${SSH_KEY}
92     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    Call Method    ${switch}    set_mgmt_user    ${MININET_USER}
93     ${connection_index}=    Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Open Connection    ${switch.mgmt_ip}    prompt=${switch.mgmt_prompt}    timeout=30s
94     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    Login With Public Key    ${switch.mgmt_user}    ${switch.ssh_key}    any
95     ${connection_index}=    Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Open Connection    ${switch.mgmt_ip}    ELSE    Set Variable
96     ...    ${connection_index}
97     [Return]    ${connection_index}
98
99 Configure Connection Index And Prompt Wrapper
100     [Arguments]    ${switch}
101     [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
102     ...    Read or Write actions happen on the correct device.
103     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Switch Connection    ${switch.connection_index}
104     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Set Client Configuration    prompt=${switch.mgmt_prompt}    timeout=5s
105     Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Switch Connection    ${switch.connection_index}
106     Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Set Prompt    ${switch.mgmt_prompt}    True
107
108 Read Wrapper
109     [Arguments]    ${switch}
110     [Documentation]    Wraps the Read command so that depending on the switch.mgmt_protocol the right
111     ...    library (Telnet or SSHLibrary) is used.
112     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Read
113     Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Read
114
115 Write Bare Wrapper
116     [Arguments]    ${switch}    ${cmd}
117     [Documentation]    Wraps the Write Bare command so that depending on the switch.mgmt_protocol the right
118     ...    library (Telnet or SSHLibrary) is used.
119     Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Write Bare    ${cmd}
120     Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Write Bare    ${cmd}
121
122 Execute Command Wrapper
123     [Arguments]    ${switch}    ${cmd}
124     [Documentation]    Wraps the Execute Command keyword so that depending on the switch.mgmt_protocol the right
125     ...    library (Telnet or SSHLibrary) is used.
126     ${output}=    Run Keyword If    "${switch.mgmt_protocol}" == "ssh"    SSHLibrary.Execute Command    ${cmd}
127     ${output}=    Run Keyword If    "${switch.mgmt_protocol}" == "telnet"    Telnet.Execute Command    ${cmd}    ELSE    Set Variable
128     ...    ${output}
129     [Return]    ${output}
130
131 Connect To Switch
132     [Arguments]    ${switch}
133     [Documentation]    Will Open a connection to the switch, which will set the switch.connection_index.
134     ...    For each switch.connection_configs string, a write bare will be executed on the
135     ...    switch connection. \ The write bare is done becuase some switch consoles require
136     ...    extra input (CR/LF, etc.) that are needed. \ The connection_configs strings should
137     ...    be sufficient to put the switch console in to a usuable state so that further
138     ...    interactions with the switch can be used with the robot keyword "Execute
139     ...    Command"
140     ${connection_index}=    Open Connection Wrapper    ${switch}
141     Call Method    ${switch}    set_connection_index    ${connection_index}
142     Configure Connection Index And Prompt Wrapper    ${switch}
143     : FOR    ${cmd}    IN    @{switch.connection_configs}
144     \    Write Bare Wrapper    ${switch}    ${cmd}
145     \    Sleep    1
146     \    Read Wrapper    ${switch}
147
148 Cleanup Switch
149     [Arguments]    ${switch}
150     [Documentation]    will execute and command strings stored in switch.cleanup_cmds
151     Iterate Switch Commands From List    ${switch}    ${switch.cleanup_cmds}
152
153 Initialize Switch
154     [Arguments]    ${switch}
155     [Documentation]    Will connect and execute all switch.initialization_cmds on the given switch.
156     ...    In some cases, this may be a reboot. \ If so, the switch.initialization_type can
157     ...    be set to "reboot" and further logic is invoked to wait for the reboot to complete
158     ...    and a reconnect to the switch is made.
159     Connect To Switch    ${switch}
160     Configure Connection Index And Prompt Wrapper    ${switch}
161     : FOR    ${cmd}    IN    @{switch.initialization_cmds}
162     \    Write Bare Wrapper    ${switch}    ${cmd}
163     \    Sleep    1
164     \    Run Keyword And Ignore Error    Read Wrapper    ${switch}
165     Run Keyword If    "${switch.initialization_type}" == "reboot"    Wait For Switch Reboot    ${switch}
166     Run Keyword If    "${switch.initialization_type}" == "reboot"    Connect To Switch    ${switch}
167
168 Wait For Switch Reboot
169     [Arguments]    ${switch}
170     [Documentation]    If a switch has been set to reboot, it may take some time. \ This keyword will first
171     ...    make sure the switch has gone down (10 pings over 10 seconds should not see
172     ...    a 100% success, although it may respond for a short time after the reload is
173     ...    issued). \ Then a poll is done with a single ping request every 5s until a success
174     ...    is found, at which point it is assumed the switch is up and ready.
175     ${output}=    Run    ping ${switch.mgmt_ip} -c 10 -W 1
176     Should Not Contain    ${output}    10 packets transmitted, 10 received, 0% packet loss    Does not appear that switch has rebooted
177     Wait Until Keyword Succeeds    240s    5s    Ping    ${switch.mgmt_ip}
178
179 Ping
180     [Arguments]    ${ip}
181     ${output}=    Run    ping ${ip} -c 1 -W 1
182     Should Contain    ${output}    1 packets transmitted, 1 received