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_IP_On_VLAN_Via_REST
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*** Keywords ***
202
203
204Test Setup Execution
205    [Documentation]  Check and delete all previously created VLAN if any.
206
207    Printn
208    ${lan_config}=  Get LAN Print Dict
209    Return From Keyword If  '${lan_config['802.1q VLAN ID']}' == 'Disabled'
210
211    # Get all VLAN ID on interface eth0.
212    ${vlan_ids}=  Get VLAN IDs
213
214    ${initial_vlan_config}=  Create List
215    Set Suite Variable  ${initial_vlan_config}
216
217    FOR  ${vlan_id}  IN  @{vlan_ids}
218        ${vlan_records}=  Read Properties
219        ...  ${NETWORK_MANAGER}eth0_${vlan_id}${/}enumerate  quiet=1
220        ${vlan_record}=  Filter Struct
221        ...  ${vlan_records}  [('Origin', '${static_network_resource}')]
222
223        ${id}=  Convert To Integer  ${vlan_id}
224        Set Initial VLAN Config  ${vlan_record}  ${id}
225    END
226    Rprint Vars  initial_vlan_config
227
228    Delete VLANs  ${vlan_ids}
229
230
231Set Initial VLAN Config
232    [Documentation]  Set suite level list of Initial VLAN Config.
233    [Arguments]  ${vlan_record}  ${id}
234
235    # Description of argument(s):
236    # vlan_record  Dictionary of IP configuration information of a VLAN.
237    # Example:
238    #  /xyz/openbmc_project/network/eth0_55/ipv4/5fb2cfe6": {
239    #  "Address": "x.x.x.x",
240    #  "Gateway": "",
241    #  "Origin": "xyz.openbmc_project.Network.IP.AddressOrigin.Static",
242    #  "PrefixLength": 16,
243    #  "Type": "xyz.openbmc_project.Network.IP.Protocol.IPv4"}
244    #
245    # id  The VLAN ID corresponding to the IP Configuration records contained
246    #     in the variable "vlan_record".
247
248    ${uris}=  Get Dictionary Keys  ${vlan_record}
249
250    FOR  ${uri}  IN  @{uris}
251        Append To List  ${initial_vlan_config}  ${id}  ${vlan_record['${uri}']['Address']}
252        ...  ${vlan_record['${uri}']['PrefixLength']}
253    END
254
255    Run Keyword If  @{uris} == @{EMPTY}
256    ...  Append To List  ${initial_vlan_config}  ${id}  ${EMPTY}  ${EMPTY}
257
258
259Suite Teardown Execution
260    [Documentation]  Restore VLAN configuration.
261
262    ${length}=  Get Length  ${initial_vlan_config}
263    Return From Keyword If  ${length} == ${0}
264
265    ${previous_id}=  Set Variable  ${EMPTY}
266    FOR  ${index}  IN RANGE  0  ${length}  3
267
268        Run Keyword If  '${initial_vlan_config[${index+1}]}' == '${EMPTY}'
269        ...  Create VLAN  ${initial_vlan_config[${index}]}
270        ...  ELSE IF  '${previous_id}' == '${initial_vlan_config[${index}]}'
271        ...  Configure Network Settings On VLAN  ${initial_vlan_config[${index}]}
272        ...  ${initial_vlan_config[${index+1}]}  ${initial_vlan_config[${index+2}]}
273        ...  ELSE  Run Keywords  Create VLAN  ${initial_vlan_config[${index}]}  AND
274        ...  Configure Network Settings On VLAN  ${initial_vlan_config[${index}]}
275        ...  ${initial_vlan_config[${index+1}]}  ${initial_vlan_config[${index+2}]}
276
277        ${previous_id}=  Set Variable  ${initial_vlan_config[${index}]}
278    END
279
280
281Delete VLANs
282    [Documentation]  Delete one or more VLANs.
283    [Arguments]  ${ids}  ${interface}=eth0
284
285    # Description of argument(s):
286    # ids                           A list of VLAN IDs (e.g. ['53'] or ['53', '35', '12']). Note that the
287    #                               caller may simply pass a list variable or he/she may specify a
288    #                               python-like list specification (see examples below).
289    # interface                     The physical interface for the VLAN (e.g. 'eth0').
290
291    # Example calls:
292    # Delete VLANs  ${vlan_ids}
293    # Delete Vlans  [53, 35]
294
295    # Allow for python-like list specifications (e.g. ids=['53']).
296    ${vlan_ids}=  Source To Object  ${ids}
297
298    FOR  ${id}  IN  @{vlan_ids}
299        OpenBMC Delete Request  ${NETWORK_MANAGER}${interface}_${id}
300    END
301    Run Key U  Sleep \ ${NETWORK_TIMEOUT}s
302
303
304Create VLAN
305    [Documentation]  Create a VLAN.
306    [Arguments]  ${id}  ${interface}=eth0  ${expected_result}=valid
307
308    # Description of argument(s):
309    # id  The VLAN ID (e.g. '53').
310    # interface  The physical interface for the VLAN(e.g. 'eth0').
311
312    @{data_vlan_id}=  Create List  ${interface}  ${id}
313    ${data}=  Create Dictionary   data=@{data_vlan_id}
314    ${resp}=  OpenBMC Post Request  ${vlan_resource}  data=${data}
315    ${resp.status_code}=  Convert To String  ${resp.status_code}
316    ${status}=  Run Keyword And Return Status
317    ...  Valid Value  resp.status_code  ["${HTTP_OK}"]
318
319    Run Keyword If  '${expected_result}' == 'error'
320    ...      Should Be Equal  ${status}  ${False}
321    ...      msg=Configuration of an invalid VLAN ID Failed.
322    ...  ELSE
323    ...      Should Be Equal  ${status}  ${True}
324    ...      msg=Configuration of a valid VLAN ID Failed.
325
326    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
339Configure Network Settings On VLAN
340    [Documentation]  Configure network settings.
341    [Arguments]  ${id}  ${ip_addr}  ${prefix_len}  ${gateway_ip}=${gateway}
342    ...  ${expected_result}=valid  ${interface}=eth0
343
344    # Description of argument(s):
345    # id               The VLAN ID (e.g. '53').
346    # ip_addr          IP address of VLAN Interface.
347    # prefix_len       Prefix length of VLAN Interface.
348    # gateway_ip       Gateway IP address of VLAN Interface.
349    # expected_result  Expected status of network setting configuration.
350    # interface        Physical Interface on which the VLAN is defined.
351
352    @{ip_parm_list}=  Create List  ${network_resource}
353    ...  ${ip_addr}  ${prefix_len}  ${gateway_ip}
354
355    ${data}=  Create Dictionary  data=@{ip_parm_list}
356
357    Run Keyword And Ignore Error  OpenBMC Post Request
358    ...  ${NETWORK_MANAGER}${interface}_${id}/action/IP  data=${data}
359
360    # After any modification on network interface, BMC restarts network
361    # module, wait until it is reachable. Then wait 15 seconds for new
362    # configuration to be updated on BMC.
363
364    Wait For Host To Ping  ${OPENBMC_HOST}  ${NETWORK_TIMEOUT}
365    ...  ${NETWORK_RETRY_TIME}
366    Sleep  ${NETWORK_TIMEOUT}s
367
368    # Verify whether new IP address is populated on BMC system.
369    # It should not allow to configure invalid settings.
370    ${status}=  Run Keyword And Return Status
371    ...  Verify IP On BMC  ${ip_addr}
372
373    Run Keyword If  '${expected_result}' == 'error'
374    ...      Should Be Equal  ${status}  ${False}
375    ...      msg=Configuration of invalid IP Failed.
376    ...  ELSE
377    ...      Should Be Equal  ${status}  ${True}
378    ...      msg=Configuration of valid IP Failed.
379
380
381Get VLAN URI For IP
382    [Documentation]  Get and return the URI for a VLAN IP.
383    [Arguments]  ${vlan_id}  ${vlan_ip}  ${expected_result}=valid
384
385    # Description of argument(s):
386    # vlan_id  The VLAN ID (e.g. '53').
387    # vlan_ip  The VLAN IP (e.g. 'x.x.x.x').
388
389    ${vlan_records}=  Read Properties
390    ...  ${NETWORK_MANAGER}eth0_${vlan_id}${/}enumerate  quiet=1
391    ${vlan_record}=  Filter Struct  ${vlan_records}  [('Address', '${vlan_ip}')]
392    ${num_vlan_records}=  Get Length  ${vlan_record}
393    ${status}=  Run Keyword And Return Status  Should Be True  ${num_vlan_records} > 0
394    ...  msg=Could not find a uri for vlan "${vlan_id}" with IP "${vlan_ip}".
395
396    Run Keyword If  '${expected_result}' == 'valid'
397    ...      Should Be Equal  ${status}  ${True}
398    ...      msg=VLAN IP URI doesn't exist!.
399    ...  ELSE
400    ...      Should Be Equal  ${status}  ${False}
401    ...      msg=VLAN IP URI exists!.
402    ${uris}=  Get Dictionary Keys  ${vlan_record}
403    Return From Keyword If  @{uris} == @{EMPTY}
404
405    [Return]  ${uris[${0}]}
406
407
408Verify Existence Of VLAN
409    [Documentation]  Verify VLAN ID exists.
410    [Arguments]  ${id}  ${interface}=eth0  ${expected_result}=valid
411
412    # Description of argument(s):
413    # id  The VLAN ID (e.g. id:'53').
414    # interface        Physical Interface on which the VLAN is defined.
415    # expected_result  Expected status to check existence or non-existence of VLAN.
416
417    ${vlan_ids}=  Get VLAN IDs
418    ${cli_status}=  Run Keyword And Return Status
419    ...  Valid List  vlan_ids  required_values=['${id}']
420
421    ${network_records}=  Read Properties  ${NETWORK_MANAGER}
422    ${rest_status}=  Run Keyword And Return Status  Valid List  network_records
423    ...  required_values=['${NETWORK_MANAGER}${interface}_${id}']
424
425    Should Be Equal  ${rest_status}  ${cli_status}
426    ...  msg=REST and CLI Output are not the same.
427    Run Keyword If  '${expected_result}' == 'valid'
428    ...      Should Be Equal  ${rest_status}  ${True}
429    ...      msg=VLAN ID doesn't exist!.
430    ...  ELSE
431    ...      Should Be Equal  ${rest_status}  ${False}
432    ...      msg=VLAN ID exists!.
433