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