1*** Settings ***
2Documentation    Vmi network utilities keywords.
3
4Resource         ../../lib/resource.robot
5Resource         ../../lib/bmc_redfish_resource.robot
6Resource         ../../lib/openbmc_ffdc.robot
7Resource         ../../lib/bmc_redfish_utils.robot
8Resource         ../../lib/state_manager.robot
9Resource         ../../lib/bmc_network_utils.robot
10Library          ../../lib/bmc_network_utils.py
11
12*** Variables ***
13
14&{DHCP_ENABLED}           DHCPEnabled=${True}
15&{DHCP_DISABLED}          DHCPEnabled=${False}
16
17&{ENABLE_DHCP}            DHCPv4=&{DHCP_ENABLED}
18&{DISABLE_DHCP}           DHCPv4=&{DHCP_DISABLED}
19
20${wait_time}              10s
21
22*** Keywords ***
23
24Set Static IPv4 Address To VMI And Verify
25    [Documentation]  Set static IPv4 address to VMI.
26    [Arguments]  ${ip}  ${gateway}  ${netmask}  ${valid_status_code}=${HTTP_ACCEPTED}
27    ...  ${interface}=eth0
28
29    # Description of argument(s):
30    # ip                 VMI IPv4 address.
31    # gateway            Gateway for VMI IP.
32    # netmask            Subnetmask for VMI IP.
33    # valid_status_code  Expected valid status code from GET request. Default is HTTP_ACCEPTED.
34    # interface          VMI interface (eg. eth0 or eth1).
35
36    ${ip_details}=  Create dictionary  Address=${ip}  SubnetMask=${netmask}  Gateway=${gateway}
37    ${ip_data}=  Create List  ${ip_details}
38    ${resp}=  Redfish.Patch  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${interface}
39    ...  body={'IPv4StaticAddresses':${ip_data}}  valid_status_codes=[${valid_status_code}]
40
41    # Wait few seconds for new configuration to get populated on runtime.
42    Sleep  ${wait_time}
43
44    Return From Keyword If  ${valid_status_code} != ${HTTP_ACCEPTED}
45    ${host_power_state}  ${host_state}=   Redfish Get Host State
46    Run Keyword If  '${host_power_state}' == 'On' and '${host_state}' == 'Enabled'
47    ...  Verify VMI Network Interface Details  ${ip}  Static  ${gateway}  ${netmask}  ${interface}
48
49Verify VMI Network Interface Details
50    [Documentation]  Verify VMI network interface details.
51    [Arguments]  ${ip}  ${origin}  ${gateway}  ${netmask}
52    ...  ${interface}=eth0  ${valid_status_code}=${HTTP_OK}
53
54    # Description of argument(s):
55    # ip                 VMI IPv4 address.
56    # origin             Origin of IPv4 address eg. Static or DHCP.
57    # gateway            Gateway for VMI IP.
58    # netmask            Subnetmask for VMI IP.
59    # interface          VMI interface (eg. eth0 or eth1).
60    # valid_status_code  Expected valid status code from GET request. Default is HTTP_OK.
61
62    ${vmi_ip}=  Get VMI Network Interface Details  ${interface}  ${valid_status_code}
63    Should Be Equal As Strings  ${origin}  ${vmi_ip["IPv4_AddressOrigin"]}
64    Should Be Equal As Strings  ${gateway}  ${vmi_ip["IPv4_Gateway"]}
65    Should Be Equal As Strings  ${netmask}  ${vmi_ip["IPv4_SubnetMask"]}
66    Should Be Equal As Strings  ${ip}  ${vmi_ip["IPv4_Address"]}
67
68Delete VMI IPv4 Address
69    [Documentation]  Delete VMI IPv4 address.
70    [Arguments]  ${delete_param}=IPv4StaticAddresses  ${valid_status_code}=${HTTP_ACCEPTED}
71    ...  ${interface}=eth0
72
73    # Description of argument(s):
74    # delete_param       Parameter to be deleted eg. IPv4StaticAddresses or IPv4Addresses.
75    #                    Default is IPv4StaticAddresses.
76    # valid_status_code  Expected valid status code from PATCH request. Default is HTTP_OK.
77    # interface          VMI interface (eg. eth0 or eth1).
78
79    ${data}=  Set Variable  {"${delete_param}": [${Null}]}
80    ${resp}=  Redfish.Patch
81    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${interface}
82    ...  body=${data}  valid_status_codes=[${valid_status_code}]
83
84    Return From Keyword If  ${valid_status_code} != ${HTTP_ACCEPTED}
85
86    # Wait few seconds for configuration to get effective.
87    Sleep  ${wait_time}
88    ${vmi_ip}=  Get VMI Network Interface Details  ${interface}
89    Should Be Empty  ${vmi_ip["IPv4_Address"]}
90
91Set VMI IPv4 Origin
92    [Documentation]  Set VMI IPv4 origin.
93    [Arguments]  ${dhcp_enabled}=${False}  ${valid_status_code}=${HTTP_ACCEPTED}
94    ...  ${interface}=eth0
95
96    # Description of argument(s):
97    # dhcp_enabled       True if user wants to enable DHCP. Default is Static, hence value is set to False.
98    # valid_status_code  Expected valid status code from PATCH request. Default is HTTP_OK.
99    # interface          VMI interface (eg. eth0 or eth1).
100
101    ${data}=  Set Variable If  ${dhcp_enabled} == ${False}  ${DISABLE_DHCP}  ${ENABLE_DHCP}
102    ${resp}=  Redfish.Patch
103    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${interface}
104    ...  body=${data}  valid_status_codes=[${valid_status_code}]
105
106    Sleep  ${wait_time}
107    Return From Keyword If  ${valid_status_code} != ${HTTP_ACCEPTED}
108    ${resp}=  Redfish.Get
109    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${interface}
110    Should Be Equal  ${resp.dict["DHCPv4"]["DHCPEnabled"]}  ${dhcp_enabled}
111
112Get VMI Network Interface Details
113    [Documentation]  Get VMI network interface details.
114    [Arguments]  ${interface}=eth0  ${valid_status_code}=${HTTP_OK}
115
116    # Description of argument(s):
117    # interface          VMI interface (eg. eth0 or eth1).
118    # valid_status_code  Expected valid status code from GET request.
119
120    # Note: It returns a dictionary of VMI ethernet interface parameters.
121
122    ${resp}=  Redfish.Get
123    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${interface}
124    ...  valid_status_codes=[${valid_status_code}]
125
126    ${ip_resp}=  Evaluate  json.loads(r'''${resp.text}''')  json
127
128    ${ip_exists}=  Set Variable If  ${ip_resp["IPv4Addresses"]} == @{empty}  ${False}  ${True}
129    ${static_exists}=  Set Variable If  ${ip_resp["IPv4StaticAddresses"]} == @{empty}  ${False}  ${True}
130
131    ${vmi_ip}=  Run Keyword If   ${ip_exists} == ${True}
132    ...  Create Dictionary  DHCPv4=${${ip_resp["DHCPv4"]["DHCPEnabled"]}}  Id=${ip_resp["Id"]}
133    ...  Description=${ip_resp["Description"]}  IPv4_Address=${ip_resp["IPv4Addresses"][0]["Address"]}
134    ...  IPv4_AddressOrigin=${ip_resp["IPv4Addresses"][0]["AddressOrigin"]}  Name=${ip_resp["Name"]}
135    ...  IPv4_Gateway=${ip_resp["IPv4Addresses"][0]["Gateway"]}
136    ...  InterfaceEnabled=${${ip_resp["InterfaceEnabled"]}}
137    ...  IPv4_SubnetMask=${ip_resp["IPv4Addresses"][0]["SubnetMask"]}
138    ...  IPv4StaticAddresses=${${static_exists}}
139    ...  ELSE
140    ...  Create Dictionary  DHCPv4=${${ip_resp["DHCPv4"]["DHCPEnabled"]}}  Id=${ip_resp["Id"]}
141    ...  Description=${ip_resp["Description"]}  IPv4StaticAddresses=${ip_resp["IPv4StaticAddresses"]}
142    ...  IPv4_Address=${ip_resp["IPv4Addresses"]}  Name=${ip_resp["Name"]}
143    ...  InterfaceEnabled=${${ip_resp["InterfaceEnabled"]}}
144
145    [Return]  &{vmi_ip}
146
147
148Get VMI Interfaces
149    [Documentation]  Get VMI network interface.
150    [Arguments]  ${valid_status_code}=${HTTP_OK}
151
152    # Description of argument(s):
153    # valid_status_code  Expected valid status code from GET request.
154    #                    By default set to ${HTTP_OK}.
155
156    ${resp}=  Redfish.Get  /redfish/v1/Systems/hypervisor/EthernetInterfaces
157    ...  valid_status_codes=[${valid_status_code}]
158
159    ${resp}=  Evaluate  json.loads(r'''${resp.text}''')  json
160    ${interfaces_uri}=  Set Variable  ${resp["Members"]}
161    ${interface_list}=  Create List
162    ${number_of_interfaces}=  Get Length  ${interfaces_uri}
163    FOR  ${interface}  IN RANGE  ${number_of_interfaces}
164        ${_}  ${interface_value}=  Split String From Right  ${interfaces_uri[${interface}]}[@odata.id]  /  1
165        Append To List  ${interface_list}  ${interface_value}
166    END
167
168   [Return]  @{interface_list}
169
170
171Verify VMI EthernetInterfaces
172    [Documentation]  Verify VMI ethernet interfaces.
173    [Arguments]  ${valid_status_code}=${HTTP_OK}
174
175    # Description of argument(s):
176    # valid_status_code  Expected valid status code from GET request.
177
178    ${resp}=  Redfish.Get  /redfish/v1/Systems/hypervisor/EthernetInterfaces
179    ...  valid_status_codes=[${valid_status_code}]
180
181    ${resp}=  Evaluate  json.loads(r'''${resp.text}''')  json
182    ${interfaces}=  Set Variable  ${resp["Members"]}
183
184    ${number_of_interfaces}=  Get Length  ${interfaces}
185    FOR  ${i}  IN RANGE  ${number_of_interfaces}
186        Should Be Equal As Strings  ${interfaces[${i}]}[@odata.id]
187        ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/eth${i}
188    END
189    Should Be Equal  ${resp["Members@odata.count"]}  ${number_of_interfaces}
190
191Get And Set Static VMI IP
192    [Documentation]  Get a suitable VMI IP and set it.
193    [Arguments]   ${host}=${OPENBMC_HOST}  ${network_active_channel}=${CHANNEL_NUMBER}
194    ...  ${interface}=eth0  ${valid_status_code}=${HTTP_ACCEPTED}
195
196    # Description of argument(s):
197    # host                    BMC host name or IP address.
198    # network_active_channel  Ethernet channel number (e.g.1 or 2).
199    # interface               VMI interface (eg. eth0 or eth1).
200    # valid_status_code       Expected valid status code from PATCH request. Default is HTTP_ACCEPTED.
201
202    ${vmi_ip}=  Get First Non Pingable IP From Subnet  ${host}
203    ${bmc_ip_data}=  Get Network Configuration  ${network_active_channel}
204
205    Set Static IPv4 Address To VMI And Verify  ${vmi_ip}  ${bmc_ip_data[0]['Gateway']}
206    ...  ${bmc_ip_data[0]['SubnetMask']}  ${valid_status_code}  ${interface}
207
208    [Return]   ${vmi_ip}  ${bmc_ip_data}
209