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
80    # Wait few seconds for configuration to get effective.
81    Sleep  ${wait_time}
82    ${vmi_ip}=  Get VMI Network Interface Details  ${interface}
83    Should Be Empty  ${vmi_ip["IPv4_Address"]}
84
85Set VMI IPv4 Origin
86    [Documentation]  Set VMI IPv4 origin.
87    [Arguments]  ${dhcp_enabled}=${False}  ${valid_status_code}=${HTTP_ACCEPTED}
88    ...  ${interface}=eth0
89
90    # Description of argument(s):
91    # dhcp_enabled       True if user wants to enable DHCP. Default is Static, hence value is set to False.
92    # valid_status_code  Expected valid status code from PATCH request. Default is HTTP_OK.
93    # interface          VMI interface (eg. eth0 or eth1).
94
95    ${data}=  Set Variable If  ${dhcp_enabled} == ${False}  ${DISABLE_DHCP}  ${ENABLE_DHCP}
96    ${resp}=  Redfish.Patch
97    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${interface}
98    ...  body=${data}  valid_status_codes=[${valid_status_code}]
99
100    Sleep  ${wait_time}
101    Return From Keyword If  ${valid_status_code} != ${HTTP_ACCEPTED}
102    ${resp}=  Redfish.Get
103    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${interface}
104    Should Be Equal  ${resp.dict["DHCPv4"]["DHCPEnabled"]}  ${dhcp_enabled}
105
106Get VMI Network Interface Details
107    [Documentation]  Get VMI network interface details.
108    [Arguments]  ${interface}=eth0  ${valid_status_code}=${HTTP_OK}
109
110    # Description of argument(s):
111    # interface          VMI interface (eg. eth0 or eth1).
112    # valid_status_code  Expected valid status code from GET request.
113
114    # Note: It returns a dictionary of VMI ethernet interface parameters.
115
116    ${resp}=  Redfish.Get
117    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${interface}
118    ...  valid_status_codes=[${valid_status_code}]
119
120    ${ip_resp}=  Evaluate  json.loads(r'''${resp.text}''')  json
121
122    ${ip_exists}=  Set Variable If  ${ip_resp["IPv4Addresses"]} == @{empty}  ${False}  ${True}
123    ${static_exists}=  Set Variable If  ${ip_resp["IPv4StaticAddresses"]} == @{empty}  ${False}  ${True}
124
125    ${vmi_ip}=  Run Keyword If   ${ip_exists} == ${True}
126    ...  Create Dictionary  DHCPv4=${${ip_resp["DHCPv4"]["DHCPEnabled"]}}  Id=${ip_resp["Id"]}
127    ...  Description=${ip_resp["Description"]}  IPv4_Address=${ip_resp["IPv4Addresses"][0]["Address"]}
128    ...  IPv4_AddressOrigin=${ip_resp["IPv4Addresses"][0]["AddressOrigin"]}  Name=${ip_resp["Name"]}
129    ...  IPv4_Gateway=${ip_resp["IPv4Addresses"][0]["Gateway"]}
130    ...  InterfaceEnabled=${${ip_resp["InterfaceEnabled"]}}
131    ...  IPv4_SubnetMask=${ip_resp["IPv4Addresses"][0]["SubnetMask"]}
132    ...  IPv4StaticAddresses=${${static_exists}}
133    ...  ELSE
134    ...  Create Dictionary  DHCPv4=${${ip_resp["DHCPv4"]["DHCPEnabled"]}}  Id=${ip_resp["Id"]}
135    ...  Description=${ip_resp["Description"]}  IPv4StaticAddresses=${ip_resp["IPv4StaticAddresses"]}
136    ...  IPv4_Address=${ip_resp["IPv4Addresses"]}  Name=${ip_resp["Name"]}
137    ...  InterfaceEnabled=${${ip_resp["InterfaceEnabled"]}}
138
139    [Return]  &{vmi_ip}
140
141
142Get VMI Interfaces
143    [Documentation]  Get VMI network interface.
144    [Arguments]  ${valid_status_code}=${HTTP_OK}
145
146    # Description of argument(s):
147    # valid_status_code  Expected valid status code from GET request.
148    #                    By default set to ${HTTP_OK}.
149
150    ${resp}=  Redfish.Get  /redfish/v1/Systems/hypervisor/EthernetInterfaces
151    ...  valid_status_codes=[${valid_status_code}]
152
153    ${resp}=  Evaluate  json.loads(r'''${resp.text}''')  json
154    ${interfaces_uri}=  Set Variable  ${resp["Members"]}
155    ${interface_list}=  Create List
156    ${number_of_interfaces}=  Get Length  ${interfaces_uri}
157    FOR  ${interface}  IN RANGE  ${number_of_interfaces}
158        ${_}  ${interface_value}=  Split String From Right  ${interfaces_uri[${interface}]}[@odata.id]  /  1
159        Append To List  ${interface_list}  ${interface_value}
160    END
161
162   [Return]  @{interface_list}
163
164
165Verify VMI EthernetInterfaces
166    [Documentation]  Verify VMI ethernet interfaces.
167    [Arguments]  ${valid_status_code}=${HTTP_OK}
168
169    # Description of argument(s):
170    # valid_status_code  Expected valid status code from GET request.
171
172    ${resp}=  Redfish.Get  /redfish/v1/Systems/hypervisor/EthernetInterfaces
173    ...  valid_status_codes=[${valid_status_code}]
174
175    ${resp}=  Evaluate  json.loads(r'''${resp.text}''')  json
176    ${interfaces}=  Set Variable  ${resp["Members"]}
177
178    ${number_of_interfaces}=  Get Length  ${interfaces}
179    FOR  ${i}  IN RANGE  ${number_of_interfaces}
180        Should Be Equal As Strings  ${interfaces[${i}]}[@odata.id]
181        ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/eth${i}
182    END
183    Should Be Equal  ${resp["Members@odata.count"]}  ${number_of_interfaces}
184
185Get And Set Static VMI IP
186    [Documentation]  Get a suitable VMI IP and set it.
187    [Arguments]   ${host}=${OPENBMC_HOST}  ${network_active_channel}=${CHANNEL_NUMBER}
188    ...  ${interface}=eth0  ${valid_status_code}=${HTTP_ACCEPTED}
189
190    # Description of argument(s):
191    # host                    BMC host name or IP address.
192    # network_active_channel  Ethernet channel number (e.g.1 or 2).
193    # interface               VMI interface (eg. eth0 or eth1).
194    # valid_status_code       Expected valid status code from PATCH request. Default is HTTP_ACCEPTED.
195
196    ${vmi_ip}=  Get First Non Pingable IP From Subnet  ${host}
197    ${bmc_ip_data}=  Get Network Configuration  ${network_active_channel}
198
199    Set Static IPv4 Address To VMI And Verify  ${vmi_ip}  ${bmc_ip_data[0]['Gateway']}
200    ...  ${bmc_ip_data[0]['SubnetMask']}  ${valid_status_code}  ${interface}
201
202    [Return]   ${vmi_ip}  ${bmc_ip_data}
203