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