a5c03883034a868e24f427e9fa4ab2f7d4f8fcb3
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / controller / msoCommitController.js
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * VID\r
4  * ================================================================================\r
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 "use strict";\r
22 \r
23 /*\r
24  * "msoCommitController.js" provides controller code to commit MSO requests.\r
25  * \r
26  * HIGHLIGHTS:\r
27  * \r
28  * Parent HTML/JSP code is expected to include "msoCommit.htm" (via\r
29  * "ng-include") and this file (via "<script>"). "msoCommit.jsp" (displayed\r
30  * when the parent code includes "msoCommit.htm") renders a popup window, but\r
31  * initially hides the display.\r
32  * \r
33  * The parent controller code is expected to broadcast either the\r
34  * "createInstance" or "deleteInstance" events when it is ready to commit the\r
35  * code.\r
36  * \r
37  * This controller receives these events (via "$scope.on" declarations), calls\r
38  * "$scope.init()" to "unhide" and initialize the popup display and then makes a\r
39  * REST request to the appropriate server Java controller.\r
40  * \r
41  * The initial REST response is handled by "handleInitialResponse". This method\r
42  * then polls the server (via "getRequestStatus") if the request is successful.\r
43  * \r
44  * The subsequent "getRequestStatus" responses are handled by\r
45  * "handleGetResponse".\r
46  * \r
47  * "handleInitialResponse" and "handleGetResponse" primarily filter response\r
48  * data, manipulate the display and provide error handling.\r
49  * \r
50  * The mechanism has these dependencies (in addition to "msoCommit.htm"):\r
51  * \r
52  * 1) Three services: MsoService, PropertyService and UtilityService\r
53  * \r
54  * 2) The popup window directive found in "popupWindow.htm" and\r
55  * "popupWindowDirective.js"\r
56  * \r
57  * 2) Display styling defined in "dialogs.css"\r
58  * \r
59  * CAVEATS:\r
60  * \r
61  * The parent HTML is assumed to be the "popup-window" directive and the\r
62  * corresponding parent controller is assumed to define the object\r
63  * "$scope.popup".\r
64  */\r
65 \r
66 var msoCommitController = function(COMPONENT, FIELD, $scope, $http, $timeout, $window, $log, $uibModal,\r
67                 MsoService, PropertyService, UtilityService, TestEnvironmentsService, DataService) {\r
68 \r
69         $scope.isViewVisible = false;\r
70         $scope.progressBarControl = {};\r
71         $scope.popupWindowControl = {};\r
72     var getRequestStatusFunc = getOrchestrationRequestStatus; //default\r
73         var _this = this;\r
74 \r
75         $scope.showReportWindow = function() {\r
76                 let requestInfo = {};\r
77                 requestInfo.requestId = _this.requestId;\r
78                 requestInfo.serviceUuid = $scope.instanceId;\r
79 \r
80                 const modalWindow = $uibModal.open({\r
81                         templateUrl: 'app/vid/scripts/modals/report-modal/report-modal.html',\r
82                         controller: 'reportModalInstanceController',\r
83                         controllerAs: 'vm',\r
84                         resolve: {\r
85                                 errorMsg: function () {\r
86                                         return $scope.log;\r
87                                 },\r
88                                 requestInfo: function () {\r
89                                         return requestInfo;\r
90                                 }\r
91                         }\r
92                 });\r
93 \r
94                 $scope.isViewVisible = false;\r
95                 $scope.popup.isVisible = false;\r
96         };\r
97 \r
98         $scope.$on("createInstance", function(event, request) {\r
99                 init(request, COMPONENT.MSO_CREATE_REQ, getOrchestrationRequestStatus );\r
100                 MsoService.createInstance(request, handleInitialResponse);\r
101         });\r
102 \r
103         $scope.$on("deleteInstance", function(event, request) {\r
104         init(request, COMPONENT.MSO_DELETE_REQ, getOrchestrationRequestStatus);\r
105                 MsoService.deleteInstance(request, handleInitialResponse);\r
106         });\r
107 \r
108     $scope.$on(COMPONENT.MSO_CREATE_ENVIRONMENT, function(event, request) {\r
109         init(request, COMPONENT.MSO_CREATE_ENVIRONMENT, getCloudResourcesRequestStatus);\r
110         TestEnvironmentsService.createApplicationEnv(request).then(handleInitialResponse);\r
111     });\r
112 \r
113     $scope.$on(COMPONENT.MSO_DEACTIVATE_ENVIRONMENT, function(event, request) {\r
114         init(request, COMPONENT.MSO_DEACTIVATE_ENVIRONMENT, getCloudResourcesRequestStatus);\r
115         TestEnvironmentsService.deactivateApplicationEnv(request).then(handleInitialResponse)\r
116     });\r
117 \r
118     $scope.$on(COMPONENT.MSO_ACTIVATE_ENVIRONMENT, function(event, request) {\r
119         init(request, COMPONENT.MSO_ACTIVATE_ENVIRONMENT, getCloudResourcesRequestStatus);\r
120         TestEnvironmentsService.activateApplicationEnv(request).then(handleInitialResponse)\r
121     });\r
122 \r
123 \r
124     var init = function(request, msoRequestType, getStatusRequest ) {\r
125         getRequestStatusFunc = getStatusRequest;\r
126                 $scope.status = FIELD.STATUS.SUBMITTING_REQUEST;\r
127                 $scope.isSpinnerVisible = true;\r
128                 $scope.isProgressVisible = true;\r
129                 $scope.error = "";\r
130                 $scope.log = "";\r
131                 $scope.isCloseEnabled = false;\r
132                 $scope.isViewVisible = true;\r
133                 $scope.popup.isVisible = true;\r
134 \r
135                 _this.pollAttempts = 0;\r
136                 _this.callbackFunction = request.callbackFunction;\r
137                 _this.componentId = request.componentId;\r
138                 _this.msoRequestType = msoRequestType;\r
139                 _this.isMsoError = false;\r
140 \r
141                 if (angular.isFunction($scope.progressBarControl.reset)) {\r
142                         $scope.progressBarControl.reset();\r
143                 }\r
144                 $scope.percentProgress = 2; // Show "a little" progress\r
145 \r
146                 UtilityService.setHttpErrorHandler(function(response) {\r
147                         $scope.isCloseEnabled = true;\r
148                         _this.isMsoError = true;\r
149                         showError(FIELD.ERROR.SYSTEM_FAILURE, UtilityService\r
150                                         .getHttpErrorMessage(response));\r
151                 });\r
152         };\r
153 \r
154         var handleInitialResponse = function(response) {\r
155                 try {\r
156                         updateViewAfterInitialResponse(response);\r
157                         \r
158                         _this.timer = $timeout(getRequestStatusFunc, PropertyService\r
159                                         .getMsoMaxPollingIntervalMsec());\r
160 \r
161                         $scope.instanceId = response.data.entity.instanceId;\r
162                         if ($scope.instanceId == null) { \r
163                                 $scope.instanceId = response.data.entity.requestReferences.instanceId;\r
164                         }\r
165                 } catch (error) {\r
166                         if ( response.data != null && response.data.status != null ) {\r
167                                 if (response.data.status > 299 || response.data.status < 200 ) {\r
168                                         // MSO returned an error\r
169                                         _this.isMsoError = true;\r
170                                 }\r
171                         }\r
172                         MsoService.showResponseContentError(error, showError);\r
173                 }\r
174         }\r
175 \r
176         function getOrchestrationRequestStatus() {\r
177                 MsoService.getOrchestrationRequest(_this.requestId, handleGetStatusResponse);\r
178         }\r
179 \r
180         function getCloudResourcesRequestStatus() {\r
181         TestEnvironmentsService.getRequestStatus(_this.requestId, handleGetStatusResponse);\r
182     }\r
183 \r
184 \r
185     var handleGetStatusResponse = function(response) {\r
186                 try {\r
187                         if (isUpdateViewAfterGetResponseComplete(response)) {\r
188                                 return;\r
189                         }\r
190                         //console.log ( "msoCommitController _this.pollAttempts=" + _this.pollAttempts + " max polls=" + PropertyService.getMsoMaxPolls());\r
191                         if (++_this.pollAttempts > PropertyService.getMsoMaxPolls()) {\r
192                                 _this.isMsoError = true;\r
193                                 showError(FIELD.ERROR.MAX_POLLS_EXCEEDED);\r
194                         } else {\r
195                                 _this.timer = $timeout(getRequestStatusFunc, PropertyService\r
196                                                 .getMsoMaxPollingIntervalMsec());\r
197                         }\r
198                 } catch (error) {\r
199                         _this.isMsoError = true;\r
200                         MsoService.showResponseContentError(error, showError);\r
201                 }\r
202         }\r
203 \r
204         var updateViewAfterInitialResponse = function(response) {\r
205                 $scope.isCloseEnabled = true;\r
206 \r
207                 updateLog(response);\r
208 \r
209                 _this.requestId = UtilityService.checkUndefined(FIELD.ID.REQUEST_ID,\r
210                                 UtilityService.checkUndefined(FIELD.ID.REQUEST_REFERENCES,\r
211                                                 response.data.entity.requestReferences).requestId);\r
212 \r
213                 $scope.percentProgress = 4; // Show "a little more" progress\r
214                 $scope.status = FIELD.STATUS.IN_PROGRESS;\r
215         }\r
216 \r
217         /*\r
218          * Updates the view and returns "true" if the MSO operation has returned a\r
219          * "Complete" status.\r
220          */\r
221         var isUpdateViewAfterGetResponseComplete = function(response) {\r
222                 //console.log("msoCommitController isUpdateViewAfterGetResponseComplete");\r
223                 updateLogFinalResponse(response);\r
224 \r
225                 var requestStatus = UtilityService.checkUndefined(FIELD.ID.REQUEST_STATUS,\r
226                                 UtilityService.checkUndefined(FIELD.ID.REQUEST,\r
227                                                 response.data.entity.request).requestStatus);\r
228 \r
229                 var requestState = requestStatus.requestState;\r
230                 console.log("msoCommitController requestState=" + requestState);\r
231                 // look for "progress" or "pending"\r
232                 var patt1 = /progress/i;\r
233                 var patt2 = /pending/i;\r
234                 var result1 = patt1.test(requestState);\r
235                 var result2 = patt2.test(requestState)\r
236                 if (result1 || result2) {\r
237                         requestState = FIELD.STATUS.IN_PROGRESS;\r
238                 }\r
239                 var statusMessage = requestStatus.statusMessage;\r
240                 console.log("msoCommitController statusMessage=" + statusMessage);\r
241                 if (UtilityService.hasContents(statusMessage)) {\r
242                         $scope.status = requestState + " - " + statusMessage;\r
243                 } else {\r
244                         $scope.status = requestState;\r
245                 }\r
246                 if (UtilityService.hasContents(requestStatus.percentProgress)) {\r
247                         $scope.percentProgress = requestStatus.percentProgress;\r
248                 }\r
249 \r
250                 if ( (requestState.toLowerCase() === FIELD.STATUS.FAILED.toLowerCase()) || (requestState.toLowerCase() === FIELD.STATUS.UNLOCKED.toLowerCase())) {\r
251                         throw {\r
252                                 type : FIELD.STATUS.MSO_FAILURE\r
253                         };\r
254                 }\r
255 \r
256                 if (requestState.toLowerCase() === FIELD.STATUS.COMPLETE.toLowerCase()) {\r
257                         $scope.isSpinnerVisible = false;\r
258                         return true;\r
259                 }\r
260 \r
261                 return false;\r
262         }\r
263 \r
264         var updateLog = function(response) {\r
265                 $scope.log = MsoService.getFormattedCommonResponse(response)\r
266                                 + $scope.log;\r
267                 UtilityService.checkUndefined("entity", response.data.entity);\r
268                 UtilityService.checkUndefined("status", response.data.status);\r
269                 MsoService.checkValidStatus(response);\r
270         }\r
271         var updateLogFinalResponse = function(response) {\r
272                 $scope.log = MsoService.getFormattedSingleGetOrchestrationRequestResponse(response)\r
273                                 + $scope.log;\r
274                 UtilityService.checkUndefined("entity", response.data.entity);\r
275                 UtilityService.checkUndefined("status", response.data.status);\r
276                 MsoService.checkValidStatus(response);\r
277         }\r
278         $scope.close = function() {\r
279                 if (_this.timer !== undefined) {\r
280                         $timeout.cancel(_this.timer);\r
281                 }\r
282                 $scope.isViewVisible = false;\r
283                 if (angular.isFunction(_this.callbackFunction)) {\r
284                         if ($scope.error === "") {\r
285                                 _this.callbackFunction({\r
286                                         isSuccessful : true,\r
287                                         instanceId : $scope.instanceId\r
288                                 });\r
289                         } else {\r
290                                 _this.callbackFunction({\r
291                                         isSuccessful : false\r
292                                 });\r
293                                 findNextPage(_this.componentId, _this.msoRequestType, _this.isMsoError);\r
294                         }\r
295                 } else {\r
296                         $scope.popup.isVisible = false;\r
297                 }\r
298         }\r
299 \r
300         var showError = function(summary, details) {\r
301                 var message = summary;\r
302                 if (UtilityService.hasContents(details)) {\r
303                         message += " (" + details + ")";\r
304                 }\r
305                 $scope.isSpinnerVisible = false;\r
306                 $scope.isProgressVisible = false;\r
307                 $scope.error = message;\r
308                 $scope.status = FIELD.STATUS.ERROR;\r
309         }\r
310         \r
311         var findNextPage = function ( cid, msoRequestType, isMsoError ) {\r
312                 //console.log ("window.location.href" + window.location.href);\r
313                 //console.log ("component id " + cid);\r
314                 var originalUrl = window.location.href;\r
315                 if  ( (cid === COMPONENT.SERVICE) && (msoRequestType === COMPONENT.MSO_CREATE_REQ) ) {\r
316                         var url = originalUrl;\r
317                         var x;\r
318                         var count = 0;\r
319                         var firstStr = "";\r
320                         for ( x = 0; x < url.length; x++) {\r
321                                 if ( url.charAt(x) == '/' ) {\r
322                                         count++;\r
323                                         if ( count == 4 ) {\r
324                                                 firstStr = url.substring (0,x);\r
325                                                 break;\r
326                                         }\r
327                                 }\r
328                         }\r
329                         // By default show the search existing service instances screen\r
330                         //var newUrl = firstStr + COMPONENT.SERVICEMODELS_INSTANCES_SERVICES_PATH;\r
331                         var  newUrl = COMPONENT.SERVICEMODELS_INSTANCES_SERVICES_PATH;\r
332                         \r
333                         if ( isMsoError ) {\r
334                                 //Show the Browse SDC models screen\r
335                                 //newUrl = firstStr + COMPONENT.SERVICEMODELS_MODELS_SERVICES_PATH;\r
336                                 newUrl = COMPONENT.SERVICEMODELS_MODELS_SERVICES_PATH;\r
337                         }\r
338                         window.location.href = newUrl;\r
339                 }       \r
340         }\r
341 }\r
342 \r
343 appDS2.controller("msoCommitController", [ "COMPONENT", "FIELD", "$scope", "$http", "$timeout",\r
344                 "$window", "$log", "$uibModal", "MsoService", "PropertyService", "UtilityService", "TestEnvironmentsService", "DataService",\r
345                 msoCommitController ]);\r