blob: 9bec7469b37913d1dcb495684f2af439a61b5b4b [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;
Iftekharul Islam2a489552017-11-02 13:23:08 -050027
Gunnar Millsa3f75322018-07-10 17:06:02 -050028 loadNetworkInfo();
29
Andrew Geisslerd27bb132018-05-24 11:07:27 -070030 $scope.selectInterface = function(interfaceId) {
31 $scope.interface = $scope.network.interfaces[interfaceId];
Gunnar Millsa45c3852018-05-30 16:18:45 -050032 // Copy the interface so we know later if changes were made to the page
33 $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
Andrew Geisslerd27bb132018-05-24 11:07:27 -070034 $scope.selectedInterface = interfaceId;
35 $scope.networkDevice = false;
36 };
Gunnar Millsbc3ab722018-07-10 15:11:01 -050037
38 $scope.addDNSField = function() {
39 $scope.interface.Nameservers.push('');
40 };
41
Gunnar Mills7ddc7272018-04-12 16:12:03 -050042 $scope.setNetworkSettings = function() {
Gunnar Millsd01504c2018-05-03 13:01:51 -050043 // Hides the confirm network settings modal
44 $scope.confirm_settings = false;
Gunnar Mills7ddc7272018-04-12 16:12:03 -050045 $scope.set_network_error = '';
46 $scope.set_network_success = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050047 $scope.loading = true;
Gunnar Millsdca79d72018-05-30 13:07:01 -050048 var promises = [];
49
Gunnar Mills659651e2018-05-30 15:21:07 -050050 // MAC Address are case-insensitive
51 if ($scope.interface.MACAddress.toLowerCase() !=
52 dataService.mac_address.toLowerCase()) {
53 promises.push(setMACAddress());
54 }
55 if ($scope.defaultgateway != dataService.defaultgateway) {
56 promises.push(setDefaultGateway());
57 }
58 if ($scope.hostname != dataService.hostname) {
59 promises.push(setHostname());
60 }
Gunnar Millscb2c3062018-05-31 13:13:30 -050061 if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) {
62 promises.push(setDHCPEnabled());
63 }
Gunnar Mills309e06a2018-05-30 13:18:10 -050064
Gunnar Mills82658292018-06-06 16:51:32 -050065 // Remove any empty strings from the array. Important because we add an
66 // empty string to the end so the user can add a new DNS server, if the
67 // user doesn't fill out the field, we don't want to add.
68 $scope.interface.Nameservers =
69 $scope.interface.Nameservers.filter(Boolean);
Gunnar Mills06467822018-06-06 15:43:18 -050070 // toString() is a cheap way to compare 2 string arrays
71 if ($scope.interface.Nameservers.toString() !=
72 $scope.old_interface.Nameservers.toString()) {
73 promises.push(setNameservers());
74 }
75
Gunnar Millsa45c3852018-05-30 16:18:45 -050076 // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways
77 if (!$scope.interface.DHCPEnabled) {
78 for (var i in $scope.interface.ipv4.values) {
Gunnar Mills65491142018-06-04 14:23:33 -050079 if (!APIUtils.validIPV4IP(
80 $scope.interface.ipv4.values[i].Address)) {
81 $scope.set_network_error =
82 $scope.interface.ipv4.values[i].Address +
83 ' invalid IP parameter';
84 $scope.loading = false;
85 return;
86 }
87 if (!APIUtils.validIPV4IP(
88 $scope.interface.ipv4.values[i].Gateway)) {
89 $scope.set_network_error =
90 $scope.interface.ipv4.values[i].Address +
91 ' invalid gateway parameter';
92 $scope.loading = false;
93 return;
94 }
95 // The netmask prefix length will be undefined if outside range
96 if (!$scope.interface.ipv4.values[i].PrefixLength) {
97 $scope.set_network_error =
98 $scope.interface.ipv4.values[i].Address +
99 ' invalid Prefix Length parameter';
100 $scope.loading = false;
101 return;
102 }
Gunnar Millsa45c3852018-05-30 16:18:45 -0500103 if ($scope.interface.ipv4.values[i].Address !=
104 $scope.old_interface.ipv4.values[i].Address ||
105 $scope.interface.ipv4.values[i].PrefixLength !=
106 $scope.old_interface.ipv4.values[i].PrefixLength ||
107 $scope.interface.ipv4.values[i].Gateway !=
108 $scope.old_interface.ipv4.values[i].Gateway) {
109 promises.push(setIPV4(i));
110 }
111 }
112 }
113
Gunnar Mills0af165b2018-06-01 16:24:56 -0500114 if (promises.length) {
115 $q.all(promises).finally(function() {
116 $scope.loading = false;
117 if (!$scope.set_network_error) {
118 $scope.set_network_success = true;
119 // Since an IPV4 interface (e.g. IP address, gateway, or netmask)
120 // edit is a delete then an add and the GUI can't calculate the
121 // interface id (e.g. 5c083707) beforehand and it is not returned
122 // by the REST call, openbmc#3227, reload the page after an edit,
123 // which makes a 2nd REST call.
124 // Do this for all network changes due to the possibility of a set
125 // network failing even though it returned success, openbmc#1641,
126 // and to update dataService and old_interface to know which
127 // data has changed if the user continues to edit network
128 // settings.
129 // TODO: The reload is not ideal. Revisit this.
130 $timeout(function() {
Gunnar Millsa3f75322018-07-10 17:06:02 -0500131 loadNetworkInfo();
Gunnar Mills0af165b2018-06-01 16:24:56 -0500132 }, 4000);
133 }
134 });
135 } else {
Gunnar Mills84981f02018-05-31 15:19:01 -0500136 $scope.loading = false;
Gunnar Mills0af165b2018-06-01 16:24:56 -0500137 }
Gunnar Millsdca79d72018-05-30 13:07:01 -0500138 };
139
140 function setMACAddress() {
141 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500142 .setMACAddress(
143 $scope.selectedInterface, $scope.interface.MACAddress)
144 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -0500145 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500146 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -0500147 console.log(JSON.stringify(error));
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500148 $scope.set_network_error = 'MAC Address';
149 });
Gunnar Millsdca79d72018-05-30 13:07:01 -0500150 }
151
152 function setDefaultGateway() {
153 return APIUtils.setDefaultGateway($scope.defaultgateway)
154 .then(
155 function(data) {},
156 function(error) {
157 console.log(JSON.stringify(error));
158 $scope.set_network_error = 'Default Gateway';
159 });
160 }
Gunnar Mills309e06a2018-05-30 13:18:10 -0500161
162 function setHostname() {
163 return APIUtils.setHostname($scope.hostname)
164 .then(
165 function(data) {},
166 function(error) {
167 console.log(JSON.stringify(error));
168 $scope.set_network_error = 'Hostname';
169 });
170 }
171
Gunnar Millscb2c3062018-05-31 13:13:30 -0500172 function setDHCPEnabled() {
173 return APIUtils
174 .setDHCPEnabled(
175 $scope.selectedInterface, $scope.interface.DHCPEnabled)
176 .then(
177 function(data) {},
178 function(error) {
179 console.log(JSON.stringify(error));
180 $scope.set_network_error = 'DHCP';
181 });
182 }
183
Gunnar Mills06467822018-06-06 15:43:18 -0500184 function setNameservers() {
Gunnar Mills82658292018-06-06 16:51:32 -0500185 // Nameservers does not allow an empty array, since we remove all empty
Gunnar Mills9863d122018-07-11 13:36:43 -0500186 // strings above, could have an empty array. TODO: openbmc/openbmc#3240
Gunnar Mills82658292018-06-06 16:51:32 -0500187 if ($scope.interface.Nameservers.length == 0) {
188 $scope.interface.Nameservers.push('');
189 }
Gunnar Mills06467822018-06-06 15:43:18 -0500190 return APIUtils
191 .setNameservers(
192 $scope.selectedInterface, $scope.interface.Nameservers)
193 .then(
194 function(data) {},
195 function(error) {
196 console.log(JSON.stringify(error));
197 $scope.set_network_error = 'DNS Servers';
198 });
199 }
200
Gunnar Millsa45c3852018-05-30 16:18:45 -0500201 function setIPV4(index) {
202 // The correct way to edit an IPV4 interface is to remove it and then
203 // add a new one
204 return APIUtils
205 .deleteIPV4(
206 $scope.selectedInterface, $scope.interface.ipv4.ids[index])
207 .then(
208 function(data) {
209 return APIUtils
210 .addIPV4(
211 $scope.selectedInterface,
212 $scope.interface.ipv4.values[index].Address,
213 $scope.interface.ipv4.values[index].PrefixLength,
214 $scope.interface.ipv4.values[index].Gateway)
215 .then(
216 function(data) {},
217 function(error) {
218 console.log(JSON.stringify(error));
219 $scope.set_network_error =
220 $scope.interface.ipv4.values[index].Address;
221 });
222 },
223 function(error) {
224 console.log(JSON.stringify(error));
225 $scope.set_network_error =
226 $scope.interface.ipv4.values[index].Address;
227 });
228 }
229
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500230 $scope.refresh = function() {
Gunnar Millsa3f75322018-07-10 17:06:02 -0500231 loadNetworkInfo();
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500232 };
Gunnar Millsa3f75322018-07-10 17:06:02 -0500233
234 function loadNetworkInfo() {
235 APIUtils.getNetworkInfo().then(function(data) {
236 dataService.setNetworkInfo(data);
237 $scope.network = data.formatted_data;
238 $scope.hostname = data.hostname;
239 $scope.defaultgateway = data.defaultgateway;
240 if ($scope.network.interface_ids.length) {
Rebecca Shawffdef962018-07-19 13:37:37 -0500241 // Use the first network interface if the user hasn't chosen one
Gunnar Millsa3f75322018-07-10 17:06:02 -0500242 if (!$scope.selectedInterface ||
243 !$scope.network.interfaces[$scope.selectedInterface]) {
244 $scope.selectedInterface = $scope.network.interface_ids[0];
245 }
246 $scope.interface =
247 $scope.network.interfaces[$scope.selectedInterface];
248 // Copy the interface so we know later if changes were made to the
249 // page
250 $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
251 }
252 });
253 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700254 }
255 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500256})(angular);