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