blob: 0eee6fbf546a3100dc3562aa144e967261bddc83 [file] [log] [blame]
Iftekharul Islamcd789502017-04-19 14:37:55 -05001/**
2 * Controller for network
3 *
4 * @module app/configuration
5 * @exports networkController
6 * @name networkController
Iftekharul Islamcd789502017-04-19 14:37:55 -05007 */
8
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07009window.angular && (function(angular) {
10 'use strict';
Iftekharul Islamcd789502017-04-19 14:37:55 -050011
Andrew Geisslerd27bb132018-05-24 11:07:27 -070012 angular.module('app.configuration').controller('networkController', [
Gunnar Mills0af165b2018-06-01 16:24:56 -050013 '$scope', '$window', 'APIUtils', 'dataService', '$timeout', '$route', '$q',
beccabroek27ce84d2019-02-05 15:43:17 -060014 'toastService',
beccabroek9a3b5422019-01-15 14:45:12 -060015 function(
beccabroek27ce84d2019-02-05 15:43:17 -060016 $scope, $window, APIUtils, dataService, $timeout, $route, $q,
17 toastService) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070018 $scope.dataService = dataService;
19 $scope.network = {};
beccabroek067a1cd2018-10-05 10:25:21 -050020 $scope.oldInterface = {};
Andrew Geisslerd27bb132018-05-24 11:07:27 -070021 $scope.interface = {};
22 $scope.networkDevice = false;
23 $scope.hostname = '';
beccabroek067a1cd2018-10-05 10:25:21 -050024 $scope.defaultGateway = '';
Gunnar Mills7ddc7272018-04-12 16:12:03 -050025 $scope.selectedInterface = '';
beccabroek067a1cd2018-10-05 10:25:21 -050026 $scope.confirmSettings = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050027 $scope.loading = false;
beccabroek067a1cd2018-10-05 10:25:21 -050028 $scope.ipv4sToDelete = [];
Iftekharul Islam2a489552017-11-02 13:23:08 -050029
Gunnar Millsa3f75322018-07-10 17:06:02 -050030 loadNetworkInfo();
31
Andrew Geisslerd27bb132018-05-24 11:07:27 -070032 $scope.selectInterface = function(interfaceId) {
33 $scope.interface = $scope.network.interfaces[interfaceId];
Gunnar Millsa45c3852018-05-30 16:18:45 -050034 // Copy the interface so we know later if changes were made to the page
beccabroek067a1cd2018-10-05 10:25:21 -050035 $scope.oldInterface = JSON.parse(JSON.stringify($scope.interface));
Andrew Geisslerd27bb132018-05-24 11:07:27 -070036 $scope.selectedInterface = interfaceId;
37 $scope.networkDevice = false;
38 };
Gunnar Millsbc3ab722018-07-10 15:11:01 -050039
40 $scope.addDNSField = function() {
41 $scope.interface.Nameservers.push('');
42 };
43
beccabroekcff61502018-09-13 14:39:23 -050044 $scope.removeDNSField = function(index) {
45 $scope.interface.Nameservers.splice(index, 1);
46 };
47
beccabroek1a0e7d02018-09-24 15:03:40 -050048 $scope.addIpv4Field = function() {
49 $scope.interface.ipv4.values.push(
50 {Address: '', PrefixLength: '', Gateway: ''});
51 };
52
beccabroek971ac1a2018-09-24 13:14:05 -050053 $scope.removeIpv4Address = function(index) {
54 // Check if the IPV4 being removed has an id. This indicates that it is
55 // an existing address and needs to be removed in the back end.
56 if ($scope.interface.ipv4.values[index].id) {
beccabroek067a1cd2018-10-05 10:25:21 -050057 $scope.ipv4sToDelete.push($scope.interface.ipv4.values[index]);
beccabroek971ac1a2018-09-24 13:14:05 -050058 }
59 $scope.interface.ipv4.values.splice(index, 1);
60 };
61
Gunnar Mills7ddc7272018-04-12 16:12:03 -050062 $scope.setNetworkSettings = function() {
Gunnar Millsd01504c2018-05-03 13:01:51 -050063 // Hides the confirm network settings modal
beccabroek067a1cd2018-10-05 10:25:21 -050064 $scope.confirmSettings = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050065 $scope.loading = true;
Gunnar Millsdca79d72018-05-30 13:07:01 -050066 var promises = [];
67
Gunnar Mills659651e2018-05-30 15:21:07 -050068 // MAC Address are case-insensitive
69 if ($scope.interface.MACAddress.toLowerCase() !=
70 dataService.mac_address.toLowerCase()) {
71 promises.push(setMACAddress());
72 }
beccabroek067a1cd2018-10-05 10:25:21 -050073 if ($scope.defaultGateway != dataService.defaultgateway) {
Gunnar Mills659651e2018-05-30 15:21:07 -050074 promises.push(setDefaultGateway());
75 }
76 if ($scope.hostname != dataService.hostname) {
77 promises.push(setHostname());
78 }
beccabroek067a1cd2018-10-05 10:25:21 -050079 if ($scope.interface.DHCPEnabled != $scope.oldInterface.DHCPEnabled) {
Gunnar Millscb2c3062018-05-31 13:13:30 -050080 promises.push(setDHCPEnabled());
81 }
Gunnar Mills309e06a2018-05-30 13:18:10 -050082
Gunnar Mills82658292018-06-06 16:51:32 -050083 // Remove any empty strings from the array. Important because we add an
84 // empty string to the end so the user can add a new DNS server, if the
85 // user doesn't fill out the field, we don't want to add.
86 $scope.interface.Nameservers =
87 $scope.interface.Nameservers.filter(Boolean);
Gunnar Mills06467822018-06-06 15:43:18 -050088 // toString() is a cheap way to compare 2 string arrays
89 if ($scope.interface.Nameservers.toString() !=
beccabroek067a1cd2018-10-05 10:25:21 -050090 $scope.oldInterface.Nameservers.toString()) {
Gunnar Mills06467822018-06-06 15:43:18 -050091 promises.push(setNameservers());
92 }
93
Gunnar Millsa45c3852018-05-30 16:18:45 -050094 // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways
95 if (!$scope.interface.DHCPEnabled) {
beccabroek971ac1a2018-09-24 13:14:05 -050096 // Delete existing IPV4 addresses that were removed
97 promises.push(removeIPV4s());
beccabroek1a0e7d02018-09-24 15:03:40 -050098 // Update any changed IPV4 addresses and add new
Gunnar Millsa45c3852018-05-30 16:18:45 -050099 for (var i in $scope.interface.ipv4.values) {
Gunnar Mills65491142018-06-04 14:23:33 -0500100 if (!APIUtils.validIPV4IP(
101 $scope.interface.ipv4.values[i].Address)) {
beccabroek27ce84d2019-02-05 15:43:17 -0600102 toastService.error(
beccabroek9a3b5422019-01-15 14:45:12 -0600103 $scope.interface.ipv4.values[i].Address +
104 ' invalid IP parameter');
Gunnar Mills65491142018-06-04 14:23:33 -0500105 $scope.loading = false;
106 return;
107 }
Derick Montaguede2da852019-10-08 17:16:36 -0500108 if ($scope.interface.ipv4.values[i].Gateway &&
109 !APIUtils.validIPV4IP(
Gunnar Mills65491142018-06-04 14:23:33 -0500110 $scope.interface.ipv4.values[i].Gateway)) {
beccabroek27ce84d2019-02-05 15:43:17 -0600111 toastService.error(
beccabroek9a3b5422019-01-15 14:45:12 -0600112 $scope.interface.ipv4.values[i].Address +
113 ' invalid gateway parameter');
Gunnar Mills65491142018-06-04 14:23:33 -0500114 $scope.loading = false;
115 return;
116 }
117 // The netmask prefix length will be undefined if outside range
118 if (!$scope.interface.ipv4.values[i].PrefixLength) {
beccabroek27ce84d2019-02-05 15:43:17 -0600119 toastService.error(
beccabroek9a3b5422019-01-15 14:45:12 -0600120 $scope.interface.ipv4.values[i].Address +
121 ' invalid Prefix Length parameter');
Gunnar Mills65491142018-06-04 14:23:33 -0500122 $scope.loading = false;
123 return;
124 }
beccabroek067a1cd2018-10-05 10:25:21 -0500125 if ($scope.interface.ipv4.values[i].updateAddress ||
126 $scope.interface.ipv4.values[i].updateGateway ||
127 $scope.interface.ipv4.values[i].updatePrefix) {
beccabroek1a0e7d02018-09-24 15:03:40 -0500128 // If IPV4 has an id it means it already exists in the back end,
129 // and in order to update it is required to remove previous IPV4
130 // address and add new one. See openbmc/openbmc/issues/2163.
131 // TODO: update to use PUT once issue 2163 is resolved.
132 if ($scope.interface.ipv4.values[i].id) {
133 promises.push(updateIPV4(i));
134 } else {
135 promises.push(addIPV4(i));
136 }
Gunnar Millsa45c3852018-05-30 16:18:45 -0500137 }
138 }
139 }
140
Gunnar Mills0af165b2018-06-01 16:24:56 -0500141 if (promises.length) {
beccabroek9a3b5422019-01-15 14:45:12 -0600142 $q.all(promises).then(
143 function(response) {
144 // Since an IPV4 interface (e.g. IP address, gateway, or
145 // netmask) edit is a delete then an add and the GUI can't
146 // calculate the interface id (e.g. 5c083707) beforehand and it
147 // is not returned by the REST call, openbmc#3227, reload the
148 // page after an edit, which makes a 2nd REST call. Do this for
149 // all network changes due to the possibility of a set network
150 // failing even though it returned success, openbmc#1641, and to
151 // update dataService and oldInterface to know which data has
152 // changed if the user continues to edit network settings.
153 // TODO: The reload is not ideal. Revisit this.
154 $timeout(function() {
155 loadNetworkInfo();
156 $scope.loading = false;
beccabroek27ce84d2019-02-05 15:43:17 -0600157 toastService.success('Network settings saved');
beccabroek9a3b5422019-01-15 14:45:12 -0600158 }, 4000);
159 },
160 function(error) {
161 $scope.loading = false;
beccabroek27ce84d2019-02-05 15:43:17 -0600162 toastService.error('Network settings could not be saved');
beccabroek9a3b5422019-01-15 14:45:12 -0600163 })
Gunnar Mills0af165b2018-06-01 16:24:56 -0500164 } else {
Gunnar Mills84981f02018-05-31 15:19:01 -0500165 $scope.loading = false;
Gunnar Mills0af165b2018-06-01 16:24:56 -0500166 }
Gunnar Millsdca79d72018-05-30 13:07:01 -0500167 };
168
169 function setMACAddress() {
170 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500171 .setMACAddress(
172 $scope.selectedInterface, $scope.interface.MACAddress)
173 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -0500174 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500175 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -0500176 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600177 return $q.reject();
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500178 });
Gunnar Millsdca79d72018-05-30 13:07:01 -0500179 }
180
181 function setDefaultGateway() {
beccabroek067a1cd2018-10-05 10:25:21 -0500182 return APIUtils.setDefaultGateway($scope.defaultGateway)
Gunnar Millsdca79d72018-05-30 13:07:01 -0500183 .then(
184 function(data) {},
185 function(error) {
186 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600187 return $q.reject();
Gunnar Millsdca79d72018-05-30 13:07:01 -0500188 });
189 }
Gunnar Mills309e06a2018-05-30 13:18:10 -0500190
191 function setHostname() {
192 return APIUtils.setHostname($scope.hostname)
193 .then(
194 function(data) {},
195 function(error) {
196 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600197 return $q.reject();
Gunnar Mills309e06a2018-05-30 13:18:10 -0500198 });
199 }
200
Gunnar Millscb2c3062018-05-31 13:13:30 -0500201 function setDHCPEnabled() {
202 return APIUtils
203 .setDHCPEnabled(
204 $scope.selectedInterface, $scope.interface.DHCPEnabled)
205 .then(
206 function(data) {},
207 function(error) {
208 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600209 return $q.reject();
Gunnar Millscb2c3062018-05-31 13:13:30 -0500210 });
211 }
212
Gunnar Mills06467822018-06-06 15:43:18 -0500213 function setNameservers() {
214 return APIUtils
215 .setNameservers(
216 $scope.selectedInterface, $scope.interface.Nameservers)
217 .then(
218 function(data) {},
219 function(error) {
220 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600221 return $q.reject();
Gunnar Mills06467822018-06-06 15:43:18 -0500222 });
223 }
224
beccabroek971ac1a2018-09-24 13:14:05 -0500225 function removeIPV4s() {
beccabroek067a1cd2018-10-05 10:25:21 -0500226 return $scope.ipv4sToDelete.map(function(ipv4) {
beccabroek971ac1a2018-09-24 13:14:05 -0500227 return APIUtils.deleteIPV4($scope.selectedInterface, ipv4.id)
228 .then(
229 function(data) {},
230 function(error) {
231 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600232 return $q.reject();
beccabroek971ac1a2018-09-24 13:14:05 -0500233 })
234 });
235 }
236
beccabroek1a0e7d02018-09-24 15:03:40 -0500237 function addIPV4(index) {
238 return APIUtils
239 .addIPV4(
240 $scope.selectedInterface,
241 $scope.interface.ipv4.values[index].Address,
242 $scope.interface.ipv4.values[index].PrefixLength,
243 $scope.interface.ipv4.values[index].Gateway)
244 .then(
245 function(data) {},
246 function(error) {
247 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600248 return $q.reject();
beccabroek1a0e7d02018-09-24 15:03:40 -0500249 })
250 }
251
252 function updateIPV4(index) {
Gunnar Millsa45c3852018-05-30 16:18:45 -0500253 // The correct way to edit an IPV4 interface is to remove it and then
254 // add a new one
255 return APIUtils
256 .deleteIPV4(
beccabroek1a0e7d02018-09-24 15:03:40 -0500257 $scope.selectedInterface,
258 $scope.interface.ipv4.values[index].id)
Gunnar Millsa45c3852018-05-30 16:18:45 -0500259 .then(
260 function(data) {
261 return APIUtils
262 .addIPV4(
263 $scope.selectedInterface,
264 $scope.interface.ipv4.values[index].Address,
265 $scope.interface.ipv4.values[index].PrefixLength,
266 $scope.interface.ipv4.values[index].Gateway)
267 .then(
268 function(data) {},
269 function(error) {
270 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600271 return $q.reject();
Gunnar Millsa45c3852018-05-30 16:18:45 -0500272 });
273 },
274 function(error) {
275 console.log(JSON.stringify(error));
beccabroek9a3b5422019-01-15 14:45:12 -0600276 return $q.reject();
Gunnar Millsa45c3852018-05-30 16:18:45 -0500277 });
278 }
279
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500280 $scope.refresh = function() {
Gunnar Millsa3f75322018-07-10 17:06:02 -0500281 loadNetworkInfo();
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500282 };
Gunnar Millsa3f75322018-07-10 17:06:02 -0500283
284 function loadNetworkInfo() {
285 APIUtils.getNetworkInfo().then(function(data) {
286 dataService.setNetworkInfo(data);
287 $scope.network = data.formatted_data;
288 $scope.hostname = data.hostname;
beccabroek067a1cd2018-10-05 10:25:21 -0500289 $scope.defaultGateway = data.defaultgateway;
Gunnar Millsa3f75322018-07-10 17:06:02 -0500290 if ($scope.network.interface_ids.length) {
Rebecca Shawffdef962018-07-19 13:37:37 -0500291 // Use the first network interface if the user hasn't chosen one
Gunnar Millsa3f75322018-07-10 17:06:02 -0500292 if (!$scope.selectedInterface ||
293 !$scope.network.interfaces[$scope.selectedInterface]) {
294 $scope.selectedInterface = $scope.network.interface_ids[0];
295 }
296 $scope.interface =
297 $scope.network.interfaces[$scope.selectedInterface];
298 // Copy the interface so we know later if changes were made to the
299 // page
beccabroek067a1cd2018-10-05 10:25:21 -0500300 $scope.oldInterface = JSON.parse(JSON.stringify($scope.interface));
Gunnar Millsa3f75322018-07-10 17:06:02 -0500301 }
beccabroek1a0e7d02018-09-24 15:03:40 -0500302 // Add id values and update flags to corresponding IPV4 objects
beccabroek971ac1a2018-09-24 13:14:05 -0500303 for (var i = 0; i < $scope.interface.ipv4.values.length; i++) {
304 $scope.interface.ipv4.values[i].id = $scope.interface.ipv4.ids[i];
beccabroek067a1cd2018-10-05 10:25:21 -0500305 $scope.interface.ipv4.values[i].updateAddress = false;
306 $scope.interface.ipv4.values[i].updateGateway = false;
307 $scope.interface.ipv4.values[i].updatePrefix = false;
beccabroek971ac1a2018-09-24 13:14:05 -0500308 }
Gunnar Millsa3f75322018-07-10 17:06:02 -0500309 });
310 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700311 }
312 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500313})(angular);