xref: /openbmc/phosphor-webui/app/configuration/controllers/network-controller.js (revision 84981f0a5835bdbe125e2edb13fd21338889262e)
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', [
13dca79d73SGunnar Mills    '$scope', '$window', 'APIUtils', 'dataService', '$route', '$q',
14dca79d73SGunnar Mills    function($scope, $window, APIUtils, dataService, $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;
26*84981f0aSGunnar 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;
40*84981f0aSGunnar 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
58a45c3852SGunnar Mills        // Set IPV4 IP Addresses, Netmask Prefix Lengths, and Gateways
59a45c3852SGunnar Mills        if (!$scope.interface.DHCPEnabled) {
60a45c3852SGunnar Mills          for (var i in $scope.interface.ipv4.values) {
61a45c3852SGunnar Mills            if ($scope.interface.ipv4.values[i].Address !=
62a45c3852SGunnar Mills                    $scope.old_interface.ipv4.values[i].Address ||
63a45c3852SGunnar Mills                $scope.interface.ipv4.values[i].PrefixLength !=
64a45c3852SGunnar Mills                    $scope.old_interface.ipv4.values[i].PrefixLength ||
65a45c3852SGunnar Mills                $scope.interface.ipv4.values[i].Gateway !=
66a45c3852SGunnar Mills                    $scope.old_interface.ipv4.values[i].Gateway) {
67a45c3852SGunnar Mills              promises.push(setIPV4(i));
68a45c3852SGunnar Mills            }
69a45c3852SGunnar Mills          }
70a45c3852SGunnar Mills        }
71a45c3852SGunnar Mills
72dca79d73SGunnar Mills        $q.all(promises).finally(function() {
73*84981f0aSGunnar Mills          $scope.loading = false;
74*84981f0aSGunnar Mills          // $q.all with an empty array resolves immediately but don't show
75*84981f0aSGunnar Mills          // the success message
76*84981f0aSGunnar Mills          if (!$scope.set_network_error && promises.length) {
77dca79d73SGunnar Mills            $scope.set_network_success = true;
78dca79d73SGunnar Mills          }
79dca79d73SGunnar Mills        });
80dca79d73SGunnar Mills
81dca79d73SGunnar Mills      };
82dca79d73SGunnar Mills
83dca79d73SGunnar Mills      function setMACAddress() {
84dca79d73SGunnar Mills        return APIUtils
857ddc7274SGunnar Mills            .setMACAddress(
867ddc7274SGunnar Mills                $scope.selectedInterface, $scope.interface.MACAddress)
877ddc7274SGunnar Mills            .then(
88dca79d73SGunnar Mills                function(data) {},
897ddc7274SGunnar Mills                function(error) {
90dca79d73SGunnar Mills                  console.log(JSON.stringify(error));
917ddc7274SGunnar Mills                  $scope.set_network_error = 'MAC Address';
927ddc7274SGunnar Mills                });
93dca79d73SGunnar Mills      }
94dca79d73SGunnar Mills
95dca79d73SGunnar Mills      function setDefaultGateway() {
96dca79d73SGunnar Mills        return APIUtils.setDefaultGateway($scope.defaultgateway)
97dca79d73SGunnar Mills            .then(
98dca79d73SGunnar Mills                function(data) {},
99dca79d73SGunnar Mills                function(error) {
100dca79d73SGunnar Mills                  console.log(JSON.stringify(error));
101dca79d73SGunnar Mills                  $scope.set_network_error = 'Default Gateway';
102dca79d73SGunnar Mills                });
103dca79d73SGunnar Mills      }
104309e06abSGunnar Mills
105309e06abSGunnar Mills      function setHostname() {
106309e06abSGunnar Mills        return APIUtils.setHostname($scope.hostname)
107309e06abSGunnar Mills            .then(
108309e06abSGunnar Mills                function(data) {},
109309e06abSGunnar Mills                function(error) {
110309e06abSGunnar Mills                  console.log(JSON.stringify(error));
111309e06abSGunnar Mills                  $scope.set_network_error = 'Hostname';
112309e06abSGunnar Mills                });
113309e06abSGunnar Mills      }
114309e06abSGunnar Mills
115cb2c3060SGunnar Mills      function setDHCPEnabled() {
116cb2c3060SGunnar Mills        return APIUtils
117cb2c3060SGunnar Mills            .setDHCPEnabled(
118cb2c3060SGunnar Mills                $scope.selectedInterface, $scope.interface.DHCPEnabled)
119cb2c3060SGunnar Mills            .then(
120cb2c3060SGunnar Mills                function(data) {},
121cb2c3060SGunnar Mills                function(error) {
122cb2c3060SGunnar Mills                  console.log(JSON.stringify(error));
123cb2c3060SGunnar Mills                  $scope.set_network_error = 'DHCP';
124cb2c3060SGunnar Mills                });
125cb2c3060SGunnar Mills      }
126cb2c3060SGunnar Mills
127a45c3852SGunnar Mills      function setIPV4(index) {
128a45c3852SGunnar Mills        // The correct way to edit an IPV4 interface is to remove it and then
129a45c3852SGunnar Mills        // add a new one
130a45c3852SGunnar Mills        return APIUtils
131a45c3852SGunnar Mills            .deleteIPV4(
132a45c3852SGunnar Mills                $scope.selectedInterface, $scope.interface.ipv4.ids[index])
133a45c3852SGunnar Mills            .then(
134a45c3852SGunnar Mills                function(data) {
135a45c3852SGunnar Mills                  return APIUtils
136a45c3852SGunnar Mills                      .addIPV4(
137a45c3852SGunnar Mills                          $scope.selectedInterface,
138a45c3852SGunnar Mills                          $scope.interface.ipv4.values[index].Address,
139a45c3852SGunnar Mills                          $scope.interface.ipv4.values[index].PrefixLength,
140a45c3852SGunnar Mills                          $scope.interface.ipv4.values[index].Gateway)
141a45c3852SGunnar Mills                      .then(
142a45c3852SGunnar Mills                          function(data) {},
143a45c3852SGunnar Mills                          function(error) {
144a45c3852SGunnar Mills                            console.log(JSON.stringify(error));
145a45c3852SGunnar Mills                            $scope.set_network_error =
146a45c3852SGunnar Mills                                $scope.interface.ipv4.values[index].Address;
147a45c3852SGunnar Mills                          });
148a45c3852SGunnar Mills                },
149a45c3852SGunnar Mills                function(error) {
150a45c3852SGunnar Mills                  console.log(JSON.stringify(error));
151a45c3852SGunnar Mills                  $scope.set_network_error =
152a45c3852SGunnar Mills                      $scope.interface.ipv4.values[index].Address;
153a45c3852SGunnar Mills                });
154a45c3852SGunnar Mills      }
155a45c3852SGunnar Mills
1569a0094dcSGunnar Mills      $scope.refresh = function() {
1579a0094dcSGunnar Mills        $route.reload();
1589a0094dcSGunnar Mills      };
1592a489554SIftekharul Islam      APIUtils.getNetworkInfo().then(function(data) {
160659651e8SGunnar Mills        dataService.setNetworkInfo(data);
1612a489554SIftekharul Islam        $scope.network = data.formatted_data;
1622a489554SIftekharul Islam        $scope.hostname = data.hostname;
163e9f5fe77SGunnar Mills        $scope.defaultgateway = data.defaultgateway;
1642a489554SIftekharul Islam        if ($scope.network.interface_ids.length) {
1652a489554SIftekharul Islam          $scope.selectedInterface = $scope.network.interface_ids[0];
166d27bb135SAndrew Geissler          $scope.interface =
167d27bb135SAndrew Geissler              $scope.network.interfaces[$scope.selectedInterface];
168a45c3852SGunnar Mills          // Copy the interface so we know later if changes were made to the
169a45c3852SGunnar Mills          // page
170a45c3852SGunnar Mills          $scope.old_interface = JSON.parse(JSON.stringify($scope.interface));
1712a489554SIftekharul Islam        }
1722a489554SIftekharul Islam      });
173cd789508SIftekharul Islam    }
174ba5e3f34SAndrew Geissler  ]);
175cd789508SIftekharul Islam
176cd789508SIftekharul Islam})(angular);
177