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
21    # Description of argument(s):
22    # ip                 VMI IPv4 address.
23    # gateway            Gateway for VMI IP.
24    # netmask            Subnetmask for VMI IP.
25    # valid_status_code  Expected valid status code from GET request. Default is HTTP_ACCEPTED.
26
27    ${data}=  Set Variable
28    ...  {"IPv4StaticAddresses":
29    ...   [{"Address": "${ip}",
30    ...     "SubnetMask":"${netmask}",
31    ...     "Gateway": "${gateway}"}
32    ...   ]}
33
34    ${resp}=  Redfish.Patch
35    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${ethernet_interface}
36    ...  body=${data}  valid_status_codes=[${valid_status_code}]
37
38    # Wait few seconds for new configuration to get populated on runtime.
39    Sleep  ${wait_time}
40
41    Return From Keyword If  ${valid_status_code} != ${HTTP_ACCEPTED}
42    ${host_power_state}  ${host_state}=   Redfish Get Host State
43    Run Keyword If  '${host_power_state}' == 'On' and '${host_state}' == 'Enabled'
44    ...  Verify VMI Network Interface Details  ${ip}  Static  ${gateway}  ${netmask}
45
46Verify VMI Network Interface Details
47    [Documentation]  Verify VMI network interface details.
48    [Arguments]  ${ip}  ${origin}  ${gateway}  ${netmask}
49    ...  ${valid_status_code}=${HTTP_OK}
50
51    # Description of argument(s):
52    # ip                 VMI IPv4 address.
53    # origin             Origin of IPv4 address eg. Static or DHCP.
54    # gateway            Gateway for VMI IP.
55    # netmask            Subnetmask for VMI IP.
56    # valid_status_code  Expected valid status code from GET request. Default is HTTP_OK.
57
58    ${vmi_ip}=  Get VMI Network Interface Details  ${valid_status_code}
59    Should Be Equal As Strings  ${origin}  ${vmi_ip["IPv4_AddressOrigin"]}
60    Should Be Equal As Strings  ${gateway}  ${vmi_ip["IPv4_Gateway"]}
61    Should Be Equal As Strings  ${netmask}  ${vmi_ip["IPv4_SubnetMask"]}
62    Should Be Equal As Strings  ${ip}  ${vmi_ip["IPv4_Address"]}
63
64Delete VMI IPv4 Address
65    [Documentation]  Delete VMI IPv4 address.
66    [Arguments]  ${delete_param}=IPv4StaticAddresses  ${valid_status_code}=${HTTP_ACCEPTED}
67
68    # Description of argument(s):
69    # delete_param       Parameter to be deleted eg. IPv4StaticAddresses or IPv4Addresses.
70    #                    Default is IPv4StaticAddresses.
71    # valid_status_code  Expected valid status code from PATCH request. Default is HTTP_OK.
72
73    ${data}=  Set Variable  {"${delete_param}": [${Null}]}
74    ${resp}=  Redfish.Patch
75    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${ethernet_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
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
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
90    ${data}=  Set Variable If  ${dhcp_enabled} == ${False}  ${DISABLE_DHCP}  ${ENABLE_DHCP}
91    ${resp}=  Redfish.Patch
92    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${ethernet_interface}
93    ...  body=${data}  valid_status_codes=[${valid_status_code}]
94
95    Sleep  ${wait_time}
96    Return From Keyword If  ${valid_status_code} != ${HTTP_ACCEPTED}
97    ${resp}=  Redfish.Get
98    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${ethernet_interface}
99    Should Be Equal  ${resp.dict["DHCPv4"]["DHCPEnabled"]}  ${dhcp_enabled}
100
101Get VMI Network Interface Details
102    [Documentation]  Get VMI network interface details.
103    [Arguments]  ${valid_status_code}=${HTTP_OK}
104
105    # Description of argument(s):
106    # valid_status_code  Expected valid status code from GET request.
107
108    # Note: It returns a dictionary of VMI ethernet interface parameters.
109
110    ${resp}=  Redfish.Get
111    ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/${ethernet_interface}
112    ...  valid_status_codes=[${valid_status_code}]
113
114    ${ip_resp}=  Evaluate  json.loads(r'''${resp.text}''')  json
115
116    ${ip_exists}=  Set Variable If  ${ip_resp["IPv4Addresses"]} == @{empty}  ${False}  ${True}
117    ${static_exists}=  Set Variable If  ${ip_resp["IPv4StaticAddresses"]} == @{empty}  ${False}  ${True}
118
119    ${vmi_ip}=  Run Keyword If   ${ip_exists} == ${True}
120    ...  Create Dictionary  DHCPv4=${${ip_resp["DHCPv4"]["DHCPEnabled"]}}  Id=${ip_resp["Id"]}
121    ...  Description=${ip_resp["Description"]}  IPv4_Address=${ip_resp["IPv4Addresses"][0]["Address"]}
122    ...  IPv4_AddressOrigin=${ip_resp["IPv4Addresses"][0]["AddressOrigin"]}  Name=${ip_resp["Name"]}
123    ...  IPv4_Gateway=${ip_resp["IPv4Addresses"][0]["Gateway"]}
124    ...  InterfaceEnabled=${${ip_resp["InterfaceEnabled"]}}
125    ...  IPv4_SubnetMask=${ip_resp["IPv4Addresses"][0]["SubnetMask"]}
126    ...  IPv4StaticAddresses=${${static_exists}}
127    ...  ELSE
128    ...  Create Dictionary  DHCPv4=${${ip_resp["DHCPv4"]["DHCPEnabled"]}}  Id=${ip_resp["Id"]}
129    ...  Description=${ip_resp["Description"]}  IPv4StaticAddresses=${ip_resp["IPv4StaticAddresses"]}
130    ...  IPv4_Address=${ip_resp["IPv4Addresses"]}  Name=${ip_resp["Name"]}
131    ...  InterfaceEnabled=${${ip_resp["InterfaceEnabled"]}}
132
133    [Return]  &{vmi_ip}
134
135Verify VMI EthernetInterfaces
136    [Documentation]  Verify VMI ethernet interfaces.
137    [Arguments]  ${valid_status_code}=${HTTP_OK}
138
139    # Description of argument(s):
140    # valid_status_code  Expected valid status code from GET request.
141
142    ${resp}=  Redfish.Get  /redfish/v1/Systems/hypervisor/EthernetInterfaces
143    ...  valid_status_codes=[${valid_status_code}]
144
145    ${resp}=  Evaluate  json.loads(r'''${resp.text}''')  json
146    ${interfaces}=  Set Variable  ${resp["Members"]}
147
148    ${number_of_interfaces}=  Get Length  ${interfaces}
149    FOR  ${i}  IN RANGE  ${number_of_interfaces}
150        Should Be Equal As Strings  ${interfaces[${i}]}[@odata.id]
151        ...  /redfish/v1/Systems/hypervisor/EthernetInterfaces/eth${i}
152    END
153    Should Be Equal  ${resp["Members@odata.count"]}  ${number_of_interfaces}
154