1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const NetworkStore = {
5  namespaced: true,
6  state: {
7    ethernetData: [],
8    firstInterfaceId: '', //used for setting global DHCP settings
9    globalNetworkSettings: [],
10    selectedInterfaceId: '', // which tab is selected
11    selectedInterfaceIndex: 0, // which tab is selected
12  },
13  getters: {
14    ethernetData: (state) => state.ethernetData,
15    firstInterfaceId: (state) => state.firstInterfaceId,
16    globalNetworkSettings: (state) => state.globalNetworkSettings,
17    selectedInterfaceId: (state) => state.selectedInterfaceId,
18    selectedInterfaceIndex: (state) => state.selectedInterfaceIndex,
19  },
20  mutations: {
21    setDomainNameState: (state, domainState) =>
22      (state.domainState = domainState),
23    setDnsState: (state, dnsState) => (state.dnsState = dnsState),
24    setEthernetData: (state, ethernetData) =>
25      (state.ethernetData = ethernetData),
26    setFirstInterfaceId: (state, firstInterfaceId) =>
27      (state.firstInterfaceId = firstInterfaceId),
28    setGlobalNetworkSettings: (state, data) => {
29      state.globalNetworkSettings = data.map(({ data }) => {
30        const {
31          DHCPv4,
32          HostName,
33          IPv4Addresses,
34          IPv4StaticAddresses,
35          LinkStatus,
36          MACAddress,
37        } = data;
38        return {
39          defaultGateway: IPv4StaticAddresses[0]?.Gateway, //First static gateway is the default gateway
40          dhcpAddress: IPv4Addresses.filter(
41            (ipv4) => ipv4.AddressOrigin === 'DHCP'
42          ),
43          dhcpEnabled: DHCPv4.DHCPEnabled,
44          hostname: HostName,
45          macAddress: MACAddress,
46          linkStatus: LinkStatus,
47          staticAddress: IPv4StaticAddresses[0]?.Address, // Display first static address on overview page
48          useDnsEnabled: DHCPv4.UseDNSServers,
49          useDomainNameEnabled: DHCPv4.UseDomainName,
50          useNtpEnabled: DHCPv4.UseNTPServers,
51        };
52      });
53    },
54    setNtpState: (state, ntpState) => (state.ntpState = ntpState),
55    setSelectedInterfaceId: (state, selectedInterfaceId) =>
56      (state.selectedInterfaceId = selectedInterfaceId),
57    setSelectedInterfaceIndex: (state, selectedInterfaceIndex) =>
58      (state.selectedInterfaceIndex = selectedInterfaceIndex),
59  },
60  actions: {
61    async getEthernetData({ commit }) {
62      return await api
63        .get('/redfish/v1/Managers/bmc/EthernetInterfaces')
64        .then((response) =>
65          response.data.Members.map(
66            (ethernetInterface) => ethernetInterface['@odata.id']
67          )
68        )
69        .then((ethernetInterfaceIds) =>
70          api.all(
71            ethernetInterfaceIds.map((ethernetInterface) =>
72              api.get(ethernetInterface)
73            )
74          )
75        )
76        .then((ethernetInterfaces) => {
77          const ethernetData = ethernetInterfaces.map(
78            (ethernetInterface) => ethernetInterface.data
79          );
80          const firstInterfaceId = ethernetData[0].Id;
81
82          commit('setEthernetData', ethernetData);
83          commit('setFirstInterfaceId', firstInterfaceId);
84          commit('setSelectedInterfaceId', firstInterfaceId);
85          commit('setGlobalNetworkSettings', ethernetInterfaces);
86        })
87        .catch((error) => {
88          console.log('Network Data:', error);
89        });
90    },
91    async saveDhcpEnabledState({ state, dispatch }, dhcpState) {
92      const data = {
93        DHCPv4: {
94          DHCPEnabled: dhcpState,
95        },
96      };
97      return api
98        .patch(
99          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
100          data
101        )
102        .then(dispatch('getEthernetData'))
103        .then(() => {
104          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
105            setting: i18n.t('pageNetwork.dhcp'),
106          });
107        })
108        .catch((error) => {
109          console.log(error);
110          throw new Error(
111            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
112              setting: i18n.t('pageNetwork.dhcp'),
113            })
114          );
115        });
116    },
117    async saveDomainNameState({ commit, state }, domainState) {
118      commit('setDomainNameState', domainState);
119      const data = {
120        DHCPv4: {
121          UseDomainName: domainState,
122        },
123      };
124      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
125      // on all interfaces
126      return api
127        .patch(
128          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
129          data
130        )
131        .then(() => {
132          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
133            setting: i18n.t('pageNetwork.domainName'),
134          });
135        })
136        .catch((error) => {
137          console.log(error);
138          commit('setDomainNameState', !domainState);
139          throw new Error(
140            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
141              setting: i18n.t('pageNetwork.domainName'),
142            })
143          );
144        });
145    },
146    async saveDnsState({ commit, state }, dnsState) {
147      commit('setDnsState', dnsState);
148      const data = {
149        DHCPv4: {
150          UseDNSServers: dnsState,
151        },
152      };
153      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
154      // on all interfaces
155      return api
156        .patch(
157          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
158          data
159        )
160        .then(() => {
161          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
162            setting: i18n.t('pageNetwork.dns'),
163          });
164        })
165        .catch((error) => {
166          console.log(error);
167          commit('setDnsState', !dnsState);
168          throw new Error(
169            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
170              setting: i18n.t('pageNetwork.dns'),
171            })
172          );
173        });
174    },
175    async saveNtpState({ commit, state }, ntpState) {
176      commit('setNtpState', ntpState);
177      const data = {
178        DHCPv4: {
179          UseNTPServers: ntpState,
180        },
181      };
182      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
183      // on all interfaces
184      return api
185        .patch(
186          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
187          data
188        )
189        .then(() => {
190          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
191            setting: i18n.t('pageNetwork.ntp'),
192          });
193        })
194        .catch((error) => {
195          console.log(error);
196          commit('setNtpState', !ntpState);
197          throw new Error(
198            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
199              setting: i18n.t('pageNetwork.ntp'),
200            })
201          );
202        });
203    },
204    async setSelectedTabIndex({ commit }, tabIndex) {
205      commit('setSelectedInterfaceIndex', tabIndex);
206    },
207    async setSelectedTabId({ commit }, tabId) {
208      commit('setSelectedInterfaceId', tabId);
209    },
210    async saveIpv4Address({ dispatch, state }, ipv4Form) {
211      const originalAddresses = state.ethernetData[
212        state.selectedInterfaceIndex
213      ].IPv4StaticAddresses.map((ipv4) => {
214        const { Address, SubnetMask, Gateway } = ipv4;
215        return {
216          Address,
217          SubnetMask,
218          Gateway,
219        };
220      });
221      const newAddress = [ipv4Form];
222      return api
223        .patch(
224          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
225          { IPv4StaticAddresses: originalAddresses.concat(newAddress) }
226        )
227        .then(dispatch('getEthernetData'))
228        .then(() => {
229          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
230            setting: i18n.t('pageNetwork.ipv4'),
231          });
232        })
233        .catch((error) => {
234          console.log(error);
235          throw new Error(
236            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
237              setting: i18n.t('pageNetwork.ipv4'),
238            })
239          );
240        });
241    },
242    async editIpv4Address({ dispatch, state }, ipv4TableData) {
243      return api
244        .patch(
245          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
246          { IPv4StaticAddresses: ipv4TableData }
247        )
248        .then(dispatch('getEthernetData'))
249        .then(() => {
250          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
251            setting: i18n.t('pageNetwork.ipv4'),
252          });
253        })
254        .catch((error) => {
255          console.log(error);
256          throw new Error(
257            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
258              setting: i18n.t('pageNetwork.ipv4'),
259            })
260          );
261        });
262    },
263    async saveSettings({ state, dispatch }, interfaceSettingsForm) {
264      return api
265        .patch(
266          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
267          interfaceSettingsForm
268        )
269        .then(dispatch('getEthernetData'))
270        .then(() => {
271          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
272            setting: i18n.t('pageNetwork.network'),
273          });
274        })
275        .catch((error) => {
276          console.log(error);
277          throw new Error(
278            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
279              setting: i18n.t('pageNetwork.network'),
280            })
281          );
282        });
283    },
284    async saveDnsAddress({ dispatch, state }, dnsForm) {
285      const newAddress = dnsForm;
286      const originalAddresses =
287        state.ethernetData[state.selectedInterfaceIndex].StaticNameServers;
288      const newDnsArray = originalAddresses.concat(newAddress);
289      return api
290        .patch(
291          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
292          { StaticNameServers: newDnsArray }
293        )
294        .then(dispatch('getEthernetData'))
295        .then(() => {
296          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
297            setting: i18n.t('pageNetwork.dns'),
298          });
299        })
300        .catch((error) => {
301          console.log(error);
302          throw new Error(
303            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
304              setting: i18n.t('pageNetwork.dns'),
305            })
306          );
307        });
308    },
309    async editDnsAddress({ dispatch, state }, dnsTableData) {
310      return api
311        .patch(
312          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
313          { StaticNameServers: dnsTableData }
314        )
315        .then(dispatch('getEthernetData'))
316        .then(() => {
317          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
318            setting: i18n.t('pageNetwork.dns'),
319          });
320        })
321        .catch((error) => {
322          console.log(error);
323          throw new Error(
324            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
325              setting: i18n.t('pageNetwork.dns'),
326            })
327          );
328        });
329    },
330  },
331};
332
333export default NetworkStore;
334