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