blob: 6ebbf4f8173cbe126754321bdd9d2c403268d166 [file] [log] [blame]
Iftekharul Islamcd789502017-04-19 14:37:55 -05001/**
2 * Controller for network
3 *
4 * @module app/configuration
5 * @exports networkController
6 * @name networkController
Iftekharul Islamcd789502017-04-19 14:37:55 -05007 */
8
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07009window.angular && (function(angular) {
10 'use strict';
Iftekharul Islamcd789502017-04-19 14:37:55 -050011
Andrew Geisslerd27bb132018-05-24 11:07:27 -070012 angular.module('app.configuration').controller('networkController', [
Gunnar Mills0af165b2018-06-01 16:24:56 -050013 '$scope', '$window', 'APIUtils', 'dataService', '$timeout', '$route', '$q',
14 function($scope, $window, APIUtils, dataService, $timeout, $route, $q) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -070015 $scope.dataService = dataService;
16 $scope.network = {};
Gunnar Millsa45c3852018-05-30 16:18:45 -050017 $scope.old_interface = {};
Andrew Geisslerd27bb132018-05-24 11:07:27 -070018 $scope.interface = {};
19 $scope.networkDevice = false;
20 $scope.hostname = '';
Gunnar Millse9f5fe72018-05-04 13:43:10 -050021 $scope.defaultgateway = '';
Gunnar Mills7ddc7272018-04-12 16:12:03 -050022 $scope.set_network_error = '';
23 $scope.set_network_success = false;
24 $scope.selectedInterface = '';
Gunnar Millsd01504c2018-05-03 13:01:51 -050025 $scope.confirm_settings = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050026 $scope.loading = false;
Iftekharul Islam2a489552017-11-02 13:23:08 -050027
Andrew Geisslerd27bb132018-05-24 11:07:27 -070028 $scope.selectInterface = function(interfaceId) {
29 $scope.interface = $scope.network.interfaces[interfaceId];
Gunnar Millsa45c3852018-05-30 16:18:45 -050030 // 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 Geisslerd27bb132018-05-24 11:07:27 -070032 $scope.selectedInterface = interfaceId;
33 $scope.networkDevice = false;
34 };
Gunnar Mills7ddc7272018-04-12 16:12:03 -050035 $scope.setNetworkSettings = function() {
Gunnar Millsd01504c2018-05-03 13:01:51 -050036 // Hides the confirm network settings modal
37 $scope.confirm_settings = false;
Gunnar Mills7ddc7272018-04-12 16:12:03 -050038 $scope.set_network_error = '';
39 $scope.set_network_success = false;
Gunnar Mills84981f02018-05-31 15:19:01 -050040 $scope.loading = true;
Gunnar Millsdca79d72018-05-30 13:07:01 -050041 var promises = [];
42
Gunnar Mills659651e2018-05-30 15:21:07 -050043 // 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 Millscb2c3062018-05-31 13:13:30 -050054 if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) {
55 promises.push(setDHCPEnabled());
56 }
Gunnar Mills309e06a2018-05-30 13:18:10 -050057
Gunnar Millsa45c3852018-05-30 16:18:45 -050058 // 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 Mills0af165b2018-06-01 16:24:56 -050072 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 Mills84981f02018-05-31 15:19:01 -050094 $scope.loading = false;
Gunnar Mills0af165b2018-06-01 16:24:56 -050095 }
Gunnar Millsdca79d72018-05-30 13:07:01 -050096
97 };
98
99 function setMACAddress() {
100 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500101 .setMACAddress(
102 $scope.selectedInterface, $scope.interface.MACAddress)
103 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -0500104 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500105 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -0500106 console.log(JSON.stringify(error));
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500107 $scope.set_network_error = 'MAC Address';
108 });
Gunnar Millsdca79d72018-05-30 13:07:01 -0500109 }
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 Mills309e06a2018-05-30 13:18:10 -0500120
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 Millscb2c3062018-05-31 13:13:30 -0500131 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 Millsa45c3852018-05-30 16:18:45 -0500143 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 Mills9a0094d2018-05-02 21:50:56 -0500172 $scope.refresh = function() {
173 $route.reload();
174 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700175 APIUtils.getNetworkInfo().then(function(data) {
Gunnar Mills659651e2018-05-30 15:21:07 -0500176 dataService.setNetworkInfo(data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700177 $scope.network = data.formatted_data;
178 $scope.hostname = data.hostname;
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500179 $scope.defaultgateway = data.defaultgateway;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700180 if ($scope.network.interface_ids.length) {
181 $scope.selectedInterface = $scope.network.interface_ids[0];
182 $scope.interface =
183 $scope.network.interfaces[$scope.selectedInterface];
Gunnar Millsa45c3852018-05-30 16:18:45 -0500184 // 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 Geisslerd27bb132018-05-24 11:07:27 -0700187 }
188 });
189 }
190 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500191
192})(angular);