blob: d528e7ac10fe244888e9eff0fc2b751a36d19591 [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 Mills7ddc7272018-04-12 16:12:03 -050037 $scope.setNetworkSettings = function() {
Gunnar Millsd01504c2018-05-03 13:01:51 -050038 // Hides the confirm network settings modal
39 $scope.confirm_settings = false;
Gunnar Mills7ddc7272018-04-12 16:12:03 -050040 $scope.set_network_error = '';
41 $scope.set_network_success = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050042 $scope.loading = true;
Gunnar Millsdca79d72018-05-30 13:07:01 -050043 var promises = [];
44
Gunnar Mills659651e2018-05-30 15:21:07 -050045 // MAC Address are case-insensitive
46 if ($scope.interface.MACAddress.toLowerCase() !=
47 dataService.mac_address.toLowerCase()) {
48 promises.push(setMACAddress());
49 }
50 if ($scope.defaultgateway != dataService.defaultgateway) {
51 promises.push(setDefaultGateway());
52 }
53 if ($scope.hostname != dataService.hostname) {
54 promises.push(setHostname());
55 }
Gunnar Millscb2c3062018-05-31 13:13:30 -050056 if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) {
57 promises.push(setDHCPEnabled());
58 }
Gunnar Mills309e06a2018-05-30 13:18:10 -050059
Gunnar Mills82658292018-06-06 16:51:32 -050060 // Remove any empty strings from the array. Important because we add an
61 // empty string to the end so the user can add a new DNS server, if the
62 // user doesn't fill out the field, we don't want to add.
63 $scope.interface.Nameservers =
64 $scope.interface.Nameservers.filter(Boolean);
Gunnar Mills06467822018-06-06 15:43:18 -050065 // toString() is a cheap way to compare 2 string arrays
66 if ($scope.interface.Nameservers.toString() !=
67 $scope.old_interface.Nameservers.toString()) {
68 promises.push(setNameservers());
69 }
70
Gunnar Millsa45c3852018-05-30 16:18:45 -050071 // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways
72 if (!$scope.interface.DHCPEnabled) {
73 for (var i in $scope.interface.ipv4.values) {
Gunnar Mills65491142018-06-04 14:23:33 -050074 if (!APIUtils.validIPV4IP(
75 $scope.interface.ipv4.values[i].Address)) {
76 $scope.set_network_error =
77 $scope.interface.ipv4.values[i].Address +
78 ' invalid IP parameter';
79 $scope.loading = false;
80 return;
81 }
82 if (!APIUtils.validIPV4IP(
83 $scope.interface.ipv4.values[i].Gateway)) {
84 $scope.set_network_error =
85 $scope.interface.ipv4.values[i].Address +
86 ' invalid gateway parameter';
87 $scope.loading = false;
88 return;
89 }
90 // The netmask prefix length will be undefined if outside range
91 if (!$scope.interface.ipv4.values[i].PrefixLength) {
92 $scope.set_network_error =
93 $scope.interface.ipv4.values[i].Address +
94 ' invalid Prefix Length parameter';
95 $scope.loading = false;
96 return;
97 }
Gunnar Millsa45c3852018-05-30 16:18:45 -050098 if ($scope.interface.ipv4.values[i].Address !=
99 $scope.old_interface.ipv4.values[i].Address ||
100 $scope.interface.ipv4.values[i].PrefixLength !=
101 $scope.old_interface.ipv4.values[i].PrefixLength ||
102 $scope.interface.ipv4.values[i].Gateway !=
103 $scope.old_interface.ipv4.values[i].Gateway) {
104 promises.push(setIPV4(i));
105 }
106 }
107 }
108
Gunnar Mills0af165b2018-06-01 16:24:56 -0500109 if (promises.length) {
110 $q.all(promises).finally(function() {
111 $scope.loading = false;
112 if (!$scope.set_network_error) {
113 $scope.set_network_success = true;
114 // Since an IPV4 interface (e.g. IP address, gateway, or netmask)
115 // edit is a delete then an add and the GUI can't calculate the
116 // interface id (e.g. 5c083707) beforehand and it is not returned
117 // by the REST call, openbmc#3227, reload the page after an edit,
118 // which makes a 2nd REST call.
119 // Do this for all network changes due to the possibility of a set
120 // network failing even though it returned success, openbmc#1641,
121 // and to update dataService and old_interface to know which
122 // data has changed if the user continues to edit network
123 // settings.
124 // TODO: The reload is not ideal. Revisit this.
125 $timeout(function() {
Gunnar Millsa3f75322018-07-10 17:06:02 -0500126 loadNetworkInfo();
Gunnar Mills0af165b2018-06-01 16:24:56 -0500127 }, 4000);
128 }
129 });
130 } else {
Gunnar Mills84981f02018-05-31 15:19:01 -0500131 $scope.loading = false;
Gunnar Mills0af165b2018-06-01 16:24:56 -0500132 }
Gunnar Millsdca79d72018-05-30 13:07:01 -0500133
134 };
135
136 function setMACAddress() {
137 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500138 .setMACAddress(
139 $scope.selectedInterface, $scope.interface.MACAddress)
140 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -0500141 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500142 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -0500143 console.log(JSON.stringify(error));
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500144 $scope.set_network_error = 'MAC Address';
145 });
Gunnar Millsdca79d72018-05-30 13:07:01 -0500146 }
147
148 function setDefaultGateway() {
149 return APIUtils.setDefaultGateway($scope.defaultgateway)
150 .then(
151 function(data) {},
152 function(error) {
153 console.log(JSON.stringify(error));
154 $scope.set_network_error = 'Default Gateway';
155 });
156 }
Gunnar Mills309e06a2018-05-30 13:18:10 -0500157
158 function setHostname() {
159 return APIUtils.setHostname($scope.hostname)
160 .then(
161 function(data) {},
162 function(error) {
163 console.log(JSON.stringify(error));
164 $scope.set_network_error = 'Hostname';
165 });
166 }
167
Gunnar Millscb2c3062018-05-31 13:13:30 -0500168 function setDHCPEnabled() {
169 return APIUtils
170 .setDHCPEnabled(
171 $scope.selectedInterface, $scope.interface.DHCPEnabled)
172 .then(
173 function(data) {},
174 function(error) {
175 console.log(JSON.stringify(error));
176 $scope.set_network_error = 'DHCP';
177 });
178 }
179
Gunnar Mills06467822018-06-06 15:43:18 -0500180 function setNameservers() {
Gunnar Mills82658292018-06-06 16:51:32 -0500181 // Nameservers does not allow an empty array, since we remove all empty
182 // strings above, could have an empty array.
183 if ($scope.interface.Nameservers.length == 0) {
184 $scope.interface.Nameservers.push('');
185 }
Gunnar Mills06467822018-06-06 15:43:18 -0500186 return APIUtils
187 .setNameservers(
188 $scope.selectedInterface, $scope.interface.Nameservers)
189 .then(
190 function(data) {},
191 function(error) {
192 console.log(JSON.stringify(error));
193 $scope.set_network_error = 'DNS Servers';
194 });
195 }
196
Gunnar Millsa45c3852018-05-30 16:18:45 -0500197 function setIPV4(index) {
198 // The correct way to edit an IPV4 interface is to remove it and then
199 // add a new one
200 return APIUtils
201 .deleteIPV4(
202 $scope.selectedInterface, $scope.interface.ipv4.ids[index])
203 .then(
204 function(data) {
205 return APIUtils
206 .addIPV4(
207 $scope.selectedInterface,
208 $scope.interface.ipv4.values[index].Address,
209 $scope.interface.ipv4.values[index].PrefixLength,
210 $scope.interface.ipv4.values[index].Gateway)
211 .then(
212 function(data) {},
213 function(error) {
214 console.log(JSON.stringify(error));
215 $scope.set_network_error =
216 $scope.interface.ipv4.values[index].Address;
217 });
218 },
219 function(error) {
220 console.log(JSON.stringify(error));
221 $scope.set_network_error =
222 $scope.interface.ipv4.values[index].Address;
223 });
224 }
225
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500226 $scope.refresh = function() {
Gunnar Millsa3f75322018-07-10 17:06:02 -0500227 loadNetworkInfo();
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500228 };
Gunnar Millsa3f75322018-07-10 17:06:02 -0500229
230 function loadNetworkInfo() {
231 APIUtils.getNetworkInfo().then(function(data) {
232 dataService.setNetworkInfo(data);
233 $scope.network = data.formatted_data;
234 $scope.hostname = data.hostname;
235 $scope.defaultgateway = data.defaultgateway;
236 if ($scope.network.interface_ids.length) {
237 // Use the first network interface if the user hasn't choosen one
238 if (!$scope.selectedInterface ||
239 !$scope.network.interfaces[$scope.selectedInterface]) {
240 $scope.selectedInterface = $scope.network.interface_ids[0];
241 }
242 $scope.interface =
243 $scope.network.interfaces[$scope.selectedInterface];
244 // Copy the interface so we know later if changes were made to the
245 // page
246 $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
247 }
248 });
249 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700250 }
251 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500252
253})(angular);