blob: b0daefdcec51fa0d4c2c81b850574dc0db8527c6 [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
Andrew Geisslerd27bb132018-05-24 11:07:27 -070028 $scope.selectInterface = function(interfaceId) {
29 $scope.interface = $scope.network.interfaces[interfaceId];
Gunnar Millsa45c3852018-05-30 16:18:45 -050030 // Copy the interface so we know later if changes were made to the page
31 $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
Andrew Geisslerd27bb132018-05-24 11:07:27 -070032 $scope.selectedInterface = interfaceId;
33 $scope.networkDevice = false;
34 };
Gunnar Mills7ddc7272018-04-12 16:12:03 -050035 $scope.setNetworkSettings = function() {
Gunnar Millsd01504c2018-05-03 13:01:51 -050036 // Hides the confirm network settings modal
37 $scope.confirm_settings = false;
Gunnar Mills7ddc7272018-04-12 16:12:03 -050038 $scope.set_network_error = '';
39 $scope.set_network_success = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050040 $scope.loading = true;
Gunnar Millsdca79d72018-05-30 13:07:01 -050041 var promises = [];
42
Gunnar Mills659651e2018-05-30 15:21:07 -050043 // MAC Address are case-insensitive
44 if ($scope.interface.MACAddress.toLowerCase() !=
45 dataService.mac_address.toLowerCase()) {
46 promises.push(setMACAddress());
47 }
48 if ($scope.defaultgateway != dataService.defaultgateway) {
49 promises.push(setDefaultGateway());
50 }
51 if ($scope.hostname != dataService.hostname) {
52 promises.push(setHostname());
53 }
Gunnar Millscb2c3062018-05-31 13:13:30 -050054 if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) {
55 promises.push(setDHCPEnabled());
56 }
Gunnar Mills309e06a2018-05-30 13:18:10 -050057
Gunnar Mills82658292018-06-06 16:51:32 -050058 // Remove any empty strings from the array. Important because we add an
59 // empty string to the end so the user can add a new DNS server, if the
60 // user doesn't fill out the field, we don't want to add.
61 $scope.interface.Nameservers =
62 $scope.interface.Nameservers.filter(Boolean);
Gunnar Mills06467822018-06-06 15:43:18 -050063 // toString() is a cheap way to compare 2 string arrays
64 if ($scope.interface.Nameservers.toString() !=
65 $scope.old_interface.Nameservers.toString()) {
66 promises.push(setNameservers());
67 }
68
Gunnar Millsa45c3852018-05-30 16:18:45 -050069 // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways
70 if (!$scope.interface.DHCPEnabled) {
71 for (var i in $scope.interface.ipv4.values) {
Gunnar Mills65491142018-06-04 14:23:33 -050072 if (!APIUtils.validIPV4IP(
73 $scope.interface.ipv4.values[i].Address)) {
74 $scope.set_network_error =
75 $scope.interface.ipv4.values[i].Address +
76 ' invalid IP parameter';
77 $scope.loading = false;
78 return;
79 }
80 if (!APIUtils.validIPV4IP(
81 $scope.interface.ipv4.values[i].Gateway)) {
82 $scope.set_network_error =
83 $scope.interface.ipv4.values[i].Address +
84 ' invalid gateway parameter';
85 $scope.loading = false;
86 return;
87 }
88 // The netmask prefix length will be undefined if outside range
89 if (!$scope.interface.ipv4.values[i].PrefixLength) {
90 $scope.set_network_error =
91 $scope.interface.ipv4.values[i].Address +
92 ' invalid Prefix Length parameter';
93 $scope.loading = false;
94 return;
95 }
Gunnar Millsa45c3852018-05-30 16:18:45 -050096 if ($scope.interface.ipv4.values[i].Address !=
97 $scope.old_interface.ipv4.values[i].Address ||
98 $scope.interface.ipv4.values[i].PrefixLength !=
99 $scope.old_interface.ipv4.values[i].PrefixLength ||
100 $scope.interface.ipv4.values[i].Gateway !=
101 $scope.old_interface.ipv4.values[i].Gateway) {
102 promises.push(setIPV4(i));
103 }
104 }
105 }
106
Gunnar Mills0af165b2018-06-01 16:24:56 -0500107 if (promises.length) {
108 $q.all(promises).finally(function() {
109 $scope.loading = false;
110 if (!$scope.set_network_error) {
111 $scope.set_network_success = true;
112 // Since an IPV4 interface (e.g. IP address, gateway, or netmask)
113 // edit is a delete then an add and the GUI can't calculate the
114 // interface id (e.g. 5c083707) beforehand and it is not returned
115 // by the REST call, openbmc#3227, reload the page after an edit,
116 // which makes a 2nd REST call.
117 // Do this for all network changes due to the possibility of a set
118 // network failing even though it returned success, openbmc#1641,
119 // and to update dataService and old_interface to know which
120 // data has changed if the user continues to edit network
121 // settings.
122 // TODO: The reload is not ideal. Revisit this.
123 $timeout(function() {
124 $route.reload();
125 }, 4000);
126 }
127 });
128 } else {
Gunnar Mills84981f02018-05-31 15:19:01 -0500129 $scope.loading = false;
Gunnar Mills0af165b2018-06-01 16:24:56 -0500130 }
Gunnar Millsdca79d72018-05-30 13:07:01 -0500131
132 };
133
134 function setMACAddress() {
135 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500136 .setMACAddress(
137 $scope.selectedInterface, $scope.interface.MACAddress)
138 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -0500139 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500140 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -0500141 console.log(JSON.stringify(error));
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500142 $scope.set_network_error = 'MAC Address';
143 });
Gunnar Millsdca79d72018-05-30 13:07:01 -0500144 }
145
146 function setDefaultGateway() {
147 return APIUtils.setDefaultGateway($scope.defaultgateway)
148 .then(
149 function(data) {},
150 function(error) {
151 console.log(JSON.stringify(error));
152 $scope.set_network_error = 'Default Gateway';
153 });
154 }
Gunnar Mills309e06a2018-05-30 13:18:10 -0500155
156 function setHostname() {
157 return APIUtils.setHostname($scope.hostname)
158 .then(
159 function(data) {},
160 function(error) {
161 console.log(JSON.stringify(error));
162 $scope.set_network_error = 'Hostname';
163 });
164 }
165
Gunnar Millscb2c3062018-05-31 13:13:30 -0500166 function setDHCPEnabled() {
167 return APIUtils
168 .setDHCPEnabled(
169 $scope.selectedInterface, $scope.interface.DHCPEnabled)
170 .then(
171 function(data) {},
172 function(error) {
173 console.log(JSON.stringify(error));
174 $scope.set_network_error = 'DHCP';
175 });
176 }
177
Gunnar Mills06467822018-06-06 15:43:18 -0500178 function setNameservers() {
Gunnar Mills82658292018-06-06 16:51:32 -0500179 // Nameservers does not allow an empty array, since we remove all empty
180 // strings above, could have an empty array.
181 if ($scope.interface.Nameservers.length == 0) {
182 $scope.interface.Nameservers.push('');
183 }
Gunnar Mills06467822018-06-06 15:43:18 -0500184 return APIUtils
185 .setNameservers(
186 $scope.selectedInterface, $scope.interface.Nameservers)
187 .then(
188 function(data) {},
189 function(error) {
190 console.log(JSON.stringify(error));
191 $scope.set_network_error = 'DNS Servers';
192 });
193 }
194
Gunnar Millsa45c3852018-05-30 16:18:45 -0500195 function setIPV4(index) {
196 // The correct way to edit an IPV4 interface is to remove it and then
197 // add a new one
198 return APIUtils
199 .deleteIPV4(
200 $scope.selectedInterface, $scope.interface.ipv4.ids[index])
201 .then(
202 function(data) {
203 return APIUtils
204 .addIPV4(
205 $scope.selectedInterface,
206 $scope.interface.ipv4.values[index].Address,
207 $scope.interface.ipv4.values[index].PrefixLength,
208 $scope.interface.ipv4.values[index].Gateway)
209 .then(
210 function(data) {},
211 function(error) {
212 console.log(JSON.stringify(error));
213 $scope.set_network_error =
214 $scope.interface.ipv4.values[index].Address;
215 });
216 },
217 function(error) {
218 console.log(JSON.stringify(error));
219 $scope.set_network_error =
220 $scope.interface.ipv4.values[index].Address;
221 });
222 }
223
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500224 $scope.refresh = function() {
225 $route.reload();
226 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700227 APIUtils.getNetworkInfo().then(function(data) {
Gunnar Mills659651e2018-05-30 15:21:07 -0500228 dataService.setNetworkInfo(data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700229 $scope.network = data.formatted_data;
230 $scope.hostname = data.hostname;
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500231 $scope.defaultgateway = data.defaultgateway;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700232 if ($scope.network.interface_ids.length) {
233 $scope.selectedInterface = $scope.network.interface_ids[0];
234 $scope.interface =
235 $scope.network.interfaces[$scope.selectedInterface];
Gunnar Millsa45c3852018-05-30 16:18:45 -0500236 // Copy the interface so we know later if changes were made to the
237 // page
238 $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700239 }
240 });
241 }
242 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500243
244})(angular);