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; 272a489554SIftekharul Islam 28a3f75320SGunnar Mills loadNetworkInfo(); 29a3f75320SGunnar Mills 302a489554SIftekharul Islam $scope.selectInterface = function(interfaceId) { 312a489554SIftekharul Islam $scope.interface = $scope.network.interfaces[interfaceId]; 32a45c3852SGunnar Mills // Copy the interface so we know later if changes were made to the page 33a45c3852SGunnar Mills $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); 342a489554SIftekharul Islam $scope.selectedInterface = interfaceId; 352a489554SIftekharul Islam $scope.networkDevice = false; 36ba5e3f34SAndrew Geissler }; 37bc3ab72cSGunnar Mills 38bc3ab72cSGunnar Mills $scope.addDNSField = function() { 39bc3ab72cSGunnar Mills $scope.interface.Nameservers.push(''); 40bc3ab72cSGunnar Mills }; 41bc3ab72cSGunnar Mills 42*cff61508Sbeccabroek $scope.removeDNSField = function(index) { 43*cff61508Sbeccabroek $scope.interface.Nameservers.splice(index, 1); 44*cff61508Sbeccabroek }; 45*cff61508Sbeccabroek 467ddc7274SGunnar Mills $scope.setNetworkSettings = function() { 47d01504cfSGunnar Mills // Hides the confirm network settings modal 48d01504cfSGunnar Mills $scope.confirm_settings = false; 497ddc7274SGunnar Mills $scope.set_network_error = ''; 507ddc7274SGunnar Mills $scope.set_network_success = false; 5184981f0aSGunnar Mills $scope.loading = true; 52dca79d73SGunnar Mills var promises = []; 53dca79d73SGunnar Mills 54659651e8SGunnar Mills // MAC Address are case-insensitive 55659651e8SGunnar Mills if ($scope.interface.MACAddress.toLowerCase() != 56659651e8SGunnar Mills dataService.mac_address.toLowerCase()) { 57dca79d73SGunnar Mills promises.push(setMACAddress()); 58659651e8SGunnar Mills } 59659651e8SGunnar Mills if ($scope.defaultgateway != dataService.defaultgateway) { 60dca79d73SGunnar Mills promises.push(setDefaultGateway()); 61659651e8SGunnar Mills } 62659651e8SGunnar Mills if ($scope.hostname != dataService.hostname) { 63309e06abSGunnar Mills promises.push(setHostname()); 64659651e8SGunnar Mills } 65cb2c3060SGunnar Mills if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) { 66cb2c3060SGunnar Mills promises.push(setDHCPEnabled()); 67cb2c3060SGunnar Mills } 68309e06abSGunnar Mills 6982658298SGunnar Mills // Remove any empty strings from the array. Important because we add an 7082658298SGunnar Mills // empty string to the end so the user can add a new DNS server, if the 7182658298SGunnar Mills // user doesn't fill out the field, we don't want to add. 7282658298SGunnar Mills $scope.interface.Nameservers = 7382658298SGunnar Mills $scope.interface.Nameservers.filter(Boolean); 740646782dSGunnar Mills // toString() is a cheap way to compare 2 string arrays 750646782dSGunnar Mills if ($scope.interface.Nameservers.toString() != 760646782dSGunnar Mills $scope.old_interface.Nameservers.toString()) { 770646782dSGunnar Mills promises.push(setNameservers()); 780646782dSGunnar Mills } 790646782dSGunnar Mills 80a45c3852SGunnar Mills // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways 81a45c3852SGunnar Mills if (!$scope.interface.DHCPEnabled) { 82a45c3852SGunnar Mills for (var i in $scope.interface.ipv4.values) { 836549114eSGunnar Mills if (!APIUtils.validIPV4IP( 846549114eSGunnar Mills $scope.interface.ipv4.values[i].Address)) { 856549114eSGunnar Mills $scope.set_network_error = 866549114eSGunnar Mills $scope.interface.ipv4.values[i].Address + 876549114eSGunnar Mills ' invalid IP parameter'; 886549114eSGunnar Mills $scope.loading = false; 896549114eSGunnar Mills return; 906549114eSGunnar Mills } 916549114eSGunnar Mills if (!APIUtils.validIPV4IP( 926549114eSGunnar Mills $scope.interface.ipv4.values[i].Gateway)) { 936549114eSGunnar Mills $scope.set_network_error = 946549114eSGunnar Mills $scope.interface.ipv4.values[i].Address + 956549114eSGunnar Mills ' invalid gateway parameter'; 966549114eSGunnar Mills $scope.loading = false; 976549114eSGunnar Mills return; 986549114eSGunnar Mills } 996549114eSGunnar Mills // The netmask prefix length will be undefined if outside range 1006549114eSGunnar Mills if (!$scope.interface.ipv4.values[i].PrefixLength) { 1016549114eSGunnar Mills $scope.set_network_error = 1026549114eSGunnar Mills $scope.interface.ipv4.values[i].Address + 1036549114eSGunnar Mills ' invalid Prefix Length parameter'; 1046549114eSGunnar Mills $scope.loading = false; 1056549114eSGunnar Mills return; 1066549114eSGunnar Mills } 107a45c3852SGunnar Mills if ($scope.interface.ipv4.values[i].Address != 108a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].Address || 109a45c3852SGunnar Mills $scope.interface.ipv4.values[i].PrefixLength != 110a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].PrefixLength || 111a45c3852SGunnar Mills $scope.interface.ipv4.values[i].Gateway != 112a45c3852SGunnar Mills $scope.old_interface.ipv4.values[i].Gateway) { 113a45c3852SGunnar Mills promises.push(setIPV4(i)); 114a45c3852SGunnar Mills } 115a45c3852SGunnar Mills } 116a45c3852SGunnar Mills } 117a45c3852SGunnar Mills 1180af165b9SGunnar Mills if (promises.length) { 119dca79d73SGunnar Mills $q.all(promises).finally(function() { 12084981f0aSGunnar Mills $scope.loading = false; 1210af165b9SGunnar Mills if (!$scope.set_network_error) { 122dca79d73SGunnar Mills $scope.set_network_success = true; 1230af165b9SGunnar Mills // Since an IPV4 interface (e.g. IP address, gateway, or netmask) 1240af165b9SGunnar Mills // edit is a delete then an add and the GUI can't calculate the 1250af165b9SGunnar Mills // interface id (e.g. 5c083707) beforehand and it is not returned 1260af165b9SGunnar Mills // by the REST call, openbmc#3227, reload the page after an edit, 1270af165b9SGunnar Mills // which makes a 2nd REST call. 1280af165b9SGunnar Mills // Do this for all network changes due to the possibility of a set 1290af165b9SGunnar Mills // network failing even though it returned success, openbmc#1641, 1300af165b9SGunnar Mills // and to update dataService and old_interface to know which 1310af165b9SGunnar Mills // data has changed if the user continues to edit network 1320af165b9SGunnar Mills // settings. 1330af165b9SGunnar Mills // TODO: The reload is not ideal. Revisit this. 1340af165b9SGunnar Mills $timeout(function() { 135a3f75320SGunnar Mills loadNetworkInfo(); 1360af165b9SGunnar Mills }, 4000); 137dca79d73SGunnar Mills } 138dca79d73SGunnar Mills }); 1390af165b9SGunnar Mills } else { 1400af165b9SGunnar Mills $scope.loading = false; 1410af165b9SGunnar Mills } 142dca79d73SGunnar Mills }; 143dca79d73SGunnar Mills 144dca79d73SGunnar Mills function setMACAddress() { 145dca79d73SGunnar Mills return APIUtils 1467ddc7274SGunnar Mills .setMACAddress( 1477ddc7274SGunnar Mills $scope.selectedInterface, $scope.interface.MACAddress) 1487ddc7274SGunnar Mills .then( 149dca79d73SGunnar Mills function(data) {}, 1507ddc7274SGunnar Mills function(error) { 151dca79d73SGunnar Mills console.log(JSON.stringify(error)); 1527ddc7274SGunnar Mills $scope.set_network_error = 'MAC Address'; 1537ddc7274SGunnar Mills }); 154dca79d73SGunnar Mills } 155dca79d73SGunnar Mills 156dca79d73SGunnar Mills function setDefaultGateway() { 157dca79d73SGunnar Mills return APIUtils.setDefaultGateway($scope.defaultgateway) 158dca79d73SGunnar Mills .then( 159dca79d73SGunnar Mills function(data) {}, 160dca79d73SGunnar Mills function(error) { 161dca79d73SGunnar Mills console.log(JSON.stringify(error)); 162dca79d73SGunnar Mills $scope.set_network_error = 'Default Gateway'; 163dca79d73SGunnar Mills }); 164dca79d73SGunnar Mills } 165309e06abSGunnar Mills 166309e06abSGunnar Mills function setHostname() { 167309e06abSGunnar Mills return APIUtils.setHostname($scope.hostname) 168309e06abSGunnar Mills .then( 169309e06abSGunnar Mills function(data) {}, 170309e06abSGunnar Mills function(error) { 171309e06abSGunnar Mills console.log(JSON.stringify(error)); 172309e06abSGunnar Mills $scope.set_network_error = 'Hostname'; 173309e06abSGunnar Mills }); 174309e06abSGunnar Mills } 175309e06abSGunnar Mills 176cb2c3060SGunnar Mills function setDHCPEnabled() { 177cb2c3060SGunnar Mills return APIUtils 178cb2c3060SGunnar Mills .setDHCPEnabled( 179cb2c3060SGunnar Mills $scope.selectedInterface, $scope.interface.DHCPEnabled) 180cb2c3060SGunnar Mills .then( 181cb2c3060SGunnar Mills function(data) {}, 182cb2c3060SGunnar Mills function(error) { 183cb2c3060SGunnar Mills console.log(JSON.stringify(error)); 184cb2c3060SGunnar Mills $scope.set_network_error = 'DHCP'; 185cb2c3060SGunnar Mills }); 186cb2c3060SGunnar Mills } 187cb2c3060SGunnar Mills 1880646782dSGunnar Mills function setNameservers() { 18982658298SGunnar Mills // Nameservers does not allow an empty array, since we remove all empty 1909863d121SGunnar Mills // strings above, could have an empty array. TODO: openbmc/openbmc#3240 19182658298SGunnar Mills if ($scope.interface.Nameservers.length == 0) { 19282658298SGunnar Mills $scope.interface.Nameservers.push(''); 19382658298SGunnar Mills } 1940646782dSGunnar Mills return APIUtils 1950646782dSGunnar Mills .setNameservers( 1960646782dSGunnar Mills $scope.selectedInterface, $scope.interface.Nameservers) 1970646782dSGunnar Mills .then( 1980646782dSGunnar Mills function(data) {}, 1990646782dSGunnar Mills function(error) { 2000646782dSGunnar Mills console.log(JSON.stringify(error)); 2010646782dSGunnar Mills $scope.set_network_error = 'DNS Servers'; 2020646782dSGunnar Mills }); 2030646782dSGunnar Mills } 2040646782dSGunnar Mills 205a45c3852SGunnar Mills function setIPV4(index) { 206a45c3852SGunnar Mills // The correct way to edit an IPV4 interface is to remove it and then 207a45c3852SGunnar Mills // add a new one 208a45c3852SGunnar Mills return APIUtils 209a45c3852SGunnar Mills .deleteIPV4( 210a45c3852SGunnar Mills $scope.selectedInterface, $scope.interface.ipv4.ids[index]) 211a45c3852SGunnar Mills .then( 212a45c3852SGunnar Mills function(data) { 213a45c3852SGunnar Mills return APIUtils 214a45c3852SGunnar Mills .addIPV4( 215a45c3852SGunnar Mills $scope.selectedInterface, 216a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address, 217a45c3852SGunnar Mills $scope.interface.ipv4.values[index].PrefixLength, 218a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Gateway) 219a45c3852SGunnar Mills .then( 220a45c3852SGunnar Mills function(data) {}, 221a45c3852SGunnar Mills function(error) { 222a45c3852SGunnar Mills console.log(JSON.stringify(error)); 223a45c3852SGunnar Mills $scope.set_network_error = 224a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address; 225a45c3852SGunnar Mills }); 226a45c3852SGunnar Mills }, 227a45c3852SGunnar Mills function(error) { 228a45c3852SGunnar Mills console.log(JSON.stringify(error)); 229a45c3852SGunnar Mills $scope.set_network_error = 230a45c3852SGunnar Mills $scope.interface.ipv4.values[index].Address; 231a45c3852SGunnar Mills }); 232a45c3852SGunnar Mills } 233a45c3852SGunnar Mills 2349a0094dcSGunnar Mills $scope.refresh = function() { 235a3f75320SGunnar Mills loadNetworkInfo(); 2369a0094dcSGunnar Mills }; 237a3f75320SGunnar Mills 238a3f75320SGunnar Mills function loadNetworkInfo() { 2392a489554SIftekharul Islam APIUtils.getNetworkInfo().then(function(data) { 240659651e8SGunnar Mills dataService.setNetworkInfo(data); 2412a489554SIftekharul Islam $scope.network = data.formatted_data; 2422a489554SIftekharul Islam $scope.hostname = data.hostname; 243e9f5fe77SGunnar Mills $scope.defaultgateway = data.defaultgateway; 2442a489554SIftekharul Islam if ($scope.network.interface_ids.length) { 245ffdef96dSRebecca Shaw // Use the first network interface if the user hasn't chosen one 246a3f75320SGunnar Mills if (!$scope.selectedInterface || 247a3f75320SGunnar Mills !$scope.network.interfaces[$scope.selectedInterface]) { 2482a489554SIftekharul Islam $scope.selectedInterface = $scope.network.interface_ids[0]; 249a3f75320SGunnar Mills } 250d27bb135SAndrew Geissler $scope.interface = 251d27bb135SAndrew Geissler $scope.network.interfaces[$scope.selectedInterface]; 252a45c3852SGunnar Mills // Copy the interface so we know later if changes were made to the 253a45c3852SGunnar Mills // page 254a45c3852SGunnar Mills $scope.old_interface = JSON.parse(JSON.stringify($scope.interface)); 2552a489554SIftekharul Islam } 2562a489554SIftekharul Islam }); 257cd789508SIftekharul Islam } 258a3f75320SGunnar Mills } 259ba5e3f34SAndrew Geissler ]); 260cd789508SIftekharul Islam})(angular); 261