blob: 92da0b291172a924f0d8bac1090c31fb1ab1e01b [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 = {};
Gunnar Millsa45c3852018-05-30 16:18:45 -050017 $scope.old_interface = {};
Andrew Geisslerd27bb132018-05-24 11:07:27 -070018 $scope.interface = {};
19 $scope.networkDevice = false;
20 $scope.hostname = '';
Gunnar Millse9f5fe72018-05-04 13:43:10 -050021 $scope.defaultgateway = '';
Gunnar Mills7ddc7272018-04-12 16:12:03 -050022 $scope.set_network_error = '';
23 $scope.set_network_success = false;
24 $scope.selectedInterface = '';
Gunnar Millsd01504c2018-05-03 13:01:51 -050025 $scope.confirm_settings = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050026 $scope.loading = false;
beccabroek971ac1a2018-09-24 13:14:05 -050027 $scope.IPV4s_to_delete = [];
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
34 $scope.old_interface = 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) {
56 $scope.IPV4s_to_delete.push($scope.interface.ipv4.values[index]);
57 }
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
63 $scope.confirm_settings = false;
Gunnar Mills7ddc7272018-04-12 16:12:03 -050064 $scope.set_network_error = '';
65 $scope.set_network_success = 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 }
74 if ($scope.defaultgateway != dataService.defaultgateway) {
75 promises.push(setDefaultGateway());
76 }
77 if ($scope.hostname != dataService.hostname) {
78 promises.push(setHostname());
79 }
Gunnar Millscb2c3062018-05-31 13:13:30 -050080 if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) {
81 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() !=
91 $scope.old_interface.Nameservers.toString()) {
92 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)) {
103 $scope.set_network_error =
104 $scope.interface.ipv4.values[i].Address +
105 ' invalid IP parameter';
106 $scope.loading = false;
107 return;
108 }
109 if (!APIUtils.validIPV4IP(
110 $scope.interface.ipv4.values[i].Gateway)) {
111 $scope.set_network_error =
112 $scope.interface.ipv4.values[i].Address +
113 ' invalid gateway parameter';
114 $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) {
119 $scope.set_network_error =
120 $scope.interface.ipv4.values[i].Address +
121 ' invalid Prefix Length parameter';
122 $scope.loading = false;
123 return;
124 }
beccabroek1a0e7d02018-09-24 15:03:40 -0500125 if ($scope.interface.ipv4.values[i].update_address ||
126 $scope.interface.ipv4.values[i].update_gateway ||
127 $scope.interface.ipv4.values[i].update_prefix) {
128 // 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) {
142 $q.all(promises).finally(function() {
143 $scope.loading = false;
144 if (!$scope.set_network_error) {
145 $scope.set_network_success = true;
146 // Since an IPV4 interface (e.g. IP address, gateway, or netmask)
147 // edit is a delete then an add and the GUI can't calculate the
148 // interface id (e.g. 5c083707) beforehand and it is not returned
149 // by the REST call, openbmc#3227, reload the page after an edit,
150 // which makes a 2nd REST call.
151 // Do this for all network changes due to the possibility of a set
152 // network failing even though it returned success, openbmc#1641,
153 // and to update dataService and old_interface to know which
154 // data has changed if the user continues to edit network
155 // settings.
156 // TODO: The reload is not ideal. Revisit this.
157 $timeout(function() {
Gunnar Millsa3f75322018-07-10 17:06:02 -0500158 loadNetworkInfo();
Gunnar Mills0af165b2018-06-01 16:24:56 -0500159 }, 4000);
160 }
161 });
162 } else {
Gunnar Mills84981f02018-05-31 15:19:01 -0500163 $scope.loading = false;
Gunnar Mills0af165b2018-06-01 16:24:56 -0500164 }
Gunnar Millsdca79d72018-05-30 13:07:01 -0500165 };
166
167 function setMACAddress() {
168 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500169 .setMACAddress(
170 $scope.selectedInterface, $scope.interface.MACAddress)
171 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -0500172 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500173 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -0500174 console.log(JSON.stringify(error));
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500175 $scope.set_network_error = 'MAC Address';
176 });
Gunnar Millsdca79d72018-05-30 13:07:01 -0500177 }
178
179 function setDefaultGateway() {
180 return APIUtils.setDefaultGateway($scope.defaultgateway)
181 .then(
182 function(data) {},
183 function(error) {
184 console.log(JSON.stringify(error));
185 $scope.set_network_error = 'Default Gateway';
186 });
187 }
Gunnar Mills309e06a2018-05-30 13:18:10 -0500188
189 function setHostname() {
190 return APIUtils.setHostname($scope.hostname)
191 .then(
192 function(data) {},
193 function(error) {
194 console.log(JSON.stringify(error));
195 $scope.set_network_error = 'Hostname';
196 });
197 }
198
Gunnar Millscb2c3062018-05-31 13:13:30 -0500199 function setDHCPEnabled() {
200 return APIUtils
201 .setDHCPEnabled(
202 $scope.selectedInterface, $scope.interface.DHCPEnabled)
203 .then(
204 function(data) {},
205 function(error) {
206 console.log(JSON.stringify(error));
207 $scope.set_network_error = 'DHCP';
208 });
209 }
210
Gunnar Mills06467822018-06-06 15:43:18 -0500211 function setNameservers() {
Gunnar Mills82658292018-06-06 16:51:32 -0500212 // Nameservers does not allow an empty array, since we remove all empty
Gunnar Mills9863d122018-07-11 13:36:43 -0500213 // strings above, could have an empty array. TODO: openbmc/openbmc#3240
Gunnar Mills82658292018-06-06 16:51:32 -0500214 if ($scope.interface.Nameservers.length == 0) {
215 $scope.interface.Nameservers.push('');
216 }
Gunnar Mills06467822018-06-06 15:43:18 -0500217 return APIUtils
218 .setNameservers(
219 $scope.selectedInterface, $scope.interface.Nameservers)
220 .then(
221 function(data) {},
222 function(error) {
223 console.log(JSON.stringify(error));
224 $scope.set_network_error = 'DNS Servers';
225 });
226 }
227
beccabroek971ac1a2018-09-24 13:14:05 -0500228 function removeIPV4s() {
229 return $scope.IPV4s_to_delete.map(function(ipv4) {
230 return APIUtils.deleteIPV4($scope.selectedInterface, ipv4.id)
231 .then(
232 function(data) {},
233 function(error) {
234 console.log(JSON.stringify(error));
235 $scope.set_network_error = ipv4.Address;
236 })
237 });
238 }
239
beccabroek1a0e7d02018-09-24 15:03:40 -0500240 function addIPV4(index) {
241 return APIUtils
242 .addIPV4(
243 $scope.selectedInterface,
244 $scope.interface.ipv4.values[index].Address,
245 $scope.interface.ipv4.values[index].PrefixLength,
246 $scope.interface.ipv4.values[index].Gateway)
247 .then(
248 function(data) {},
249 function(error) {
250 console.log(JSON.stringify(error));
251 $scope.set_network_error =
252 $scope.interface.ipv4.values[index].Address;
253 })
254 }
255
256 function updateIPV4(index) {
Gunnar Millsa45c3852018-05-30 16:18:45 -0500257 // The correct way to edit an IPV4 interface is to remove it and then
258 // add a new one
259 return APIUtils
260 .deleteIPV4(
beccabroek1a0e7d02018-09-24 15:03:40 -0500261 $scope.selectedInterface,
262 $scope.interface.ipv4.values[index].id)
Gunnar Millsa45c3852018-05-30 16:18:45 -0500263 .then(
264 function(data) {
265 return APIUtils
266 .addIPV4(
267 $scope.selectedInterface,
268 $scope.interface.ipv4.values[index].Address,
269 $scope.interface.ipv4.values[index].PrefixLength,
270 $scope.interface.ipv4.values[index].Gateway)
271 .then(
272 function(data) {},
273 function(error) {
274 console.log(JSON.stringify(error));
275 $scope.set_network_error =
276 $scope.interface.ipv4.values[index].Address;
277 });
278 },
279 function(error) {
280 console.log(JSON.stringify(error));
281 $scope.set_network_error =
282 $scope.interface.ipv4.values[index].Address;
283 });
284 }
285
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500286 $scope.refresh = function() {
Gunnar Millsa3f75322018-07-10 17:06:02 -0500287 loadNetworkInfo();
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500288 };
Gunnar Millsa3f75322018-07-10 17:06:02 -0500289
290 function loadNetworkInfo() {
291 APIUtils.getNetworkInfo().then(function(data) {
292 dataService.setNetworkInfo(data);
293 $scope.network = data.formatted_data;
294 $scope.hostname = data.hostname;
295 $scope.defaultgateway = data.defaultgateway;
296 if ($scope.network.interface_ids.length) {
Rebecca Shawffdef962018-07-19 13:37:37 -0500297 // Use the first network interface if the user hasn't chosen one
Gunnar Millsa3f75322018-07-10 17:06:02 -0500298 if (!$scope.selectedInterface ||
299 !$scope.network.interfaces[$scope.selectedInterface]) {
300 $scope.selectedInterface = $scope.network.interface_ids[0];
301 }
302 $scope.interface =
303 $scope.network.interfaces[$scope.selectedInterface];
304 // Copy the interface so we know later if changes were made to the
305 // page
306 $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
307 }
beccabroek1a0e7d02018-09-24 15:03:40 -0500308 // Add id values and update flags to corresponding IPV4 objects
beccabroek971ac1a2018-09-24 13:14:05 -0500309 for (var i = 0; i < $scope.interface.ipv4.values.length; i++) {
310 $scope.interface.ipv4.values[i].id = $scope.interface.ipv4.ids[i];
beccabroek1a0e7d02018-09-24 15:03:40 -0500311 $scope.interface.ipv4.values[i].update_address = false;
312 $scope.interface.ipv4.values[i].update_gateway = false;
313 $scope.interface.ipv4.values[i].update_prefix = false;
beccabroek971ac1a2018-09-24 13:14:05 -0500314 }
Gunnar Millsa3f75322018-07-10 17:06:02 -0500315 });
316 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700317 }
318 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500319})(angular);