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