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    Wait Until Keyword Succeeds  1min  5sec
46    ...  Redfish.Patch  ${REDFISH_BASE_URI}Managers/${MANAGER_ID}  body={'DateTime': '${date_time}'}  &{kwargs}
47
48
49Set Time To Manual Mode
50    [Documentation]  Set date time to manual mode via Redfish.
51
52    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTP':{'ProtocolEnabled': ${False}}}
53    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
54
55
56Set BMC Date And Verify
57    [Documentation]  Set BMC Date Time at a given host state and verify.
58    [Arguments]  ${host_state}
59    # Description of argument(s):
60    # host_state  Host state at which date time will be updated for verification
61    #             (eg. on, off).
62
63    Run Keyword If  '${host_state}' == 'on'
64    ...    Redfish Power On  stack_mode=skip
65    ...  ELSE
66    ...    Redfish Power off  stack_mode=skip
67    ${current_date}=  Get Current Date  time_zone=UTC
68    ${new_value}=  Subtract Time From Date  ${current_date}  1 day
69    Redfish Set DateTime  ${new_value}  valid_status_codes=[${HTTP_OK}]
70    ${current_value}=  Redfish Get DateTime
71    ${time_diff}=  Subtract Date From Date  ${current_value}  ${new_value}
72    Should Be True  '${time_diff}'<='3'
73
74
75Set NTP state
76    [Documentation]  Set NTP service inactive.
77    [Arguments]  ${state}
78
79    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTP':{'ProtocolEnabled': ${state}}}
80    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
81
82
83Get NTP Initial Status
84    [Documentation]  Get NTP service Status.
85
86    ${original_ntp}=  Redfish.Get Attribute  ${REDFISH_NW_PROTOCOL_URI}  NTP
87    Set Suite Variable  ${original_ntp}
88
89
90Restore NTP Status
91    [Documentation]  Restore NTP Status.
92
93    Run Keyword If  '${original_ntp["ProtocolEnabled"]}' == 'True'
94    ...    Set NTP state  ${TRUE}
95    ...  ELSE  Set NTP state  ${FALSE}
96
97
98Verify NTP Servers Are Populated
99    [Documentation]  Redfish GET request /redfish/v1/Managers/${MANAGER_ID}/NetworkProtocol response
100    ...              and verify if NTP servers are populated.
101
102    ${network_protocol}=  Redfish.Get Properties  ${REDFISH_NW_PROTOCOL_URI}
103    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_1}
104    ...  msg=NTP server value ${ntp_server_1} not stored.
105    Should Contain  ${network_protocol["NTP"]["NTPServers"]}  ${ntp_server_2}
106    ...  msg=NTP server value ${ntp_server_2} not stored.
107
108
109Verify System Time Sync Status
110    [Documentation]  Verify the status of service systemd-timesyncd matches the NTP protocol enabled state.
111    [Arguments]  ${expected_sync_status}=${True}
112
113    # Description of argument(s):
114    # expected_sync_status  expected status at which NTP protocol enabled will be updated for verification
115    #                       (eg. True, False).
116
117    ${resp}=  BMC Execute Command
118    ...  systemctl status systemd-timesyncd
119    ...  ignore_err=${1}
120    ${sync_status}=  Get Lines Matching Regexp  ${resp[0]}  .*Active.*
121    Run Keyword If  ${expected_sync_status}==${True}
122    ...  Should Contain  ${sync_status}  active (running)
123    Run Keyword If  ${expected_sync_status}==${False}
124    ...  Should Contain  ${sync_status}  inactive (dead)
125
126
127Enable NTP And Add NTP Address
128    [Documentation]  Enable NTP Protocol and Add NTP Address.
129
130    Set NTP state  ${TRUE}
131
132    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}  body={'NTP':{'NTPServers': ${NTP_SERVER_ADDRESSES}}}
133    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
134
135    Wait Until Keyword Succeeds  1 min  10 sec  Check Date And Time Was Changed
136
137
138Check Date And Time Was Changed
139    [Documentation]  Verify date was current date and time.
140
141    ${new_date_time}=  CLI Get BMC DateTime
142    Should Not Contain  ${new_date_time}  ${year_without_ntp}
143
144
145Restore NTP Mode
146    [Documentation]  Restore the original NTP mode.
147
148
149    Return From Keyword If  &{original_ntp} == &{EMPTY}
150    Print Timen  Restore NTP Mode.
151    Redfish.Patch  ${REDFISH_NW_PROTOCOL_URI}
152    ...  body={'NTP':{'ProtocolEnabled': ${original_ntp["ProtocolEnabled"]}}}
153    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
154
155
156