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 12d27bb135SAndrew Geissler angular.module('app.configuration').controller('networkController', [ 130af165b9SGunnar Mills '$scope', '$window', 'APIUtils', 'dataService', '$timeout', '$route', '$q', 140af165b9SGunnar Mills function($scope, $window, APIUtils, dataService, $timeout, $route, $q) { 15cd789508SIftekharul Islam $scope.dataService = dataService; 162a489554SIftekharul Islam $scope.network = {}; 17a45c3852SGunnar Mills $scope.old_interface = {}; 182a489554SIftekharul Islam $scope.interface = {}; 192a489554SIftekharul Islam $scope.networkDevice = false; 20ba5e3f34SAndrew Geissler $scope.hostname = ''; 21e9f5fe77SGunnar Mills $scope.defaultgateway = ''; 227ddc7274SGunnar Mills $scope.set_network_error = ''; 237ddc7274SGunnar Mills $scope.set_network_success = false; 247ddc7274SGunnar Mills $scope.selectedInterface = ''; 25d01504cfSGunnar Mills $scope.confirm_settings = false; 2684981f0aSGunnar Mills $scope.loading = false; 27*971ac1aaSbeccabroek $scope.IPV4s_to_delete = []; 282a489554SIftekharul Islam 29a3f75320SGunnar Mills loadNetworkInfo(); 30a3f75320SGunnar Mills 312a489554SIftekharul Islam $scope.selectInterface = function(interfaceId) { 322a489554SIftekharul Islam $scope.interface = $scope.network.interfaces[interfaceId]; 33a45c3852SGunnar Mills // Copy the interface so we know later if changes were made to the page 34a45c3852SGunnar Mills $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); 352a489554SIftekharul Islam $scope.selectedInterface = interfaceId; 362a489554SIftekharul Islam $scope.networkDevice = false; 37ba5e3f34SAndrew Geissler }; 38bc3ab72cSGunnar Mills 39bc3ab72cSGunnar Mills $scope.addDNSField = function() { 40bc3ab72cSGunnar Mills $scope.interface.Nameservers.push(''); 41bc3ab72cSGunnar Mills }; 42bc3ab72cSGunnar Mills 43cff61508Sbeccabroek $scope.removeDNSField = function(index) { 44cff61508Sbeccabroek $scope.interface.Nameservers.splice(index, 1); 45cff61508Sbeccabroek }; 46cff61508Sbeccabroek 47*971ac1aaSbeccabroek $scope.removeIpv4Address = function(index) { 48*971ac1aaSbeccabroek // Check if the IPV4 being removed has an id. This indicates that it is 49*971ac1aaSbeccabroek // an existing address and needs to be removed in the back end. 50*971ac1aaSbeccabroek if ($scope.interface.ipv4.values[index].id) { 51*971ac1aaSbeccabroek $scope.IPV4s_to_delete.push($scope.interface.ipv4.values[index]); 52*971ac1aaSbeccabroek } 53*971ac1aaSbeccabroek $scope.interface.ipv4.values.splice(index, 1); 54*971ac1aaSbeccabroek }; 55*971ac1aaSbeccabroek 567ddc7274SGunnar Mills $scope.setNetworkSettings = function() { 57d01504cfSGunnar Mills // Hides the confirm network settings modal 58d01504cfSGunnar Mills $scope.confirm_settings = false; 597ddc7274SGunnar Mills $scope.set_network_error = ''; 607ddc7274SGunnar Mills $scope.set_network_success = false; 6184981f0aSGunnar Mills $scope.loading = true; 62dca79d73SGunnar Mills var promises = []; 63dca79d73SGunnar Mills 64659651e8SGunnar Mills // MAC Address are case-insensitive 65659651e8SGunnar Mills if ($scope.interface.MACAddress.toLowerCase() != 66659651e8SGunnar Mills dataService.mac_address.toLowerCase()) { 67dca79d73SGunnar Mills promises.push(setMACAddress()); 68659651e8SGunnar Mills } 69659651e8SGunnar Mills if ($scope.defaultgateway != dataService.defaultgateway) { 70dca79d73SGunnar Mills promises.push(setDefaultGateway()); 71659651e8SGunnar Mills } 72659651e8SGunnar Mills if ($scope.hostname != dataService.hostname) { 73309e06abSGunnar Mills promises.push(setHostname()); 74659651e8SGunnar Mills } 75cb2c3060SGunnar Mills if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) { 76cb2c3060SGunnar Mills promises.push(setDHCPEnabled()); 77cb2c3060SGunnar Mills } 78309e06abSGunnar Mills 7982658298SGunnar Mills // Remove any empty strings from the array. Important because we add an 8082658298SGunnar Mills // empty string to the end so the user can add a new DNS server, if the 8182658298SGunnar Mills // user doesn't fill out the field, we don't want to add. 8282658298SGunnar Mills $scope.interface.Nameservers = 8382658298SGunnar Mills $scope.interface.Nameservers.filter(Boolean); 840646782dSGunnar Mills // toString() is a cheap way to compare 2 string arrays 850646782dSGunnar Mills if ($scope.interface.Nameservers.toString() != 860646782dSGunnar Mills $scope.old_interface.Nameservers.toString()) { 870646782dSGunnar Mills promises.push(setNameservers()); 880646782dSGunnar Mills } 890646782dSGunnar Mills 90a45c3852SGunnar Mills // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways 91a45c3852SGunnar Mills if (!$scope.interface.DHCPEnabled) { 92*971ac1aaSbeccabroek // Delete existing IPV4 addresses that were removed 93*971ac1aaSbeccabroek promises.push(removeIPV4s()); 94*971ac1aaSbeccabroek // Update any changed IPV4 addresses 95a45c3852SGunnar Mills for (var i in $scope.interface.ipv4.values) { 966549114eSGunnar Mills if (!APIUtils.validIPV4IP( 976549114eSGunnar Mills $scope.interface.ipv4.values[i].Address)) { 986549114eSGunnar Mills $scope.set_network_error = 996549114eSGunnar Mills $scope.interface.ipv4.values[i].Address + 1006549114eSGunnar Mills ' invalid IP parameter'; 1016549114eSGunnar Mills $scope.loading = false; 1026549114eSGunnar Mills return; 1036549114eSGunnar Mills } 1046549114eSGunnar Mills if (!APIUtils.validIPV4IP( 1056549114eSGunnar Mills $scope.interface.ipv4.values[i].Gateway)) { 1066549114eSGunnar Mills $scope.set_network_error = 1076549114eSGunnar Mills $scope.interface.ipv4.values[i].Address + 1086549114eSGunnar Mills ' invalid gateway parameter'; 1096549114eSGunnar Mills $scope.loading = false; 1106549114eSGunnar Mills return; 1116549114eSGunnar Mills } 1126549114eSGunnar Mills // The netmask prefix length will be undefined if outside range 1136549114eSGunnar Mills if (!$scope.interface.ipv4.values[i].PrefixLength) { 1146549114eSGunnar Mills $scope.set_network_error = 1156549114eSGunnar Mills $scope.interface.ipv4.values[i].Address + 1166549114eSGunnar Mills ' invalid Prefix Length parameter'; 1176549114eSGunnar Mills $scope.loading = false; 1186549114eSGunnar Mills return; 1196549114eSGunnar Mills } 120a45c3852SGunnar Mills if ($scope.interface.ipv4.values[i].Address != 121a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].Address || 122a45c3852SGunnar Mills $scope.interface.ipv4.values[i].PrefixLength != 123a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].PrefixLength || 124a45c3852SGunnar Mills $scope.interface.ipv4.values[i].Gateway != 125a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].Gateway) { 126a45c3852SGunnar Mills promises.push(setIPV4(i)); 127a45c3852SGunnar Mills } 128a45c3852SGunnar Mills } 129a45c3852SGunnar Mills } 130a45c3852SGunnar Mills 1310af165b9SGunnar Mills if (promises.length) { 132dca79d73SGunnar Mills $q.all(promises).finally(function() { 13384981f0aSGunnar Mills $scope.loading = false; 1340af165b9SGunnar Mills if (!$scope.set_network_error) { 135dca79d73SGunnar Mills $scope.set_network_success = true; 1360af165b9SGunnar Mills // Since an IPV4 interface (e.g. IP address, gateway, or netmask) 1370af165b9SGunnar Mills // edit is a delete then an add and the GUI can't calculate the 1380af165b9SGunnar Mills // interface id (e.g. 5c083707) beforehand and it is not returned 1390af165b9SGunnar Mills // by the REST call, openbmc#3227, reload the page after an edit, 1400af165b9SGunnar Mills // which makes a 2nd REST call. 1410af165b9SGunnar Mills // Do this for all network changes due to the possibility of a set 1420af165b9SGunnar Mills // network failing even though it returned success, openbmc#1641, 1430af165b9SGunnar Mills // and to update dataService and old_interface to know which 1440af165b9SGunnar Mills // data has changed if the user continues to edit network 1450af165b9SGunnar Mills // settings. 1460af165b9SGunnar Mills // TODO: The reload is not ideal. Revisit this. 1470af165b9SGunnar Mills $timeout(function() { 148a3f75320SGunnar Mills loadNetworkInfo(); 1490af165b9SGunnar Mills }, 4000); 150dca79d73SGunnar Mills } 151dca79d73SGunnar Mills }); 1520af165b9SGunnar Mills } else { 1530af165b9SGunnar Mills $scope.loading = false; 1540af165b9SGunnar Mills } 155dca79d73SGunnar Mills }; 156dca79d73SGunnar Mills 157dca79d73SGunnar Mills function setMACAddress() { 158dca79d73SGunnar Mills return APIUtils 1597ddc7274SGunnar Mills .setMACAddress( 1607ddc7274SGunnar Mills $scope.selectedInterface, $scope.interface.MACAddress) 1617ddc7274SGunnar Mills .then( 162dca79d73SGunnar Mills function(data) {}, 1637ddc7274SGunnar Mills function(error) { 164dca79d73SGunnar Mills console.log(JSON.stringify(error)); 1657ddc7274SGunnar Mills $scope.set_network_error = 'MAC Address'; 1667ddc7274SGunnar Mills }); 167dca79d73SGunnar Mills } 168dca79d73SGunnar Mills 169dca79d73SGunnar Mills function setDefaultGateway() { 170dca79d73SGunnar Mills return APIUtils.setDefaultGateway($scope.defaultgateway) 171dca79d73SGunnar Mills .then( 172dca79d73SGunnar Mills function(data) {}, 173dca79d73SGunnar Mills function(error) { 174dca79d73SGunnar Mills console.log(JSON.stringify(error)); 175dca79d73SGunnar Mills $scope.set_network_error = 'Default Gateway'; 176dca79d73SGunnar Mills }); 177dca79d73SGunnar Mills } 178309e06abSGunnar Mills 179309e06abSGunnar Mills function setHostname() { 180309e06abSGunnar Mills return APIUtils.setHostname($scope.hostname) 181309e06abSGunnar Mills .then( 182309e06abSGunnar Mills function(data) {}, 183309e06abSGunnar Mills function(error) { 184309e06abSGunnar Mills console.log(JSON.stringify(error)); 185309e06abSGunnar Mills $scope.set_network_error = 'Hostname'; 186309e06abSGunnar Mills }); 187309e06abSGunnar Mills } 188309e06abSGunnar Mills 189cb2c3060SGunnar Mills function setDHCPEnabled() { 190cb2c3060SGunnar Mills return APIUtils 191cb2c3060SGunnar Mills .setDHCPEnabled( 192cb2c3060SGunnar Mills $scope.selectedInterface, $scope.interface.DHCPEnabled) 193cb2c3060SGunnar Mills .then( 194cb2c3060SGunnar Mills function(data) {}, 195cb2c3060SGunnar Mills function(error) { 196cb2c3060SGunnar Mills console.log(JSON.stringify(error)); 197cb2c3060SGunnar Mills $scope.set_network_error = 'DHCP'; 198cb2c3060SGunnar Mills }); 199cb2c3060SGunnar Mills } 200cb2c3060SGunnar Mills 2010646782dSGunnar Mills function setNameservers() { 20282658298SGunnar Mills // Nameservers does not allow an empty array, since we remove all empty 2039863d121SGunnar Mills // strings above, could have an empty array. TODO: openbmc/openbmc#3240 20482658298SGunnar Mills if ($scope.interface.Nameservers.length == 0) { 20582658298SGunnar Mills $scope.interface.Nameservers.push(''); 20682658298SGunnar Mills } 2070646782dSGunnar Mills return APIUtils 2080646782dSGunnar Mills .setNameservers( 2090646782dSGunnar Mills $scope.selectedInterface, $scope.interface.Nameservers) 2100646782dSGunnar Mills .then( 2110646782dSGunnar Mills function(data) {}, 2120646782dSGunnar Mills function(error) { 2130646782dSGunnar Mills console.log(JSON.stringify(error)); 2140646782dSGunnar Mills $scope.set_network_error = 'DNS Servers'; 2150646782dSGunnar Mills }); 2160646782dSGunnar Mills } 2170646782dSGunnar Mills 218*971ac1aaSbeccabroek function removeIPV4s() { 219*971ac1aaSbeccabroek return $scope.IPV4s_to_delete.map(function(ipv4) { 220*971ac1aaSbeccabroek return APIUtils.deleteIPV4($scope.selectedInterface, ipv4.id) 221*971ac1aaSbeccabroek .then( 222*971ac1aaSbeccabroek function(data) {}, 223*971ac1aaSbeccabroek function(error) { 224*971ac1aaSbeccabroek console.log(JSON.stringify(error)); 225*971ac1aaSbeccabroek $scope.set_network_error = ipv4.Address; 226*971ac1aaSbeccabroek }) 227*971ac1aaSbeccabroek }); 228*971ac1aaSbeccabroek } 229*971ac1aaSbeccabroek 230a45c3852SGunnar Mills function setIPV4(index) { 231a45c3852SGunnar Mills // The correct way to edit an IPV4 interface is to remove it and then 232a45c3852SGunnar Mills // add a new one 233a45c3852SGunnar Mills return APIUtils 234a45c3852SGunnar Mills .deleteIPV4( 235a45c3852SGunnar Mills $scope.selectedInterface, $scope.interface.ipv4.ids[index]) 236a45c3852SGunnar Mills .then( 237a45c3852SGunnar Mills function(data) { 238a45c3852SGunnar Mills return APIUtils 239a45c3852SGunnar Mills .addIPV4( 240a45c3852SGunnar Mills $scope.selectedInterface, 241a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address, 242a45c3852SGunnar Mills $scope.interface.ipv4.values[index].PrefixLength, 243a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Gateway) 244a45c3852SGunnar Mills .then( 245a45c3852SGunnar Mills function(data) {}, 246a45c3852SGunnar Mills function(error) { 247a45c3852SGunnar Mills console.log(JSON.stringify(error)); 248a45c3852SGunnar Mills $scope.set_network_error = 249a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address; 250a45c3852SGunnar Mills }); 251a45c3852SGunnar Mills }, 252a45c3852SGunnar Mills function(error) { 253a45c3852SGunnar Mills console.log(JSON.stringify(error)); 254a45c3852SGunnar Mills $scope.set_network_error = 255a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address; 256a45c3852SGunnar Mills }); 257a45c3852SGunnar Mills } 258a45c3852SGunnar Mills 2599a0094dcSGunnar Mills $scope.refresh = function() { 260a3f75320SGunnar Mills loadNetworkInfo(); 2619a0094dcSGunnar Mills }; 262a3f75320SGunnar Mills 263a3f75320SGunnar Mills function loadNetworkInfo() { 2642a489554SIftekharul Islam APIUtils.getNetworkInfo().then(function(data) { 265659651e8SGunnar Mills dataService.setNetworkInfo(data); 2662a489554SIftekharul Islam $scope.network = data.formatted_data; 2672a489554SIftekharul Islam $scope.hostname = data.hostname; 268e9f5fe77SGunnar Mills $scope.defaultgateway = data.defaultgateway; 2692a489554SIftekharul Islam if ($scope.network.interface_ids.length) { 270ffdef96dSRebecca Shaw // Use the first network interface if the user hasn't chosen one 271a3f75320SGunnar Mills if (!$scope.selectedInterface || 272a3f75320SGunnar Mills !$scope.network.interfaces[$scope.selectedInterface]) { 2732a489554SIftekharul Islam $scope.selectedInterface = $scope.network.interface_ids[0]; 274a3f75320SGunnar Mills } 275d27bb135SAndrew Geissler $scope.interface = 276d27bb135SAndrew Geissler $scope.network.interfaces[$scope.selectedInterface]; 277a45c3852SGunnar Mills // Copy the interface so we know later if changes were made to the 278a45c3852SGunnar Mills // page 279a45c3852SGunnar Mills $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); 2802a489554SIftekharul Islam } 281*971ac1aaSbeccabroek // Add id values to corresponding IPV4 objects 282*971ac1aaSbeccabroek for (var i = 0; i < $scope.interface.ipv4.values.length; i++) { 283*971ac1aaSbeccabroek $scope.interface.ipv4.values[i].id = $scope.interface.ipv4.ids[i]; 284*971ac1aaSbeccabroek } 2852a489554SIftekharul Islam }); 286cd789508SIftekharul Islam } 287a3f75320SGunnar Mills } 288ba5e3f34SAndrew Geissler ]); 289cd789508SIftekharul Islam})(angular); 290