1f67f769fSSandeepa Singhimport api from '@/store/api';
2f67f769fSSandeepa Singhimport i18n from '@/i18n';
3f67f769fSSandeepa Singh
4f67f769fSSandeepa Singhconst NetworkStore = {
5f67f769fSSandeepa Singh  namespaced: true,
6f67f769fSSandeepa Singh  state: {
7f67f769fSSandeepa Singh    ethernetData: [],
8c4b8757eSDixsie Wolmers    firstInterfaceId: '', //used for setting global DHCP settings
9182b3f1fSDixsie Wolmers    globalNetworkSettings: [],
10b34349d4SDixsie Wolmers    selectedInterfaceId: '', // which tab is selected
11b34349d4SDixsie Wolmers    selectedInterfaceIndex: 0, // which tab is selected
12f67f769fSSandeepa Singh  },
13f67f769fSSandeepa Singh  getters: {
14f67f769fSSandeepa Singh    ethernetData: (state) => state.ethernetData,
15c4b8757eSDixsie Wolmers    firstInterfaceId: (state) => state.firstInterfaceId,
16182b3f1fSDixsie Wolmers    globalNetworkSettings: (state) => state.globalNetworkSettings,
17b34349d4SDixsie Wolmers    selectedInterfaceId: (state) => state.selectedInterfaceId,
18b34349d4SDixsie Wolmers    selectedInterfaceIndex: (state) => state.selectedInterfaceIndex,
19f67f769fSSandeepa Singh  },
20f67f769fSSandeepa Singh  mutations: {
21b34349d4SDixsie Wolmers    setDomainNameState: (state, domainState) =>
22b34349d4SDixsie Wolmers      (state.domainState = domainState),
23b34349d4SDixsie Wolmers    setDnsState: (state, dnsState) => (state.dnsState = dnsState),
24f67f769fSSandeepa Singh    setEthernetData: (state, ethernetData) =>
25f67f769fSSandeepa Singh      (state.ethernetData = ethernetData),
26c4b8757eSDixsie Wolmers    setFirstInterfaceId: (state, firstInterfaceId) =>
27c4b8757eSDixsie Wolmers      (state.firstInterfaceId = firstInterfaceId),
28182b3f1fSDixsie Wolmers    setGlobalNetworkSettings: (state, data) => {
29182b3f1fSDixsie Wolmers      state.globalNetworkSettings = data.map(({ data }) => {
30182b3f1fSDixsie Wolmers        const {
31c4b8757eSDixsie Wolmers          DHCPv4,
32182b3f1fSDixsie Wolmers          HostName,
33182b3f1fSDixsie Wolmers          IPv4Addresses,
34c4b8757eSDixsie Wolmers          IPv4StaticAddresses,
35c4b8757eSDixsie Wolmers          LinkStatus,
3612dc20c3SDixsie Wolmers          MACAddress,
37182b3f1fSDixsie Wolmers        } = data;
38182b3f1fSDixsie Wolmers        return {
39b34349d4SDixsie Wolmers          defaultGateway: IPv4StaticAddresses[0]?.Gateway, //First static gateway is the default gateway
40182b3f1fSDixsie Wolmers          dhcpAddress: IPv4Addresses.filter(
418132399cSEd Tanous            (ipv4) => ipv4.AddressOrigin === 'DHCP',
42182b3f1fSDixsie Wolmers          ),
43e8cb2c6aSNikhil Ashoka          dhcpEnabled: DHCPv4.DHCPEnabled,
44c4b8757eSDixsie Wolmers          hostname: HostName,
4512dc20c3SDixsie Wolmers          macAddress: MACAddress,
46c4b8757eSDixsie Wolmers          linkStatus: LinkStatus,
47c4b8757eSDixsie Wolmers          staticAddress: IPv4StaticAddresses[0]?.Address, // Display first static address on overview page
48c4b8757eSDixsie Wolmers          useDnsEnabled: DHCPv4.UseDNSServers,
49c4b8757eSDixsie Wolmers          useDomainNameEnabled: DHCPv4.UseDomainName,
50c4b8757eSDixsie Wolmers          useNtpEnabled: DHCPv4.UseNTPServers,
51182b3f1fSDixsie Wolmers        };
52182b3f1fSDixsie Wolmers      });
53182b3f1fSDixsie Wolmers    },
54b34349d4SDixsie Wolmers    setNtpState: (state, ntpState) => (state.ntpState = ntpState),
55b34349d4SDixsie Wolmers    setSelectedInterfaceId: (state, selectedInterfaceId) =>
56b34349d4SDixsie Wolmers      (state.selectedInterfaceId = selectedInterfaceId),
57b34349d4SDixsie Wolmers    setSelectedInterfaceIndex: (state, selectedInterfaceIndex) =>
58b34349d4SDixsie Wolmers      (state.selectedInterfaceIndex = selectedInterfaceIndex),
59f67f769fSSandeepa Singh  },
60f67f769fSSandeepa Singh  actions: {
61f67f769fSSandeepa Singh    async getEthernetData({ commit }) {
62f67f769fSSandeepa Singh      return await api
63*8841b7d4SSean Zhang        .get(`${await this.dispatch('global/getBmcPath')}/EthernetInterfaces`)
64f67f769fSSandeepa Singh        .then((response) =>
65f67f769fSSandeepa Singh          response.data.Members.map(
668132399cSEd Tanous            (ethernetInterface) => ethernetInterface['@odata.id'],
678132399cSEd Tanous          ),
68f67f769fSSandeepa Singh        )
69f67f769fSSandeepa Singh        .then((ethernetInterfaceIds) =>
70f67f769fSSandeepa Singh          api.all(
71f67f769fSSandeepa Singh            ethernetInterfaceIds.map((ethernetInterface) =>
728132399cSEd Tanous              api.get(ethernetInterface),
738132399cSEd Tanous            ),
748132399cSEd Tanous          ),
75f67f769fSSandeepa Singh        )
76f67f769fSSandeepa Singh        .then((ethernetInterfaces) => {
77f67f769fSSandeepa Singh          const ethernetData = ethernetInterfaces.map(
788132399cSEd Tanous            (ethernetInterface) => ethernetInterface.data,
79f67f769fSSandeepa Singh          );
80c4b8757eSDixsie Wolmers          const firstInterfaceId = ethernetData[0].Id;
81f67f769fSSandeepa Singh
82f67f769fSSandeepa Singh          commit('setEthernetData', ethernetData);
83c4b8757eSDixsie Wolmers          commit('setFirstInterfaceId', firstInterfaceId);
84b34349d4SDixsie Wolmers          commit('setSelectedInterfaceId', firstInterfaceId);
85c4b8757eSDixsie Wolmers          commit('setGlobalNetworkSettings', ethernetInterfaces);
86f67f769fSSandeepa Singh        })
87f67f769fSSandeepa Singh        .catch((error) => {
88f67f769fSSandeepa Singh          console.log('Network Data:', error);
89f67f769fSSandeepa Singh        });
90f67f769fSSandeepa Singh    },
91e8cb2c6aSNikhil Ashoka    async saveDhcpEnabledState({ state, dispatch }, dhcpState) {
92e8cb2c6aSNikhil Ashoka      const data = {
93e8cb2c6aSNikhil Ashoka        DHCPv4: {
94e8cb2c6aSNikhil Ashoka          DHCPEnabled: dhcpState,
95e8cb2c6aSNikhil Ashoka        },
96e8cb2c6aSNikhil Ashoka      };
97e8cb2c6aSNikhil Ashoka      return api
98e8cb2c6aSNikhil Ashoka        .patch(
99*8841b7d4SSean Zhang          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
1008132399cSEd Tanous          data,
101e8cb2c6aSNikhil Ashoka        )
102e8cb2c6aSNikhil Ashoka        .then(dispatch('getEthernetData'))
103e8cb2c6aSNikhil Ashoka        .then(() => {
104e8cb2c6aSNikhil Ashoka          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
105e8cb2c6aSNikhil Ashoka            setting: i18n.t('pageNetwork.dhcp'),
106e8cb2c6aSNikhil Ashoka          });
107e8cb2c6aSNikhil Ashoka        })
108e8cb2c6aSNikhil Ashoka        .catch((error) => {
109e8cb2c6aSNikhil Ashoka          console.log(error);
110e8cb2c6aSNikhil Ashoka          throw new Error(
111e8cb2c6aSNikhil Ashoka            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
112e8cb2c6aSNikhil Ashoka              setting: i18n.t('pageNetwork.dhcp'),
1138132399cSEd Tanous            }),
114e8cb2c6aSNikhil Ashoka          );
115e8cb2c6aSNikhil Ashoka        });
116e8cb2c6aSNikhil Ashoka    },
117c4b8757eSDixsie Wolmers    async saveDomainNameState({ commit, state }, domainState) {
118c4b8757eSDixsie Wolmers      commit('setDomainNameState', domainState);
119f67f769fSSandeepa Singh      const data = {
120f67f769fSSandeepa Singh        DHCPv4: {
121c4b8757eSDixsie Wolmers          UseDomainName: domainState,
122f67f769fSSandeepa Singh        },
123f67f769fSSandeepa Singh      };
124c4b8757eSDixsie Wolmers      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
125c4b8757eSDixsie Wolmers      // on all interfaces
126c4b8757eSDixsie Wolmers      return api
127f67f769fSSandeepa Singh        .patch(
128*8841b7d4SSean Zhang          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.firstInterfaceId}`,
1298132399cSEd Tanous          data,
130f67f769fSSandeepa Singh        )
131f67f769fSSandeepa Singh        .then(() => {
132c4b8757eSDixsie Wolmers          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
133c4b8757eSDixsie Wolmers            setting: i18n.t('pageNetwork.domainName'),
134c4b8757eSDixsie Wolmers          });
135f67f769fSSandeepa Singh        })
136f67f769fSSandeepa Singh        .catch((error) => {
137f67f769fSSandeepa Singh          console.log(error);
138c4b8757eSDixsie Wolmers          commit('setDomainNameState', !domainState);
139c4b8757eSDixsie Wolmers          throw new Error(
140c4b8757eSDixsie Wolmers            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
141c4b8757eSDixsie Wolmers              setting: i18n.t('pageNetwork.domainName'),
1428132399cSEd Tanous            }),
143c4b8757eSDixsie Wolmers          );
144c4b8757eSDixsie Wolmers        });
145c4b8757eSDixsie Wolmers    },
146c4b8757eSDixsie Wolmers    async saveDnsState({ commit, state }, dnsState) {
147c4b8757eSDixsie Wolmers      commit('setDnsState', dnsState);
148c4b8757eSDixsie Wolmers      const data = {
149c4b8757eSDixsie Wolmers        DHCPv4: {
150c4b8757eSDixsie Wolmers          UseDNSServers: dnsState,
151c4b8757eSDixsie Wolmers        },
152c4b8757eSDixsie Wolmers      };
153c4b8757eSDixsie Wolmers      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
154c4b8757eSDixsie Wolmers      // on all interfaces
155c4b8757eSDixsie Wolmers      return api
156c4b8757eSDixsie Wolmers        .patch(
157*8841b7d4SSean Zhang          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.firstInterfaceId}`,
1588132399cSEd Tanous          data,
159c4b8757eSDixsie Wolmers        )
160c4b8757eSDixsie Wolmers        .then(() => {
161c4b8757eSDixsie Wolmers          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
162c4b8757eSDixsie Wolmers            setting: i18n.t('pageNetwork.dns'),
163c4b8757eSDixsie Wolmers          });
164c4b8757eSDixsie Wolmers        })
165c4b8757eSDixsie Wolmers        .catch((error) => {
166c4b8757eSDixsie Wolmers          console.log(error);
167c4b8757eSDixsie Wolmers          commit('setDnsState', !dnsState);
168c4b8757eSDixsie Wolmers          throw new Error(
169c4b8757eSDixsie Wolmers            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
170c4b8757eSDixsie Wolmers              setting: i18n.t('pageNetwork.dns'),
1718132399cSEd Tanous            }),
172c4b8757eSDixsie Wolmers          );
173c4b8757eSDixsie Wolmers        });
174c4b8757eSDixsie Wolmers    },
175c4b8757eSDixsie Wolmers    async saveNtpState({ commit, state }, ntpState) {
176c4b8757eSDixsie Wolmers      commit('setNtpState', ntpState);
177c4b8757eSDixsie Wolmers      const data = {
178c4b8757eSDixsie Wolmers        DHCPv4: {
179e61534a4SKenneth Fullbright          UseNTPServers: ntpState,
180c4b8757eSDixsie Wolmers        },
181c4b8757eSDixsie Wolmers      };
182c4b8757eSDixsie Wolmers      // Saving to the first interface automatically updates DHCPv4 and DHCPv6
183c4b8757eSDixsie Wolmers      // on all interfaces
184c4b8757eSDixsie Wolmers      return api
185c4b8757eSDixsie Wolmers        .patch(
186*8841b7d4SSean Zhang          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.firstInterfaceId}`,
1878132399cSEd Tanous          data,
188c4b8757eSDixsie Wolmers        )
189c4b8757eSDixsie Wolmers        .then(() => {
190c4b8757eSDixsie Wolmers          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
191c4b8757eSDixsie Wolmers            setting: i18n.t('pageNetwork.ntp'),
192c4b8757eSDixsie Wolmers          });
193c4b8757eSDixsie Wolmers        })
194c4b8757eSDixsie Wolmers        .catch((error) => {
195c4b8757eSDixsie Wolmers          console.log(error);
196c4b8757eSDixsie Wolmers          commit('setNtpState', !ntpState);
197c4b8757eSDixsie Wolmers          throw new Error(
198c4b8757eSDixsie Wolmers            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
199c4b8757eSDixsie Wolmers              setting: i18n.t('pageNetwork.ntp'),
2008132399cSEd Tanous            }),
201c4b8757eSDixsie Wolmers          );
202f67f769fSSandeepa Singh        });
203f67f769fSSandeepa Singh    },
204b34349d4SDixsie Wolmers    async setSelectedTabIndex({ commit }, tabIndex) {
205b34349d4SDixsie Wolmers      commit('setSelectedInterfaceIndex', tabIndex);
206b34349d4SDixsie Wolmers    },
207b34349d4SDixsie Wolmers    async setSelectedTabId({ commit }, tabId) {
208b34349d4SDixsie Wolmers      commit('setSelectedInterfaceId', tabId);
209b34349d4SDixsie Wolmers    },
210b34349d4SDixsie Wolmers    async saveIpv4Address({ dispatch, state }, ipv4Form) {
211b34349d4SDixsie Wolmers      const originalAddresses = state.ethernetData[
212b34349d4SDixsie Wolmers        state.selectedInterfaceIndex
213b34349d4SDixsie Wolmers      ].IPv4StaticAddresses.map((ipv4) => {
214b34349d4SDixsie Wolmers        const { Address, SubnetMask, Gateway } = ipv4;
215b34349d4SDixsie Wolmers        return {
216b34349d4SDixsie Wolmers          Address,
217b34349d4SDixsie Wolmers          SubnetMask,
218b34349d4SDixsie Wolmers          Gateway,
219b34349d4SDixsie Wolmers        };
220b34349d4SDixsie Wolmers      });
221b34349d4SDixsie Wolmers      const newAddress = [ipv4Form];
222b34349d4SDixsie Wolmers      return api
223b34349d4SDixsie Wolmers        .patch(
224*8841b7d4SSean Zhang          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
2258132399cSEd Tanous          { IPv4StaticAddresses: originalAddresses.concat(newAddress) },
226b34349d4SDixsie Wolmers        )
227b34349d4SDixsie Wolmers        .then(dispatch('getEthernetData'))
228b34349d4SDixsie Wolmers        .then(() => {
229b34349d4SDixsie Wolmers          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
230b34349d4SDixsie Wolmers            setting: i18n.t('pageNetwork.ipv4'),
231b34349d4SDixsie Wolmers          });
232b34349d4SDixsie Wolmers        })
233b34349d4SDixsie Wolmers        .catch((error) => {
234b34349d4SDixsie Wolmers          console.log(error);
235b34349d4SDixsie Wolmers          throw new Error(
236b34349d4SDixsie Wolmers            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
237b34349d4SDixsie Wolmers              setting: i18n.t('pageNetwork.ipv4'),
2388132399cSEd Tanous            }),
239b34349d4SDixsie Wolmers          );
240b34349d4SDixsie Wolmers        });
241b34349d4SDixsie Wolmers    },
242b34349d4SDixsie Wolmers    async editIpv4Address({ dispatch, state }, ipv4TableData) {
243b34349d4SDixsie Wolmers      return api
244b34349d4SDixsie Wolmers        .patch(
245*8841b7d4SSean Zhang          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
2468132399cSEd Tanous          { IPv4StaticAddresses: ipv4TableData },
247b34349d4SDixsie Wolmers        )
248b34349d4SDixsie Wolmers        .then(dispatch('getEthernetData'))
249b34349d4SDixsie Wolmers        .then(() => {
250b34349d4SDixsie Wolmers          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
251b34349d4SDixsie Wolmers            setting: i18n.t('pageNetwork.ipv4'),
252b34349d4SDixsie Wolmers          });
253b34349d4SDixsie Wolmers        })
254b34349d4SDixsie Wolmers        .catch((error) => {
255b34349d4SDixsie Wolmers          console.log(error);
256b34349d4SDixsie Wolmers          throw new Error(
257b34349d4SDixsie Wolmers            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
258b34349d4SDixsie Wolmers              setting: i18n.t('pageNetwork.ipv4'),
2598132399cSEd Tanous            }),
260b34349d4SDixsie Wolmers          );
261b34349d4SDixsie Wolmers        });
262b34349d4SDixsie Wolmers    },
26312dc20c3SDixsie Wolmers    async saveSettings({ state, dispatch }, interfaceSettingsForm) {
26412dc20c3SDixsie Wolmers      return api
26512dc20c3SDixsie Wolmers        .patch(
266*8841b7d4SSean Zhang          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
2678132399cSEd Tanous          interfaceSettingsForm,
26812dc20c3SDixsie Wolmers        )
26912dc20c3SDixsie Wolmers        .then(dispatch('getEthernetData'))
27012dc20c3SDixsie Wolmers        .then(() => {
27112dc20c3SDixsie Wolmers          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
27212dc20c3SDixsie Wolmers            setting: i18n.t('pageNetwork.network'),
27312dc20c3SDixsie Wolmers          });
27412dc20c3SDixsie Wolmers        })
27512dc20c3SDixsie Wolmers        .catch((error) => {
27612dc20c3SDixsie Wolmers          console.log(error);
27712dc20c3SDixsie Wolmers          throw new Error(
27812dc20c3SDixsie Wolmers            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
27912dc20c3SDixsie Wolmers              setting: i18n.t('pageNetwork.network'),
2808132399cSEd Tanous            }),
28112dc20c3SDixsie Wolmers          );
28212dc20c3SDixsie Wolmers        });
28312dc20c3SDixsie Wolmers    },
284b34349d4SDixsie Wolmers    async saveDnsAddress({ dispatch, state }, dnsForm) {
285b34349d4SDixsie Wolmers      const newAddress = dnsForm;
286b34349d4SDixsie Wolmers      const originalAddresses =
287b34349d4SDixsie Wolmers        state.ethernetData[state.selectedInterfaceIndex].StaticNameServers;
288b34349d4SDixsie Wolmers      const newDnsArray = originalAddresses.concat(newAddress);
289b34349d4SDixsie Wolmers      return api
290b34349d4SDixsie Wolmers        .patch(
291*8841b7d4SSean Zhang          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
2928132399cSEd Tanous          { StaticNameServers: newDnsArray },
293b34349d4SDixsie Wolmers        )
294b34349d4SDixsie Wolmers        .then(dispatch('getEthernetData'))
295b34349d4SDixsie Wolmers        .then(() => {
296b34349d4SDixsie Wolmers          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
297b34349d4SDixsie Wolmers            setting: i18n.t('pageNetwork.dns'),
298b34349d4SDixsie Wolmers          });
299b34349d4SDixsie Wolmers        })
300b34349d4SDixsie Wolmers        .catch((error) => {
301b34349d4SDixsie Wolmers          console.log(error);
302b34349d4SDixsie Wolmers          throw new Error(
303b34349d4SDixsie Wolmers            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
304b34349d4SDixsie Wolmers              setting: i18n.t('pageNetwork.dns'),
3058132399cSEd Tanous            }),
306b34349d4SDixsie Wolmers          );
307b34349d4SDixsie Wolmers        });
308b34349d4SDixsie Wolmers    },
309b34349d4SDixsie Wolmers    async editDnsAddress({ dispatch, state }, dnsTableData) {
310b34349d4SDixsie Wolmers      return api
311b34349d4SDixsie Wolmers        .patch(
312*8841b7d4SSean Zhang          `${await this.dispatch('global/getBmcPath')}/EthernetInterfaces/${state.selectedInterfaceId}`,
3138132399cSEd Tanous          { StaticNameServers: dnsTableData },
314b34349d4SDixsie Wolmers        )
315b34349d4SDixsie Wolmers        .then(dispatch('getEthernetData'))
316b34349d4SDixsie Wolmers        .then(() => {
317b34349d4SDixsie Wolmers          return i18n.t('pageNetwork.toast.successSaveNetworkSettings', {
318b34349d4SDixsie Wolmers            setting: i18n.t('pageNetwork.dns'),
319b34349d4SDixsie Wolmers          });
320b34349d4SDixsie Wolmers        })
321b34349d4SDixsie Wolmers        .catch((error) => {
322b34349d4SDixsie Wolmers          console.log(error);
323b34349d4SDixsie Wolmers          throw new Error(
324b34349d4SDixsie Wolmers            i18n.t('pageNetwork.toast.errorSaveNetworkSettings', {
325b34349d4SDixsie Wolmers              setting: i18n.t('pageNetwork.dns'),
3268132399cSEd Tanous            }),
327b34349d4SDixsie Wolmers          );
328b34349d4SDixsie Wolmers        });
329b34349d4SDixsie Wolmers    },
330f67f769fSSandeepa Singh  },
331f67f769fSSandeepa Singh};
332f67f769fSSandeepa Singh
333f67f769fSSandeepa Singhexport default NetworkStore;
334