1cd789508SIftekharul Islam/** 2cd789508SIftekharul Islam * Controller for network 3cd789508SIftekharul Islam * 4cd789508SIftekharul Islam * @module app/configuration 5cd789508SIftekharul Islam * @exports networkController 6cd789508SIftekharul Islam * @name networkController 7cd789508SIftekharul Islam */ 8cd789508SIftekharul Islam 9cd789508SIftekharul Islamwindow.angular && (function(angular) { 10cd789508SIftekharul Islam 'use strict'; 11cd789508SIftekharul Islam 12*bc3ab72cSGunnar Mills angular.module('app.configuration').directive('setFocusDnsField', function() { 13*bc3ab72cSGunnar Mills return function(scope, element, attrs) { 14*bc3ab72cSGunnar Mills var elem = window.document.getElementById(element[0].id); 15*bc3ab72cSGunnar Mills // Focus on the newly created DNS server field 16*bc3ab72cSGunnar Mills // Since this directive is also called when initializing DNS server fields 17*bc3ab72cSGunnar Mills // on a page load, need to determine if the call is from a page load or 18*bc3ab72cSGunnar Mills // from the user pressing the "Add new DNS server" button. The easiest way 19*bc3ab72cSGunnar Mills // to do this is to check if the field is empty, if it is we know 20*bc3ab72cSGunnar Mills // this is a new field since all empty fields are removed from the array. 21*bc3ab72cSGunnar Mills if (!scope[attrs.ngModel] && elem) { 22*bc3ab72cSGunnar Mills elem.focus(); 23*bc3ab72cSGunnar Mills } 24*bc3ab72cSGunnar Mills }; 25*bc3ab72cSGunnar Mills }); 26*bc3ab72cSGunnar Mills 27d27bb135SAndrew Geissler angular.module('app.configuration').controller('networkController', [ 280af165b9SGunnar Mills '$scope', '$window', 'APIUtils', 'dataService', '$timeout', '$route', '$q', 290af165b9SGunnar Mills function($scope, $window, APIUtils, dataService, $timeout, $route, $q) { 30cd789508SIftekharul Islam $scope.dataService = dataService; 312a489554SIftekharul Islam $scope.network = {}; 32a45c3852SGunnar Mills $scope.old_interface = {}; 332a489554SIftekharul Islam $scope.interface = {}; 342a489554SIftekharul Islam $scope.networkDevice = false; 35ba5e3f34SAndrew Geissler $scope.hostname = ''; 36e9f5fe77SGunnar Mills $scope.defaultgateway = ''; 377ddc7274SGunnar Mills $scope.set_network_error = ''; 387ddc7274SGunnar Mills $scope.set_network_success = false; 397ddc7274SGunnar Mills $scope.selectedInterface = ''; 40d01504cfSGunnar Mills $scope.confirm_settings = false; 4184981f0aSGunnar Mills $scope.loading = false; 422a489554SIftekharul Islam 43a3f75320SGunnar Mills loadNetworkInfo(); 44a3f75320SGunnar Mills 452a489554SIftekharul Islam $scope.selectInterface = function(interfaceId) { 462a489554SIftekharul Islam $scope.interface = $scope.network.interfaces[interfaceId]; 47a45c3852SGunnar Mills // Copy the interface so we know later if changes were made to the page 48a45c3852SGunnar Mills $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); 492a489554SIftekharul Islam $scope.selectedInterface = interfaceId; 502a489554SIftekharul Islam $scope.networkDevice = false; 51ba5e3f34SAndrew Geissler }; 52*bc3ab72cSGunnar Mills 53*bc3ab72cSGunnar Mills $scope.addDNSField = function() { 54*bc3ab72cSGunnar Mills $scope.interface.Nameservers.push(''); 55*bc3ab72cSGunnar Mills }; 56*bc3ab72cSGunnar Mills 577ddc7274SGunnar Mills $scope.setNetworkSettings = function() { 58d01504cfSGunnar Mills // Hides the confirm network settings modal 59d01504cfSGunnar Mills $scope.confirm_settings = false; 607ddc7274SGunnar Mills $scope.set_network_error = ''; 617ddc7274SGunnar Mills $scope.set_network_success = false; 6284981f0aSGunnar Mills $scope.loading = true; 63dca79d73SGunnar Mills var promises = []; 64dca79d73SGunnar Mills 65659651e8SGunnar Mills // MAC Address are case-insensitive 66659651e8SGunnar Mills if ($scope.interface.MACAddress.toLowerCase() != 67659651e8SGunnar Mills dataService.mac_address.toLowerCase()) { 68dca79d73SGunnar Mills promises.push(setMACAddress()); 69659651e8SGunnar Mills } 70659651e8SGunnar Mills if ($scope.defaultgateway != dataService.defaultgateway) { 71dca79d73SGunnar Mills promises.push(setDefaultGateway()); 72659651e8SGunnar Mills } 73659651e8SGunnar Mills if ($scope.hostname != dataService.hostname) { 74309e06abSGunnar Mills promises.push(setHostname()); 75659651e8SGunnar Mills } 76cb2c3060SGunnar Mills if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) { 77cb2c3060SGunnar Mills promises.push(setDHCPEnabled()); 78cb2c3060SGunnar Mills } 79309e06abSGunnar Mills 8082658298SGunnar Mills // Remove any empty strings from the array. Important because we add an 8182658298SGunnar Mills // empty string to the end so the user can add a new DNS server, if the 8282658298SGunnar Mills // user doesn't fill out the field, we don't want to add. 8382658298SGunnar Mills $scope.interface.Nameservers = 8482658298SGunnar Mills $scope.interface.Nameservers.filter(Boolean); 850646782dSGunnar Mills // toString() is a cheap way to compare 2 string arrays 860646782dSGunnar Mills if ($scope.interface.Nameservers.toString() != 870646782dSGunnar Mills $scope.old_interface.Nameservers.toString()) { 880646782dSGunnar Mills promises.push(setNameservers()); 890646782dSGunnar Mills } 900646782dSGunnar Mills 91a45c3852SGunnar Mills // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways 92a45c3852SGunnar Mills if (!$scope.interface.DHCPEnabled) { 93a45c3852SGunnar Mills for (var i in $scope.interface.ipv4.values) { 946549114eSGunnar Mills if (!APIUtils.validIPV4IP( 956549114eSGunnar Mills $scope.interface.ipv4.values[i].Address)) { 966549114eSGunnar Mills $scope.set_network_error = 976549114eSGunnar Mills $scope.interface.ipv4.values[i].Address + 986549114eSGunnar Mills ' invalid IP parameter'; 996549114eSGunnar Mills $scope.loading = false; 1006549114eSGunnar Mills return; 1016549114eSGunnar Mills } 1026549114eSGunnar Mills if (!APIUtils.validIPV4IP( 1036549114eSGunnar Mills $scope.interface.ipv4.values[i].Gateway)) { 1046549114eSGunnar Mills $scope.set_network_error = 1056549114eSGunnar Mills $scope.interface.ipv4.values[i].Address + 1066549114eSGunnar Mills ' invalid gateway parameter'; 1076549114eSGunnar Mills $scope.loading = false; 1086549114eSGunnar Mills return; 1096549114eSGunnar Mills } 1106549114eSGunnar Mills // The netmask prefix length will be undefined if outside range 1116549114eSGunnar Mills if (!$scope.interface.ipv4.values[i].PrefixLength) { 1126549114eSGunnar Mills $scope.set_network_error = 1136549114eSGunnar Mills $scope.interface.ipv4.values[i].Address + 1146549114eSGunnar Mills ' invalid Prefix Length parameter'; 1156549114eSGunnar Mills $scope.loading = false; 1166549114eSGunnar Mills return; 1176549114eSGunnar Mills } 118a45c3852SGunnar Mills if ($scope.interface.ipv4.values[i].Address != 119a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].Address || 120a45c3852SGunnar Mills $scope.interface.ipv4.values[i].PrefixLength != 121a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].PrefixLength || 122a45c3852SGunnar Mills $scope.interface.ipv4.values[i].Gateway != 123a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].Gateway) { 124a45c3852SGunnar Mills promises.push(setIPV4(i)); 125a45c3852SGunnar Mills } 126a45c3852SGunnar Mills } 127a45c3852SGunnar Mills } 128a45c3852SGunnar Mills 1290af165b9SGunnar Mills if (promises.length) { 130dca79d73SGunnar Mills $q.all(promises).finally(function() { 13184981f0aSGunnar Mills $scope.loading = false; 1320af165b9SGunnar Mills if (!$scope.set_network_error) { 133dca79d73SGunnar Mills $scope.set_network_success = true; 1340af165b9SGunnar Mills // Since an IPV4 interface (e.g. IP address, gateway, or netmask) 1350af165b9SGunnar Mills // edit is a delete then an add and the GUI can't calculate the 1360af165b9SGunnar Mills // interface id (e.g. 5c083707) beforehand and it is not returned 1370af165b9SGunnar Mills // by the REST call, openbmc#3227, reload the page after an edit, 1380af165b9SGunnar Mills // which makes a 2nd REST call. 1390af165b9SGunnar Mills // Do this for all network changes due to the possibility of a set 1400af165b9SGunnar Mills // network failing even though it returned success, openbmc#1641, 1410af165b9SGunnar Mills // and to update dataService and old_interface to know which 1420af165b9SGunnar Mills // data has changed if the user continues to edit network 1430af165b9SGunnar Mills // settings. 1440af165b9SGunnar Mills // TODO: The reload is not ideal. Revisit this. 1450af165b9SGunnar Mills $timeout(function() { 146a3f75320SGunnar Mills loadNetworkInfo(); 1470af165b9SGunnar Mills }, 4000); 148dca79d73SGunnar Mills } 149dca79d73SGunnar Mills }); 1500af165b9SGunnar Mills } else { 1510af165b9SGunnar Mills $scope.loading = false; 1520af165b9SGunnar Mills } 153dca79d73SGunnar Mills 154dca79d73SGunnar Mills }; 155dca79d73SGunnar Mills 156dca79d73SGunnar Mills function setMACAddress() { 157dca79d73SGunnar Mills return APIUtils 1587ddc7274SGunnar Mills .setMACAddress( 1597ddc7274SGunnar Mills $scope.selectedInterface, $scope.interface.MACAddress) 1607ddc7274SGunnar Mills .then( 161dca79d73SGunnar Mills function(data) {}, 1627ddc7274SGunnar Mills function(error) { 163dca79d73SGunnar Mills console.log(JSON.stringify(error)); 1647ddc7274SGunnar Mills $scope.set_network_error = 'MAC Address'; 1657ddc7274SGunnar Mills }); 166dca79d73SGunnar Mills } 167dca79d73SGunnar Mills 168dca79d73SGunnar Mills function setDefaultGateway() { 169dca79d73SGunnar Mills return APIUtils.setDefaultGateway($scope.defaultgateway) 170dca79d73SGunnar Mills .then( 171dca79d73SGunnar Mills function(data) {}, 172dca79d73SGunnar Mills function(error) { 173dca79d73SGunnar Mills console.log(JSON.stringify(error)); 174dca79d73SGunnar Mills $scope.set_network_error = 'Default Gateway'; 175dca79d73SGunnar Mills }); 176dca79d73SGunnar Mills } 177309e06abSGunnar Mills 178309e06abSGunnar Mills function setHostname() { 179309e06abSGunnar Mills return APIUtils.setHostname($scope.hostname) 180309e06abSGunnar Mills .then( 181309e06abSGunnar Mills function(data) {}, 182309e06abSGunnar Mills function(error) { 183309e06abSGunnar Mills console.log(JSON.stringify(error)); 184309e06abSGunnar Mills $scope.set_network_error = 'Hostname'; 185309e06abSGunnar Mills }); 186309e06abSGunnar Mills } 187309e06abSGunnar Mills 188cb2c3060SGunnar Mills function setDHCPEnabled() { 189cb2c3060SGunnar Mills return APIUtils 190cb2c3060SGunnar Mills .setDHCPEnabled( 191cb2c3060SGunnar Mills $scope.selectedInterface, $scope.interface.DHCPEnabled) 192cb2c3060SGunnar Mills .then( 193cb2c3060SGunnar Mills function(data) {}, 194cb2c3060SGunnar Mills function(error) { 195cb2c3060SGunnar Mills console.log(JSON.stringify(error)); 196cb2c3060SGunnar Mills $scope.set_network_error = 'DHCP'; 197cb2c3060SGunnar Mills }); 198cb2c3060SGunnar Mills } 199cb2c3060SGunnar Mills 2000646782dSGunnar Mills function setNameservers() { 20182658298SGunnar Mills // Nameservers does not allow an empty array, since we remove all empty 2029863d121SGunnar Mills // strings above, could have an empty array. TODO: openbmc/openbmc#3240 20382658298SGunnar Mills if ($scope.interface.Nameservers.length == 0) { 20482658298SGunnar Mills $scope.interface.Nameservers.push(''); 20582658298SGunnar Mills } 2060646782dSGunnar Mills return APIUtils 2070646782dSGunnar Mills .setNameservers( 2080646782dSGunnar Mills $scope.selectedInterface, $scope.interface.Nameservers) 2090646782dSGunnar Mills .then( 2100646782dSGunnar Mills function(data) {}, 2110646782dSGunnar Mills function(error) { 2120646782dSGunnar Mills console.log(JSON.stringify(error)); 2130646782dSGunnar Mills $scope.set_network_error = 'DNS Servers'; 2140646782dSGunnar Mills }); 2150646782dSGunnar Mills } 2160646782dSGunnar Mills 217a45c3852SGunnar Mills function setIPV4(index) { 218a45c3852SGunnar Mills // The correct way to edit an IPV4 interface is to remove it and then 219a45c3852SGunnar Mills // add a new one 220a45c3852SGunnar Mills return APIUtils 221a45c3852SGunnar Mills .deleteIPV4( 222a45c3852SGunnar Mills $scope.selectedInterface, $scope.interface.ipv4.ids[index]) 223a45c3852SGunnar Mills .then( 224a45c3852SGunnar Mills function(data) { 225a45c3852SGunnar Mills return APIUtils 226a45c3852SGunnar Mills .addIPV4( 227a45c3852SGunnar Mills $scope.selectedInterface, 228a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address, 229a45c3852SGunnar Mills $scope.interface.ipv4.values[index].PrefixLength, 230a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Gateway) 231a45c3852SGunnar Mills .then( 232a45c3852SGunnar Mills function(data) {}, 233a45c3852SGunnar Mills function(error) { 234a45c3852SGunnar Mills console.log(JSON.stringify(error)); 235a45c3852SGunnar Mills $scope.set_network_error = 236a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address; 237a45c3852SGunnar Mills }); 238a45c3852SGunnar Mills }, 239a45c3852SGunnar Mills function(error) { 240a45c3852SGunnar Mills console.log(JSON.stringify(error)); 241a45c3852SGunnar Mills $scope.set_network_error = 242a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address; 243a45c3852SGunnar Mills }); 244a45c3852SGunnar Mills } 245a45c3852SGunnar Mills 2469a0094dcSGunnar Mills $scope.refresh = function() { 247a3f75320SGunnar Mills loadNetworkInfo(); 2489a0094dcSGunnar Mills }; 249a3f75320SGunnar Mills 250a3f75320SGunnar Mills function loadNetworkInfo() { 2512a489554SIftekharul Islam APIUtils.getNetworkInfo().then(function(data) { 252659651e8SGunnar Mills dataService.setNetworkInfo(data); 2532a489554SIftekharul Islam $scope.network = data.formatted_data; 2542a489554SIftekharul Islam $scope.hostname = data.hostname; 255e9f5fe77SGunnar Mills $scope.defaultgateway = data.defaultgateway; 2562a489554SIftekharul Islam if ($scope.network.interface_ids.length) { 257ffdef96dSRebecca Shaw // Use the first network interface if the user hasn't chosen one 258a3f75320SGunnar Mills if (!$scope.selectedInterface || 259a3f75320SGunnar Mills !$scope.network.interfaces[$scope.selectedInterface]) { 2602a489554SIftekharul Islam $scope.selectedInterface = $scope.network.interface_ids[0]; 261a3f75320SGunnar Mills } 262d27bb135SAndrew Geissler $scope.interface = 263d27bb135SAndrew Geissler $scope.network.interfaces[$scope.selectedInterface]; 264a45c3852SGunnar Mills // Copy the interface so we know later if changes were made to the 265a45c3852SGunnar Mills // page 266a45c3852SGunnar Mills $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); 2672a489554SIftekharul Islam } 2682a489554SIftekharul Islam }); 269cd789508SIftekharul Islam } 270a3f75320SGunnar Mills } 271ba5e3f34SAndrew Geissler ]); 272cd789508SIftekharul Islam 273cd789508SIftekharul Islam})(angular); 274