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