xref: /openbmc/phosphor-webui/app/configuration/controllers/network-controller.js (revision 0646782dc72ecd1ba146858c686e090d70dab0c1)
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
282a489554SIftekharul Islam      $scope.selectInterface = function(interfaceId) {
292a489554SIftekharul Islam        $scope.interface = $scope.network.interfaces[interfaceId];
30a45c3852SGunnar Mills        // Copy the interface so we know later if changes were made to the page
31a45c3852SGunnar Mills        $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
322a489554SIftekharul Islam        $scope.selectedInterface = interfaceId;
332a489554SIftekharul Islam        $scope.networkDevice = false;
34ba5e3f34SAndrew Geissler      };
357ddc7274SGunnar Mills      $scope.setNetworkSettings = function() {
36d01504cfSGunnar Mills        // Hides the confirm network settings modal
37d01504cfSGunnar Mills        $scope.confirm_settings = false;
387ddc7274SGunnar Mills        $scope.set_network_error = '';
397ddc7274SGunnar Mills        $scope.set_network_success = false;
4084981f0aSGunnar Mills        $scope.loading = true;
41dca79d73SGunnar Mills        var promises = [];
42dca79d73SGunnar Mills
43659651e8SGunnar Mills        // MAC Address are case-insensitive
44659651e8SGunnar Mills        if ($scope.interface.MACAddress.toLowerCase() !=
45659651e8SGunnar Mills            dataService.mac_address.toLowerCase()) {
46dca79d73SGunnar Mills          promises.push(setMACAddress());
47659651e8SGunnar Mills        }
48659651e8SGunnar Mills        if ($scope.defaultgateway != dataService.defaultgateway) {
49dca79d73SGunnar Mills          promises.push(setDefaultGateway());
50659651e8SGunnar Mills        }
51659651e8SGunnar Mills        if ($scope.hostname != dataService.hostname) {
52309e06abSGunnar Mills          promises.push(setHostname());
53659651e8SGunnar Mills        }
54cb2c3060SGunnar Mills        if ($scope.interface.DHCPEnabled != $scope.old_interface.DHCPEnabled) {
55cb2c3060SGunnar Mills          promises.push(setDHCPEnabled());
56cb2c3060SGunnar Mills        }
57309e06abSGunnar Mills
58*0646782dSGunnar Mills        // toString() is a cheap way to compare 2 string arrays
59*0646782dSGunnar Mills        if ($scope.interface.Nameservers.toString() !=
60*0646782dSGunnar Mills            $scope.old_interface.Nameservers.toString()) {
61*0646782dSGunnar Mills          promises.push(setNameservers());
62*0646782dSGunnar Mills        }
63*0646782dSGunnar Mills
64a45c3852SGunnar Mills        // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways
65a45c3852SGunnar Mills        if (!$scope.interface.DHCPEnabled) {
66a45c3852SGunnar Mills          for (var i in $scope.interface.ipv4.values) {
676549114eSGunnar Mills            if (!APIUtils.validIPV4IP(
686549114eSGunnar Mills                    $scope.interface.ipv4.values[i].Address)) {
696549114eSGunnar Mills              $scope.set_network_error =
706549114eSGunnar Mills                  $scope.interface.ipv4.values[i].Address +
716549114eSGunnar Mills                  ' invalid IP parameter';
726549114eSGunnar Mills              $scope.loading = false;
736549114eSGunnar Mills              return;
746549114eSGunnar Mills            }
756549114eSGunnar Mills            if (!APIUtils.validIPV4IP(
766549114eSGunnar Mills                    $scope.interface.ipv4.values[i].Gateway)) {
776549114eSGunnar Mills              $scope.set_network_error =
786549114eSGunnar Mills                  $scope.interface.ipv4.values[i].Address +
796549114eSGunnar Mills                  ' invalid gateway parameter';
806549114eSGunnar Mills              $scope.loading = false;
816549114eSGunnar Mills              return;
826549114eSGunnar Mills            }
836549114eSGunnar Mills            // The netmask prefix length will be undefined if outside range
846549114eSGunnar Mills            if (!$scope.interface.ipv4.values[i].PrefixLength) {
856549114eSGunnar Mills              $scope.set_network_error =
866549114eSGunnar Mills                  $scope.interface.ipv4.values[i].Address +
876549114eSGunnar Mills                  ' invalid Prefix Length parameter';
886549114eSGunnar Mills              $scope.loading = false;
896549114eSGunnar Mills              return;
906549114eSGunnar Mills            }
91a45c3852SGunnar Mills            if ($scope.interface.ipv4.values[i].Address !=
92a45c3852SGunnar Mills                    $scope.old_interface.ipv4.values[i].Address ||
93a45c3852SGunnar Mills                $scope.interface.ipv4.values[i].PrefixLength !=
94a45c3852SGunnar Mills                    $scope.old_interface.ipv4.values[i].PrefixLength ||
95a45c3852SGunnar Mills                $scope.interface.ipv4.values[i].Gateway !=
96a45c3852SGunnar Mills                    $scope.old_interface.ipv4.values[i].Gateway) {
97a45c3852SGunnar Mills              promises.push(setIPV4(i));
98a45c3852SGunnar Mills            }
99a45c3852SGunnar Mills          }
100a45c3852SGunnar Mills        }
101a45c3852SGunnar Mills
1020af165b9SGunnar Mills        if (promises.length) {
103dca79d73SGunnar Mills          $q.all(promises).finally(function() {
10484981f0aSGunnar Mills            $scope.loading = false;
1050af165b9SGunnar Mills            if (!$scope.set_network_error) {
106dca79d73SGunnar Mills              $scope.set_network_success = true;
1070af165b9SGunnar Mills              // Since an IPV4 interface (e.g. IP address, gateway, or netmask)
1080af165b9SGunnar Mills              // edit is a delete then an add and the GUI can't calculate the
1090af165b9SGunnar Mills              // interface id (e.g. 5c083707) beforehand and it is not returned
1100af165b9SGunnar Mills              // by the REST call, openbmc#3227, reload the page after an edit,
1110af165b9SGunnar Mills              // which makes a 2nd REST call.
1120af165b9SGunnar Mills              // Do this for all network changes due to the possibility of a set
1130af165b9SGunnar Mills              // network failing even though it returned success, openbmc#1641,
1140af165b9SGunnar Mills              // and to update dataService and old_interface to know which
1150af165b9SGunnar Mills              // data has changed if the user continues to edit network
1160af165b9SGunnar Mills              // settings.
1170af165b9SGunnar Mills              // TODO: The reload is not ideal. Revisit this.
1180af165b9SGunnar Mills              $timeout(function() {
1190af165b9SGunnar Mills                $route.reload();
1200af165b9SGunnar Mills              }, 4000);
121dca79d73SGunnar Mills            }
122dca79d73SGunnar Mills          });
1230af165b9SGunnar Mills        } else {
1240af165b9SGunnar Mills          $scope.loading = false;
1250af165b9SGunnar Mills        }
126dca79d73SGunnar Mills
127dca79d73SGunnar Mills      };
128dca79d73SGunnar Mills
129dca79d73SGunnar Mills      function setMACAddress() {
130dca79d73SGunnar Mills        return APIUtils
1317ddc7274SGunnar Mills            .setMACAddress(
1327ddc7274SGunnar Mills                $scope.selectedInterface, $scope.interface.MACAddress)
1337ddc7274SGunnar Mills            .then(
134dca79d73SGunnar Mills                function(data) {},
1357ddc7274SGunnar Mills                function(error) {
136dca79d73SGunnar Mills                  console.log(JSON.stringify(error));
1377ddc7274SGunnar Mills                  $scope.set_network_error = 'MAC Address';
1387ddc7274SGunnar Mills                });
139dca79d73SGunnar Mills      }
140dca79d73SGunnar Mills
141dca79d73SGunnar Mills      function setDefaultGateway() {
142dca79d73SGunnar Mills        return APIUtils.setDefaultGateway($scope.defaultgateway)
143dca79d73SGunnar Mills            .then(
144dca79d73SGunnar Mills                function(data) {},
145dca79d73SGunnar Mills                function(error) {
146dca79d73SGunnar Mills                  console.log(JSON.stringify(error));
147dca79d73SGunnar Mills                  $scope.set_network_error = 'Default Gateway';
148dca79d73SGunnar Mills                });
149dca79d73SGunnar Mills      }
150309e06abSGunnar Mills
151309e06abSGunnar Mills      function setHostname() {
152309e06abSGunnar Mills        return APIUtils.setHostname($scope.hostname)
153309e06abSGunnar Mills            .then(
154309e06abSGunnar Mills                function(data) {},
155309e06abSGunnar Mills                function(error) {
156309e06abSGunnar Mills                  console.log(JSON.stringify(error));
157309e06abSGunnar Mills                  $scope.set_network_error = 'Hostname';
158309e06abSGunnar Mills                });
159309e06abSGunnar Mills      }
160309e06abSGunnar Mills
161cb2c3060SGunnar Mills      function setDHCPEnabled() {
162cb2c3060SGunnar Mills        return APIUtils
163cb2c3060SGunnar Mills            .setDHCPEnabled(
164cb2c3060SGunnar Mills                $scope.selectedInterface, $scope.interface.DHCPEnabled)
165cb2c3060SGunnar Mills            .then(
166cb2c3060SGunnar Mills                function(data) {},
167cb2c3060SGunnar Mills                function(error) {
168cb2c3060SGunnar Mills                  console.log(JSON.stringify(error));
169cb2c3060SGunnar Mills                  $scope.set_network_error = 'DHCP';
170cb2c3060SGunnar Mills                });
171cb2c3060SGunnar Mills      }
172cb2c3060SGunnar Mills
173*0646782dSGunnar Mills      function setNameservers() {
174*0646782dSGunnar Mills        return APIUtils
175*0646782dSGunnar Mills            .setNameservers(
176*0646782dSGunnar Mills                $scope.selectedInterface, $scope.interface.Nameservers)
177*0646782dSGunnar Mills            .then(
178*0646782dSGunnar Mills                function(data) {},
179*0646782dSGunnar Mills                function(error) {
180*0646782dSGunnar Mills                  console.log(JSON.stringify(error));
181*0646782dSGunnar Mills                  $scope.set_network_error = 'DNS Servers';
182*0646782dSGunnar Mills                });
183*0646782dSGunnar Mills      }
184*0646782dSGunnar Mills
185a45c3852SGunnar Mills      function setIPV4(index) {
186a45c3852SGunnar Mills        // The correct way to edit an IPV4 interface is to remove it and then
187a45c3852SGunnar Mills        // add a new one
188a45c3852SGunnar Mills        return APIUtils
189a45c3852SGunnar Mills            .deleteIPV4(
190a45c3852SGunnar Mills                $scope.selectedInterface, $scope.interface.ipv4.ids[index])
191a45c3852SGunnar Mills            .then(
192a45c3852SGunnar Mills                function(data) {
193a45c3852SGunnar Mills                  return APIUtils
194a45c3852SGunnar Mills                      .addIPV4(
195a45c3852SGunnar Mills                          $scope.selectedInterface,
196a45c3852SGunnar Mills                          $scope.interface.ipv4.values[index].Address,
197a45c3852SGunnar Mills                          $scope.interface.ipv4.values[index].PrefixLength,
198a45c3852SGunnar Mills                          $scope.interface.ipv4.values[index].Gateway)
199a45c3852SGunnar Mills                      .then(
200a45c3852SGunnar Mills                          function(data) {},
201a45c3852SGunnar Mills                          function(error) {
202a45c3852SGunnar Mills                            console.log(JSON.stringify(error));
203a45c3852SGunnar Mills                            $scope.set_network_error =
204a45c3852SGunnar Mills                                $scope.interface.ipv4.values[index].Address;
205a45c3852SGunnar Mills                          });
206a45c3852SGunnar Mills                },
207a45c3852SGunnar Mills                function(error) {
208a45c3852SGunnar Mills                  console.log(JSON.stringify(error));
209a45c3852SGunnar Mills                  $scope.set_network_error =
210a45c3852SGunnar Mills                      $scope.interface.ipv4.values[index].Address;
211a45c3852SGunnar Mills                });
212a45c3852SGunnar Mills      }
213a45c3852SGunnar Mills
2149a0094dcSGunnar Mills      $scope.refresh = function() {
2159a0094dcSGunnar Mills        $route.reload();
2169a0094dcSGunnar Mills      };
2172a489554SIftekharul Islam      APIUtils.getNetworkInfo().then(function(data) {
218659651e8SGunnar Mills        dataService.setNetworkInfo(data);
2192a489554SIftekharul Islam        $scope.network = data.formatted_data;
2202a489554SIftekharul Islam        $scope.hostname = data.hostname;
221e9f5fe77SGunnar Mills        $scope.defaultgateway = data.defaultgateway;
2222a489554SIftekharul Islam        if ($scope.network.interface_ids.length) {
2232a489554SIftekharul Islam          $scope.selectedInterface = $scope.network.interface_ids[0];
224d27bb135SAndrew Geissler          $scope.interface =
225d27bb135SAndrew Geissler              $scope.network.interfaces[$scope.selectedInterface];
226a45c3852SGunnar Mills          // Copy the interface so we know later if changes were made to the
227a45c3852SGunnar Mills          // page
228a45c3852SGunnar Mills          $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
2292a489554SIftekharul Islam        }
2302a489554SIftekharul Islam      });
231cd789508SIftekharul Islam    }
232ba5e3f34SAndrew Geissler  ]);
233cd789508SIftekharul Islam
234cd789508SIftekharul Islam})(angular);
235