blob: da40a47bb0ec034c1896853c9733c769e39e3e6d [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',
14 function($scope, $window, APIUtils, dataService, $timeout, $route, $q) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070015 $scope.dataService = dataService;
16 $scope.network = {};
beccabroek067a1cd2018-10-05 10:25:21 -050017 $scope.oldInterface = {};
Andrew Geisslerd27bb132018-05-24 11:07:27 -070018 $scope.interface = {};
19 $scope.networkDevice = false;
20 $scope.hostname = '';
beccabroek067a1cd2018-10-05 10:25:21 -050021 $scope.defaultGateway = '';
22 $scope.setNetworkError = '';
23 $scope.setNetworkSuccess = false;
Gunnar Mills7ddc7272018-04-12 16:12:03 -050024 $scope.selectedInterface = '';
beccabroek067a1cd2018-10-05 10:25:21 -050025 $scope.confirmSettings = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050026 $scope.loading = false;
beccabroek067a1cd2018-10-05 10:25:21 -050027 $scope.ipv4sToDelete = [];
Iftekharul Islam2a489552017-11-02 13:23:08 -050028
Gunnar Millsa3f75322018-07-10 17:06:02 -050029 loadNetworkInfo();
30
Andrew Geisslerd27bb132018-05-24 11:07:27 -070031 $scope.selectInterface = function(interfaceId) {
32 $scope.interface = $scope.network.interfaces[interfaceId];
Gunnar Millsa45c3852018-05-30 16:18:45 -050033 // Copy the interface so we know later if changes were made to the page
beccabroek067a1cd2018-10-05 10:25:21 -050034 $scope.oldInterface = JSON.parse(JSON.stringify($scope.interface));
Andrew Geisslerd27bb132018-05-24 11:07:27 -070035 $scope.selectedInterface = interfaceId;
36 $scope.networkDevice = false;
37 };
Gunnar Millsbc3ab722018-07-10 15:11:01 -050038
39 $scope.addDNSField = function() {
40 $scope.interface.Nameservers.push('');
41 };
42
beccabroekcff61502018-09-13 14:39:23 -050043 $scope.removeDNSField = function(index) {
44 $scope.interface.Nameservers.splice(index, 1);
45 };
46
beccabroek1a0e7d02018-09-24 15:03:40 -050047 $scope.addIpv4Field = function() {
48 $scope.interface.ipv4.values.push(
49 {Address: '', PrefixLength: '', Gateway: ''});
50 };
51
beccabroek971ac1a2018-09-24 13:14:05 -050052 $scope.removeIpv4Address = function(index) {
53 // Check if the IPV4 being removed has an id. This indicates that it is
54 // an existing address and needs to be removed in the back end.
55 if ($scope.interface.ipv4.values[index].id) {
beccabroek067a1cd2018-10-05 10:25:21 -050056 $scope.ipv4sToDelete.push($scope.interface.ipv4.values[index]);
beccabroek971ac1a2018-09-24 13:14:05 -050057 }
58 $scope.interface.ipv4.values.splice(index, 1);
59 };
60
Gunnar Mills7ddc7272018-04-12 16:12:03 -050061 $scope.setNetworkSettings = function() {
Gunnar Millsd01504c2018-05-03 13:01:51 -050062 // Hides the confirm network settings modal
beccabroek067a1cd2018-10-05 10:25:21 -050063 $scope.confirmSettings = false;
64 $scope.setNetworkError = '';
65 $scope.setNetworkSuccess = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050066 $scope.loading = true;
Gunnar Millsdca79d72018-05-30 13:07:01 -050067 var promises = [];
68
Gunnar Mills659651e2018-05-30 15:21:07 -050069 // MAC Address are case-insensitive
70 if ($scope.interface.MACAddress.toLowerCase() !=
71 dataService.mac_address.toLowerCase()) {
72 promises.push(setMACAddress());
73 }
beccabroek067a1cd2018-10-05 10:25:21 -050074 if ($scope.defaultGateway != dataService.defaultgateway) {
Gunnar Mills659651e2018-05-30 15:21:07 -050075 promises.push(setDefaultGateway());
76 }
77 if ($scope.hostname != dataService.hostname) {
78 promises.push(setHostname());
79 }
beccabroek067a1cd2018-10-05 10:25:21 -050080 if ($scope.interface.DHCPEnabled != $scope.oldInterface.DHCPEnabled) {
Gunnar Millscb2c3062018-05-31 13:13:30 -050081 promises.push(setDHCPEnabled());
82 }
Gunnar Mills309e06a2018-05-30 13:18:10 -050083
Gunnar Mills82658292018-06-06 16:51:32 -050084 // Remove any empty strings from the array. Important because we add an
85 // empty string to the end so the user can add a new DNS server, if the
86 // user doesn't fill out the field, we don't want to add.
87 $scope.interface.Nameservers =
88 $scope.interface.Nameservers.filter(Boolean);
Gunnar Mills06467822018-06-06 15:43:18 -050089 // toString() is a cheap way to compare 2 string arrays
90 if ($scope.interface.Nameservers.toString() !=
beccabroek067a1cd2018-10-05 10:25:21 -050091 $scope.oldInterface.Nameservers.toString()) {
Gunnar Mills06467822018-06-06 15:43:18 -050092 promises.push(setNameservers());
93 }
94
Gunnar Millsa45c3852018-05-30 16:18:45 -050095 // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways
96 if (!$scope.interface.DHCPEnabled) {
beccabroek971ac1a2018-09-24 13:14:05 -050097 // Delete existing IPV4 addresses that were removed
98 promises.push(removeIPV4s());
beccabroek1a0e7d02018-09-24 15:03:40 -050099 // Update any changed IPV4 addresses and add new
Gunnar Millsa45c3852018-05-30 16:18:45 -0500100 for (var i in $scope.interface.ipv4.values) {
Gunnar Mills65491142018-06-04 14:23:33 -0500101 if (!APIUtils.validIPV4IP(
102 $scope.interface.ipv4.values[i].Address)) {
beccabroek067a1cd2018-10-05 10:25:21 -0500103 $scope.setNetworkError = $scope.interface.ipv4.values[i].Address +
Gunnar Mills65491142018-06-04 14:23:33 -0500104 ' invalid IP parameter';
105 $scope.loading = false;
106 return;
107 }
108 if (!APIUtils.validIPV4IP(
109 $scope.interface.ipv4.values[i].Gateway)) {
beccabroek067a1cd2018-10-05 10:25:21 -0500110 $scope.setNetworkError = $scope.interface.ipv4.values[i].Address +
Gunnar Mills65491142018-06-04 14:23:33 -0500111 ' invalid gateway parameter';
112 $scope.loading = false;
113 return;
114 }
115 // The netmask prefix length will be undefined if outside range
116 if (!$scope.interface.ipv4.values[i].PrefixLength) {
beccabroek067a1cd2018-10-05 10:25:21 -0500117 $scope.setNetworkError = $scope.interface.ipv4.values[i].Address +
Gunnar Mills65491142018-06-04 14:23:33 -0500118 ' invalid Prefix Length parameter';
119 $scope.loading = false;
120 return;
121 }
beccabroek067a1cd2018-10-05 10:25:21 -0500122 if ($scope.interface.ipv4.values[i].updateAddress ||
123 $scope.interface.ipv4.values[i].updateGateway ||
124 $scope.interface.ipv4.values[i].updatePrefix) {
beccabroek1a0e7d02018-09-24 15:03:40 -0500125 // If IPV4 has an id it means it already exists in the back end,
126 // and in order to update it is required to remove previous IPV4
127 // address and add new one. See openbmc/openbmc/issues/2163.
128 // TODO: update to use PUT once issue 2163 is resolved.
129 if ($scope.interface.ipv4.values[i].id) {
130 promises.push(updateIPV4(i));
131 } else {
132 promises.push(addIPV4(i));
133 }
Gunnar Millsa45c3852018-05-30 16:18:45 -0500134 }
135 }
136 }
137
Gunnar Mills0af165b2018-06-01 16:24:56 -0500138 if (promises.length) {
139 $q.all(promises).finally(function() {
140 $scope.loading = false;
beccabroek067a1cd2018-10-05 10:25:21 -0500141 if (!$scope.setNetworkError) {
142 $scope.setNetworkSuccess = true;
Gunnar Mills0af165b2018-06-01 16:24:56 -0500143 // Since an IPV4 interface (e.g. IP address, gateway, or netmask)
144 // edit is a delete then an add and the GUI can't calculate the
145 // interface id (e.g. 5c083707) beforehand and it is not returned
146 // by the REST call, openbmc#3227, reload the page after an edit,
147 // which makes a 2nd REST call.
148 // Do this for all network changes due to the possibility of a set
149 // network failing even though it returned success, openbmc#1641,
beccabroek067a1cd2018-10-05 10:25:21 -0500150 // and to update dataService and oldInterface to know which
Gunnar Mills0af165b2018-06-01 16:24:56 -0500151 // data has changed if the user continues to edit network
152 // settings.
153 // TODO: The reload is not ideal. Revisit this.
154 $timeout(function() {
Gunnar Millsa3f75322018-07-10 17:06:02 -0500155 loadNetworkInfo();
Gunnar Mills0af165b2018-06-01 16:24:56 -0500156 }, 4000);
157 }
158 });
159 } else {
Gunnar Mills84981f02018-05-31 15:19:01 -0500160 $scope.loading = false;
Gunnar Mills0af165b2018-06-01 16:24:56 -0500161 }
Gunnar Millsdca79d72018-05-30 13:07:01 -0500162 };
163
164 function setMACAddress() {
165 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500166 .setMACAddress(
167 $scope.selectedInterface, $scope.interface.MACAddress)
168 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -0500169 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500170 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -0500171 console.log(JSON.stringify(error));
beccabroek067a1cd2018-10-05 10:25:21 -0500172 $scope.setNetworkError = 'MAC Address';
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500173 });
Gunnar Millsdca79d72018-05-30 13:07:01 -0500174 }
175
176 function setDefaultGateway() {
beccabroek067a1cd2018-10-05 10:25:21 -0500177 return APIUtils.setDefaultGateway($scope.defaultGateway)
Gunnar Millsdca79d72018-05-30 13:07:01 -0500178 .then(
179 function(data) {},
180 function(error) {
181 console.log(JSON.stringify(error));
beccabroek067a1cd2018-10-05 10:25:21 -0500182 $scope.setNetworkError = 'Default Gateway';
Gunnar Millsdca79d72018-05-30 13:07:01 -0500183 });
184 }
Gunnar Mills309e06a2018-05-30 13:18:10 -0500185
186 function setHostname() {
187 return APIUtils.setHostname($scope.hostname)
188 .then(
189 function(data) {},
190 function(error) {
191 console.log(JSON.stringify(error));
beccabroek067a1cd2018-10-05 10:25:21 -0500192 $scope.setNetworkError = 'Hostname';
Gunnar Mills309e06a2018-05-30 13:18:10 -0500193 });
194 }
195
Gunnar Millscb2c3062018-05-31 13:13:30 -0500196 function setDHCPEnabled() {
197 return APIUtils
198 .setDHCPEnabled(
199 $scope.selectedInterface, $scope.interface.DHCPEnabled)
200 .then(
201 function(data) {},
202 function(error) {
203 console.log(JSON.stringify(error));
beccabroek067a1cd2018-10-05 10:25:21 -0500204 $scope.setNetworkError = 'DHCP';
Gunnar Millscb2c3062018-05-31 13:13:30 -0500205 });
206 }
207
Gunnar Mills06467822018-06-06 15:43:18 -0500208 function setNameservers() {
Gunnar Mills82658292018-06-06 16:51:32 -0500209 // Nameservers does not allow an empty array, since we remove all empty
Gunnar Mills9863d122018-07-11 13:36:43 -0500210 // strings above, could have an empty array. TODO: openbmc/openbmc#3240
Gunnar Mills82658292018-06-06 16:51:32 -0500211 if ($scope.interface.Nameservers.length == 0) {
212 $scope.interface.Nameservers.push('');
213 }
Gunnar Mills06467822018-06-06 15:43:18 -0500214 return APIUtils
215 .setNameservers(
216 $scope.selectedInterface, $scope.interface.Nameservers)
217 .then(
218 function(data) {},
219 function(error) {
220 console.log(JSON.stringify(error));
beccabroek067a1cd2018-10-05 10:25:21 -0500221 $scope.setNetworkError = 'DNS Servers';
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));
beccabroek067a1cd2018-10-05 10:25:21 -0500232 $scope.setNetworkError = ipv4.Address;
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));
beccabroek067a1cd2018-10-05 10:25:21 -0500248 $scope.setNetworkError =
beccabroek1a0e7d02018-09-24 15:03:40 -0500249 $scope.interface.ipv4.values[index].Address;
250 })
251 }
252
253 function updateIPV4(index) {
Gunnar Millsa45c3852018-05-30 16:18:45 -0500254 // The correct way to edit an IPV4 interface is to remove it and then
255 // add a new one
256 return APIUtils
257 .deleteIPV4(
beccabroek1a0e7d02018-09-24 15:03:40 -0500258 $scope.selectedInterface,
259 $scope.interface.ipv4.values[index].id)
Gunnar Millsa45c3852018-05-30 16:18:45 -0500260 .then(
261 function(data) {
262 return APIUtils
263 .addIPV4(
264 $scope.selectedInterface,
265 $scope.interface.ipv4.values[index].Address,
266 $scope.interface.ipv4.values[index].PrefixLength,
267 $scope.interface.ipv4.values[index].Gateway)
268 .then(
269 function(data) {},
270 function(error) {
271 console.log(JSON.stringify(error));
beccabroek067a1cd2018-10-05 10:25:21 -0500272 $scope.setNetworkError =
Gunnar Millsa45c3852018-05-30 16:18:45 -0500273 $scope.interface.ipv4.values[index].Address;
274 });
275 },
276 function(error) {
277 console.log(JSON.stringify(error));
beccabroek067a1cd2018-10-05 10:25:21 -0500278 $scope.setNetworkError =
Gunnar Millsa45c3852018-05-30 16:18:45 -0500279 $scope.interface.ipv4.values[index].Address;
280 });
281 }
282
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500283 $scope.refresh = function() {
Gunnar Millsa3f75322018-07-10 17:06:02 -0500284 loadNetworkInfo();
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500285 };
Gunnar Millsa3f75322018-07-10 17:06:02 -0500286
287 function loadNetworkInfo() {
288 APIUtils.getNetworkInfo().then(function(data) {
289 dataService.setNetworkInfo(data);
290 $scope.network = data.formatted_data;
291 $scope.hostname = data.hostname;
beccabroek067a1cd2018-10-05 10:25:21 -0500292 $scope.defaultGateway = data.defaultgateway;
Gunnar Millsa3f75322018-07-10 17:06:02 -0500293 if ($scope.network.interface_ids.length) {
Rebecca Shawffdef962018-07-19 13:37:37 -0500294 // Use the first network interface if the user hasn't chosen one
Gunnar Millsa3f75322018-07-10 17:06:02 -0500295 if (!$scope.selectedInterface ||
296 !$scope.network.interfaces[$scope.selectedInterface]) {
297 $scope.selectedInterface = $scope.network.interface_ids[0];
298 }
299 $scope.interface =
300 $scope.network.interfaces[$scope.selectedInterface];
301 // Copy the interface so we know later if changes were made to the
302 // page
beccabroek067a1cd2018-10-05 10:25:21 -0500303 $scope.oldInterface = JSON.parse(JSON.stringify($scope.interface));
Gunnar Millsa3f75322018-07-10 17:06:02 -0500304 }
beccabroek1a0e7d02018-09-24 15:03:40 -0500305 // Add id values and update flags to corresponding IPV4 objects
beccabroek971ac1a2018-09-24 13:14:05 -0500306 for (var i = 0; i < $scope.interface.ipv4.values.length; i++) {
307 $scope.interface.ipv4.values[i].id = $scope.interface.ipv4.ids[i];
beccabroek067a1cd2018-10-05 10:25:21 -0500308 $scope.interface.ipv4.values[i].updateAddress = false;
309 $scope.interface.ipv4.values[i].updateGateway = false;
310 $scope.interface.ipv4.values[i].updatePrefix = false;
beccabroek971ac1a2018-09-24 13:14:05 -0500311 }
Gunnar Millsa3f75322018-07-10 17:06:02 -0500312 });
313 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700314 }
315 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500316})(angular);