fixing bugs connected with undefined parameters
[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                 if(_this.requestId !== undefined) {\r
78                         requestInfo.requestId = _this.requestId;\r
79                 } else {\r
80                         requestInfo.requestId = null;\r
81                 }\r
82                 if($scope.service !== undefined) {\r
83                         requestInfo.serviceUuid = $scope.service.model.service.uuid;\r
84                 } else {\r
85                         requestInfo.serviceUuid = null;\r
86                 }\r
87 \r
88                 const modalWindow = $uibModal.open({\r
89                         templateUrl: 'app/vid/scripts/modals/report-modal/report-modal.html',\r
90                         controller: 'reportModalInstanceController',\r
91                         controllerAs: 'vm',\r
92                         resolve: {\r
93                                 errorMsg: function () {\r
94                                         return $scope.log;\r
95                                 },\r
96                                 requestInfo: function () {\r
97                                         return requestInfo;\r
98                                 }\r
99                         }\r
100                 });\r
101 \r
102                 $scope.isViewVisible = false;\r
103                 $scope.popup.isVisible = false;\r
104         };\r
105 \r
106         $scope.$on("createInstance", function(event, request) {\r
107                 init(request, COMPONENT.MSO_CREATE_REQ, getOrchestrationRequestStatus );\r
108                 MsoService.createInstance(request, handleInitialResponse);\r
109         });\r
110 \r
111         $scope.$on("deleteInstance", function(event, request) {\r
112         init(request, COMPONENT.MSO_DELETE_REQ, getOrchestrationRequestStatus);\r
113                 MsoService.deleteInstance(request, handleInitialResponse);\r
114         });\r
115 \r
116     $scope.$on(COMPONENT.MSO_CREATE_ENVIRONMENT, function(event, request) {\r
117         init(request, COMPONENT.MSO_CREATE_ENVIRONMENT, getCloudResourcesRequestStatus);\r
118         TestEnvironmentsService.createApplicationEnv(request).then(handleInitialResponse);\r
119     });\r
120 \r
121     $scope.$on(COMPONENT.MSO_DEACTIVATE_ENVIRONMENT, function(event, request) {\r
122         init(request, COMPONENT.MSO_DEACTIVATE_ENVIRONMENT, getCloudResourcesRequestStatus);\r
123         TestEnvironmentsService.deactivateApplicationEnv(request).then(handleInitialResponse)\r
124     });\r
125 \r
126     $scope.$on(COMPONENT.MSO_ACTIVATE_ENVIRONMENT, function(event, request) {\r
127         init(request, COMPONENT.MSO_ACTIVATE_ENVIRONMENT, getCloudResourcesRequestStatus);\r
128         TestEnvironmentsService.activateApplicationEnv(request).then(handleInitialResponse)\r
129     });\r
130 \r
131 \r
132     var init = function(request, msoRequestType, getStatusRequest ) {\r
133         getRequestStatusFunc = getStatusRequest;\r
134                 $scope.status = FIELD.STATUS.SUBMITTING_REQUEST;\r
135                 $scope.isSpinnerVisible = true;\r
136                 $scope.isProgressVisible = true;\r
137                 $scope.error = "";\r
138                 $scope.log = "";\r
139                 $scope.isCloseEnabled = false;\r
140                 $scope.isViewVisible = true;\r
141                 $scope.popup.isVisible = true;\r
142 \r
143                 _this.pollAttempts = 0;\r
144                 _this.callbackFunction = request.callbackFunction;\r
145                 _this.componentId = request.componentId;\r
146                 _this.msoRequestType = msoRequestType;\r
147                 _this.isMsoError = false;\r
148 \r
149                 if (angular.isFunction($scope.progressBarControl.reset)) {\r
150                         $scope.progressBarControl.reset();\r
151                 }\r
152                 $scope.percentProgress = 2; // Show "a little" progress\r
153 \r
154                 UtilityService.setHttpErrorHandler(function(response) {\r
155                         $scope.isCloseEnabled = true;\r
156                         _this.isMsoError = true;\r
157                         showError(FIELD.ERROR.SYSTEM_FAILURE, UtilityService\r
158                                         .getHttpErrorMessage(response));\r
159                 });\r
160         };\r
161 \r
162         var handleInitialResponse = function(response) {\r
163                 try {\r
164                         updateViewAfterInitialResponse(response);\r
165                         \r
166                         _this.timer = $timeout(getRequestStatusFunc, PropertyService\r
167                                         .getMsoMaxPollingIntervalMsec());\r
168 \r
169                         $scope.instanceId = response.data.entity.instanceId;\r
170                         if ($scope.instanceId == null) { \r
171                                 $scope.instanceId = response.data.entity.requestReferences.instanceId;\r
172                         }\r
173                 } catch (error) {\r
174                         if ( response.data != null && response.data.status != null ) {\r
175                                 if (response.data.status > 299 || response.data.status < 200 ) {\r
176                                         // MSO returned an error\r
177                                         _this.isMsoError = true;\r
178                                 }\r
179                         }\r
180                         MsoService.showResponseContentError(error, showError);\r
181                 }\r
182         }\r
183 \r
184         function getOrchestrationRequestStatus() {\r
185                 MsoService.getOrchestrationRequest(_this.requestId, handleGetStatusResponse);\r
186         }\r
187 \r
188         function getCloudResourcesRequestStatus() {\r
189         TestEnvironmentsService.getRequestStatus(_this.requestId, handleGetStatusResponse);\r
190     }\r
191 \r
192 \r
193     var handleGetStatusResponse = function(response) {\r
194                 try {\r
195                         if (isUpdateViewAfterGetResponseComplete(response)) {\r
196                                 return;\r
197                         }\r
198                         //console.log ( "msoCommitController _this.pollAttempts=" + _this.pollAttempts + " max polls=" + PropertyService.getMsoMaxPolls());\r
199                         if (++_this.pollAttempts > PropertyService.getMsoMaxPolls()) {\r
200                                 _this.isMsoError = true;\r
201                                 showError(FIELD.ERROR.MAX_POLLS_EXCEEDED);\r
202                         } else {\r
203                                 _this.timer = $timeout(getRequestStatusFunc, PropertyService\r
204                                                 .getMsoMaxPollingIntervalMsec());\r
205                         }\r
206                 } catch (error) {\r
207                         _this.isMsoError = true;\r
208                         MsoService.showResponseContentError(error, showError);\r
209                 }\r
210         }\r
211 \r
212         var updateViewAfterInitialResponse = function(response) {\r
213                 $scope.isCloseEnabled = true;\r
214 \r
215                 updateLog(response);\r
216 \r
217                 _this.requestId = UtilityService.checkUndefined(FIELD.ID.REQUEST_ID,\r
218                                 UtilityService.checkUndefined(FIELD.ID.REQUEST_REFERENCES,\r
219                                                 response.data.entity.requestReferences).requestId);\r
220 \r
221                 $scope.percentProgress = 4; // Show "a little more" progress\r
222                 $scope.status = FIELD.STATUS.IN_PROGRESS;\r
223         }\r
224 \r
225         /*\r
226          * Updates the view and returns "true" if the MSO operation has returned a\r
227          * "Complete" status.\r
228          */\r
229         var isUpdateViewAfterGetResponseComplete = function(response) {\r
230                 //console.log("msoCommitController isUpdateViewAfterGetResponseComplete");\r
231                 updateLogFinalResponse(response);\r
232 \r
233                 var requestStatus = UtilityService.checkUndefined(FIELD.ID.REQUEST_STATUS,\r
234                                 UtilityService.checkUndefined(FIELD.ID.REQUEST,\r
235                                                 response.data.entity.request).requestStatus);\r
236 \r
237                 var requestState = requestStatus.requestState;\r
238                 console.log("msoCommitController requestState=" + requestState);\r
239                 // look for "progress" or "pending"\r
240                 var patt1 = /progress/i;\r
241                 var patt2 = /pending/i;\r
242                 var result1 = patt1.test(requestState);\r
243                 var result2 = patt2.test(requestState)\r
244                 if (result1 || result2) {\r
245                         requestState = FIELD.STATUS.IN_PROGRESS;\r
246                 }\r
247                 var statusMessage = requestStatus.statusMessage;\r
248                 console.log("msoCommitController statusMessage=" + statusMessage);\r
249                 if (UtilityService.hasContents(statusMessage)) {\r
250                         $scope.status = requestState + " - " + statusMessage;\r
251                 } else {\r
252                         $scope.status = requestState;\r
253                 }\r
254                 if (UtilityService.hasContents(requestStatus.percentProgress)) {\r
255                         $scope.percentProgress = requestStatus.percentProgress;\r
256                 }\r
257 \r
258                 if ( (requestState.toLowerCase() === FIELD.STATUS.FAILED.toLowerCase()) || (requestState.toLowerCase() === FIELD.STATUS.UNLOCKED.toLowerCase())) {\r
259                         throw {\r
260                                 type : FIELD.STATUS.MSO_FAILURE\r
261                         };\r
262                 }\r
263 \r
264                 if (requestState.toLowerCase() === FIELD.STATUS.COMPLETE.toLowerCase()) {\r
265                         $scope.isSpinnerVisible = false;\r
266                         return true;\r
267                 }\r
268 \r
269                 return false;\r
270         }\r
271 \r
272         var updateLog = function(response) {\r
273                 $scope.log = MsoService.getFormattedCommonResponse(response)\r
274                                 + $scope.log;\r
275                 UtilityService.checkUndefined("entity", response.data.entity);\r
276                 UtilityService.checkUndefined("status", response.data.status);\r
277                 MsoService.checkValidStatus(response);\r
278         }\r
279         var updateLogFinalResponse = function(response) {\r
280                 $scope.log = MsoService.getFormattedSingleGetOrchestrationRequestResponse(response)\r
281                                 + $scope.log;\r
282                 UtilityService.checkUndefined("entity", response.data.entity);\r
283                 UtilityService.checkUndefined("status", response.data.status);\r
284                 MsoService.checkValidStatus(response);\r
285         }\r
286         $scope.close = function() {\r
287                 if (_this.timer !== undefined) {\r
288                         $timeout.cancel(_this.timer);\r
289                 }\r
290                 $scope.isViewVisible = false;\r
291                 if (angular.isFunction(_this.callbackFunction)) {\r
292                         if ($scope.error === "") {\r
293                                 _this.callbackFunction({\r
294                                         isSuccessful : true,\r
295                                         instanceId : $scope.instanceId\r
296                                 });\r
297                         } else {\r
298                                 _this.callbackFunction({\r
299                                         isSuccessful : false\r
300                                 });\r
301                                 findNextPage(_this.componentId, _this.msoRequestType, _this.isMsoError);\r
302                         }\r
303                 } else {\r
304                         $scope.popup.isVisible = false;\r
305                 }\r
306         }\r
307 \r
308         var showError = function(summary, details) {\r
309                 var message = summary;\r
310                 if (UtilityService.hasContents(details)) {\r
311                         message += " (" + details + ")";\r
312                 }\r
313                 $scope.isSpinnerVisible = false;\r
314                 $scope.isProgressVisible = false;\r
315                 $scope.error = message;\r
316                 $scope.status = FIELD.STATUS.ERROR;\r
317         }\r
318         \r
319         var findNextPage = function ( cid, msoRequestType, isMsoError ) {\r
320                 //console.log ("window.location.href" + window.location.href);\r
321                 //console.log ("component id " + cid);\r
322                 var originalUrl = window.location.href;\r
323                 if  ( (cid === COMPONENT.SERVICE) && (msoRequestType === COMPONENT.MSO_CREATE_REQ) ) {\r
324                         var url = originalUrl;\r
325                         var x;\r
326                         var count = 0;\r
327                         var firstStr = "";\r
328                         for ( x = 0; x < url.length; x++) {\r
329                                 if ( url.charAt(x) == '/' ) {\r
330                                         count++;\r
331                                         if ( count == 4 ) {\r
332                                                 firstStr = url.substring (0,x);\r
333                                                 break;\r
334                                         }\r
335                                 }\r
336                         }\r
337                         // By default show the search existing service instances screen\r
338                         //var newUrl = firstStr + COMPONENT.SERVICEMODELS_INSTANCES_SERVICES_PATH;\r
339                         var  newUrl = COMPONENT.SERVICEMODELS_INSTANCES_SERVICES_PATH;\r
340                         \r
341                         if ( isMsoError ) {\r
342                                 //Show the Browse SDC models screen\r
343                                 //newUrl = firstStr + COMPONENT.SERVICEMODELS_MODELS_SERVICES_PATH;\r
344                                 newUrl = COMPONENT.SERVICEMODELS_MODELS_SERVICES_PATH;\r
345                         }\r
346                         window.location.href = newUrl;\r
347                 }       \r
348         }\r
349 }\r
350 \r
351 appDS2.controller("msoCommitController", [ "COMPONENT", "FIELD", "$scope", "$http", "$timeout",\r
352                 "$window", "$log", "$uibModal", "MsoService", "PropertyService", "UtilityService", "TestEnvironmentsService", "DataService",\r
353                 msoCommitController ]);\r