Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for network |
| 3 | * |
| 4 | * @module app/configuration |
| 5 | * @exports networkController |
| 6 | * @name networkController |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 9 | window.angular && (function(angular) { |
| 10 | 'use strict'; |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 11 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 12 | angular.module('app.configuration').controller('networkController', [ |
Gunnar Mills | 0af165b | 2018-06-01 16:24:56 -0500 | [diff] [blame] | 13 | '$scope', '$window', 'APIUtils', 'dataService', '$timeout', '$route', '$q', |
| 14 | function($scope, $window, APIUtils, dataService, $timeout, $route, $q) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 15 | $scope.dataService = dataService; |
| 16 | $scope.network = {}; |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 17 | $scope.old_interface = {}; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 18 | $scope.interface = {}; |
| 19 | $scope.networkDevice = false; |
| 20 | $scope.hostname = ''; |
Gunnar Mills | e9f5fe7 | 2018-05-04 13:43:10 -0500 | [diff] [blame] | 21 | $scope.defaultgateway = ''; |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 22 | $scope.set_network_error = ''; |
| 23 | $scope.set_network_success = false; |
| 24 | $scope.selectedInterface = ''; |
Gunnar Mills | d01504c | 2018-05-03 13:01:51 -0500 | [diff] [blame] | 25 | $scope.confirm_settings = false; |
Gunnar Mills | 84981f0 | 2018-05-31 15:19:01 -0500 | [diff] [blame] | 26 | $scope.loading = false; |
beccabroek | 971ac1a | 2018-09-24 13:14:05 -0500 | [diff] [blame] | 27 | $scope.IPV4s_to_delete = []; |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 28 | |
Gunnar Mills | a3f7532 | 2018-07-10 17:06:02 -0500 | [diff] [blame] | 29 | loadNetworkInfo(); |
| 30 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 31 | $scope.selectInterface = function(interfaceId) { |
| 32 | $scope.interface = $scope.network.interfaces[interfaceId]; |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 33 | // 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 Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 35 | $scope.selectedInterface = interfaceId; |
| 36 | $scope.networkDevice = false; |
| 37 | }; |
Gunnar Mills | bc3ab72 | 2018-07-10 15:11:01 -0500 | [diff] [blame] | 38 | |
| 39 | $scope.addDNSField = function() { |
| 40 | $scope.interface.Nameservers.push(''); |
| 41 | }; |
| 42 | |
beccabroek | cff6150 | 2018-09-13 14:39:23 -0500 | [diff] [blame] | 43 | $scope.removeDNSField = function(index) { |
| 44 | $scope.interface.Nameservers.splice(index, 1); |
| 45 | }; |
| 46 | |
beccabroek | 971ac1a | 2018-09-24 13:14:05 -0500 | [diff] [blame] | 47 | $scope.removeIpv4Address = function(index) { |
| 48 | // Check if the IPV4 being removed has an id. This indicates that it is |
| 49 | // an existing address and needs to be removed in the back end. |
| 50 | if ($scope.interface.ipv4.values[index].id) { |
| 51 | $scope.IPV4s_to_delete.push($scope.interface.ipv4.values[index]); |
| 52 | } |
| 53 | $scope.interface.ipv4.values.splice(index, 1); |
| 54 | }; |
| 55 | |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 56 | $scope.setNetworkSettings = function() { |
Gunnar Mills | d01504c | 2018-05-03 13:01:51 -0500 | [diff] [blame] | 57 | // Hides the confirm network settings modal |
| 58 | $scope.confirm_settings = false; |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 59 | $scope.set_network_error = ''; |
| 60 | $scope.set_network_success = false; |
Gunnar Mills | 84981f0 | 2018-05-31 15:19:01 -0500 | [diff] [blame] | 61 | $scope.loading = true; |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 62 | var promises = []; |
| 63 | |
Gunnar Mills | 659651e | 2018-05-30 15:21:07 -0500 | [diff] [blame] | 64 | // MAC Address are case-insensitive |
| 65 | if ($scope.interface.MACAddress.toLowerCase() != |
| 66 | dataService.mac_address.toLowerCase()) { |
| 67 | promises.push(setMACAddress()); |
| 68 | } |
| 69 | if ($scope.defaultgateway != dataService.defaultgateway) { |
| 70 | promises.push(setDefaultGateway()); |
| 71 | } |
| 72 | if ($scope.hostname != dataService.hostname) { |
| 73 | promises.push(setHostname()); |
| 74 | } |
Gunnar Mills | cb2c306 | 2018-05-31 13:13:30 -0500 | [diff] [blame] | 75 | if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) { |
| 76 | promises.push(setDHCPEnabled()); |
| 77 | } |
Gunnar Mills | 309e06a | 2018-05-30 13:18:10 -0500 | [diff] [blame] | 78 | |
Gunnar Mills | 8265829 | 2018-06-06 16:51:32 -0500 | [diff] [blame] | 79 | // Remove any empty strings from the array. Important because we add an |
| 80 | // empty string to the end so the user can add a new DNS server, if the |
| 81 | // user doesn't fill out the field, we don't want to add. |
| 82 | $scope.interface.Nameservers = |
| 83 | $scope.interface.Nameservers.filter(Boolean); |
Gunnar Mills | 0646782 | 2018-06-06 15:43:18 -0500 | [diff] [blame] | 84 | // toString() is a cheap way to compare 2 string arrays |
| 85 | if ($scope.interface.Nameservers.toString() != |
| 86 | $scope.old_interface.Nameservers.toString()) { |
| 87 | promises.push(setNameservers()); |
| 88 | } |
| 89 | |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 90 | // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways |
| 91 | if (!$scope.interface.DHCPEnabled) { |
beccabroek | 971ac1a | 2018-09-24 13:14:05 -0500 | [diff] [blame] | 92 | // Delete existing IPV4 addresses that were removed |
| 93 | promises.push(removeIPV4s()); |
| 94 | // Update any changed IPV4 addresses |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 95 | for (var i in $scope.interface.ipv4.values) { |
Gunnar Mills | 6549114 | 2018-06-04 14:23:33 -0500 | [diff] [blame] | 96 | if (!APIUtils.validIPV4IP( |
| 97 | $scope.interface.ipv4.values[i].Address)) { |
| 98 | $scope.set_network_error = |
| 99 | $scope.interface.ipv4.values[i].Address + |
| 100 | ' invalid IP parameter'; |
| 101 | $scope.loading = false; |
| 102 | return; |
| 103 | } |
| 104 | if (!APIUtils.validIPV4IP( |
| 105 | $scope.interface.ipv4.values[i].Gateway)) { |
| 106 | $scope.set_network_error = |
| 107 | $scope.interface.ipv4.values[i].Address + |
| 108 | ' invalid gateway parameter'; |
| 109 | $scope.loading = false; |
| 110 | return; |
| 111 | } |
| 112 | // The netmask prefix length will be undefined if outside range |
| 113 | if (!$scope.interface.ipv4.values[i].PrefixLength) { |
| 114 | $scope.set_network_error = |
| 115 | $scope.interface.ipv4.values[i].Address + |
| 116 | ' invalid Prefix Length parameter'; |
| 117 | $scope.loading = false; |
| 118 | return; |
| 119 | } |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 120 | if ($scope.interface.ipv4.values[i].Address != |
| 121 | $scope.old_interface.ipv4.values[i].Address || |
| 122 | $scope.interface.ipv4.values[i].PrefixLength != |
| 123 | $scope.old_interface.ipv4.values[i].PrefixLength || |
| 124 | $scope.interface.ipv4.values[i].Gateway != |
| 125 | $scope.old_interface.ipv4.values[i].Gateway) { |
| 126 | promises.push(setIPV4(i)); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Gunnar Mills | 0af165b | 2018-06-01 16:24:56 -0500 | [diff] [blame] | 131 | if (promises.length) { |
| 132 | $q.all(promises).finally(function() { |
| 133 | $scope.loading = false; |
| 134 | if (!$scope.set_network_error) { |
| 135 | $scope.set_network_success = true; |
| 136 | // Since an IPV4 interface (e.g. IP address, gateway, or netmask) |
| 137 | // edit is a delete then an add and the GUI can't calculate the |
| 138 | // interface id (e.g. 5c083707) beforehand and it is not returned |
| 139 | // by the REST call, openbmc#3227, reload the page after an edit, |
| 140 | // which makes a 2nd REST call. |
| 141 | // Do this for all network changes due to the possibility of a set |
| 142 | // network failing even though it returned success, openbmc#1641, |
| 143 | // and to update dataService and old_interface to know which |
| 144 | // data has changed if the user continues to edit network |
| 145 | // settings. |
| 146 | // TODO: The reload is not ideal. Revisit this. |
| 147 | $timeout(function() { |
Gunnar Mills | a3f7532 | 2018-07-10 17:06:02 -0500 | [diff] [blame] | 148 | loadNetworkInfo(); |
Gunnar Mills | 0af165b | 2018-06-01 16:24:56 -0500 | [diff] [blame] | 149 | }, 4000); |
| 150 | } |
| 151 | }); |
| 152 | } else { |
Gunnar Mills | 84981f0 | 2018-05-31 15:19:01 -0500 | [diff] [blame] | 153 | $scope.loading = false; |
Gunnar Mills | 0af165b | 2018-06-01 16:24:56 -0500 | [diff] [blame] | 154 | } |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | function setMACAddress() { |
| 158 | return APIUtils |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 159 | .setMACAddress( |
| 160 | $scope.selectedInterface, $scope.interface.MACAddress) |
| 161 | .then( |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 162 | function(data) {}, |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 163 | function(error) { |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 164 | console.log(JSON.stringify(error)); |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 165 | $scope.set_network_error = 'MAC Address'; |
| 166 | }); |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | function setDefaultGateway() { |
| 170 | return APIUtils.setDefaultGateway($scope.defaultgateway) |
| 171 | .then( |
| 172 | function(data) {}, |
| 173 | function(error) { |
| 174 | console.log(JSON.stringify(error)); |
| 175 | $scope.set_network_error = 'Default Gateway'; |
| 176 | }); |
| 177 | } |
Gunnar Mills | 309e06a | 2018-05-30 13:18:10 -0500 | [diff] [blame] | 178 | |
| 179 | function setHostname() { |
| 180 | return APIUtils.setHostname($scope.hostname) |
| 181 | .then( |
| 182 | function(data) {}, |
| 183 | function(error) { |
| 184 | console.log(JSON.stringify(error)); |
| 185 | $scope.set_network_error = 'Hostname'; |
| 186 | }); |
| 187 | } |
| 188 | |
Gunnar Mills | cb2c306 | 2018-05-31 13:13:30 -0500 | [diff] [blame] | 189 | function setDHCPEnabled() { |
| 190 | return APIUtils |
| 191 | .setDHCPEnabled( |
| 192 | $scope.selectedInterface, $scope.interface.DHCPEnabled) |
| 193 | .then( |
| 194 | function(data) {}, |
| 195 | function(error) { |
| 196 | console.log(JSON.stringify(error)); |
| 197 | $scope.set_network_error = 'DHCP'; |
| 198 | }); |
| 199 | } |
| 200 | |
Gunnar Mills | 0646782 | 2018-06-06 15:43:18 -0500 | [diff] [blame] | 201 | function setNameservers() { |
Gunnar Mills | 8265829 | 2018-06-06 16:51:32 -0500 | [diff] [blame] | 202 | // Nameservers does not allow an empty array, since we remove all empty |
Gunnar Mills | 9863d12 | 2018-07-11 13:36:43 -0500 | [diff] [blame] | 203 | // strings above, could have an empty array. TODO: openbmc/openbmc#3240 |
Gunnar Mills | 8265829 | 2018-06-06 16:51:32 -0500 | [diff] [blame] | 204 | if ($scope.interface.Nameservers.length == 0) { |
| 205 | $scope.interface.Nameservers.push(''); |
| 206 | } |
Gunnar Mills | 0646782 | 2018-06-06 15:43:18 -0500 | [diff] [blame] | 207 | return APIUtils |
| 208 | .setNameservers( |
| 209 | $scope.selectedInterface, $scope.interface.Nameservers) |
| 210 | .then( |
| 211 | function(data) {}, |
| 212 | function(error) { |
| 213 | console.log(JSON.stringify(error)); |
| 214 | $scope.set_network_error = 'DNS Servers'; |
| 215 | }); |
| 216 | } |
| 217 | |
beccabroek | 971ac1a | 2018-09-24 13:14:05 -0500 | [diff] [blame] | 218 | function removeIPV4s() { |
| 219 | return $scope.IPV4s_to_delete.map(function(ipv4) { |
| 220 | return APIUtils.deleteIPV4($scope.selectedInterface, ipv4.id) |
| 221 | .then( |
| 222 | function(data) {}, |
| 223 | function(error) { |
| 224 | console.log(JSON.stringify(error)); |
| 225 | $scope.set_network_error = ipv4.Address; |
| 226 | }) |
| 227 | }); |
| 228 | } |
| 229 | |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 230 | function setIPV4(index) { |
| 231 | // The correct way to edit an IPV4 interface is to remove it and then |
| 232 | // add a new one |
| 233 | return APIUtils |
| 234 | .deleteIPV4( |
| 235 | $scope.selectedInterface, $scope.interface.ipv4.ids[index]) |
| 236 | .then( |
| 237 | function(data) { |
| 238 | return APIUtils |
| 239 | .addIPV4( |
| 240 | $scope.selectedInterface, |
| 241 | $scope.interface.ipv4.values[index].Address, |
| 242 | $scope.interface.ipv4.values[index].PrefixLength, |
| 243 | $scope.interface.ipv4.values[index].Gateway) |
| 244 | .then( |
| 245 | function(data) {}, |
| 246 | function(error) { |
| 247 | console.log(JSON.stringify(error)); |
| 248 | $scope.set_network_error = |
| 249 | $scope.interface.ipv4.values[index].Address; |
| 250 | }); |
| 251 | }, |
| 252 | function(error) { |
| 253 | console.log(JSON.stringify(error)); |
| 254 | $scope.set_network_error = |
| 255 | $scope.interface.ipv4.values[index].Address; |
| 256 | }); |
| 257 | } |
| 258 | |
Gunnar Mills | 9a0094d | 2018-05-02 21:50:56 -0500 | [diff] [blame] | 259 | $scope.refresh = function() { |
Gunnar Mills | a3f7532 | 2018-07-10 17:06:02 -0500 | [diff] [blame] | 260 | loadNetworkInfo(); |
Gunnar Mills | 9a0094d | 2018-05-02 21:50:56 -0500 | [diff] [blame] | 261 | }; |
Gunnar Mills | a3f7532 | 2018-07-10 17:06:02 -0500 | [diff] [blame] | 262 | |
| 263 | function loadNetworkInfo() { |
| 264 | APIUtils.getNetworkInfo().then(function(data) { |
| 265 | dataService.setNetworkInfo(data); |
| 266 | $scope.network = data.formatted_data; |
| 267 | $scope.hostname = data.hostname; |
| 268 | $scope.defaultgateway = data.defaultgateway; |
| 269 | if ($scope.network.interface_ids.length) { |
Rebecca Shaw | ffdef96 | 2018-07-19 13:37:37 -0500 | [diff] [blame] | 270 | // Use the first network interface if the user hasn't chosen one |
Gunnar Mills | a3f7532 | 2018-07-10 17:06:02 -0500 | [diff] [blame] | 271 | if (!$scope.selectedInterface || |
| 272 | !$scope.network.interfaces[$scope.selectedInterface]) { |
| 273 | $scope.selectedInterface = $scope.network.interface_ids[0]; |
| 274 | } |
| 275 | $scope.interface = |
| 276 | $scope.network.interfaces[$scope.selectedInterface]; |
| 277 | // Copy the interface so we know later if changes were made to the |
| 278 | // page |
| 279 | $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); |
| 280 | } |
beccabroek | 971ac1a | 2018-09-24 13:14:05 -0500 | [diff] [blame] | 281 | // Add id values to corresponding IPV4 objects |
| 282 | for (var i = 0; i < $scope.interface.ipv4.values.length; i++) { |
| 283 | $scope.interface.ipv4.values[i].id = $scope.interface.ipv4.ids[i]; |
| 284 | } |
Gunnar Mills | a3f7532 | 2018-07-10 17:06:02 -0500 | [diff] [blame] | 285 | }); |
| 286 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 287 | } |
| 288 | ]); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 289 | })(angular); |