[VID-3] Setting docker image tag
[vid.git] / vid / src / main / webapp / app / vid / scripts / controller / msoCommitController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 "use strict";
22
23 /*
24  * "msoCommitController.js" provides controller code to commit MSO requests.
25  * 
26  * HIGHLIGHTS:
27  * 
28  * Parent HTML/JSP code is expected to include "msoCommit.htm" (via
29  * "ng-include") and this file (via "<script>"). "msoCommit.jsp" (displayed
30  * when the parent code includes "msoCommit.htm") renders a popup window, but
31  * initially hides the display.
32  * 
33  * The parent controller code is expected to broadcast either the
34  * "createInstance" or "deleteInstance" events when it is ready to commit the
35  * code.
36  * 
37  * This controller receives these events (via "$scope.on" declarations), calls
38  * "$scope.init()" to "unhide" and initialize the popup display and then makes a
39  * REST request to the appropriate server Java controller.
40  * 
41  * The initial REST response is handled by "handleInitialResponse". This method
42  * then polls the server (via "getRequestStatus") if the request is successful.
43  * 
44  * The subsequent "getRequestStatus" responses are handled by
45  * "handleGetResponse".
46  * 
47  * "handleInitialResponse" and "handleGetResponse" primarily filter response
48  * data, manipulate the display and provide error handling.
49  * 
50  * The mechanism has these dependencies (in addition to "msoCommit.htm"):
51  * 
52  * 1) Three services: MsoService, PropertyService and UtilityService
53  * 
54  * 2) The popup window directive found in "popupWindow.htm" and
55  * "popupWindowDirective.js"
56  * 
57  * 2) Display styling defined in "dialogs.css"
58  * 
59  * CAVEATS:
60  * 
61  * The parent HTML is assumed to be the "popup-window" directive and the
62  * corresponding parent controller is assumed to define the object
63  * "$scope.popup".
64  */
65
66 var msoCommitController = function($scope, $http, $timeout, $window, $log,
67                 MsoService, PropertyService, UtilityService, DataService) {
68
69         $scope.isViewVisible = false;
70         $scope.progressBarControl = {};
71         $scope.popupWindowControl = {};
72
73         var _this = this;
74
75         $scope.$on("createInstance", function(event, request) {
76                 init(request);
77                 MsoService.createInstance(request, handleInitialResponse);
78         });
79
80         $scope.$on("deleteInstance", function(event, request) {
81                 init(request);
82                 MsoService.deleteInstance(request, handleInitialResponse);
83         });
84
85         var init = function(request) {
86                 $scope.status = "Submitting Request";
87                 $scope.isSpinnerVisible = true;
88                 $scope.isProgressVisible = true;
89                 $scope.error = "";
90                 $scope.log = "";
91                 $scope.isCloseEnabled = false;
92                 $scope.isViewVisible = true;
93                 $scope.popup.isVisible = true;
94
95                 _this.pollAttempts = 0;
96                 _this.callbackFunction = request.callbackFunction;
97
98                 if (angular.isFunction($scope.progressBarControl.reset)) {
99                         $scope.progressBarControl.reset();
100                 }
101                 $scope.percentProgress = 2; // Show "a little" progress
102
103                 UtilityService.setHttpErrorHandler(function(response) {
104                         $scope.isCloseEnabled = true;
105                         showError("System failure", UtilityService
106                                         .getHttpErrorMessage(response));
107                 });
108         }
109
110         var handleInitialResponse = function(response) {
111                 try {
112                         updateViewAfterInitialResponse(response);
113                         _this.timer = $timeout(getRequestStatus, PropertyService
114                                         .getMsoMaxPollingIntervalMsec());
115
116                         $scope.instanceId = response.data.entity.instanceId;
117                         if ($scope.instanceId == null) { 
118                                 $scope.instanceId = response.data.entity.requestReferences.instanceId;
119                         }
120                 } catch (error) {
121                         MsoService.showResponseContentError(error, showError);
122                 }
123         }
124
125         var getRequestStatus = function() {
126                 MsoService.getOrchestrationRequest(_this.requestId, handleGetResponse);
127         }
128
129         var handleGetResponse = function(response) {
130                 try {
131                         if (isUpdateViewAfterGetResponseComplete(response)) {
132                                 return;
133                         }
134                         console.log ( "msoCommitController _this.pollAttempts=" + _this.pollAttempts + " max polls=" + PropertyService.getMsoMaxPolls());
135                         if (++_this.pollAttempts > PropertyService.getMsoMaxPolls()) {
136                                 showError("Maximum number of poll attempts exceeded");
137                         } else {
138                                 _this.timer = $timeout(getRequestStatus, PropertyService
139                                                 .getMsoMaxPollingIntervalMsec());
140                         }
141                 } catch (error) {
142                         MsoService.showResponseContentError(error, showError);
143                 }
144         }
145
146         var updateViewAfterInitialResponse = function(response) {
147                 $scope.isCloseEnabled = true;
148
149                 updateLog(response);
150
151                 _this.requestId = UtilityService.checkUndefined("requestId",
152                                 UtilityService.checkUndefined("requestReferences",
153                                                 response.data.entity.requestReferences).requestId);
154
155                 $scope.percentProgress = 4; // Show "a little more" progress
156                 $scope.status = "In Progress";
157         }
158
159         /*
160          * Updates the view and returns "true" if the MSO operation has returned a
161          * "Complete" status.
162          */
163         var isUpdateViewAfterGetResponseComplete = function(response) {
164                 console.log("msoCommitController isUpdateViewAfterGetResponseComplete");
165                 updateLog(response);
166
167                 var requestStatus = UtilityService.checkUndefined("requestStatus",
168                                 UtilityService.checkUndefined("request",
169                                                 response.data.entity.request).requestStatus);
170
171                 var requestState = requestStatus.requestState;
172                 console.log("msoCommitController requestState=" + requestState);
173                 // look for "progress" or "pending"
174                 var patt1 = /progress/i;
175                 var patt2 = /pending/i;
176                 var result1 = patt1.test(requestState);
177                 var result2 = patt2.test(requestState)
178                 if (result1 || result2) {
179                         requestState = "In Progress";
180                 }
181                 var statusMessage = requestStatus.statusMessage;
182                 console.log("msoCommitController statusMessage=" + statusMessage);
183                 if (UtilityService.hasContents(statusMessage)) {
184                         $scope.status = requestState + " - " + statusMessage;
185                 } else {
186                         $scope.status = requestState;
187                 }
188                 if (UtilityService.hasContents(requestStatus.percentProgress)) {
189                         $scope.percentProgress = requestStatus.percentProgress;
190                 }
191
192                 if (requestState.toLowerCase() === "Failed".toLowerCase()) {
193                         throw {
194                                 type : "msoFailure"
195                         };
196                 }
197
198                 if (requestState.toLowerCase() === "Complete".toLowerCase()) {
199                         $scope.isSpinnerVisible = false;
200                         return true;
201                 }
202
203                 return false;
204         }
205
206         var updateLog = function(response) {
207                 $scope.log = MsoService.getFormattedCommonResponse(response)
208                                 + $scope.log;
209                 UtilityService.checkUndefined("entity", response.data.entity);
210                 UtilityService.checkUndefined("status", response.data.status);
211                 MsoService.checkValidStatus(response);
212         }
213
214         $scope.close = function() {
215                 if (_this.timer !== undefined) {
216                         $timeout.cancel(_this.timer);
217                 }
218                 $scope.isViewVisible = false;
219                 if (angular.isFunction(_this.callbackFunction)) {
220                         if ($scope.error === "") {
221                                 _this.callbackFunction({
222                                         isSuccessful : true,
223                                         instanceId : $scope.instanceId
224                                 });
225                         } else {
226                                 _this.callbackFunction({
227                                         isSuccessful : false
228                                 });
229                         }
230                 } else {
231                         $scope.popup.isVisible = false;
232                 }
233         }
234
235         var showError = function(summary, details) {
236                 var message = summary;
237                 if (UtilityService.hasContents(details)) {
238                         message += " (" + details + ")";
239                 }
240                 $scope.isSpinnerVisible = false;
241                 $scope.isProgressVisible = false;
242                 $scope.error = message;
243                 $scope.status = "Error";
244         }
245 }
246
247 app.controller("msoCommitController", [ "$scope", "$http", "$timeout",
248                 "$window", "$log", "MsoService", "PropertyService", "UtilityService",
249                 msoCommitController ]);