org.onap migration
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / controller / msoCommitModalController.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 var msoCommitModalController = function(COMPONENT, FIELD, $scope, $http, $timeout, $window, $log,
24                 MsoService, PropertyService, UtilityService, DataService, $uibModalInstance, msoType, requestParams, vidService) {
25
26     $scope.isSpinnerVisible = true;
27     $scope.isProgressVisible = true;
28     $scope.status = FIELD.STATUS.SUBMITTING_REQUEST;
29     $scope.error = "";
30     $scope.log = "";
31     $scope.progressBarControl = {};
32     $scope.isCloseEnabled = false;
33
34     var _this = this;
35     _this.pollAttempts = 0;
36     _this.callbackFunction = requestParams.callbackFunction;
37     _this.isMsoError = false;
38
39
40     if (angular.isFunction($scope.progressBarControl.reset)) {
41         $scope.progressBarControl.reset();
42     }
43     $scope.percentProgress = 2; // Show "a little" progress
44
45
46     /*
47          * Updates the view and returns "true" if the MSO operation has returned a
48          * "Complete" status.
49          */
50     var isUpdateViewAfterGetResponseComplete = function(response) {
51         //console.log("msoCommitController isUpdateViewAfterGetResponseComplete");
52         updateLogFinalResponse(response);
53
54         var requestStatus = UtilityService.checkUndefined(FIELD.ID.REQUEST_STATUS,
55             UtilityService.checkUndefined(FIELD.ID.REQUEST,
56                 response.data.entity.request).requestStatus);
57
58         var requestState = requestStatus.requestState;
59         console.log("msoCommitController requestState=" + requestState);
60         // look for "progress" or "pending"
61         var patt1 = /progress/i;
62         var patt2 = /pending/i;
63         var result1 = patt1.test(requestState);
64         var result2 = patt2.test(requestState);
65         if (result1 || result2) {
66             requestState = FIELD.STATUS.IN_PROGRESS;
67         }
68         var statusMessage = requestStatus.statusMessage;
69         console.log("msoCommitController statusMessage=" + statusMessage);
70         if (UtilityService.hasContents(statusMessage)) {
71             $scope.status = requestState + " - " + statusMessage;
72         } else {
73             $scope.status = requestState;
74         }
75         if (UtilityService.hasContents(requestStatus.percentProgress)) {
76             $scope.percentProgress = requestStatus.percentProgress;
77         }
78
79         if ( (requestState.toLowerCase() === FIELD.STATUS.FAILED.toLowerCase()) || (requestState.toLowerCase() === FIELD.STATUS.UNLOCKED.toLowerCase())) {
80             throw {
81                 type : FIELD.STATUS.MSO_FAILURE
82             };
83         }
84
85         if (requestState.toLowerCase() === FIELD.STATUS.COMPLETE.toLowerCase()) {
86             $scope.isSpinnerVisible = false;
87             return true;
88         }
89
90         return false;
91     };
92
93     var handleGetResponse = function(response) {
94         try {
95             if (isUpdateViewAfterGetResponseComplete(response)) {
96                 return;
97             }
98             if (++_this.pollAttempts > PropertyService.getMsoMaxPolls()) {
99                 _this.isMsoError = true;
100                 showError(FIELD.ERROR.MAX_POLLS_EXCEEDED);
101             } else {
102                 _this.timer = $timeout(getRequestStatus, PropertyService
103                     .getMsoMaxPollingIntervalMsec());
104             }
105         } catch (error) {
106             _this.isMsoError = true;
107             MsoService.showResponseContentError(error, showError);
108         }
109     };
110
111     var showError = function(summary, details) {
112         var message = summary;
113         if (UtilityService.hasContents(details)) {
114             message += " (" + details + ")";
115         }
116         $scope.isSpinnerVisible = false;
117         $scope.isProgressVisible = false;
118         $scope.error = message;
119         $scope.status = FIELD.STATUS.ERROR;
120     };
121
122     var getRequestStatus = function() {
123         MsoService.getOrchestrationRequest(_this.requestId, handleGetResponse);
124     };
125
126     var updateLog = function(response) {
127         $scope.log = MsoService.getFormattedCommonResponse(response)
128             + $scope.log;
129         UtilityService.checkUndefined("entity", response.data.entity);
130         UtilityService.checkUndefined("status", response.data.status);
131         MsoService.checkValidStatus(response);
132     };
133
134     var updateLogFinalResponse = function(response) {
135         $scope.log = MsoService.getFormattedSingleGetOrchestrationRequestResponse(response)
136             + $scope.log;
137         UtilityService.checkUndefined("entity", response.data.entity);
138         UtilityService.checkUndefined("status", response.data.status);
139         MsoService.checkValidStatus(response);
140     };
141
142     var updateViewAfterInitialResponse = function(response) {
143         $scope.isCloseEnabled = true;
144         updateLog(response);
145
146         _this.requestId = UtilityService.checkUndefined(FIELD.ID.REQUEST_ID,
147             UtilityService.checkUndefined(FIELD.ID.REQUEST_REFERENCES,
148                 response.data.entity.requestReferences).requestId);
149
150         $scope.percentProgress = 4; // Show "a little more" progress
151         $scope.status = FIELD.STATUS.IN_PROGRESS;
152     };
153
154     var init = function(msoType) {
155         switch(msoType) {
156             case COMPONENT.MSO_CREATE_REQ:
157                 return MsoService.createConfigurationInstance(requestParams);
158             case  COMPONENT.MSO_CHANGE_CONFIG_STATUS_REQ:
159                 return MsoService.toggleConfigurationStatus(requestParams);
160             case COMPONENT.MSO_CHANGE_PORT_STATUS_REQ:
161                 return MsoService.togglePortStatus(requestParams);
162             case COMPONENT.MSO_CREATE_REALATIONSHIP:
163                 return MsoService.associatePnf(requestParams);
164             case COMPONENT.MSO_REMOVE_RELATIONSHIP:
165                 return MsoService.dissociatePnf(requestParams);
166             case COMPONENT.MSO_ACTIVATE_SERVICE_REQ:
167                 return MsoService.activateInstance(requestParams);
168             case COMPONENT.MSO_DEACTIVATE_SERVICE_REQ:
169                 return MsoService.deactivateInstance(requestParams);
170         }
171     };
172
173     var successCallbackFunction = function(response) {
174         try {
175             updateViewAfterInitialResponse(response);
176             _this.timer = $timeout(getRequestStatus, PropertyService
177                 .getMsoMaxPollingIntervalMsec());
178
179             $scope.instanceId = response.data.entity.instanceId;
180             if ($scope.instanceId == null) {
181                 $scope.instanceId = response.data.entity.requestReferences.instanceId;
182             }
183         } catch (error) {
184             if ( response.data != null && response.data.status != null ) {
185                 if (response.data.status > 299 || response.data.status < 200 ) {
186                     // MSO returned an error
187                     _this.isMsoError = true;
188                 }
189             }
190             MsoService.showResponseContentError(error, showError);
191         }
192     };
193
194     var errorCallbackFunction = function (error) {
195         UtilityService.setHttpErrorHandler(function(error) {
196             $scope.isCloseEnabled = true;
197             _this.isMsoError = true;
198             showError(FIELD.ERROR.SYSTEM_FAILURE, UtilityService
199                 .getHttpErrorMessage(error));
200         });
201     };
202
203     $scope.close = function() {
204         $uibModalInstance.dismiss('cancel');
205
206         if (_this.timer !== undefined) {
207             $timeout.cancel(_this.timer);
208         }
209
210         if (angular.isFunction(_this.callbackFunction)) {
211             if ($scope.error === "") {
212                 _this.callbackFunction({
213                     isSuccessful : true,
214                     instanceId : $scope.instanceId
215                 });
216             } else {
217                 _this.callbackFunction({
218                     isSuccessful : false
219                 });
220             }
221         }
222     };
223
224     _this.msoRequestType = msoType;
225
226     init(_this.msoRequestType)
227         .then(function (response) {
228             successCallbackFunction(response);
229         })
230         .catch(function (error) {
231             errorCallbackFunction(error);
232         });
233 };
234
235 appDS2.controller("msoCommitModalController", [ "COMPONENT", "FIELD", "$scope", "$http", "$timeout",
236                 "$window", "$log", "MsoService", "PropertyService", "UtilityService", "DataService", "$uibModalInstance", "msoType", "requestParams", "vidService",
237     msoCommitModalController ]);