blob: d5fdc00cf33ec5f88cba9bf206f415d7796d88b6 [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) {
Gunnar Mills65491142018-06-04 14:23:33 -050061 if (!APIUtils.validIPV4IP(
62 $scope.interface.ipv4.values[i].Address)) {
63 $scope.set_network_error =
64 $scope.interface.ipv4.values[i].Address +
65 ' invalid IP parameter';
66 $scope.loading = false;
67 return;
68 }
69 if (!APIUtils.validIPV4IP(
70 $scope.interface.ipv4.values[i].Gateway)) {
71 $scope.set_network_error =
72 $scope.interface.ipv4.values[i].Address +
73 ' invalid gateway parameter';
74 $scope.loading = false;
75 return;
76 }
77 // The netmask prefix length will be undefined if outside range
78 if (!$scope.interface.ipv4.values[i].PrefixLength) {
79 $scope.set_network_error =
80 $scope.interface.ipv4.values[i].Address +
81 ' invalid Prefix Length parameter';
82 $scope.loading = false;
83 return;
84 }
Gunnar Millsa45c3852018-05-30 16:18:45 -050085 if ($scope.interface.ipv4.values[i].Address !=
86 $scope.old_interface.ipv4.values[i].Address ||
87 $scope.interface.ipv4.values[i].PrefixLength !=
88 $scope.old_interface.ipv4.values[i].PrefixLength ||
89 $scope.interface.ipv4.values[i].Gateway !=
90 $scope.old_interface.ipv4.values[i].Gateway) {
91 promises.push(setIPV4(i));
92 }
93 }
94 }
95
Gunnar Mills0af165b2018-06-01 16:24:56 -050096 if (promises.length) {
97 $q.all(promises).finally(function() {
98 $scope.loading = false;
99 if (!$scope.set_network_error) {
100 $scope.set_network_success = true;
101 // Since an IPV4 interface (e.g. IP address, gateway, or netmask)
102 // edit is a delete then an add and the GUI can't calculate the
103 // interface id (e.g. 5c083707) beforehand and it is not returned
104 // by the REST call, openbmc#3227, reload the page after an edit,
105 // which makes a 2nd REST call.
106 // Do this for all network changes due to the possibility of a set
107 // network failing even though it returned success, openbmc#1641,
108 // and to update dataService and old_interface to know which
109 // data has changed if the user continues to edit network
110 // settings.
111 // TODO: The reload is not ideal. Revisit this.
112 $timeout(function() {
113 $route.reload();
114 }, 4000);
115 }
116 });
117 } else {
Gunnar Mills84981f02018-05-31 15:19:01 -0500118 $scope.loading = false;
Gunnar Mills0af165b2018-06-01 16:24:56 -0500119 }
Gunnar Millsdca79d72018-05-30 13:07:01 -0500120
121 };
122
123 function setMACAddress() {
124 return APIUtils
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500125 .setMACAddress(
126 $scope.selectedInterface, $scope.interface.MACAddress)
127 .then(
Gunnar Millsdca79d72018-05-30 13:07:01 -0500128 function(data) {},
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500129 function(error) {
Gunnar Millsdca79d72018-05-30 13:07:01 -0500130 console.log(JSON.stringify(error));
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500131 $scope.set_network_error = 'MAC Address';
132 });
Gunnar Millsdca79d72018-05-30 13:07:01 -0500133 }
134
135 function setDefaultGateway() {
136 return APIUtils.setDefaultGateway($scope.defaultgateway)
137 .then(
138 function(data) {},
139 function(error) {
140 console.log(JSON.stringify(error));
141 $scope.set_network_error = 'Default Gateway';
142 });
143 }
Gunnar Mills309e06a2018-05-30 13:18:10 -0500144
145 function setHostname() {
146 return APIUtils.setHostname($scope.hostname)
147 .then(
148 function(data) {},
149 function(error) {
150 console.log(JSON.stringify(error));
151 $scope.set_network_error = 'Hostname';
152 });
153 }
154
Gunnar Millscb2c3062018-05-31 13:13:30 -0500155 function setDHCPEnabled() {
156 return APIUtils
157 .setDHCPEnabled(
158 $scope.selectedInterface, $scope.interface.DHCPEnabled)
159 .then(
160 function(data) {},
161 function(error) {
162 console.log(JSON.stringify(error));
163 $scope.set_network_error = 'DHCP';
164 });
165 }
166
Gunnar Millsa45c3852018-05-30 16:18:45 -0500167 function setIPV4(index) {
168 // The correct way to edit an IPV4 interface is to remove it and then
169 // add a new one
170 return APIUtils
171 .deleteIPV4(
172 $scope.selectedInterface, $scope.interface.ipv4.ids[index])
173 .then(
174 function(data) {
175 return APIUtils
176 .addIPV4(
177 $scope.selectedInterface,
178 $scope.interface.ipv4.values[index].Address,
179 $scope.interface.ipv4.values[index].PrefixLength,
180 $scope.interface.ipv4.values[index].Gateway)
181 .then(
182 function(data) {},
183 function(error) {
184 console.log(JSON.stringify(error));
185 $scope.set_network_error =
186 $scope.interface.ipv4.values[index].Address;
187 });
188 },
189 function(error) {
190 console.log(JSON.stringify(error));
191 $scope.set_network_error =
192 $scope.interface.ipv4.values[index].Address;
193 });
194 }
195
Gunnar Mills9a0094d2018-05-02 21:50:56 -0500196 $scope.refresh = function() {
197 $route.reload();
198 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700199 APIUtils.getNetworkInfo().then(function(data) {
Gunnar Mills659651e2018-05-30 15:21:07 -0500200 dataService.setNetworkInfo(data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700201 $scope.network = data.formatted_data;
202 $scope.hostname = data.hostname;
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500203 $scope.defaultgateway = data.defaultgateway;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700204 if ($scope.network.interface_ids.length) {
205 $scope.selectedInterface = $scope.network.interface_ids[0];
206 $scope.interface =
207 $scope.network.interfaces[$scope.selectedInterface];
Gunnar Millsa45c3852018-05-30 16:18:45 -0500208 // Copy the interface so we know later if changes were made to the
209 // page
210 $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700211 }
212 });
213 }
214 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500215
216})(angular);