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