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