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