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
13        .module('app.serverControl')
14        .controller('powerOperationsController', [
15            '$scope',
16            'APIUtils',
17            'dataService',
18            'Constants',
19            '$timeout',
20            '$interval',
21            '$interpolate',
22            '$q',
23            function($scope, APIUtils, dataService, Constants, $timeout,
24                     $interval, $interpolate, $q){
25                $scope.dataService = dataService;
26                $scope.confirm = false;
27                $scope.power_confirm = false;
28                $scope.warmboot_confirm = false;
29                $scope.coldboot_confirm = false;
30                $scope.orderly_confirm = false;
31                $scope.immediately_confirm = false;
32                $scope.loading = false;
33
34                var pollChassisStatusTimer = undefined;
35                var pollHostStatusTimer = undefined;
36                var pollStartTime = null;
37
38                //@TODO: call api and get proper state
39                $scope.toggleState = function(){
40                    dataService.server_state = (dataService.server_state == 'Running') ? 'Off': 'Running';
41                }
42
43                $scope.powerOn = function(){
44                    $scope.loading = true;
45                    dataService.setUnreachableState();
46                    APIUtils.hostPowerOn().then(function(response){
47                        return response;
48                    }).then(function(lastStatus) {
49                        pollStartTime = new Date();
50                        return pollHostStatusTillOn();
51                    }).then(function(hostState) {
52                        $scope.loading = false;
53                    }).catch(function(error){
54                        dataService.activateErrorModal(
55                          {title: Constants.MESSAGES.POWER_OP.POWER_ON_FAILED,
56                           description: error.statusText ?
57                               $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
58                                   {status: error.status, description: error.statusText}) :
59                               error });
60                        $scope.loading = false;
61                    });
62                }
63                $scope.powerOnConfirm = function(){
64                    if($scope.confirm) {
65                        return;
66                    }
67                    $scope.confirm = true;
68                    $scope.power_confirm = true;
69                };
70
71                function setHostState(state){
72                    if(state == Constants.HOST_STATE_TEXT.off_code){
73                        dataService.setPowerOffState();
74                    }else if(state == Constants.HOST_STATE_TEXT.on_code){
75                        dataService.setPowerOnState();
76                    }else{
77                        dataService.setErrorState();
78                    }
79                }
80
81                function pollChassisStatusTillOff(){
82                    var deferred = $q.defer();
83                    pollChassisStatusTimer = $interval(function(){
84                        var now = new Date();
85                        if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.CHASSIS_OFF){
86                            $interval.cancel(pollChassisStatusTimer);
87                            pollChassisStatusTimer = undefined;
88                            deferred.reject(new Error(Constants.MESSAGES.POLL.CHASSIS_OFF_TIMEOUT));
89                        }
90                        APIUtils.getChassisState().then(function(state){
91                            if(state === Constants.CHASSIS_POWER_STATE.off_code){
92                                $interval.cancel(pollChassisStatusTimer);
93                                pollChassisStatusTimer = undefined;
94                                deferred.resolve(state);
95                            }
96                        }).catch(function(error){
97                            $interval.cancel(pollChassisStatusTimer);
98                            pollChassisStatusTimer = undefined;
99                            deferred.reject(error);
100                        });
101                    }, Constants.POLL_INTERVALS.POWER_OP);
102
103                    return deferred.promise;
104                }
105                function pollHostStatusTillOn(){
106                    var deferred = $q.defer();
107                    pollHostStatusTimer = $interval(function(){
108                        var now = new Date();
109                        if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_ON){
110                            $interval.cancel(pollHostStatusTimer);
111                            pollHostStatusTimer = undefined;
112                            deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT));
113                        }
114                        APIUtils.getHostState().then(function(state){
115                            setHostState(state);
116                            if(state === Constants.HOST_STATE_TEXT.on_code){
117                                $interval.cancel(pollHostStatusTimer);
118                                pollHostStatusTimer = undefined;
119                                deferred.resolve(state);
120                            }else if(state === Constants.HOST_STATE_TEXT.error_code){
121                                $interval.cancel(pollHostStatusTimer);
122                                pollHostStatusTimer = undefined;
123                                deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
124                            }
125                        }).catch(function(error){
126                            $interval.cancel(pollHostStatusTimer);
127                            pollHostStatusTimer = undefined;
128                            deferred.reject(error);
129                        });
130                    }, Constants.POLL_INTERVALS.POWER_OP);
131
132                    return deferred.promise;
133                }
134                function pollHostStatusTillOff(){
135                    var deferred = $q.defer();
136                    pollHostStatusTimer = $interval(function(){
137                        var now = new Date();
138                        if((now.getTime() - pollStartTime.getTime()) >= Constants.TIMEOUT.HOST_OFF){
139                            $interval.cancel(pollHostStatusTimer);
140                            pollHostStatusTimer = undefined;
141                            deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT));
142                        }
143                        APIUtils.getHostState().then(function(state){
144                            setHostState(state);
145                            if(state === Constants.HOST_STATE_TEXT.off_code){
146                                $interval.cancel(pollHostStatusTimer);
147                                pollHostStatusTimer = undefined;
148                                deferred.resolve(state);
149                            }
150                        }).catch(function(error){
151                            $interval.cancel(pollHostStatusTimer);
152                            pollHostStatusTimer = undefined;
153                            deferred.reject(error);
154                        });
155                    }, Constants.POLL_INTERVALS.POWER_OP);
156
157                    return deferred.promise;
158                }
159                $scope.warmReboot = function(){
160                    $scope.loading = true;
161                    dataService.setUnreachableState();
162                    APIUtils.hostReboot().then(function(response){
163                        return response;
164                    }).then(function(lastStatus) {
165                        pollStartTime = new Date();
166                        return pollHostStatusTillOff();
167                    }).then(function(hostState) {
168                        pollStartTime = new Date();
169                        return pollHostStatusTillOn();
170                    }).then(function(hostState) {
171                        $scope.loading = false;
172                    }).catch(function(error){
173                        dataService.activateErrorModal(
174                          {title: Constants.MESSAGES.POWER_OP.WARM_REBOOT_FAILED,
175                           description: error.statusText ?
176                               $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
177                                   {status: error.status, description: error.statusText}) :
178                               error });
179                        $scope.loading = false;
180                    });
181                };
182                $scope.testState = function(){
183                    $timeout(function(){
184                        dataService.setPowerOffState();
185                        $timeout(function(){
186                            dataService.setPowerOnState();
187                        }, 2000);
188                    }, 1000);
189                };
190                $scope.warmRebootConfirm = function(){
191                    if($scope.confirm) {
192                        return;
193                    }
194                    $scope.confirm = true;
195                    $scope.warmboot_confirm = true;
196                };
197
198                $scope.coldReboot = function(){
199                    $scope.loading = true;
200                    dataService.setUnreachableState();
201                    APIUtils.chassisPowerOff().then(function(state){
202                        return state;
203                    }).then(function(lastState) {
204                        pollStartTime = new Date();
205                        return pollChassisStatusTillOff();
206                    }).then(function(chassisState) {
207                        return APIUtils.hostPowerOn().then(function(hostState){
208                            return hostState;
209                        })
210                    }).then(function(hostState) {
211                        pollStartTime = new Date();
212                        return pollHostStatusTillOn();
213                    }).then(function(state) {
214                        $scope.loading = false;
215                    }).catch(function(error){
216                        dataService.activateErrorModal(
217                          {title: Constants.MESSAGES.POWER_OP.COLD_REBOOT_FAILED,
218                           description: error.statusText ?
219                               $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
220                                   {status: error.status, description: error.statusText}) :
221                               error });
222                        $scope.loading = false;
223                    });
224                };
225                $scope.coldRebootConfirm = function(){
226                    if($scope.confirm) {
227                        return;
228                    }
229                    $scope.confirm = true;
230                    $scope.coldboot_confirm = true;
231                };
232
233                $scope.orderlyShutdown = function(){
234                    $scope.loading = true;
235                    dataService.setUnreachableState();
236                    APIUtils.hostPowerOff().then(function(response){
237                        return response;
238                    }).then(function(lastStatus) {
239                        pollStartTime = new Date();
240                        return pollHostStatusTillOff()
241                    }).then(function(hostState) {
242                        pollStartTime = new Date();
243                        return pollChassisStatusTillOff();
244                    }).then(function(chassisState) {
245                        $scope.loading = false;
246                    }).catch(function(error){
247                        dataService.activateErrorModal(
248                          {title: Constants.MESSAGES.POWER_OP.ORDERLY_SHUTDOWN_FAILED,
249                           description: error.statusText ?
250                               $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
251                                   {status: error.status, description: error.statusText}) :
252                               error });
253                        $scope.loading = false;
254                    });
255                };
256                $scope.orderlyShutdownConfirm = function(){
257                    if($scope.confirm) {
258                        return;
259                    }
260                    $scope.confirm = true;
261                    $scope.orderly_confirm = true;
262                };
263
264                $scope.immediateShutdown = function(){
265                    $scope.loading = true;
266                    dataService.setUnreachableState();
267                    APIUtils.chassisPowerOff().then(function(response){
268                        return response;
269                    }).then(function(lastStatus) {
270                        pollStartTime = new Date();
271                        return pollChassisStatusTillOff();
272                    }).then(function(chassisState) {
273                        dataService.setPowerOffState();
274                        $scope.loading = false;
275                    }).catch(function(error){
276                        dataService.activateErrorModal(
277                          {title: Constants.MESSAGES.POWER_OP.IMMEDIATE_SHUTDOWN_FAILED,
278                           description: error.statusText ?
279                               $interpolate(Constants.MESSAGES.ERROR_MESSAGE_DESC_TEMPLATE)(
280                                   {status: error.status, description: error.statusText}) :
281                               error });
282                        $scope.loading = false;
283                    });
284                };
285                $scope.immediateShutdownConfirm = function(){
286                    if($scope.confirm) {
287                        return;
288                    }
289                    $scope.confirm = true;
290                    $scope.immediately_confirm = true;
291                };
292            }
293        ]
294    );
295
296})(angular);
297