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