1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const DateTimeStore = {
5  namespaced: true,
6  state: {
7    ntpServers: [],
8    isNtpProtocolEnabled: null,
9  },
10  getters: {
11    ntpServers: (state) => state.ntpServers,
12    isNtpProtocolEnabled: (state) => state.isNtpProtocolEnabled,
13  },
14  mutations: {
15    setNtpServers: (state, ntpServers) => (state.ntpServers = ntpServers),
16    setIsNtpProtocolEnabled: (state, isNtpProtocolEnabled) =>
17      (state.isNtpProtocolEnabled = isNtpProtocolEnabled),
18  },
19  actions: {
20    async getNtpData({ commit }) {
21      return await api
22        .get('/redfish/v1/Managers/bmc/NetworkProtocol')
23        .then((response) => {
24          const ntpServers = response.data.NTP.NTPServers;
25          const isNtpProtocolEnabled = response.data.NTP.ProtocolEnabled;
26          commit('setNtpServers', ntpServers);
27          commit('setIsNtpProtocolEnabled', isNtpProtocolEnabled);
28        })
29        .catch((error) => {
30          console.log(error);
31        });
32    },
33    async updateDateTime({ state }, dateTimeForm) {
34      const ntpData = {
35        NTP: {
36          ProtocolEnabled: dateTimeForm.ntpProtocolEnabled,
37        },
38      };
39      if (dateTimeForm.ntpProtocolEnabled) {
40        ntpData.NTP.NTPServers = dateTimeForm.ntpServersArray;
41      }
42      return await api
43        .patch(`/redfish/v1/Managers/bmc/NetworkProtocol`, ntpData)
44        .then(async () => {
45          if (!dateTimeForm.ntpProtocolEnabled) {
46            const dateTimeData = {
47              DateTime: dateTimeForm.updatedDateTime,
48            };
49            /**
50             * https://github.com/openbmc/phosphor-time-manager/blob/master/README.md#special-note-on-changing-ntp-setting
51             * When time mode is initially set to Manual from NTP,
52             * NTP service is disabled and the NTP service is
53             * stopping but not stopped, setting time will return an error.
54             * There are no responses from backend to notify when NTP is stopped.
55             * To work around, a timeout is set to allow NTP to fully stop
56             * TODO: remove timeout if backend solves
57             * https://github.com/openbmc/openbmc/issues/3459
58             */
59            const timeoutVal = state.isNtpProtocolEnabled ? 20000 : 0;
60            return await new Promise((resolve, reject) => {
61              setTimeout(() => {
62                return api
63                  .patch(`/redfish/v1/Managers/bmc`, dateTimeData)
64                  .then(() => resolve())
65                  .catch(() => reject());
66              }, timeoutVal);
67            });
68          }
69        })
70        .then(() => {
71          return i18n.t('pageDateTime.toast.successSaveDateTime');
72        })
73        .catch((error) => {
74          console.log(error);
75          throw new Error(i18n.t('pageDateTime.toast.errorSaveDateTime'));
76        });
77    },
78  },
79};
80
81export default DateTimeStore;
82