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; |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 27 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 28 | $scope.selectInterface = function(interfaceId) { |
| 29 | $scope.interface = $scope.network.interfaces[interfaceId]; |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 30 | // 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 Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 32 | $scope.selectedInterface = interfaceId; |
| 33 | $scope.networkDevice = false; |
| 34 | }; |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 35 | $scope.setNetworkSettings = function() { |
Gunnar Mills | d01504c | 2018-05-03 13:01:51 -0500 | [diff] [blame] | 36 | // Hides the confirm network settings modal |
| 37 | $scope.confirm_settings = false; |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 38 | $scope.set_network_error = ''; |
| 39 | $scope.set_network_success = false; |
Gunnar Mills | 84981f0 | 2018-05-31 15:19:01 -0500 | [diff] [blame] | 40 | $scope.loading = true; |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 41 | var promises = []; |
| 42 | |
Gunnar Mills | 659651e | 2018-05-30 15:21:07 -0500 | [diff] [blame] | 43 | // 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 Mills | cb2c306 | 2018-05-31 13:13:30 -0500 | [diff] [blame] | 54 | if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) { |
| 55 | promises.push(setDHCPEnabled()); |
| 56 | } |
Gunnar Mills | 309e06a | 2018-05-30 13:18:10 -0500 | [diff] [blame] | 57 | |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 58 | // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways |
| 59 | if (!$scope.interface.DHCPEnabled) { |
| 60 | for (var i in $scope.interface.ipv4.values) { |
| 61 | if ($scope.interface.ipv4.values[i].Address != |
| 62 | $scope.old_interface.ipv4.values[i].Address || |
| 63 | $scope.interface.ipv4.values[i].PrefixLength != |
| 64 | $scope.old_interface.ipv4.values[i].PrefixLength || |
| 65 | $scope.interface.ipv4.values[i].Gateway != |
| 66 | $scope.old_interface.ipv4.values[i].Gateway) { |
| 67 | promises.push(setIPV4(i)); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
Gunnar Mills | 0af165b | 2018-06-01 16:24:56 -0500 | [diff] [blame^] | 72 | if (promises.length) { |
| 73 | $q.all(promises).finally(function() { |
| 74 | $scope.loading = false; |
| 75 | if (!$scope.set_network_error) { |
| 76 | $scope.set_network_success = true; |
| 77 | // Since an IPV4 interface (e.g. IP address, gateway, or netmask) |
| 78 | // edit is a delete then an add and the GUI can't calculate the |
| 79 | // interface id (e.g. 5c083707) beforehand and it is not returned |
| 80 | // by the REST call, openbmc#3227, reload the page after an edit, |
| 81 | // which makes a 2nd REST call. |
| 82 | // Do this for all network changes due to the possibility of a set |
| 83 | // network failing even though it returned success, openbmc#1641, |
| 84 | // and to update dataService and old_interface to know which |
| 85 | // data has changed if the user continues to edit network |
| 86 | // settings. |
| 87 | // TODO: The reload is not ideal. Revisit this. |
| 88 | $timeout(function() { |
| 89 | $route.reload(); |
| 90 | }, 4000); |
| 91 | } |
| 92 | }); |
| 93 | } else { |
Gunnar Mills | 84981f0 | 2018-05-31 15:19:01 -0500 | [diff] [blame] | 94 | $scope.loading = false; |
Gunnar Mills | 0af165b | 2018-06-01 16:24:56 -0500 | [diff] [blame^] | 95 | } |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 96 | |
| 97 | }; |
| 98 | |
| 99 | function setMACAddress() { |
| 100 | return APIUtils |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 101 | .setMACAddress( |
| 102 | $scope.selectedInterface, $scope.interface.MACAddress) |
| 103 | .then( |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 104 | function(data) {}, |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 105 | function(error) { |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 106 | console.log(JSON.stringify(error)); |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 107 | $scope.set_network_error = 'MAC Address'; |
| 108 | }); |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | function setDefaultGateway() { |
| 112 | return APIUtils.setDefaultGateway($scope.defaultgateway) |
| 113 | .then( |
| 114 | function(data) {}, |
| 115 | function(error) { |
| 116 | console.log(JSON.stringify(error)); |
| 117 | $scope.set_network_error = 'Default Gateway'; |
| 118 | }); |
| 119 | } |
Gunnar Mills | 309e06a | 2018-05-30 13:18:10 -0500 | [diff] [blame] | 120 | |
| 121 | function setHostname() { |
| 122 | return APIUtils.setHostname($scope.hostname) |
| 123 | .then( |
| 124 | function(data) {}, |
| 125 | function(error) { |
| 126 | console.log(JSON.stringify(error)); |
| 127 | $scope.set_network_error = 'Hostname'; |
| 128 | }); |
| 129 | } |
| 130 | |
Gunnar Mills | cb2c306 | 2018-05-31 13:13:30 -0500 | [diff] [blame] | 131 | function setDHCPEnabled() { |
| 132 | return APIUtils |
| 133 | .setDHCPEnabled( |
| 134 | $scope.selectedInterface, $scope.interface.DHCPEnabled) |
| 135 | .then( |
| 136 | function(data) {}, |
| 137 | function(error) { |
| 138 | console.log(JSON.stringify(error)); |
| 139 | $scope.set_network_error = 'DHCP'; |
| 140 | }); |
| 141 | } |
| 142 | |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 143 | function setIPV4(index) { |
| 144 | // The correct way to edit an IPV4 interface is to remove it and then |
| 145 | // add a new one |
| 146 | return APIUtils |
| 147 | .deleteIPV4( |
| 148 | $scope.selectedInterface, $scope.interface.ipv4.ids[index]) |
| 149 | .then( |
| 150 | function(data) { |
| 151 | return APIUtils |
| 152 | .addIPV4( |
| 153 | $scope.selectedInterface, |
| 154 | $scope.interface.ipv4.values[index].Address, |
| 155 | $scope.interface.ipv4.values[index].PrefixLength, |
| 156 | $scope.interface.ipv4.values[index].Gateway) |
| 157 | .then( |
| 158 | function(data) {}, |
| 159 | function(error) { |
| 160 | console.log(JSON.stringify(error)); |
| 161 | $scope.set_network_error = |
| 162 | $scope.interface.ipv4.values[index].Address; |
| 163 | }); |
| 164 | }, |
| 165 | function(error) { |
| 166 | console.log(JSON.stringify(error)); |
| 167 | $scope.set_network_error = |
| 168 | $scope.interface.ipv4.values[index].Address; |
| 169 | }); |
| 170 | } |
| 171 | |
Gunnar Mills | 9a0094d | 2018-05-02 21:50:56 -0500 | [diff] [blame] | 172 | $scope.refresh = function() { |
| 173 | $route.reload(); |
| 174 | }; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 175 | APIUtils.getNetworkInfo().then(function(data) { |
Gunnar Mills | 659651e | 2018-05-30 15:21:07 -0500 | [diff] [blame] | 176 | dataService.setNetworkInfo(data); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 177 | $scope.network = data.formatted_data; |
| 178 | $scope.hostname = data.hostname; |
Gunnar Mills | e9f5fe7 | 2018-05-04 13:43:10 -0500 | [diff] [blame] | 179 | $scope.defaultgateway = data.defaultgateway; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 180 | if ($scope.network.interface_ids.length) { |
| 181 | $scope.selectedInterface = $scope.network.interface_ids[0]; |
| 182 | $scope.interface = |
| 183 | $scope.network.interfaces[$scope.selectedInterface]; |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 184 | // Copy the interface so we know later if changes were made to the |
| 185 | // page |
| 186 | $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 187 | } |
| 188 | }); |
| 189 | } |
| 190 | ]); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 191 | |
| 192 | })(angular); |