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