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