merge from ecomp a88f0072 - Modern UI
[vid.git] / vid-webpack-master / src / app / drawingBoard / service-planning / objectsToTree / shared.tree.service.ts
1 import {Injectable} from "@angular/core";
2 import {NgRedux} from "@angular-redux/store";
3 import {AppState} from "../../../shared/store/reducers";
4 import {ServiceInstanceActions} from "../../../shared/models/serviceInstanceActions";
5 import {MessageBoxData} from "../../../shared/components/messageBox/messageBox.data";
6 import {MessageBoxService} from "../../../shared/components/messageBox/messageBox.service";
7 import * as _ from "lodash";
8 import {DrawingBoardModes} from "../drawing-board.modes";
9 import {AuditInfoModalComponent} from "../../../shared/components/auditInfoModal/auditInfoModal.component";
10 import {VnfModelInfo} from "./models/vnf/vnf.model.info";
11 import {ILevelNodeInfo} from "./models/basic.model.info";
12
13 @Injectable()
14 export class SharedTreeService {
15   private _sharedTreeService: SharedTreeService;
16   constructor(private _store: NgRedux<AppState>) {
17   }
18
19   /***********************************************************
20    * return if instance has missing data
21    * @param instance - vnf instance
22    * @param dynamicInputs - from the instance
23    * @param isEcompGeneratedNaming
24    ************************************************************/
25   selectedVNF: string = null;
26
27
28   getSelectedVNF(): string {
29     return this.selectedVNF;
30   }
31
32   setSelectedVNF(node): void {
33     if (_.isNil(node) || node.data.type !== 'VF') {
34       this.selectedVNF = null;
35     } else {
36       this.selectedVNF = node.data.vnfStoreKey;
37     }
38   }
39
40   hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean, requiredFields: string[]): boolean {
41     if (!isEcompGeneratedNaming && _.isEmpty(instance.instanceName)) {
42       return true;
43     }
44
45     for (let field of requiredFields) {
46       if (_.isEmpty(instance[field])) {
47         return true;
48       }
49     }
50
51     for (let field of dynamicInputs) {
52       if (field.isRequired && !_.isNil(instance.instanceParams) && _.isEmpty(instance.instanceParams[0][field.id])) {
53         return true;
54       }
55     }
56     return false;
57   }
58
59
60   addingStatusProperty(node) {
61     node['statusProperties'] = [];
62     node['statusProperties'].push({key: 'Prov Status:', value: node.provStatus, testId: 'provStatus'});
63     node['statusProperties'].push({key: 'Orch Status:', value: node.orchStatus, testId: 'orchStatus'});
64     if (node.inMaint) {
65       node['statusProperties'].push({key: 'In-maintenance', value: '', testId: 'inMaint'});
66     }
67     return node;
68   }
69
70   /**********************************************
71    * should delete or remove child instance's
72    "new" -> should remove
73    !new" -> should change action status
74    **********************************************/
75   removeDeleteAllChild(node, serviceModelId: string, callback): void {
76     for (let nodeChild of node.children) {
77       if (nodeChild.data.action === ServiceInstanceActions.Create) {
78         if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['remove'])) {
79           nodeChild.data.menuActions['remove']['method'](nodeChild, serviceModelId);
80         }
81       } else {
82         if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['delete'])) {
83           nodeChild.data.menuActions['delete']['method'](nodeChild, serviceModelId);
84         }
85       }
86     }
87     callback(node, serviceModelId);
88   }
89
90
91   /**********************************************
92    * should undo delete child instance's
93    **********************************************/
94   undoDeleteAllChild(node, serviceModelId: string, callback): void {
95     for (let nodeChild of node.children) {
96       if (!_.isNil(nodeChild.data) && !_.isNil(nodeChild.data.menuActions) && !_.isNil(nodeChild.data.menuActions['undoDelete'])) {
97         nodeChild.data.menuActions['undoDelete']['method'](nodeChild, serviceModelId);
98       }
99     }
100     callback(node, serviceModelId);
101   }
102
103   /**********************************************
104    * should return true if can delete
105    **********************************************/
106   shouldShowDelete(node): boolean {
107     const mode = this._store.getState().global.drawingBoardStatus;
108     if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['delete'])) {
109       if (mode !== DrawingBoardModes.EDIT || node.data.action === ServiceInstanceActions.Create) {
110         return false;
111       } else if (node.data.action === ServiceInstanceActions.None) {
112         return true
113       }
114       return false;
115     }
116     return false;
117   }
118
119   /**********************************************
120    * should return true if can undo delete
121    **********************************************/
122   shouldShowUndoDelete(node): boolean {
123     const mode = this._store.getState().global.drawingBoardStatus;
124     if (mode === DrawingBoardModes.EDIT && !_.isNil(node.data.action) && !_.isNil(node.data.menuActions['undoDelete'])) {
125       if (node.data.action === ServiceInstanceActions.Create || node.data.action === ServiceInstanceActions.Delete) {
126         return false;
127       } else if (node.data.action.split('_').pop() === 'Delete') {
128         return true
129       }
130       return false;
131     }
132     return false;
133   }
134   /**********************************************
135    * should return true if can remove or edit
136    * enabled only on edit/design mode and for new instances
137    **********************************************/
138   shouldShowRemoveAndEdit(node): boolean {
139     const mode = this._store.getState().global.drawingBoardStatus;
140     if (!_.isNil(node) && !_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action === ServiceInstanceActions.Create &&
141       mode !== DrawingBoardModes.VIEW && mode !== DrawingBoardModes.RETRY) {
142       return true;
143     }
144     return false;
145   }
146   /**********************************************
147    * should return true if can duplicate by mode
148    **********************************************/
149   shouldShowDuplicate(node): boolean {
150     const mode = this._store.getState().global.drawingBoardStatus;
151     return !mode.includes('RETRY');
152   }
153
154   /**********************************************
155    * should return true if can audit info
156    **********************************************/
157   shouldShowAuditInfo(node): boolean {
158     return this.isRetryMode() || (!_.isNil(node.data) && !_.isNil(node.data.action) && node.data.action !== ServiceInstanceActions.Create);
159   }
160
161
162   isRetryMode(): boolean {
163     const mode = this._store.getState().global.drawingBoardStatus;
164     return mode.includes('RETRY');
165   }
166
167
168   /**********************************************
169    * should return true if can add node instances
170    **********************************************/
171   shouldShowAddIcon(): boolean{
172     const mode = this._store.getState().global.drawingBoardStatus;
173     return mode === DrawingBoardModes.EDIT || mode=== DrawingBoardModes.CREATE;
174   }
175   /************************************************
176    return number of instances with action Delete
177    @type: vnfs networks, vngGroups (not vfModule)
178    @node : node model from the left tree
179    ************************************************/
180   getExistingInstancesWithDeleteMode(node, serviceModelId: string, type: string): number {
181     let counter = 0;
182     const existingInstances = this._store.getState().service.serviceInstance[serviceModelId][type];
183     const modelUniqueId = node.data.modelUniqueId;
184     if (!_.isNil(existingInstances)) {
185       for (let instanceKey in existingInstances) {
186         if (!_.isNil(existingInstances[instanceKey].action)) {
187           if (existingInstances[instanceKey].modelInfo.modelUniqueId === modelUniqueId && existingInstances[instanceKey].action.split('_').pop() === 'Delete') {
188             counter++;
189           }
190         }
191       }
192     }
193     return counter;
194   }
195
196
197   isServiceOnDeleteMode(serviceId: string): boolean {
198     return this._store.getState().service.serviceInstance[serviceId].action === ServiceInstanceActions.Delete;
199   }
200
201
202   openModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
203     let type: string = _.isArray(node) ? 'Service' : node.data.typeName;
204     let messageBoxData: MessageBoxData = new MessageBoxData(
205       "Mark for Delete",
206       `You are about to mark for delete this ${type} this will also mark all its children and remove all new instances just added`,
207       <any>"warning",
208       <any>"md",
209       [
210         {
211           text: "Mark and remove",
212           size: "large",
213           callback: cb.bind(this, node, serviceModelId),
214           closeModal: true
215         },
216         {text: "Don’t Remove", size: "medium", closeModal: true}
217       ]);
218
219     MessageBoxService.openModal.next(messageBoxData);
220   }
221
222   someChildHasCreateAction(nodes: any | any[]) : boolean {
223     let nodesArr = _.isArray(nodes) ? nodes : [nodes];
224     for(const node of nodesArr){
225       if(node.action === ServiceInstanceActions.Create) {return true;}
226       if(node.children){
227         for (let nodeChild of node.children) {
228           if (nodeChild.action === ServiceInstanceActions.Create) {
229             return true;
230           }
231           if(nodeChild.children && nodeChild.children.length > 0){
232             for(let child of nodeChild.children){
233               let hasCreateAction = this.someChildHasCreateAction(child);
234               if(hasCreateAction) {
235                 return true;
236               }
237             }
238           }
239         }
240       }
241     }
242     return false;
243   }
244
245   shouldShowDeleteInstanceWithChildrenModal(node : any | any[] , serviceModelId : string, cb : Function) : void {
246     if(this.someChildHasCreateAction(node)){
247       this.openModal(node , serviceModelId, cb);
248     }else {
249       cb(node, serviceModelId)
250     }
251   }
252
253
254   isFailed(node): boolean {
255     return !_.isNil(node.data) ? node.data.isFailed : false;
256   }
257
258   /************************************************
259    in a case the node is failed e.g. not instantiated correctly
260    the function will call to openRetryInstanceAuditInfoModal
261    @node : node model from the left tree
262    @serviceModelId : serviceModelId
263    @instance : instance
264    @instanceType: instanceType
265    @modelInfoService : the model (vnf, vfmodule, network, vnfgroup)object that call to the function (this)
266    ************************************************/
267   openAuditInfoModal(node, serviceModelId, instance, instanceType, modelInfoService : ILevelNodeInfo){
268     let isInstanceFailed  = this.isFailed(node);
269       AuditInfoModalComponent.openInstanceAuditInfoModal.next({
270         instanceId: serviceModelId,
271         type: instanceType,
272         model: modelInfoService.getModel(node.data.modelName, instance, this._store.getState().service.serviceHierarchy[serviceModelId]),
273         instance,
274         isInstanceFailed,
275         trackById: instance.trackById
276       });
277     }
278 }