1/**
2 * Controller for power-operations
3 *
4 * @module app/serverControl
5 * @exports powerOperationsController
6 * @name powerOperationsController
7 */
8
9window.angular && (function(angular) {
10  'use strict';
11
12  angular.module('app.serverControl').controller('powerOperationsController', [
13    '$scope', 'APIUtils', 'dataService', 'Constants', '$timeout', '$interval',
14    '$interpolate', '$q', 'toastService',
15    function(
16        $scope, APIUtils, dataService, Constants, $timeout, $interval,
17        $interpolate, $q, toastService) {
18      $scope.dataService = dataService;
19      $scope.confirm = false;
20      $scope.power_confirm = false;
21      $scope.warmboot_confirm = false;
22      $scope.coldboot_confirm = false;
23      $scope.orderly_confirm = false;
24      $scope.immediately_confirm = false;
25      $scope.loading = true;
26
27      var pollChassisStatusTimer = undefined;
28      var pollStartTime = null;
29
30      //@TODO: call api and get proper state
31
32      APIUtils.getLastPowerTime()
33          .then(
34              function(data) {
35                if (data.data == 0) {
36                  $scope.power_time = 'not available';
37                } else {
38                  $scope.power_time = data.data;
39                }
40              },
41              function(error) {
42                console.log(JSON.stringify(error));
43              })
44          .finally(function() {
45            $scope.loading = false;
46          });
47
48      $scope.toggleState = function() {
49        dataService.server_state =
50            (dataService.server_state == 'Running') ? 'Off' : 'Running';
51      };
52
53      $scope.powerOn = function() {
54        $scope.loading = true;
55        dataService.setUnreachableState();
56        APIUtils.hostPowerOn()
57            .then(function(response) {
58              return response;
59            })
60            .then(function(lastStatus) {
61              return APIUtils.pollHostStatusTillOn();
62            })
63            .then(function(hostState) {
64              $scope.loading = false;
65            })
66            .catch(function(error) {
67              toastService.error(Constants.MESSAGES.POWER_OP.POWER_ON_FAILED);
68              $scope.loading = false;
69            });
70      };
71      $scope.powerOnConfirm = function() {
72        if ($scope.confirm) {
73          return;
74        }
75        $scope.confirm = true;
76        $scope.power_confirm = true;
77      };
78
79      function pollChassisStatusTillOff() {
80        var deferred = $q.defer();
81        pollChassisStatusTimer = $interval(function() {
82          var now = new Date();
83          if ((now.getTime() - pollStartTime.getTime()) >=
84              Constants.TIMEOUT.CHASSIS_OFF) {
85            $interval.cancel(pollChassisStatusTimer);
86            pollChassisStatusTimer = undefined;
87            deferred.reject(
88                new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
89          }
90          APIUtils.getChassisState()
91              .then(function(state) {
92                if (state === Constants.CHASSIS_POWER_STATE.off_code) {
93                  $interval.cancel(pollChassisStatusTimer);
94                  pollChassisStatusTimer = undefined;
95                  deferred.resolve(state);
96                }
97              })
98              .catch(function(error) {
99                $interval.cancel(pollChassisStatusTimer);
100                pollChassisStatusTimer = undefined;
101                deferred.reject(error);
102              });
103        }, Constants.POLL_INTERVALS.POWER_OP);
104
105        return deferred.promise;
106      }
107      $scope.warmReboot = function() {
108        $scope.loading = true;
109        dataService.setUnreachableState();
110        APIUtils.hostReboot()
111            .then(function(response) {
112              return response;
113            })
114            .then(function(lastStatus) {
115              return APIUtils.pollHostStatusTilReboot();
116            })
117            .then(function(hostState) {
118              $scope.loading = false;
119            })
120            .catch(function(error) {
121              toastService.error(
122                  Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED);
123              $scope.loading = false;
124            });
125      };
126      $scope.testState = function() {
127        $timeout(function() {
128          dataService.setPowerOffState();
129          $timeout(function() {
130            dataService.setPowerOnState();
131          }, 2000);
132        }, 1000);
133      };
134      $scope.warmRebootConfirm = function() {
135        if ($scope.confirm) {
136          return;
137        }
138        $scope.confirm = true;
139        $scope.warmboot_confirm = true;
140      };
141
142      $scope.coldReboot = function() {
143        $scope.loading = true;
144        dataService.setUnreachableState();
145        APIUtils.chassisPowerOff()
146            .then(function(state) {
147              return state;
148            })
149            .then(function(lastState) {
150              pollStartTime = new Date();
151              return pollChassisStatusTillOff();
152            })
153            .then(function(chassisState) {
154              return APIUtils.hostPowerOn().then(function(hostState) {
155                return hostState;
156              });
157            })
158            .then(function(hostState) {
159              return APIUtils.pollHostStatusTillOn();
160            })
161            .then(function(state) {
162              $scope.loading = false;
163            })
164            .catch(function(error) {
165              toastService.error(
166                  Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED);
167              $scope.loading = false;
168            });
169      };
170      $scope.coldRebootConfirm = function() {
171        if ($scope.confirm) {
172          return;
173        }
174        $scope.confirm = true;
175        $scope.coldboot_confirm = true;
176      };
177
178      $scope.orderlyShutdown = function() {
179        $scope.loading = true;
180        dataService.setUnreachableState();
181        APIUtils.hostPowerOff()
182            .then(function(response) {
183              return response;
184            })
185            .then(function(lastStatus) {
186              return APIUtils.pollHostStatusTillOff();
187            })
188            .then(function(hostState) {
189              pollStartTime = new Date();
190              return pollChassisStatusTillOff();
191            })
192            .then(function(chassisState) {
193              $scope.loading = false;
194            })
195            .catch(function(error) {
196              toastService.error(
197                  Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED);
198              $scope.loading = false;
199            });
200      };
201      $scope.orderlyShutdownConfirm = function() {
202        if ($scope.confirm) {
203          return;
204        }
205        $scope.confirm = true;
206        $scope.orderly_confirm = true;
207      };
208
209      $scope.immediateShutdown = function() {
210        $scope.loading = true;
211        dataService.setUnreachableState();
212        APIUtils.chassisPowerOff()
213            .then(function(response) {
214              return response;
215            })
216            .then(function(lastStatus) {
217              pollStartTime = new Date();
218              return pollChassisStatusTillOff();
219            })
220            .then(function(chassisState) {
221              dataService.setPowerOffState();
222              $scope.loading = false;
223            })
224            .catch(function(error) {
225              toastService.error(
226                  Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED);
227              $scope.loading = false;
228            });
229      };
230      $scope.immediateShutdownConfirm = function() {
231        if ($scope.confirm) {
232          return;
233        }
234        $scope.confirm = true;
235        $scope.immediately_confirm = true;
236      };
237    }
238  ]);
239})(angular);
240