Pause Upon VF Module Failure
[vid.git] / vid-webpack-master / src / app / instantiationStatus / instantiationStatus.component.service.ts
1 import {Injectable} from '@angular/core';
2 import {ServiceInfoModel, ServiceInfoUiModel} from '../shared/server/serviceInfo/serviceInfo.model';
3 import * as _ from 'lodash';
4 import {Observable} from 'rxjs/Observable';
5 import {NgRedux} from "@angular-redux/store";
6 import {AppState} from "../shared/store/reducers";
7 import {AaiService} from "../shared/services/aaiService/aai.service";
8 import {ServiceModel} from "../shared/models/serviceModel";
9 import {FeatureFlagsService, Features} from "../shared/services/featureFlag/feature-flags.service";
10 import {DrawingBoardModes} from "../drawingBoard/service-planning/drawing-board.modes";
11 import {updateDrawingBoardStatus} from "../shared/storeUtil/utils/global/global.actions";
12 import {Router, UrlTree} from "@angular/router";
13 import {of} from "rxjs";
14 import {MsoService} from "../shared/services/msoService/mso.service";
15 import {ServiceAction} from "../shared/models/serviceInstanceActions";
16 import {InstantiationBase} from "../shared/models/InstantiationBase";
17
18 export let PENDING : string = "pending";
19 export let INPROGRESS : string = "in_progress";
20 export let PAUSE : string = "pause";
21 export let X_O : string = "x-circle-o";
22 export let SUCCESS_CIRCLE : string = "success-circle-o";
23 export let STOPPED : string = "stop";
24 export let COMPLETED_WITH_ERRORS : string = "success_with_warning";
25 export let PAUSE_UPON_COMPLETION : string = "stopped-upon-success";
26 export let FAILED_AND_PAUSED : string = "success_with_warning";
27 export let UNKNOWN : string = "question-mark-circle-o";
28
29
30 @Injectable()
31 export class InstantiationStatusComponentService {
32   constructor( private _aaiService: AaiService,
33                private _msoService: MsoService,
34                private _router : Router,
35                private _store: NgRedux<AppState>,
36                private _featureFlagsService:FeatureFlagsService) {
37   }
38
39   generateServiceInfoDataMapping(arr: ServiceInfoModel[]) : { [serviceInstanceId: string]: ServiceInfoModel[]}{
40     let serviceInfoData: { [serviceInstanceId: string]: ServiceInfoModel[]; } = {};
41     for(let item of arr){
42       if(_.isNil(serviceInfoData[item.templateId])){
43         serviceInfoData[item.templateId] = [item];
44       }else {
45         serviceInfoData[item.templateId].push(item);
46       }
47     }
48     return serviceInfoData;
49   }
50
51   convertObjectToArray(arr: ServiceInfoModel[]) : Observable<ServiceInfoUiModel[]>{
52     const obj = this.generateServiceInfoDataMapping(arr);
53     let index:number = 0;
54     let result = [];
55     for(let item in obj) {
56       obj[item].map(item => {
57         item['serviceStatus'] = this.getStatus(item.jobStatus);
58         item['serviceIndex'] = index;
59       });
60       index++;
61       result = result.concat(obj[item]);
62     }
63
64     console.log(result);
65     return of(result);
66   }
67
68   isDrawingBoardViewEdit(serviceModel: ServiceModel): boolean {
69     if (!_.isNil(serviceModel.vidNotions) && !_.isNil(serviceModel.vidNotions.viewEditUI)
70       && serviceModel.vidNotions.viewEditUI !== 'legacy'){
71       return true;
72     }
73     return false;
74   }
75
76   open(item: ServiceInfoModel): void {
77     if (this._featureFlagsService.getFlagState(Features.FLAG_1902_VNF_GROUPING)) {
78       this._aaiService.getServiceModelById(item['serviceModelId']).subscribe((result)=>{
79         const serviceModel =  new ServiceModel(result);
80
81         if (this.isDrawingBoardViewEdit(serviceModel)) {
82           this.navigateToNewViewEdit(item, DrawingBoardModes.EDIT);
83           return;
84         }
85
86         this.navigateToNewViewOnlyOrOldEditView(item);
87
88       });
89     }
90
91     /*this else is here only to save time in case we don't need to retrieve service model
92     it can be removed once it service model is always needed, and it doesn't save time*/
93     else {
94       this.navigateToNewViewOnlyOrOldEditView(item);
95     }
96   }
97
98   navigateToNewViewOnlyOrOldEditView(item: ServiceInfoModel) {
99     if (this._featureFlagsService.getFlagState(Features.FLAG_1902_NEW_VIEW_EDIT)) {
100       this.navigateToNewViewEdit(item, DrawingBoardModes.VIEW);
101     }
102     else {
103       this.navigateToOldViewEdit(item);
104     }
105   }
106
107   navigateToOldViewEdit(item: ServiceInfoModel) {
108     let query =
109       `subscriberId=${item.subscriberId}&` +
110       `subscriberName=${item.subscriberName}&` +
111       `serviceType=${item.serviceType}&` +
112       `serviceInstanceId=${item.serviceInstanceId}`;
113
114     this._store.dispatch(updateDrawingBoardStatus(DrawingBoardModes.OLD_VIEW_EDIT));
115     window.parent.location.assign('../../serviceModels.htm#/instantiate?' + query);
116   }
117
118   navigateToNewViewEdit(item: InstantiationBase, mode: DrawingBoardModes): void {
119     console.log("Mode : ", mode);
120     this._store.dispatch(updateDrawingBoardStatus(mode));
121     const viewEditUrlTree:UrlTree = this.getNewViewEditUrlTree(item, mode);
122     this._router.navigateByUrl(viewEditUrlTree);
123     window.parent.location.assign(this.getViewEditUrl(viewEditUrlTree));
124   }
125
126   getNewViewEditUrlTree(item: InstantiationBase, mode: DrawingBoardModes): UrlTree {
127     return this._router.createUrlTree(
128       ['/servicePlanning/' + mode],
129       {
130         queryParams:
131         mode==DrawingBoardModes.RECREATE ?
132           this.getRecreateQueryParams(item) :
133           this.getDefaultViewEditQueryParams(<ServiceInfoModel> item)
134       });
135   }
136
137   private getDefaultViewEditQueryParams(item: ServiceInfoModel) {
138     return {
139       serviceModelId: item.serviceModelId,
140       serviceInstanceId: item.serviceInstanceId,
141       serviceType: item.serviceType,
142       subscriberId: item.subscriberId,
143       jobId: item.jobId
144     };
145   }
146
147   private getRecreateQueryParams(item: InstantiationBase) {
148     return {
149       serviceModelId: item.serviceModelId,
150       jobId: item.jobId
151     };
152   }
153
154   getViewEditUrl(viewEditUrlTree:UrlTree): string {
155     return '../../serviceModels.htm#' + viewEditUrlTree.toString();
156   }
157
158   getStatus(status : string) : ServiceStatus {
159     switch(`${status}`.toUpperCase()) {
160       case  'PENDING' :
161         return new ServiceStatus(PENDING, 'primary', 'Pending: The action required will be sent as soon as possible.');
162       case  'IN_PROGRESS' :
163         return new ServiceStatus(INPROGRESS, 'primary', 'In-progress: the service is in process of the action required.');
164       case  'PAUSED' :
165         return new ServiceStatus(PAUSE, 'primary', 'Paused: Service has paused and waiting for your action.\n Select actions from the menu to the right.');
166       case  'FAILED' :
167         return new ServiceStatus(X_O, 'error', 'Failed: All planned actions have failed.');
168       case  'COMPLETED' :
169         return new ServiceStatus(SUCCESS_CIRCLE, 'success', 'Completed successfully: Service is successfully instantiated, updated or deleted.');
170       case  'STOPPED' :
171         return new ServiceStatus(STOPPED, 'error', 'Stopped: Due to previous failure, will not be instantiated.');
172       case  'COMPLETED_WITH_ERRORS' :
173         return new ServiceStatus(COMPLETED_WITH_ERRORS, 'success', 'Completed with errors: some of the planned actions where successfully committed while other have not.\n Open the service to check it out.');
174       case  'COMPLETED_AND_PAUSED' :
175         return new ServiceStatus(PAUSE_UPON_COMPLETION, 'default','Pause upon completion. you may resume the instantiation.\n Open the service to check it out.' );
176       case 'FAILED_AND_PAUSED' :
177         return new ServiceStatus(FAILED_AND_PAUSED, 'success','Failed and Paused: you may re-deploy the instantiation.\n Open the service to check it out.' );
178       default:
179         return new ServiceStatus(UNKNOWN, 'primary', `Unexpected status: "${status}"`);
180     }
181   }
182
183   retry(item: ServiceInfoModel): void {
184       this.navigateToNewViewEdit(item, DrawingBoardModes.RETRY_EDIT);
185   }
186
187   resume(item: ServiceInfoModel): void {
188     this.navigateToNewViewEdit(item, DrawingBoardModes.RESUME);
189   }
190
191   recreate(item: ServiceInfoModel): void {
192     this.navigateToNewViewEdit(item, DrawingBoardModes.RECREATE);
193   }
194
195   isRecreateEnabled(item: ServiceInfoModel): boolean {
196     return item.action === ServiceAction.INSTANTIATE;
197   }
198
199   isRecreateVisible(): boolean {
200     return this._featureFlagsService.getFlagState(Features.FLAG_2004_CREATE_ANOTHER_INSTANCE_FROM_TEMPLATE);
201   }
202
203   isNewViewEditVisible(): boolean {
204     return this._featureFlagsService.getFlagState(Features.FLAG_2006_NEW_VIEW_EDIT_BUTTON_IN_INSTANTIATION_STATUS);
205   }
206
207   forwardToNewViewEdit(item: ServiceInfoModel): void {
208     this.navigateToNewViewEdit(item, DrawingBoardModes.EDIT);
209   }
210 }
211
212
213 export class ServiceStatus {
214   iconClassName : string;
215   color : string;
216   tooltip : string;
217
218   constructor(_iconClassName : string, _color : string, _tooltip : string){
219     this.iconClassName = _iconClassName;
220     this.color = _color;
221     this.tooltip = _tooltip;
222   }
223 }