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