1*** Settings ***
2Documentation    Module to test dcmi management controller functionality.
3Resource         ../../lib/ipmi_client.robot
4Resource         ../../lib/openbmc_ffdc.robot
5Resource         ../../lib/bmc_network_utils.robot
6Resource         ../../lib/boot_utils.robot
7Resource         ../../lib/bmc_redfish_utils.robot
8Variables        ../../data/ipmi_raw_cmd_table.py
9Variables        ../../data/dcmi_raw_cmd_table.py
10Variables        ../../data/ipmi_variable.py
11Library          ../../lib/ipmi_utils.py
12Library          ../../lib/utilities.py
13Library          ../../lib/utils.py
14Library          JSONLibrary
15
16Force Tags       DCMI_Management_Controller
17
18*** Variables ***
19
20${hostname_file_path}  /etc/hostname
21
22*** Test Cases ***
23Validate IPMI Response Length
24    [Documentation]  Check ipmi response length.
25    [Tags]  Validate_IPMI_Response_Length
26
27    ${rsp}=  Get DCMI Management Controller Identifier String
28    @{ipmi_cmd_rsp_list}=  Split String  ${rsp}
29    # ipmi_cmd_rsp_list = ["00", "0a", "00", "01", "02", "03",
30    #                      "04", "05", "06", "07", "08", "09"]
31    # rsp_length = 10
32    # string_length = 0a
33    ${rsp_length}=  Get Length  ${ipmi_cmd_rsp_list[2:]}
34    ${string_length}=  Get Response Length In Hex  ${rsp_length}
35
36    # ipmi_cmd_rsp_list[1] = 0a
37    # string_length = 0a
38    # the above condition is equal.
39    # suppose if string_length and ipmi_cmd_rsp_list[1] not matches
40    #  then it will fails.
41    Should Be Equal As Strings  ${ipmi_cmd_rsp_list[1]}  ${string_length}
42    ...  msg=Id string length in ipmi response is showing wrongly
43
44Test Hostname Is Same With Management Controller Identifier String
45    [Documentation]  Check hostname was verified with get management
46    ...              controller identifier string.
47    [Tags]  Test_Hostname_Is_Same_With_Management_Controller_Identifier_String
48
49    ${rsp}=  Get DCMI Management Controller Identifier String
50    @{ipmi_cmd_rsp_list}=  Split String  ${rsp}
51    ${bmc_console_hostname_bytes_list}=  Get Hostname From BMC Console
52
53    Lists Should Be Equal  ${ipmi_cmd_rsp_list[2:]}  ${bmc_console_hostname_bytes_list}
54    ...  msg=response get from dcmi get mcid cmd and hostname from "cat /etc/os-release" cmd is not same.
55
56Test Get Management Controller Identifier String
57    [Documentation]  Validate set and get mcid string.
58    [Tags]  Test_Get_Management_Controller_Identifier_String
59    [Setup]  Get Default MCID
60    [Teardown]  Set Default MCID
61
62    # Set Hostname via DCMI Management Controller Identifier String Command.
63    ${cmd_rsp}=  Set DCMI Management Controller Identifier String
64    @{cmd_rsp_list}=  Split String  ${cmd_rsp}
65    Run Keyword And Continue On Failure
66    ...  Valid Value  cmd_rsp_list[1]  valid_values=['${number_of_bytes_to_write}']
67
68    ${rsp}=  Get DCMI Management Controller Identifier String
69    @{ipmi_cmd_rsp_list}=  Split String  ${rsp}
70
71    # Verify number of bytes that was set and id string length are same.
72    ${string_length}=  Get Response Length In Hex  ${random_int}
73    Run Keyword And Continue On Failure
74    ...  Should Be Equal As Strings  ${ipmi_cmd_rsp_list[1]}  ${string_length}
75    ...  msg=Number of bytes that was set and id string length are not same.
76
77    # Verify ID String Length and data.
78    # ipmi_cmd_rsp_list = ["00", "0a", "00", "01", "02", "03", "04",
79    #                      "05", "06", "07", "08", "09"]
80    # rsp_length = 10
81    # string_length = 0a
82    # ipmi_cmd_rsp_list[1] = 0a
83    # ipmi_cmd_rsp_list[1] is equal to string_length
84    # the above condition is equal.
85    # suppose if string_length and ipmi_cmd_rsp_list[1] not matches then
86    # it will fails.
87    ${rsp_length}=  Get Length  ${ipmi_cmd_rsp_list[2:]}
88    ${string_length}=  Get Response Length In Hex  ${rsp_length}
89    Run Keyword And Continue On Failure
90    ...  Should Be Equal As Strings  ${ipmi_cmd_rsp_list[1]}  ${string_length}
91    ...  msg=Id string length in ipmi response is showing wrongly
92
93    # Verify get dcmi management controller identifier string command response
94    # and the bytes used for Set DCMI MCID string.
95    ${set_dcmi_mcid_cmd}=
96    ...  convert_prefix_hex_list_to_non_prefix_hex_list  ${string_hex_list}
97    Run Keyword And Continue On Failure
98    ...  Lists Should Be Equal  ${ipmi_cmd_rsp_list[2:]}  ${set_dcmi_mcid_cmd}
99    ...  msg=Bytes given in dcmi set mcid command and string bytes got from dcmi get mcid command are not same
100
101    # Verify Hostname of cat /etc/hostname and get dcmi management controller identifier string command.
102    ${bytes_list}=  Get Hostname From BMC Console
103    Run Keyword And Continue On Failure
104    ...  Lists Should Be Equal  ${ipmi_cmd_rsp_list[2:]}  ${bytes_list}
105    ...  msg=Bytes got from dcmi get mcid command and hostname from "cat /etc/os-release" command is not same.
106
107*** Keywords ***
108Get Default MCID
109    [Documentation]  Get default mcid.
110
111    ${default_mcid}=  Get DCMI Management Controller Identifier String
112    Set Test Variable  ${default_mcid}
113
114Set Default MCID
115    [Documentation]  Set default mcid.
116
117    @{ipmi_cmd_rsp_list}=  Split String  ${default_mcid}
118    ${number_of_bytes_to_write}=  Set Variable  ${ipmi_cmd_rsp_list[1]}
119    ${bytes_in_int}=  Convert To Integer  ${number_of_bytes_to_write}  16
120    ${bytes_to_write}=  Evaluate  ${bytes_in_int} + 1
121    ${no_of_bytes_to_write}=  Get Response Length In Hex  ${bytes_to_write}
122    @{tmp_lst}=  Create List
123    FOR  ${bytes}  IN  @{ipmi_cmd_rsp_list[2:]}
124      Append To List  ${tmp_lst}  0x${bytes}
125    END
126    ${default_hex}=  Catenate  @{tmp_lst}
127
128    ${cmd}=  Catenate  ${DCMI_RAW_CMD['DCMI']['MANAGEMENT_CONTROLLER_IDENTIFIER_STRING']['SET']}
129    ...  0x${no_of_bytes_to_write} ${default_hex}
130    Run External IPMI Raw Command  ${cmd}
131
132Get DCMI Management Controller Identifier String
133    [Documentation]  Get DCMI MCID String.
134
135    ${mcid_get_cmd}=  Catenate  ${DCMI_RAW_CMD['DCMI']['MANAGEMENT_CONTROLLER_IDENTIFIER_STRING']['GET']}
136    ${resp}=  Run External IPMI Raw Command  ${mcid_get_cmd}
137
138    [Return]  ${resp}
139
140Set DCMI Management Controller Identifier String
141    [Documentation]  Set DCMI MCID String.
142
143    # 16 bytes maximum as per dcmi spec
144    ${random_int}=  Evaluate  random.randint(1, 15)  modules=random
145    ${random_string}=  Generate Random String  ${random_int}
146    ${string_hex_list}=  convert_name_into_bytes_with_prefix  ${random_string}
147    ${random_hex}=  Catenate  @{string_hex_list}
148    ${number_of_random_string}=  Evaluate  ${random_int} + 1
149    ${number_of_bytes_to_write}=  Get Response Length In Hex  ${number_of_random_string}
150
151    ${mcid_set_cmd}=  Catenate  ${DCMI_RAW_CMD['DCMI']['MANAGEMENT_CONTROLLER_IDENTIFIER_STRING']['SET']}
152    ...  0x${number_of_bytes_to_write} ${random_hex} 0x00
153    ${resp}=  Run External IPMI Raw Command  ${mcid_set_cmd}
154
155    Set Test Variable  ${string_hex_list}
156    Set Test Variable  ${random_int}
157    Set Test Variable  ${number_of_bytes_to_write}
158
159    [Return]  ${resp}
160
161Get Hostname From BMC Console
162    [Documentation]  Get hostname.
163
164    ${cmd}=  Catenate  cat ${hostname_file_path}
165    ${bmc_hostname}=  BMC Execute Command  ${cmd}
166    ${name}=  Convert To List  ${bmc_hostname}
167    ${hostname_bytes}=  convert_name_into_bytes_without_prefix  ${name[0]}
168
169    [Return]  ${hostname_bytes}
170
171Get Response Length In Hex
172    [Documentation]  Get response length in hex.
173    [Arguments]  ${resp_length}
174
175    ${length}=  Convert To Hex  ${resp_length}
176    ${length_1}=  Get Length  ${length}
177    ${length_2}=  Set Variable IF
178    ...  '${length_1}' == '1'  0${length}
179    ...  '${length_1}' != '1'  ${length}
180    ${resp_length_3}=  Convert To Lower Case  ${length_2}
181
182    [Return]  ${resp_length_3}
183