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