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