1*** Settings ***
2Documentation           Test setting VLAN and its configuration.
3
4
5Resource                        ../lib/rest_client.robot
6Resource                        ../lib/ipmi_client.robot
7Resource                        ../lib/utils.robot
8Resource                        ../lib/common_utils.robot
9Resource                        ../lib/openbmc_ffdc.robot
10Resource                        ../lib/ipmi_client.robot
11Resource                        ../lib/bmc_network_utils.robot
12Resource                        ../lib/state_manager.robot
13Library                         ../lib/utilities.py
14Library                         ../lib/ipmi_utils.py
15Library                         ../lib/var_funcs.py
16Library                         ../lib/func_args.py
17Library                         Collections
18
19Suite Teardown                  Suite Teardown Execution
20
21
22*** Variables ***
23${vlan_id}                      ${53}
24@{vlan_ids}                     ${35}  ${55}
25${invalid_vlan_id}              abc
26${vlan_resource}                ${NETWORK_MANAGER}action/VLAN
27${network_resource}             xyz.openbmc_project.Network.IP.Protocol.IPv4
28${static_network_resource}      xyz.openbmc_project.Network.IP.AddressOrigin.Static
29${ip}                           10.6.6.10
30@{ip_addresses}                 10.5.5.10  10.4.5.7
31${netmask}                      ${24}
32${gateway}                      0.0.0.0
33${initial_vlan_config}          @{EMPTY}
34
35
36*** Test Cases ***
37
38Add VLAN Via REST And Verify
39    [Documentation]  Add VLAN via REST and verify it via REST and IPMI.
40    [Tags]  Add_VLAN_Via_REST_And_Verify
41    [Setup]  Test Setup Execution
42    [Teardown]  Delete VLANs  [${vlan_id}]
43
44    Create VLAN  ${vlan_id}
45    Verify Existence Of VLAN  ${vlan_id}
46
47    ${lan_config}=  Get LAN Print Dict
48    Valid Value  lan_config['802.1q VLAN ID']  ["${vlan_id}"]
49
50
51Add Invalid VLAN Via REST And Verify
52    [Documentation]  Add Invalid VLAN via REST and verify.
53    [Tags]  Add_Invalid_VLAN_Via_REST_And_Verify
54
55    Create VLAN  ${invalid_vlan_id}  expected_result=error
56
57
58Delete VLAN Via REST
59    [Documentation]  Delete VLAN via REST and verify it via REST and IPMI.
60    [Tags]  Delete_VLAN_Via_REST
61    [Setup]  Run Keywords  Test Setup Execution  AND  Create VLAN  ${vlan_id}
62
63    Verify Existence Of VLAN  ${vlan_id}
64    Delete VLANs  [${vlan_id}]
65    Verify Existence Of VLAN  ${vlan_id}  expected_result=error
66
67    ${lan_config}=  Get LAN Print Dict
68    Valid Value  lan_config['802.1q VLAN ID']  ["Disabled"]
69
70
71Configure Network Settings On VLAN Via REST
72    [Documentation]  Configure IP on VLAN and verify it via REST and IPMI.
73    [Tags]  Configure_Network_Settings_On_VLAN_Via_REST
74    [Setup]  Run Keywords  Test Setup Execution  AND  Create VLAN  ${vlan_id}
75    [Teardown]  Delete VLANs  [${vlan_id}]
76
77    Configure Network Settings On VLAN  ${vlan_id}  ${ip}  ${netmask}
78    Get VLAN URI For IP  ${vlan_id}  ${ip}
79
80    ${lan_config}=  Get LAN Print Dict
81    Valid Value  lan_config['IP Address']  ["${ip}"]
82
83
84Delete IP On VLAN Via REST
85    [Documentation]  Delete IP on VLAN and verify it via REST and IPMI.
86    [Tags]  Delete_IP_On_VLAN_Via_REST
87    [Setup]  Run Keywords  Test Setup Execution  AND  Create VLAN  ${vlan_id}
88    [Teardown]  Delete VLANs  [${vlan_id}]
89
90    Configure Network Settings On VLAN  ${vlan_id}  ${ip}  ${netmask}
91    ${lan_config}=  Get LAN Print Dict
92    Valid Value  lan_config['IP Address']  ["${ip}"]
93
94    ${vlan_ip_uri}=  Get VLAN URI For IP  ${vlan_id}  ${ip}
95    Delete IP And Object  ${ip}  ${vlan_ip_uri}
96
97    Get VLAN URI For IP  ${vlan_id}  ${ip}  expected_result=error
98
99    ${lan_config}=  Get LAN Print Dict
100    Should Not Match  ${lan_config['IP Address']}  ${ip}
101
102
103Delete VLAN When IP Is Configured Via REST
104    [Documentation]  Delete IP on VLAN and verify using IPMI.
105    [Tags]  Delete_VLAN_When_IP_Is_Configured_Via_REST
106    [Setup]  Run Keywords  Test Setup Execution  AND  Create VLAN  ${vlan_id}
107    [Teardown]  Delete VLANs  [${vlan_id}]
108
109    Configure Network Settings On VLAN  ${vlan_id}  ${ip}  ${netmask}
110    ${lan_config}=  Get LAN Print Dict
111    Valid Value  lan_config['IP Address']  ["${ip}"]
112    Delete VLANs  [${vlan_id}]
113
114    Verify Existence Of VLAN  ${vlan_id}  expected_result=error
115
116    ${lan_config}=  Get LAN Print Dict
117    Valid Value  lan_config['802.1q VLAN ID']  ["Disabled"]
118
119
120Configure VLAN And Check Persistency On Reboot
121    [Documentation]  Create VLAN ID & IP , reboot and verify.
122    [Tags]  Configure_VLAN_And_Check_Persistency_On_Reboot
123    [Setup]  Test Setup Execution
124    [Teardown]  Delete VLANs  [${vlan_id}]
125
126    Create VLAN  ${vlan_id}
127    Configure Network Settings On VLAN  ${vlan_id}  ${ip}  ${netmask}
128    ${lan_config}=  Get LAN Print Dict
129    Valid Value  lan_config['IP Address']  ["${ip}"]
130    Valid Value  lan_config['802.1q VLAN ID']  ["${vlan_id}"]
131
132    Initiate BMC Reboot
133
134    Verify Existence Of VLAN  ${vlan_id}
135    Get VLAN URI For IP  ${vlan_id}  ${ip}
136
137    ${lan_config}=  Get LAN Print Dict
138    Valid Value  lan_config['IP Address']  ["${ip}"]
139    Valid Value  lan_config['802.1q VLAN ID']  ["${vlan_id}"]
140
141
142Add Multiple VLANs Via REST And Verify
143    [Documentation]  Add multiple VLANs via REST and verify them via CLI.
144    [Tags]  Add_Multiple_VLANs_Via_REST_And_Verify
145    [Setup]  Test Setup Execution
146    [Teardown]  Delete VLANs  ${vlan_ids}
147
148    FOR  ${vlan_id}  IN   @{vlan_ids}
149        Create VLAN  ${vlan_id}
150        Verify Existence Of VLAN  ${vlan_id}
151    END
152
153    ${lan_config}=  Get LAN Print Dict
154    ${vlan_id_ipmi}=  Convert To Integer  ${lan_config["802.1q VLAN ID"]}
155    Valid List  vlan_ids  required_values=[${vlan_id_ipmi}]
156
157Delete Multiple IPs On VLAN And Verify
158    [Documentation]  Delete multiple IPs on VLAN and verify each via REST and IPMI.
159    [Tags]  Delete_Multiple_IPs_On_VLAN_And_Verify
160    [Setup]  Run Keywords  Test Setup Execution  AND  Create VLAN  ${vlan_id}
161    [Teardown]  Delete VLANs  [${vlan_id}]
162
163    FOR  ${ip}  IN  @{ip_addresses}
164        Configure Network Settings On VLAN  ${vlan_id}  ${ip}  ${netmask}
165        ${vlan_ip_uri}=  Get VLAN URI For IP  ${vlan_id}  ${ip}
166        Delete IP And Object  ${ip}  ${vlan_ip_uri}
167
168        Get VLAN URI For IP  ${vlan_id}  ${ip}  expected_result=error
169
170        ${lan_config}=  Get LAN Print Dict
171        Should Not Match  ${lan_config['IP Address']}  ${ip}
172    END
173
174Delete Multiple VLANs Via REST
175    [Documentation]  Delete multiple VLANs via REST and verify each via REST and IPMI.
176    [Tags]  Delete_Multiple_VLANs_Via_REST
177    [Setup]  Test Setup Execution
178
179    FOR  ${vlan_id}  IN   @{vlan_ids}
180        Create VLAN  ${vlan_id}
181    END
182
183    Delete VLANs  ${vlan_ids}
184
185    ${lan_config}=  Get LAN Print Dict
186    Valid Value  lan_config['802.1q VLAN ID']  ["Disabled"]
187
188Configure Multiple IPs On VLAN Via REST
189    [Documentation]  Configure Multiple IPs on VLAN and verify each via REST.
190    [Tags]  Configure_Multiple_IPs_On_VLAN_Via_REST
191    [Setup]  Run Keywords  Test Setup Execution  AND  Create VLAN  ${vlan_id}
192    [Teardown]  Delete VLANs  [${vlan_id}]
193
194    FOR  ${ip}  IN  @{ip_addresses}
195        Configure Network Settings On VLAN  ${vlan_id}  ${ip}  ${netmask}
196    END
197
198    ${lan_config}=  Get LAN Print Dict
199    Valid Value  lan_config['IP Address']  ["${ip_addresses[0]}"]
200
201
202Create And Delete VLAN And IP On It Multiple Times With BMC Reboot
203    [Documentation]  Create and delete VLAN and IP on it multiple times with BMC reboot.
204    [Tags]  Create_And_Delete_VLAN_And_IP_On_It_Multiple_Times_With_BMC_Reboot
205    [Setup]  Test Setup Execution
206
207    FOR  ${i}  IN RANGE   ${5}
208      Create VLAN  ${vlan_id}
209      Verify Existence Of VLAN  ${vlan_id}
210      Configure Network Settings On VLAN  ${vlan_id}  ${ip}  ${netmask}
211      ${vlan_ip_uri}=  Get VLAN URI For IP  ${vlan_id}  ${ip}
212      Delete IP And Object  ${ip}  ${vlan_ip_uri}
213      Get VLAN URI For IP  ${vlan_id}  ${ip}  expected_result=error
214      Delete VLANs  [${vlan_id}]
215      Verify Existence Of VLAN  ${vlan_id}  expected_result=error
216      Initiate BMC Reboot
217      Sleep  ${NETWORK_TIMEOUT}s
218      Wait For Host To Ping  ${OPENBMC_HOST}  ${NETWORK_TIMEOUT}
219      Verify Existence Of VLAN  ${vlan_id}  expected_result=error
220    END
221
222    ${lan_config}=  Get LAN Print Dict
223    Valid Value  lan_config['802.1q VLAN ID']  ["Disabled"]
224
225
226*** Keywords ***
227
228
229Test Setup Execution
230    [Documentation]  Check and delete all previously created VLAN if any.
231
232    Printn
233    ${lan_config}=  Get LAN Print Dict
234    Return From Keyword If  '${lan_config['802.1q VLAN ID']}' == 'Disabled'
235
236    # Get all VLAN ID on interface eth0.
237    ${vlan_ids}=  Get VLAN IDs
238
239    ${initial_vlan_config}=  Create List
240    Set Suite Variable  ${initial_vlan_config}
241
242    FOR  ${vlan_id}  IN  @{vlan_ids}
243        ${vlan_records}=  Read Properties
244        ...  ${NETWORK_MANAGER}eth0_${vlan_id}${/}enumerate  quiet=1
245        ${vlan_record}=  Filter Struct
246        ...  ${vlan_records}  [('Origin', '${static_network_resource}')]
247
248        ${id}=  Convert To Integer  ${vlan_id}
249        Set Initial VLAN Config  ${vlan_record}  ${id}
250    END
251    Rprint Vars  initial_vlan_config
252
253    Delete VLANs  ${vlan_ids}
254
255
256Set Initial VLAN Config
257    [Documentation]  Set suite level list of Initial VLAN Config.
258    [Arguments]  ${vlan_record}  ${id}
259
260    # Description of argument(s):
261    # vlan_record  Dictionary of IP configuration information of a VLAN.
262    # Example:
263    #  /xyz/openbmc_project/network/eth0_55/ipv4/5fb2cfe6": {
264    #  "Address": "x.x.x.x",
265    #  "Gateway": "",
266    #  "Origin": "xyz.openbmc_project.Network.IP.AddressOrigin.Static",
267    #  "PrefixLength": 16,
268    #  "Type": "xyz.openbmc_project.Network.IP.Protocol.IPv4"}
269    #
270    # id  The VLAN ID corresponding to the IP Configuration records contained
271    #     in the variable "vlan_record".
272
273    ${uris}=  Get Dictionary Keys  ${vlan_record}
274
275    FOR  ${uri}  IN  @{uris}
276        Append To List  ${initial_vlan_config}  ${id}  ${vlan_record['${uri}']['Address']}
277        ...  ${vlan_record['${uri}']['PrefixLength']}
278    END
279
280    Run Keyword If  @{uris} == @{EMPTY}
281    ...  Append To List  ${initial_vlan_config}  ${id}  ${EMPTY}  ${EMPTY}
282
283
284Suite Teardown Execution
285    [Documentation]  Restore VLAN configuration.
286
287    ${length}=  Get Length  ${initial_vlan_config}
288    Return From Keyword If  ${length} == ${0}
289
290    ${previous_id}=  Set Variable  ${EMPTY}
291    FOR  ${index}  IN RANGE  0  ${length}  3
292
293        Run Keyword If  '${initial_vlan_config[${index+1}]}' == '${EMPTY}'
294        ...  Create VLAN  ${initial_vlan_config[${index}]}
295        ...  ELSE IF  '${previous_id}' == '${initial_vlan_config[${index}]}'
296        ...  Configure Network Settings On VLAN  ${initial_vlan_config[${index}]}
297        ...  ${initial_vlan_config[${index+1}]}  ${initial_vlan_config[${index+2}]}
298        ...  ELSE  Run Keywords  Create VLAN  ${initial_vlan_config[${index}]}  AND
299        ...  Configure Network Settings On VLAN  ${initial_vlan_config[${index}]}
300        ...  ${initial_vlan_config[${index+1}]}  ${initial_vlan_config[${index+2}]}
301
302        ${previous_id}=  Set Variable  ${initial_vlan_config[${index}]}
303    END
304
305
306Delete VLANs
307    [Documentation]  Delete one or more VLANs.
308    [Arguments]  ${ids}  ${interface}=eth0
309
310    # Description of argument(s):
311    # ids                           A list of VLAN IDs (e.g. ['53'] or ['53', '35', '12']). Note that the
312    #                               caller may simply pass a list variable or he/she may specify a
313    #                               python-like list specification (see examples below).
314    # interface                     The physical interface for the VLAN (e.g. 'eth0').
315
316    # Example calls:
317    # Delete VLANs  ${vlan_ids}
318    # Delete Vlans  [53, 35]
319
320    # Allow for python-like list specifications (e.g. ids=['53']).
321    ${vlan_ids}=  Source To Object  ${ids}
322
323    FOR  ${id}  IN  @{vlan_ids}
324        OpenBMC Delete Request  ${NETWORK_MANAGER}${interface}_${id}
325    END
326    Run Key U  Sleep \ ${NETWORK_TIMEOUT}s
327
328
329Get VLAN IDs
330    [Documentation]  Return all VLAN IDs.
331
332    ${vlan_ids}  ${stderr}  ${rc}=  BMC Execute Command
333    ...  /sbin/ip addr | grep @eth0 | cut -f1 -d@ | cut -f2 -d.
334    ${vlan_ids}=  Split String  ${vlan_ids}
335
336    [Return]  @{vlan_ids}
337
338
339Get VLAN URI For IP
340    [Documentation]  Get and return the URI for a VLAN IP.
341    [Arguments]  ${vlan_id}  ${vlan_ip}  ${expected_result}=valid
342
343    # Description of argument(s):
344    # vlan_id  The VLAN ID (e.g. '53').
345    # vlan_ip  The VLAN IP (e.g. 'x.x.x.x').
346
347    ${vlan_records}=  Read Properties
348    ...  ${NETWORK_MANAGER}eth0_${vlan_id}${/}enumerate  quiet=1
349    ${vlan_record}=  Filter Struct  ${vlan_records}  [('Address', '${vlan_ip}')]
350    ${num_vlan_records}=  Get Length  ${vlan_record}
351    ${status}=  Run Keyword And Return Status  Should Be True  ${num_vlan_records} > 0
352    ...  msg=Could not find a uri for vlan "${vlan_id}" with IP "${vlan_ip}".
353
354    Run Keyword If  '${expected_result}' == 'valid'
355    ...      Should Be Equal  ${status}  ${True}
356    ...      msg=VLAN IP URI doesn't exist!.
357    ...  ELSE
358    ...      Should Be Equal  ${status}  ${False}
359    ...      msg=VLAN IP URI exists!.
360    ${uris}=  Get Dictionary Keys  ${vlan_record}
361    Return From Keyword If  @{uris} == @{EMPTY}
362
363    [Return]  ${uris[${0}]}
364
365
366Verify Existence Of VLAN
367    [Documentation]  Verify VLAN ID exists.
368    [Arguments]  ${id}  ${interface}=eth0  ${expected_result}=valid
369
370    # Description of argument(s):
371    # id  The VLAN ID (e.g. id:'53').
372    # interface        Physical Interface on which the VLAN is defined.
373    # expected_result  Expected status to check existence or non-existence of VLAN.
374
375    ${vlan_ids}=  Get VLAN IDs
376    ${cli_status}=  Run Keyword And Return Status
377    ...  Valid List  vlan_ids  required_values=['${id}']
378
379    ${network_records}=  Read Properties  ${NETWORK_MANAGER}
380    ${rest_status}=  Run Keyword And Return Status  Valid List  network_records
381    ...  required_values=['${NETWORK_MANAGER}${interface}_${id}']
382
383    Should Be Equal  ${rest_status}  ${cli_status}
384    ...  msg=REST and CLI Output are not the same.
385    Run Keyword If  '${expected_result}' == 'valid'
386    ...      Should Be Equal  ${rest_status}  ${True}
387    ...      msg=VLAN ID doesn't exist!.
388    ...  ELSE
389    ...      Should Be Equal  ${rest_status}  ${False}
390    ...      msg=VLAN ID exists!.
391