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