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          hostname: HostName,
44          macAddress: MACAddress,
45          linkStatus: LinkStatus,
46          staticAddress: IPv4StaticAddresses[0]?.Address, // Display first static address on overview page
47          useDnsEnabled: DHCPv4.UseDNSServers,
48          useDomainNameEnabled: DHCPv4.UseDomainName,
49          useNtpEnabled: DHCPv4.UseNTPServers,
50        };
51      });
52    },
53    setNtpState: (state, ntpState) => (state.ntpState = ntpState),
54    setSelectedInterfaceId: (state, selectedInterfaceId) =>
55      (state.selectedInterfaceId = selectedInterfaceId),
56    setSelectedInterfaceIndex: (state, selectedInterfaceIndex) =>
57      (state.selectedInterfaceIndex = selectedInterfaceIndex),
58  },
59  actions: {
60    async getEthernetData({ commit }) {
61      return await api
62        .get('/redfish/v1/Managers/bmc/EthernetInterfaces')
63        .then((response) =>
64          response.data.Members.map(
65            (ethernetInterface) => ethernetInterface['@odata.id']
66          )
67        )
68        .then((ethernetInterfaceIds) =>
69          api.all(
70            ethernetInterfaceIds.map((ethernetInterface) =>
71              api.get(ethernetInterface)
72            )
73          )
74        )
75        .then((ethernetInterfaces) => {
76          const ethernetData = ethernetInterfaces.map(
77            (ethernetInterface) => ethernetInterface.data
78          );
79          const firstInterfaceId = ethernetData[0].Id;
80
81          commit('setEthernetData', ethernetData);
82          commit('setFirstInterfaceId', firstInterfaceId);
83          commit('setSelectedInterfaceId', firstInterfaceId);
84          commit('setGlobalNetworkSettings', ethernetInterfaces);
85        })
86        .catch((error) => {
87          console.log('Network Data:', error);
88        });
89    },
90    async saveDomainNameState({ commit, state }, domainState) {
91      commit('setDomainNameState', domainState);
92      const data = {
93        DHCPv4: {
94          UseDomainName: domainState,
95        },
96      };
97      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
98      // on all interfaces
99      return api
100        .patch(
101          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
102          data
103        )
104        .then(() => {
105          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
106            setting: i18n.t('pageNetwork.domainName'),
107          });
108        })
109        .catch((error) => {
110          console.log(error);
111          commit('setDomainNameState', !domainState);
112          throw new Error(
113            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
114              setting: i18n.t('pageNetwork.domainName'),
115            })
116          );
117        });
118    },
119    async saveDnsState({ commit, state }, dnsState) {
120      commit('setDnsState', dnsState);
121      const data = {
122        DHCPv4: {
123          UseDNSServers: dnsState,
124        },
125      };
126      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
127      // on all interfaces
128      return api
129        .patch(
130          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
131          data
132        )
133        .then(() => {
134          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
135            setting: i18n.t('pageNetwork.dns'),
136          });
137        })
138        .catch((error) => {
139          console.log(error);
140          commit('setDnsState', !dnsState);
141          throw new Error(
142            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
143              setting: i18n.t('pageNetwork.dns'),
144            })
145          );
146        });
147    },
148    async saveNtpState({ commit, state }, ntpState) {
149      commit('setNtpState', ntpState);
150      const data = {
151        DHCPv4: {
152          UseNTPServers: ntpState,
153        },
154      };
155      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
156      // on all interfaces
157      return api
158        .patch(
159          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.firstInterfaceId}`,
160          data
161        )
162        .then(() => {
163          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
164            setting: i18n.t('pageNetwork.ntp'),
165          });
166        })
167        .catch((error) => {
168          console.log(error);
169          commit('setNtpState', !ntpState);
170          throw new Error(
171            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
172              setting: i18n.t('pageNetwork.ntp'),
173            })
174          );
175        });
176    },
177    async setSelectedTabIndex({ commit }, tabIndex) {
178      commit('setSelectedInterfaceIndex', tabIndex);
179    },
180    async setSelectedTabId({ commit }, tabId) {
181      commit('setSelectedInterfaceId', tabId);
182    },
183    async saveIpv4Address({ dispatch, state }, ipv4Form) {
184      const originalAddresses = state.ethernetData[
185        state.selectedInterfaceIndex
186      ].IPv4StaticAddresses.map((ipv4) => {
187        const { Address, SubnetMask, Gateway } = ipv4;
188        return {
189          Address,
190          SubnetMask,
191          Gateway,
192        };
193      });
194      const newAddress = [ipv4Form];
195      return api
196        .patch(
197          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
198          { IPv4StaticAddresses: originalAddresses.concat(newAddress) }
199        )
200        .then(dispatch('getEthernetData'))
201        .then(() => {
202          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
203            setting: i18n.t('pageNetwork.ipv4'),
204          });
205        })
206        .catch((error) => {
207          console.log(error);
208          throw new Error(
209            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
210              setting: i18n.t('pageNetwork.ipv4'),
211            })
212          );
213        });
214    },
215    async editIpv4Address({ dispatch, state }, ipv4TableData) {
216      return api
217        .patch(
218          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
219          { IPv4StaticAddresses: ipv4TableData }
220        )
221        .then(dispatch('getEthernetData'))
222        .then(() => {
223          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
224            setting: i18n.t('pageNetwork.ipv4'),
225          });
226        })
227        .catch((error) => {
228          console.log(error);
229          throw new Error(
230            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
231              setting: i18n.t('pageNetwork.ipv4'),
232            })
233          );
234        });
235    },
236    async saveSettings({ state, dispatch }, interfaceSettingsForm) {
237      return api
238        .patch(
239          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
240          interfaceSettingsForm
241        )
242        .then(dispatch('getEthernetData'))
243        .then(() => {
244          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
245            setting: i18n.t('pageNetwork.network'),
246          });
247        })
248        .catch((error) => {
249          console.log(error);
250          throw new Error(
251            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
252              setting: i18n.t('pageNetwork.network'),
253            })
254          );
255        });
256    },
257    async saveDnsAddress({ dispatch, state }, dnsForm) {
258      const newAddress = dnsForm;
259      const originalAddresses =
260        state.ethernetData[state.selectedInterfaceIndex].StaticNameServers;
261      const newDnsArray = originalAddresses.concat(newAddress);
262      return api
263        .patch(
264          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
265          { StaticNameServers: newDnsArray }
266        )
267        .then(dispatch('getEthernetData'))
268        .then(() => {
269          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
270            setting: i18n.t('pageNetwork.dns'),
271          });
272        })
273        .catch((error) => {
274          console.log(error);
275          throw new Error(
276            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
277              setting: i18n.t('pageNetwork.dns'),
278            })
279          );
280        });
281    },
282    async editDnsAddress({ dispatch, state }, dnsTableData) {
283      return api
284        .patch(
285          `/redfish/v1/Managers/bmc/EthernetInterfaces/${state.selectedInterfaceId}`,
286          { StaticNameServers: dnsTableData }
287        )
288        .then(dispatch('getEthernetData'))
289        .then(() => {
290          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
291            setting: i18n.t('pageNetwork.dns'),
292          });
293        })
294        .catch((error) => {
295          console.log(error);
296          throw new Error(
297            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
298              setting: i18n.t('pageNetwork.dns'),
299            })
300          );
301        });
302    },
303  },
304};
305
306export default NetworkStore;
307