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