blob: 4e8a7e03f24b41c1f8545b76984847e97eeeb9ad [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 Mills06467822018-06-06 15:43:18 -050058 // toString() is a cheap way to compare 2 string arrays
59 if ($scope.interface.Nameservers.toString() !=
60 $scope.old_interface.Nameservers.toString()) {
61 promises.push(setNameservers());
62 }
63
Gunnar Millsa45c3852018-05-30 16:18:45 -050064 // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways
65 if (!$scope.interface.DHCPEnabled) {
66 for (var i in $scope.interface.ipv4.values) {
Gunnar Mills65491142018-06-04 14:23:33 -050067 if (!APIUtils.validIPV4IP(
68 $scope.interface.ipv4.values[i].Address)) {
69 $scope.set_network_error =
70 $scope.interface.ipv4.values[i].Address +
71 ' invalid IP parameter';
72 $scope.loading = false;
73 return;
74 }
75 if (!APIUtils.validIPV4IP(
76 $scope.interface.ipv4.values[i].Gateway)) {
77 $scope.set_network_error =
78 $scope.interface.ipv4.values[i].Address +
79 ' invalid gateway parameter';
80 $scope.loading = false;
81 return;
82 }
83 // The netmask prefix length will be undefined if outside range
84 if (!$scope.interface.ipv4.values[i].PrefixLength) {
85 $scope.set_network_error =
86 $scope.interface.ipv4.values[i].Address +
87 ' invalid Prefix Length parameter';
88 $scope.loading = false;
89 return;
90 }
Gunnar Millsa45c3852018-05-30 16:18:45 -050091 if ($scope.interface.ipv4.values[i].Address !=
92 $scope.old_interface.ipv4.values[i].Address ||
93 $scope.interface.ipv4.values[i].PrefixLength !=
94 $scope.old_interface.ipv4.values[i].PrefixLength ||
95 $scope.interface.ipv4.values[i].Gateway !=
96 $scope.old_interface.ipv4.values[i].Gateway) {
97 promises.push(setIPV4(i));
98 }
99 }
100 }
101
Gunnar Mills0af165b2018-06-01 16:24:56 -0500102 if (promises.length) {
103 $q.all(promises).finally(function() {
104 $scope.loading = false;
105 if (!$scope.set_network_error) {
106 $scope.set_network_success = true;
107 // Since an IPV4 interface (e.g. IP address, gateway, or netmask)
108 // edit is a delete then an add and the GUI can't calculate the
109 // interface id (e.g. 5c083707) beforehand and it is not returned
110 // by the REST call, openbmc#3227, reload the page after an edit,
111 // which makes a 2nd REST call.
112 // Do this for all network changes due to the possibility of a set
113 // network failing even though it returned success, openbmc#1641,
114 // and to update dataService and old_interface to know which
115 // data has changed if the user continues to edit network
116 // settings.
117 // TODO: The reload is not ideal. Revisit this.
118 $timeout(function() {
119 $route.reload();
120 }, 4000);
121 }
122 });
123 } else {
Gunnar Mills84981f02018-05-31 15:19:01 -0500124 $scope.loading = false;
Gunnar Mills0af165b2018-06-01 16:24:56 -0500125 }
Gunnar Millsdca79d72018-05-30 13:07:01 -0500126
127 };
128
129 function setMACAddress() {
130 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500131 .setMACAddress(
132 $scope.selectedInterface, $scope.interface.MACAddress)
133 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -0500134 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500135 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -0500136 console.log(JSON.stringify(error));
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500137 $scope.set_network_error = 'MAC Address';
138 });
Gunnar Millsdca79d72018-05-30 13:07:01 -0500139 }
140
141 function setDefaultGateway() {
142 return APIUtils.setDefaultGateway($scope.defaultgateway)
143 .then(
144 function(data) {},
145 function(error) {
146 console.log(JSON.stringify(error));
147 $scope.set_network_error = 'Default Gateway';
148 });
149 }
Gunnar Mills309e06a2018-05-30 13:18:10 -0500150
151 function setHostname() {
152 return APIUtils.setHostname($scope.hostname)
153 .then(
154 function(data) {},
155 function(error) {
156 console.log(JSON.stringify(error));
157 $scope.set_network_error = 'Hostname';
158 });
159 }
160
Gunnar Millscb2c3062018-05-31 13:13:30 -0500161 function setDHCPEnabled() {
162 return APIUtils
163 .setDHCPEnabled(
164 $scope.selectedInterface, $scope.interface.DHCPEnabled)
165 .then(
166 function(data) {},
167 function(error) {
168 console.log(JSON.stringify(error));
169 $scope.set_network_error = 'DHCP';
170 });
171 }
172
Gunnar Mills06467822018-06-06 15:43:18 -0500173 function setNameservers() {
174 return APIUtils
175 .setNameservers(
176 $scope.selectedInterface, $scope.interface.Nameservers)
177 .then(
178 function(data) {},
179 function(error) {
180 console.log(JSON.stringify(error));
181 $scope.set_network_error = 'DNS Servers';
182 });
183 }
184
Gunnar Millsa45c3852018-05-30 16:18:45 -0500185 function setIPV4(index) {
186 // The correct way to edit an IPV4 interface is to remove it and then
187 // add a new one
188 return APIUtils
189 .deleteIPV4(
190 $scope.selectedInterface, $scope.interface.ipv4.ids[index])
191 .then(
192 function(data) {
193 return APIUtils
194 .addIPV4(
195 $scope.selectedInterface,
196 $scope.interface.ipv4.values[index].Address,
197 $scope.interface.ipv4.values[index].PrefixLength,
198 $scope.interface.ipv4.values[index].Gateway)
199 .then(
200 function(data) {},
201 function(error) {
202 console.log(JSON.stringify(error));
203 $scope.set_network_error =
204 $scope.interface.ipv4.values[index].Address;
205 });
206 },
207 function(error) {
208 console.log(JSON.stringify(error));
209 $scope.set_network_error =
210 $scope.interface.ipv4.values[index].Address;
211 });
212 }
213
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500214 $scope.refresh = function() {
215 $route.reload();
216 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700217 APIUtils.getNetworkInfo().then(function(data) {
Gunnar Mills659651e2018-05-30 15:21:07 -0500218 dataService.setNetworkInfo(data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700219 $scope.network = data.formatted_data;
220 $scope.hostname = data.hostname;
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500221 $scope.defaultgateway = data.defaultgateway;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700222 if ($scope.network.interface_ids.length) {
223 $scope.selectedInterface = $scope.network.interface_ids[0];
224 $scope.interface =
225 $scope.network.interfaces[$scope.selectedInterface];
Gunnar Millsa45c3852018-05-30 16:18:45 -0500226 // Copy the interface so we know later if changes were made to the
227 // page
228 $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700229 }
230 });
231 }
232 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500233
234})(angular);