1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const FirmwareStore = {
5  namespaced: true,
6  state: {
7    bmcFirmware: [],
8    hostFirmware: [],
9    bmcActiveFirmwareId: null,
10    hostActiveFirmwareId: null,
11    applyTime: null,
12    httpPushUri: null,
13  },
14  getters: {
15    isSingleFileUploadEnabled: (state) => state.hostFirmware.length === 0,
16    activeBmcFirmware: (state) => {
17      return state.bmcFirmware.find(
18        (firmware) => firmware.id === state.bmcActiveFirmwareId,
19      );
20    },
21    activeHostFirmware: (state) => {
22      return state.hostFirmware.find(
23        (firmware) => firmware.id === state.hostActiveFirmwareId,
24      );
25    },
26    backupBmcFirmware: (state) => {
27      return state.bmcFirmware.find(
28        (firmware) => firmware.id !== state.bmcActiveFirmwareId,
29      );
30    },
31    backupHostFirmware: (state) => {
32      return state.hostFirmware.find(
33        (firmware) => firmware.id !== state.hostActiveFirmwareId,
34      );
35    },
36  },
37  mutations: {
38    setActiveBmcFirmwareId: (state, id) => (state.bmcActiveFirmwareId = id),
39    setActiveHostFirmwareId: (state, id) => (state.hostActiveFirmwareId = id),
40    setBmcFirmware: (state, firmware) => (state.bmcFirmware = firmware),
41    setHostFirmware: (state, firmware) => (state.hostFirmware = firmware),
42    setApplyTime: (state, applyTime) => (state.applyTime = applyTime),
43    setHttpPushUri: (state, httpPushUri) => (state.httpPushUri = httpPushUri),
44  },
45  actions: {
46    async getFirmwareInformation({ dispatch }) {
47      dispatch('getActiveHostFirmware');
48      dispatch('getActiveBmcFirmware');
49      return await dispatch('getFirmwareInventory');
50    },
51    async getActiveBmcFirmware({ commit }) {
52      return api
53        .get(`${await this.dispatch('global/getBmcPath')}`)
54        .then(({ data: { Links } }) => {
55          const id = Links?.ActiveSoftwareImage['@odata.id'].split('/').pop();
56          commit('setActiveBmcFirmwareId', id);
57        })
58        .catch((error) => console.log(error));
59    },
60    async getActiveHostFirmware({ commit }) {
61      return api
62        .get(`${await this.dispatch('global/getSystemPath')}/Bios`)
63        .then(({ data: { Links } }) => {
64          const id = Links?.ActiveSoftwareImage['@odata.id'].split('/').pop();
65          commit('setActiveHostFirmwareId', id);
66        })
67        .catch((error) => console.log(error));
68    },
69    async getFirmwareInventory({ commit }) {
70      const inventoryList = await api
71        .get('/redfish/v1/UpdateService/FirmwareInventory')
72        .then(({ data: { Members = [] } = {} }) =>
73          Members.map((item) => api.get(item['@odata.id'])),
74        )
75        .catch((error) => console.log(error));
76      await api
77        .all(inventoryList)
78        .then((response) => {
79          const bmcFirmware = [];
80          const hostFirmware = [];
81          response.forEach(({ data }) => {
82            const firmwareType = data?.RelatedItem?.[0]?.['@odata.id']
83              .split('/')
84              .pop();
85            const item = {
86              version: data?.Version,
87              id: data?.Id,
88              location: data?.['@odata.id'],
89              status: data?.Status?.Health,
90            };
91            if (firmwareType === 'bmc') {
92              bmcFirmware.push(item);
93            } else if (firmwareType === 'Bios') {
94              hostFirmware.push(item);
95            }
96          });
97          commit('setBmcFirmware', bmcFirmware);
98          commit('setHostFirmware', hostFirmware);
99        })
100        .catch((error) => {
101          console.log(error);
102        });
103    },
104    getUpdateServiceSettings({ commit }) {
105      api
106        .get('/redfish/v1/UpdateService')
107        .then(({ data }) => {
108          const applyTime =
109            data.HttpPushUriOptions.HttpPushUriApplyTime.ApplyTime;
110          commit('setApplyTime', applyTime);
111          const httpPushUri = data.HttpPushUri;
112          commit('setHttpPushUri', httpPushUri);
113        })
114        .catch((error) => console.log(error));
115    },
116    async uploadFirmware({ state }, image) {
117      return await api
118        .post(state.httpPushUri, image, {
119          headers: { 'Content-Type': 'application/octet-stream' },
120        })
121        .catch((error) => {
122          console.log(error);
123          throw new Error(i18n.t('pageFirmware.toast.errorUpdateFirmware'));
124        });
125    },
126    async switchBmcFirmwareAndReboot({ getters }) {
127      const backupLocation = getters.backupBmcFirmware.location;
128      const data = {
129        Links: {
130          ActiveSoftwareImage: {
131            '@odata.id': backupLocation,
132          },
133        },
134      };
135      return await api
136        .patch(`${await this.dispatch('global/getBmcPath')}`, data)
137        .catch((error) => {
138          console.log(error);
139          throw new Error(i18n.t('pageFirmware.toast.errorSwitchImages'));
140        });
141    },
142  },
143};
144
145export default FirmwareStore;
146