1*** Settings ***
2Documentation  This module provides general keywords for date time and ntp.
3
4Resource                     ../../lib/resource.robot
5Resource                     ../../lib/bmc_redfish_resource.robot
6Resource                     ../../lib/common_utils.robot
7Resource                     ../../lib/openbmc_ffdc.robot
8Resource                     ../../lib/utils.robot
9Resource                     ../../lib/rest_client.robot
10Library                      ../../lib/gen_robot_valid.py
11
12*** Variables ***
13
14${year_without_ntp}          1970
15
16*** Keywords ***
17
18
19Redfish Get DateTime
20    [Documentation]  Returns BMC Datetime value from Redfish.
21
22    ${date_time}=  Redfish.Get Attribute  ${REDFISH_BASE_URI}Managers/${MANAGER_ID}  DateTime
23    RETURN  ${date_time}
24
25
26Redfish Set DateTime
27    [Documentation]  Set DateTime using Redfish.
28    [Arguments]  ${date_time}=${EMPTY}  &{kwargs}
29    # Description of argument(s):
30    # date_time                     New time to set for BMC (eg.
31    #                               "2019-06-30 09:21:28"). If this value is
32    #                               empty, it will be set to the UTC current
33    #                               date time of the local system.
34    # kwargs                        Additional parameters to be passed directly to
35    #                               th Redfish.Patch function.  A good use for
36    #                               this is when testing a bad date-time, the
37    #                               caller can specify
38    #                               valid_status_codes=[${HTTP_BAD_REQUEST}].
39
40    # Assign default value of UTC current date time if date_time is empty.
41    ${date_time}=  Run Keyword If
42    ...  '${date_time}' == '${EMPTY}'  Get Current Date  time_zone=UTC
43    ...  ELSE
44    ...  Set Variable  ${date_time}
45    # Change date format to 2024-03-07T07:58:50+00:00 from 2024-03-07 07:58:50.000.
46    IF  "T" in "${date_time}"
47        Wait Until Keyword Succeeds  1min  5sec
48        ...  Redfish.Patch  ${REDFISH_BASE_URI}Managers/${MANAGER_ID}  body={'DateTime': '${date_time}'}
49        ...  &{kwargs}  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
50    ELSE
51        ${date_time_formatted}=  Convert Date  ${date_time}  result_format=%Y-%m-%dT%H:%M:%S+00:00
52        Wait Until Keyword Succeeds  1min  5sec
53        ...  Redfish.Patch  ${REDFISH_BASE_URI}Managers/${MANAGER_ID}  body={'DateTime': '${date_time_formatted}'}
54        ...  &{kwargs}  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
55    END
56
57
58Set Time To Manual Mode
59    [Documentation]  Set date time to manual mode via Redfish.
60
61    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTP':{'ProtocolEnabled': ${False}}}
62    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
63
64
65Set BMC Date And Verify
66    [Documentation]  Set BMC Date Time at a given host state and verify.
67    [Arguments]  ${host_state}
68    # Description of argument(s):
69    # host_state  Host state at which date time will be updated for verification
70    #             (eg. on, off).
71
72    Run Keyword If  '${host_state}' == 'on'
73    ...    Redfish Power On  stack_mode=skip
74    ...  ELSE
75    ...    Redfish Power off  stack_mode=skip
76    ${current_date}=  Get Current Date  time_zone=UTC
77    ${new_value}=  Subtract Time From Date  ${current_date}  1 day
78    Redfish Set DateTime  ${new_value}  valid_status_codes=[${HTTP_OK}]
79    ${current_value}=  Redfish Get DateTime
80    ${time_diff}=  Subtract Date From Date  ${current_value}  ${new_value}
81    Should Be True  '${time_diff}'<='3'
82
83
84Set NTP state
85    [Documentation]  Set NTP service inactive.
86    [Arguments]  ${state}
87
88    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTP':{'ProtocolEnabled': ${state}}}
89    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
90
91
92Get NTP Initial Status
93    [Documentation]  Get NTP service Status.
94
95    ${original_ntp}=  Redfish.Get Attribute  ${REDFISH_NW_PROTOCOL_URI}  NTP
96    Set Suite Variable  ${original_ntp}
97
98
99Restore NTP Status
100    [Documentation]  Restore NTP Status.
101
102    Run Keyword If  '${original_ntp["ProtocolEnabled"]}' == 'True'
103    ...    Set NTP state  ${TRUE}
104    ...  ELSE  Set NTP state  ${FALSE}
105
106
107Verify NTP Servers Are Populated
108    [Documentation]  Redfish GET request /redfish/v1/Managers/${MANAGER_ID}/NetworkProtocol response
109    ...              and verify if NTP servers are populated.
110
111    ${network_protocol}=  Redfish.Get Properties  ${REDFISH_NW_PROTOCOL_URI}
112    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_1}
113    ...  msg=NTP server value ${ntp_server_1} not stored.
114    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_2}
115    ...  msg=NTP server value ${ntp_server_2} not stored.
116
117
118Verify System Time Sync Status
119    [Documentation]  Verify the status of service systemd-timesyncd matches the NTP protocol enabled state.
120    [Arguments]  ${expected_sync_status}=${True}
121
122    # Description of argument(s):
123    # expected_sync_status  expected status at which NTP protocol enabled will be updated for verification
124    #                       (eg. True, False).
125
126    ${resp}=  BMC Execute Command
127    ...  systemctl status systemd-timesyncd
128    ...  ignore_err=${1}
129    ${sync_status}=  Get Lines Matching Regexp  ${resp[0]}  .*Active.*
130    Run Keyword If  ${expected_sync_status}==${True}
131    ...  Should Contain  ${sync_status}  active (running)
132    Run Keyword If  ${expected_sync_status}==${False}
133    ...  Should Contain  ${sync_status}  inactive (dead)
134
135
136Enable NTP And Add NTP Address
137    [Documentation]  Enable NTP Protocol and Add NTP Address.
138
139    Set NTP state  ${TRUE}
140
141    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTP':{'NTPServers': ${NTP_SERVER_ADDRESSES}}}
142    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
143
144    Wait Until Keyword Succeeds  1 min  10 sec  Check Date And Time Was Changed
145
146
147Check Date And Time Was Changed
148    [Documentation]  Verify date was current date and time.
149
150    ${new_date_time}=  CLI Get BMC DateTime
151    Should Not Contain  ${new_date_time}  ${year_without_ntp}
152
153
154Restore NTP Mode
155    [Documentation]  Restore the original NTP mode.
156
157
158    Return From Keyword If  &{original_ntp} == &{EMPTY}
159    Print Timen  Restore NTP Mode.
160    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}
161    ...  body={'NTP':{'ProtocolEnabled': ${original_ntp["ProtocolEnabled"]}}}
162    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
163
164
165