1cd789508SIftekharul Islam/**
2cd789508SIftekharul Islam * Controller for power-operations
3cd789508SIftekharul Islam *
4cd789508SIftekharul Islam * @module app/serverControl
5cd789508SIftekharul Islam * @exports powerOperationsController
6cd789508SIftekharul Islam * @name powerOperationsController
7cd789508SIftekharul Islam */
8cd789508SIftekharul Islam
9cd789508SIftekharul Islamwindow.angular && (function(angular) {
10cd789508SIftekharul Islam  'use strict';
11cd789508SIftekharul Islam
12d27bb135SAndrew Geissler  angular.module('app.serverControl').controller('powerOperationsController', [
13d27bb135SAndrew Geissler    '$scope', 'APIUtils', 'dataService', 'Constants', '$timeout', '$interval',
1427ce84d2Sbeccabroek    '$interpolate', '$q', 'toastService',
15d27bb135SAndrew Geissler    function(
16d27bb135SAndrew Geissler        $scope, APIUtils, dataService, Constants, $timeout, $interval,
1727ce84d2Sbeccabroek        $interpolate, $q, toastService) {
18cd789508SIftekharul Islam      $scope.dataService = dataService;
19cd789508SIftekharul Islam      $scope.confirm = false;
20*6af5d294SGunnar Mills      $scope.powerConfirm = false;
21*6af5d294SGunnar Mills      $scope.confirmWarmReboot = false;
22*6af5d294SGunnar Mills      $scope.confirmColdReboot = false;
23*6af5d294SGunnar Mills      $scope.confirmOrderlyShutdown = false;
24*6af5d294SGunnar Mills      $scope.confirmImmediateShutdown = false;
256add8325SGunnar Mills      $scope.loading = true;
26a1d238f3SIftekharul Islam
27a1d238f3SIftekharul Islam      var pollChassisStatusTimer = undefined;
28a1d238f3SIftekharul Islam      var pollStartTime = null;
29cd789508SIftekharul Islam
30cd789508SIftekharul Islam      //@TODO: call api and get proper state
315674425bSbeccabroek
326add8325SGunnar Mills      APIUtils.getLastPowerTime()
336add8325SGunnar Mills          .then(
345674425bSbeccabroek              function(data) {
355674425bSbeccabroek                if (data.data == 0) {
36*6af5d294SGunnar Mills                  $scope.powerTime = 'not available';
375674425bSbeccabroek                } else {
38*6af5d294SGunnar Mills                  $scope.powerTime = data.data;
395674425bSbeccabroek                }
405674425bSbeccabroek              },
415674425bSbeccabroek              function(error) {
425674425bSbeccabroek                console.log(JSON.stringify(error));
436add8325SGunnar Mills              })
446add8325SGunnar Mills          .finally(function() {
456add8325SGunnar Mills            $scope.loading = false;
465674425bSbeccabroek          });
476add8325SGunnar Mills
48cd789508SIftekharul Islam      $scope.toggleState = function() {
49d27bb135SAndrew Geissler        dataService.server_state =
50d27bb135SAndrew Geissler            (dataService.server_state == 'Running') ? 'Off' : 'Running';
51ba5e3f34SAndrew Geissler      };
52cd789508SIftekharul Islam
53d80c280bSCamVan Nguyen      $scope.powerOn = function() {
54d80c280bSCamVan Nguyen        $scope.loading = true;
55d80c280bSCamVan Nguyen        dataService.setUnreachableState();
56d27bb135SAndrew Geissler        APIUtils.hostPowerOn()
57d27bb135SAndrew Geissler            .then(function(response) {
58d80c280bSCamVan Nguyen              return response;
59d27bb135SAndrew Geissler            })
60d27bb135SAndrew Geissler            .then(function(lastStatus) {
61c3abaa9bSbeccabroek              return APIUtils.pollHostStatusTillOn();
62d27bb135SAndrew Geissler            })
63d27bb135SAndrew Geissler            .then(function(hostState) {
64d80c280bSCamVan Nguyen              $scope.loading = false;
65d27bb135SAndrew Geissler            })
66d27bb135SAndrew Geissler            .catch(function(error) {
6727ce84d2Sbeccabroek              toastService.error(Constants.MESSAGES.POWER_OP.POWER_ON_FAILED);
68d80c280bSCamVan Nguyen              $scope.loading = false;
69cd789508SIftekharul Islam            });
70ba5e3f34SAndrew Geissler      };
71cd789508SIftekharul Islam      $scope.powerOnConfirm = function() {
72cd789508SIftekharul Islam        if ($scope.confirm) {
73cd789508SIftekharul Islam          return;
74cd789508SIftekharul Islam        }
75cd789508SIftekharul Islam        $scope.confirm = true;
76*6af5d294SGunnar Mills        $scope.powerConfirm = true;
77cd789508SIftekharul Islam      };
78a1d238f3SIftekharul Islam
79a1d238f3SIftekharul Islam      function pollChassisStatusTillOff() {
80a1d238f3SIftekharul Islam        var deferred = $q.defer();
81a1d238f3SIftekharul Islam        pollChassisStatusTimer = $interval(function() {
82a1d238f3SIftekharul Islam          var now = new Date();
83d27bb135SAndrew Geissler          if ((now.getTime() - pollStartTime.getTime()) >=
84d27bb135SAndrew Geissler              Constants.TIMEOUT.CHASSIS_OFF) {
85a1d238f3SIftekharul Islam            $interval.cancel(pollChassisStatusTimer);
86a1d238f3SIftekharul Islam            pollChassisStatusTimer = undefined;
87d27bb135SAndrew Geissler            deferred.reject(
88d27bb135SAndrew Geissler                new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
89a1d238f3SIftekharul Islam          }
90d27bb135SAndrew Geissler          APIUtils.getChassisState()
91d27bb135SAndrew Geissler              .then(function(state) {
92a1d238f3SIftekharul Islam                if (state === Constants.CHASSIS_POWER_STATE.off_code) {
93a1d238f3SIftekharul Islam                  $interval.cancel(pollChassisStatusTimer);
94a1d238f3SIftekharul Islam                  pollChassisStatusTimer = undefined;
95a1d238f3SIftekharul Islam                  deferred.resolve(state);
96a1d238f3SIftekharul Islam                }
97d27bb135SAndrew Geissler              })
98d27bb135SAndrew Geissler              .catch(function(error) {
99a1d238f3SIftekharul Islam                $interval.cancel(pollChassisStatusTimer);
100a1d238f3SIftekharul Islam                pollChassisStatusTimer = undefined;
101a1d238f3SIftekharul Islam                deferred.reject(error);
102a1d238f3SIftekharul Islam              });
103a1d238f3SIftekharul Islam        }, Constants.POLL_INTERVALS.POWER_OP);
104a1d238f3SIftekharul Islam
105a1d238f3SIftekharul Islam        return deferred.promise;
106a1d238f3SIftekharul Islam      }
107cd789508SIftekharul Islam      $scope.warmReboot = function() {
108d80c280bSCamVan Nguyen        $scope.loading = true;
109d80c280bSCamVan Nguyen        dataService.setUnreachableState();
110d27bb135SAndrew Geissler        APIUtils.hostReboot()
111d27bb135SAndrew Geissler            .then(function(response) {
112d80c280bSCamVan Nguyen              return response;
113d27bb135SAndrew Geissler            })
114d27bb135SAndrew Geissler            .then(function(lastStatus) {
115c3abaa9bSbeccabroek              return APIUtils.pollHostStatusTilReboot();
116d27bb135SAndrew Geissler            })
117d27bb135SAndrew Geissler            .then(function(hostState) {
118d80c280bSCamVan Nguyen              $scope.loading = false;
119d27bb135SAndrew Geissler            })
120d27bb135SAndrew Geissler            .catch(function(error) {
12127ce84d2Sbeccabroek              toastService.error(
12227ce84d2Sbeccabroek                  Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED);
123d80c280bSCamVan Nguyen              $scope.loading = false;
124cd789508SIftekharul Islam            });
125cd789508SIftekharul Islam      };
126cd789508SIftekharul Islam      $scope.testState = function() {
127cd789508SIftekharul Islam        $timeout(function() {
128cd789508SIftekharul Islam          dataService.setPowerOffState();
129cd789508SIftekharul Islam          $timeout(function() {
130cd789508SIftekharul Islam            dataService.setPowerOnState();
131cd789508SIftekharul Islam          }, 2000);
132cd789508SIftekharul Islam        }, 1000);
133cd789508SIftekharul Islam      };
134cd789508SIftekharul Islam      $scope.warmRebootConfirm = function() {
135cd789508SIftekharul Islam        if ($scope.confirm) {
136cd789508SIftekharul Islam          return;
137cd789508SIftekharul Islam        }
138cd789508SIftekharul Islam        $scope.confirm = true;
139*6af5d294SGunnar Mills        $scope.confirmWarmReboot = true;
140cd789508SIftekharul Islam      };
141cd789508SIftekharul Islam
142cd789508SIftekharul Islam      $scope.coldReboot = function() {
143a1d238f3SIftekharul Islam        $scope.loading = true;
144d80c280bSCamVan Nguyen        dataService.setUnreachableState();
145d27bb135SAndrew Geissler        APIUtils.chassisPowerOff()
146d27bb135SAndrew Geissler            .then(function(state) {
147a1d238f3SIftekharul Islam              return state;
148d27bb135SAndrew Geissler            })
149d27bb135SAndrew Geissler            .then(function(lastState) {
150a1d238f3SIftekharul Islam              pollStartTime = new Date();
151a1d238f3SIftekharul Islam              return pollChassisStatusTillOff();
152d27bb135SAndrew Geissler            })
153d27bb135SAndrew Geissler            .then(function(chassisState) {
154d80c280bSCamVan Nguyen              return APIUtils.hostPowerOn().then(function(hostState) {
155a1d238f3SIftekharul Islam                return hostState;
156ba5e3f34SAndrew Geissler              });
157d27bb135SAndrew Geissler            })
158d27bb135SAndrew Geissler            .then(function(hostState) {
159c3abaa9bSbeccabroek              return APIUtils.pollHostStatusTillOn();
160d27bb135SAndrew Geissler            })
161d27bb135SAndrew Geissler            .then(function(state) {
162a1d238f3SIftekharul Islam              $scope.loading = false;
163d27bb135SAndrew Geissler            })
164d27bb135SAndrew Geissler            .catch(function(error) {
16527ce84d2Sbeccabroek              toastService.error(
16627ce84d2Sbeccabroek                  Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED);
167a1d238f3SIftekharul Islam              $scope.loading = false;
168a1d238f3SIftekharul Islam            });
169cd789508SIftekharul Islam      };
170cd789508SIftekharul Islam      $scope.coldRebootConfirm = function() {
171cd789508SIftekharul Islam        if ($scope.confirm) {
172cd789508SIftekharul Islam          return;
173cd789508SIftekharul Islam        }
174cd789508SIftekharul Islam        $scope.confirm = true;
175*6af5d294SGunnar Mills        $scope.confirmColdReboot = true;
176cd789508SIftekharul Islam      };
177cd789508SIftekharul Islam
178cd789508SIftekharul Islam      $scope.orderlyShutdown = function() {
179d80c280bSCamVan Nguyen        $scope.loading = true;
180d80c280bSCamVan Nguyen        dataService.setUnreachableState();
181d27bb135SAndrew Geissler        APIUtils.hostPowerOff()
182d27bb135SAndrew Geissler            .then(function(response) {
183d80c280bSCamVan Nguyen              return response;
184d27bb135SAndrew Geissler            })
185d27bb135SAndrew Geissler            .then(function(lastStatus) {
186c3abaa9bSbeccabroek              return APIUtils.pollHostStatusTillOff();
187d27bb135SAndrew Geissler            })
188d27bb135SAndrew Geissler            .then(function(hostState) {
189d80c280bSCamVan Nguyen              pollStartTime = new Date();
190d80c280bSCamVan Nguyen              return pollChassisStatusTillOff();
191d27bb135SAndrew Geissler            })
192d27bb135SAndrew Geissler            .then(function(chassisState) {
193d80c280bSCamVan Nguyen              $scope.loading = false;
194d27bb135SAndrew Geissler            })
195d27bb135SAndrew Geissler            .catch(function(error) {
19627ce84d2Sbeccabroek              toastService.error(
19792d13b62Sbeccabroek                  Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED);
198d80c280bSCamVan Nguyen              $scope.loading = false;
199cd789508SIftekharul Islam            });
200cd789508SIftekharul Islam      };
201cd789508SIftekharul Islam      $scope.orderlyShutdownConfirm = function() {
202cd789508SIftekharul Islam        if ($scope.confirm) {
203cd789508SIftekharul Islam          return;
204cd789508SIftekharul Islam        }
205cd789508SIftekharul Islam        $scope.confirm = true;
206*6af5d294SGunnar Mills        $scope.confirmOrderlyShutdown = true;
207cd789508SIftekharul Islam      };
208cd789508SIftekharul Islam
209cd789508SIftekharul Islam      $scope.immediateShutdown = function() {
210d80c280bSCamVan Nguyen        $scope.loading = true;
211d80c280bSCamVan Nguyen        dataService.setUnreachableState();
212d27bb135SAndrew Geissler        APIUtils.chassisPowerOff()
213d27bb135SAndrew Geissler            .then(function(response) {
214d80c280bSCamVan Nguyen              return response;
215d27bb135SAndrew Geissler            })
216d27bb135SAndrew Geissler            .then(function(lastStatus) {
217d80c280bSCamVan Nguyen              pollStartTime = new Date();
218d80c280bSCamVan Nguyen              return pollChassisStatusTillOff();
219d27bb135SAndrew Geissler            })
220d27bb135SAndrew Geissler            .then(function(chassisState) {
2213aa8b535SGunnar Mills              dataService.setPowerOffState();
222d80c280bSCamVan Nguyen              $scope.loading = false;
223d27bb135SAndrew Geissler            })
224d27bb135SAndrew Geissler            .catch(function(error) {
22527ce84d2Sbeccabroek              toastService.error(
22692d13b62Sbeccabroek                  Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED);
227d80c280bSCamVan Nguyen              $scope.loading = false;
2283aa8b535SGunnar Mills            });
229cd789508SIftekharul Islam      };
230cd789508SIftekharul Islam      $scope.immediateShutdownConfirm = function() {
231cd789508SIftekharul Islam        if ($scope.confirm) {
232cd789508SIftekharul Islam          return;
233cd789508SIftekharul Islam        }
234cd789508SIftekharul Islam        $scope.confirm = true;
235*6af5d294SGunnar Mills        $scope.confirmImmediateShutdown = true;
236cd789508SIftekharul Islam      };
237cd789508SIftekharul Islam    }
238ba5e3f34SAndrew Geissler  ]);
239cd789508SIftekharul Islam})(angular);
240