aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/webapp/app/vid/scripts/controller/msoCommitModalController.js
blob: 631d0122f673c5dc7faa452effd20d12e9cb6be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

"use strict";

var msoCommitModalController = function(COMPONENT, FIELD, $scope, $http, $timeout, $window, $log,
		MsoService, PropertyService, UtilityService, DataService, $uibModalInstance, msoType, requestParams, configuration, vidService, featureFlags) {

    $scope.isSpinnerVisible = true;
    $scope.isProgressVisible = true;
    $scope.status = FIELD.STATUS.SUBMITTING_REQUEST;
    $scope.error = "";
    $scope.log = "";
    $scope.progressBarControl = {};
    $scope.isCloseEnabled = false;

    var _this = this;
    _this.pollAttempts = 0;
    _this.callbackFunction = requestParams.callbackFunction;
    _this.isMsoError = false;


    if (angular.isFunction($scope.progressBarControl.reset)) {
        $scope.progressBarControl.reset();
    }
    $scope.percentProgress = 2; // Show "a little" progress


    /*
	 * Updates the view and returns "true" if the MSO operation has returned a
	 * "Complete" status.
	 */
    var isUpdateViewAfterGetResponseComplete = function(response) {
        //console.log("msoCommitController isUpdateViewAfterGetResponseComplete");
        updateLogFinalResponse(response);

        var requestStatus = UtilityService.checkUndefined(FIELD.ID.REQUEST_STATUS,
            UtilityService.checkUndefined(FIELD.ID.REQUEST,
                response.data.entity.request).requestStatus);

        var requestState = requestStatus.requestState;
        console.log("msoCommitController requestState=" + requestState);
        // look for "progress" or "pending"
        var patt1 = /progress/i;
        var patt2 = /pending/i;
        var result1 = patt1.test(requestState);
        var result2 = patt2.test(requestState);
        if (result1 || result2) {
            requestState = FIELD.STATUS.IN_PROGRESS;
        }
        var statusMessage = requestStatus.statusMessage;
        console.log("msoCommitController statusMessage=" + statusMessage);
        if (UtilityService.hasContents(statusMessage)) {
            $scope.status = requestState + " - " + statusMessage;
        } else {
            $scope.status = requestState;
        }
        if (UtilityService.hasContents(requestStatus.percentProgress)) {
            $scope.percentProgress = requestStatus.percentProgress;
        }

        if ( (requestState.toLowerCase() === FIELD.STATUS.FAILED.toLowerCase()) || (requestState.toLowerCase() === FIELD.STATUS.UNLOCKED.toLowerCase())) {
            throw {
                type : FIELD.STATUS.MSO_FAILURE
            };
        }

        if (requestState.toLowerCase() === FIELD.STATUS.COMPLETE.toLowerCase()) {
            $scope.isSpinnerVisible = false;
            return true;
        }

        return false;
    };

    var handleGetResponse = function(response) {
        try {
            if (isUpdateViewAfterGetResponseComplete(response)) {
                return;
            }
            if (++_this.pollAttempts > PropertyService.getMsoMaxPolls()) {
                _this.isMsoError = true;
                showError(FIELD.ERROR.MAX_POLLS_EXCEEDED);
            } else {
                _this.timer = $timeout(getRequestStatus, PropertyService
                    .getMsoMaxPollingIntervalMsec());
            }
        } catch (error) {
            _this.isMsoError = true;
            MsoService.showResponseContentError(error, showError);
        }
    };

    var showError = function(summary, details) {
        var message = summary;
        if (UtilityService.hasContents(details)) {
            message += " (" + details + ")";
        }
        $scope.isSpinnerVisible = false;
        $scope.isProgressVisible = false;
        $scope.error = message;
        $scope.status = FIELD.STATUS.ERROR;
    };

    var getRequestStatus = function() {
        MsoService.getOrchestrationRequest(_this.requestId, handleGetResponse);
    };

    var updateLog = function(response) {
        $scope.log = MsoService.getFormattedCommonResponse(response)
            + $scope.log;
        UtilityService.checkUndefined("entity", response.data.entity);
        UtilityService.checkUndefined("status", response.data.status);
        MsoService.checkValidStatus(response);
    };

    var updateLogFinalResponse = function(response) {
        $scope.log = MsoService.getFormattedSingleGetOrchestrationRequestResponse(response)
            + $scope.log;
        UtilityService.checkUndefined("entity", response.data.entity);
        UtilityService.checkUndefined("status", response.data.status);
        MsoService.checkValidStatus(response);
    };

    var updateViewAfterInitialResponse = function(response) {
        $scope.isCloseEnabled = true;
        updateLog(response);

        _this.requestId = UtilityService.checkUndefined(FIELD.ID.REQUEST_ID,
            UtilityService.checkUndefined(FIELD.ID.REQUEST_REFERENCES,
                response.data.entity.requestReferences).requestId);

        $scope.percentProgress = 4; // Show "a little more" progress
        $scope.status = FIELD.STATUS.IN_PROGRESS;
    };

    var init = function(msoType) {
        switch(msoType) {
            case COMPONENT.MSO_DELETE_CONFIGURATION_REQ :
                return MsoService.deleteConfiguration(requestParams, configuration);
            case COMPONENT.MSO_CREATE_REQ:
                return MsoService.createConfigurationInstance(requestParams);
            case  COMPONENT.MSO_CHANGE_CONFIG_STATUS_REQ:
                return MsoService.toggleConfigurationStatus(requestParams, configuration);
            case COMPONENT.MSO_CHANGE_PORT_STATUS_REQ:
                return MsoService.togglePortStatus(requestParams, configuration);
            case COMPONENT.MSO_CREATE_REALATIONSHIP:
                return MsoService.associatePnf(requestParams);
            case COMPONENT.MSO_REMOVE_RELATIONSHIP:
                return MsoService.dissociatePnf(requestParams);
            case COMPONENT.MSO_ACTIVATE_SERVICE_REQ:
                return MsoService.activateInstance(requestParams);
            case COMPONENT.MSO_DEACTIVATE_SERVICE_REQ:
                return MsoService.deactivateInstance(requestParams);
        }
    };

    var successCallbackFunction = function(response) {
        try {
            updateViewAfterInitialResponse(response);
            _this.timer = $timeout(getRequestStatus, PropertyService
                .getMsoMaxPollingIntervalMsec());

            $scope.instanceId = response.data.entity.instanceId;
            if ($scope.instanceId == null) {
                $scope.instanceId = response.data.entity.requestReferences.instanceId;
            }
        } catch (error) {
            if ( response.data != null && response.data.status != null ) {
                if (response.data.status > 299 || response.data.status < 200 ) {
                    // MSO returned an error
                    _this.isMsoError = true;
                }
            }
            MsoService.showResponseContentError(error, showError);
        }
    };

    var errorCallbackFunction = function (error) {
        UtilityService.setHttpErrorHandler(function(error) {
            $scope.isCloseEnabled = true;
            _this.isMsoError = true;
            showError(FIELD.ERROR.SYSTEM_FAILURE, UtilityService
                .getHttpErrorMessage(error));
        });
    };

    $scope.close = function() {
        $uibModalInstance.dismiss('cancel');

        if (_this.timer !== undefined) {
            $timeout.cancel(_this.timer);
        }

        if (angular.isFunction(_this.callbackFunction)) {
            if ($scope.error === "") {
                _this.callbackFunction({
                    isSuccessful : true,
                    instanceId : $scope.instanceId
                });
            } else {
                _this.callbackFunction({
                    isSuccessful : false
                });
            }
        }
    };

    _this.msoRequestType = msoType;


    init(_this.msoRequestType)
        .then(function (response) {
            successCallbackFunction(response);
        })
        .catch(function (error) {
            errorCallbackFunction(error);
        });
};

appDS2.controller("msoCommitModalController", [ "COMPONENT", "FIELD", "$scope", "$http", "$timeout",
		"$window", "$log", "MsoService", "PropertyService", "UtilityService", "DataService", "$uibModalInstance", "msoType", "requestParams", "configuration", "vidService", "featureFlags",
    msoCommitModalController ]);