10a3e67eb9cc09dfc2a2628b3b9226ee383ba178
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / controller / change-management.controller.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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 (function () {
22     'use strict';
23
24     appDS2.controller("changeManagementController", ["$uibModal", "changeManagementService", "_", "$log",  "SchedulerService", "$filter", "VIDCONFIGURATION", changeManagementController]);
25
26     function changeManagementController($uibModal, changeManagementService, _, $log, SchedulerService, $filter, VIDCONFIGURATION) {
27         var vm = this;
28
29         vm.lastTimeUpdated = "";
30         vm.hasScheduler = !!VIDCONFIGURATION.SCHEDULER_PORTAL_URL;
31         vm.currModal = null;
32
33         vm.init = function() {
34             vm.lastTimeUpdated = $filter('date')(new Date(), "MM/dd/yyyy | HH:mm:ss");
35             loadMSOChangeManagements();
36             loadSchedulerChangeManagements();
37         };
38
39         var fuseMsoAndSchedulerTaks = function() {
40             if (vm.changeManagements && vm.pendingChangeManagements) {
41                 var requestIdToVnfName = {}
42                 vm.pendingChangeManagements.forEach(function(schedulerItem) {
43                     if (schedulerItem.msoRequestId && schedulerItem.vnfName) {
44                         requestIdToVnfName[schedulerItem.msoRequestId] = schedulerItem.vnfName;
45                     }
46                 })
47                 $log.debug("requestIdToVnfName", requestIdToVnfName);
48
49                 vm.changeManagements = vm.changeManagements.map(function(msoItem) {
50                     msoItem['vnfNameFromScheduler'] = requestIdToVnfName[msoItem.requestId];
51                     return msoItem;
52                 })
53             }
54         };
55
56         var loadMSOChangeManagements = function() {
57             changeManagementService.getMSOChangeManagements()
58                 .then(function(response) {
59                     vm.changeManagements = response.data;
60                 })
61                 .then(function () {
62                     fuseMsoAndSchedulerTaks();
63                 })
64                 .catch(function (error) {
65                     $log.error(error);
66                 });
67         };
68
69         var loadSchedulerChangeManagements = function() {
70             changeManagementService.getSchedulerChangeManagements()
71                 .then(function(response) {
72                     vm.pendingChangeManagements = response.data;
73                     _.forEach(vm.pendingChangeManagements, function(changeManagement) {
74                         var callbackData = _.filter(changeManagement.scheduleRequest.domainData, {name: "WorkflowName"});
75                         if(callbackData) {
76                             var parsedModel = {};
77                             try {
78                                 parsedModel = callbackData[0].value;
79                             } catch(exception) {
80                                 $log.error(exception);
81                             }
82
83                             changeManagement.workflow = parsedModel;
84                         }
85                     });
86                 })
87                 .then(function () {
88                     fuseMsoAndSchedulerTaks();
89                 })
90                 .catch(function(error) {
91                     $log.error(error);
92                 });
93         };
94
95         vm.createNewChange = function() {
96             vm.closeCurrentModalIfOpen();
97             var modalInstance = $uibModal.open({
98                 templateUrl: 'app/vid/scripts/modals/new-change-management/new-change-management.html',
99                 controller: 'newChangeManagementModalController',
100                 controllerAs: 'vm',
101                 resolve: {}
102             });
103
104             vm.currModal = modalInstance;
105
106             modalInstance.result.then(function (result) {
107                 console.log("This is the result of the new change management modal.", result);
108             });
109         };
110
111         vm.searchChanges = function() {
112             console.log("function for searching changes: " + vm.searchChangesTerm)
113         };
114
115
116         vm.openManualTasksPopup = function($event, jobInfo, templateUrl, message) {
117
118             vm.closeCurrentModalIfOpen();
119
120             var modalInstance = $uibModal.open({
121                 templateUrl: templateUrl,
122                 controller: 'changeManagementManualTasksController',
123                 controllerAs: 'vm',
124                 backdrop: false,
125                 animation: true,
126                 appendTo: angular.element($event.currentTarget).parent(),
127                 resolve: {
128                     jobInfo: function () {
129                         return jobInfo;
130                     }
131                 }
132             });
133
134             modalInstance.result.then(function (result) {
135                 console.log(message, result);
136             });
137
138             vm.currModal = modalInstance;
139         };
140
141         vm.openFailedModal = function($event, jobInfo) {
142             vm.openManualTasksPopup($event, jobInfo,
143                 'app/vid/scripts/modals/failed-change-management/failed-change-management.html',
144                 "This is the result of the failed change management modal.")
145         };
146
147         vm.openInProgressModal = function($event, jobInfo) {
148             vm.openManualTasksPopup($event, jobInfo,
149                 'app/vid/scripts/modals/in-progress-modal-management/in-progress-change-management.html',
150                 "This is the result of the in progress change management modal.")
151         };
152
153         vm.openAlertModal = function($event, jobInfo) {
154             vm.openManualTasksPopup($event, jobInfo,
155                 'app/vid/scripts/modals/alert-change-management/alert-change-management.html',
156                 "This is the result of the alert change management modal.")
157         };
158
159         vm.openBasicAlertModal = function(jobInfo) {
160             vm.closeCurrentModalIfOpen();
161             var modalInstance = $uibModal.open({
162                 templateUrl: 'app/vid/scripts/modals/alert-modal/alert-modal.html',
163                 controller: 'alertModalController',
164                 controllerAs: 'vm',
165                 backdrop: false,
166                 animation: true,
167                 appendTo: angular.element(".jobs-table").eq(0),
168                 resolve: {
169                     jobInfo: function () {
170                         return jobInfo;
171                     }
172                 }
173             });
174             vm.currModal = modalInstance;
175
176             modalInstance.result.then(function (result) {
177                 console.log("This is the result of the alert change management modal.", result);
178             });
179         };
180         vm.openPendingModal = function($event, changeManagement) {
181
182             vm.closeCurrentModalIfOpen();
183
184             var modalInstance = $uibModal.open({
185                 templateUrl: 'app/vid/scripts/modals/cancel-pending-workflow/cancel-pending-workflow.html',
186                 controller: 'cancelPendingWorkflowController',
187                 controllerAs: 'vm',
188                 backdrop: false,
189                 animation: true,
190                 appendTo: angular.element($event.currentTarget).parent(),
191                 resolve: {
192                     changeManagement: function () {
193                         return changeManagement;
194                     }
195                 }
196             });
197
198             vm.currModal = modalInstance;
199
200             modalInstance.result.then(function (result) {
201                 // send to service
202              if(result){
203                     SchedulerService.cancelScheduleRequest(changeManagement.scheduleRequest.scheduleId,
204                         function(response) {
205
206                             //success popup
207                             var jobInfo= {
208                                 status:"success",
209                                 message: "Cancel workflow "+changeManagement.scheduleRequest.scheduleName+" succeeded."};
210                             vm.openBasicAlertModal(jobInfo);
211
212                             return response;
213                         }, function(error) {
214
215                             //failed popup
216                             var jobInfo = {
217                                 status:"failed",
218                                 message: "Cancel workflow "+changeManagement.scheduleRequest.scheduleName+" failed due to an unexpected error."};
219                             vm.openBasicAlertModal(jobInfo);
220
221                         });
222             }});
223
224         };
225         vm.isChangeManagementDeleted = function(changeManagement) {
226             return changeManagement.scheduleRequest.status!=='Deleted'
227         };
228
229         vm.closeCurrentModalIfOpen = function() {
230             if (vm.currModal != null) {
231                 vm.currModal.close();
232                 vm.currModal = null;
233             }
234         }
235
236
237         vm.init();
238     }
239 })();