aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/drawingBoard/service-planning/objectsToTree/models/vfModule/vfModule.model.info.ts
blob: 47a6dcb50751c6d53a83d6727ced373d6c08b5b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import {DynamicInputsService} from "../../dynamicInputs.service";
import {ILevelNodeInfo} from "../basic.model.info";
import * as _ from "lodash";
import {VNFModel} from "../../../../../shared/models/vnfModel";
import {SharedTreeService} from "../../shared.tree.service";
import {VfModuleTreeNode} from "../../../../../shared/models/vfModuleTreeNode";
import {VfModuleInstance} from "../../../../../shared/models/vfModuleInstance";
import {VfModule} from "../../../../../shared/models/vfModule";
import {NgRedux} from "@angular-redux/store";
import {ITreeNode} from "angular-tree-component/dist/defs/api";
import {
  GenericFormPopupComponent,
  PopupType
} from "../../../../../shared/components/genericFormPopup/generic-form-popup.component";
import {DialogService} from "ng2-bootstrap-modal";
import {VfModulePopuopService} from "../../../../../shared/components/genericFormPopup/genericFormServices/vfModule/vfModule.popuop.service";
import {AppState} from "../../../../../shared/store/reducers";
import {MessageBoxData} from "../../../../../shared/components/messageBox/messageBox.data";
import {MessageBoxService} from "../../../../../shared/components/messageBox/messageBox.service";
import {AvailableNodeIcons} from "../../../available-models-tree/available-models-tree.service";
import {IframeService} from "../../../../../shared/utils/iframe.service";
import {
  deleteActionVfModuleInstance,
  removeVfModuleInstance,
  undoDeleteVfModuleInstance,
  undoUgradeVFModule,
  updateVFModulePosition,
  upgradeVFModule
} from "../../../../../shared/storeUtil/utils/vfModule/vfModule.actions";
import {ComponentInfoService} from "../../../component-info/component-info.service";
import {ComponentInfoType} from "../../../component-info/component-info-model";
import {ModelInformationItem} from "../../../../../shared/components/model-information/model-information.component";

export class VFModuleModelInfo implements ILevelNodeInfo {
  constructor(private _dynamicInputsService: DynamicInputsService,
              private _sharedTreeService: SharedTreeService,
              private _dialogService: DialogService,
              private _vfModulePopupService: VfModulePopuopService,
              private _iframeService: IframeService,
              private _store: NgRedux<AppState>,
              private _componentInfoService: ComponentInfoService) {
  }

  name: string = 'vfModules';
  type: string = 'Module';
  typeName: string = 'M';
  componentInfoType = ComponentInfoType.VFMODULE;

  /***********************************************************
   * return if user should provide instance name or not.
   *        get info from parent (VNF)
   * @param currentModel - current Model object
   * @param parentModel - current parent Model object
   ************************************************************/
  isEcompGeneratedNaming(currentModel, parentModel): boolean {
    const ecompGeneratedNaming = !_.isNil(parentModel.properties) ? parentModel.properties.ecomp_generated_naming : undefined;
    return ecompGeneratedNaming === "true";
  }

  /***********************************************************
   * return model dynamic inputs
   * @param currentModel - current Model object
   ************************************************************/
  updateDynamicInputsDataFromModel = (currentModel): any => {
    let displayInputs = _.isNil(currentModel) ? [] : currentModel.inputs;
    return _.isEmpty(displayInputs) ? [] : this._dynamicInputsService.getArbitraryInputs(displayInputs);
  };

  /***********************************************************
   * return vfModule model
   * @param vfModuleModelId - current Model id
   * @param instance
   * @param serviceHierarchy - serviceHierarchy
   ************************************************************/
  getModel = (vfModuleModelId: string, instance, serviceHierarchy): any => {
    if (!_.isNil(serviceHierarchy)) {
      if (!_.isNil(serviceHierarchy[this.name]) && !_.isNil(serviceHierarchy[this.name][vfModuleModelId])) {
        return serviceHierarchy[this.name][vfModuleModelId];
      }
    }
    return {};
  };

  createNode(instance: VfModuleInstance, currentModel: VfModule, parentModel: VNFModel, modelName: string, index: number): VfModuleTreeNode {
    let dynamicModelName = Object.keys(instance)[index];
    instance = instance[Object.keys(instance)[index]];
    const isEcompGeneratedNaming: boolean = this.isEcompGeneratedNaming(currentModel, parentModel);

    const dynamicInputs = this.updateDynamicInputsDataFromModel(currentModel);
    let newVfModule: VfModuleTreeNode = new VfModuleTreeNode(instance, currentModel, modelName, dynamicInputs, isEcompGeneratedNaming, dynamicModelName);

    newVfModule.missingData = this._sharedTreeService.hasMissingData(instance, newVfModule.dynamicInputs, isEcompGeneratedNaming, []);
    newVfModule.typeName = this.typeName;
    newVfModule.menuActions = this.getMenuAction(<any>newVfModule, currentModel.uuid);
    newVfModule.isFailed = _.isNil(instance.isFailed) ? false : instance.isFailed;
    newVfModule.statusMessage = !_.isNil(instance.statusMessage) ? instance.statusMessage: "";

    newVfModule = this._sharedTreeService.addingStatusProperty(newVfModule);
    return newVfModule;
  }

  createInstanceTreeNode(instance: VfModuleInstance, currentModel: VfModule, parentModel: VNFModel, modelName: string): VfModuleTreeNode | VfModuleTreeNode[] {
    let numberOfChilds = Object.keys(instance).length;
    if (numberOfChilds > 1) {
      let result: VfModuleTreeNode[] = [];
      for (let i = 0; i < numberOfChilds; i++) {
        result.push(this.createNode(instance, currentModel, parentModel, modelName, i));
      }
      return result;
    } else {
      return this.createNode(instance, currentModel, parentModel, modelName, 0);
    }
  }

  /***********************************************************
   * return next level object (null because is last level)
   ************************************************************/
  getNextLevelObject(): any {
    return null;
  }

  getTooltip = (): string => 'VFmodule';

  getType = (): string => 'VFmodule';

  /***********************************************************
   * return if instance has missing data
   * @param instance - vnf instance
   * @param dynamicInputs
   * @param isEcompGeneratedNaming
   ************************************************************/
  hasMissingData(instance, dynamicInputs: any, isEcompGeneratedNaming: boolean): boolean {
    return this._sharedTreeService.hasMissingData(instance, dynamicInputs, isEcompGeneratedNaming, []);
  }


  /***********************************************************
   * return if instance has missing data
   * @param node - VFModule node
   * @param serviceModelId - current service id
   ************************************************************/
  onClickAdd(node: ITreeNode, serviceModelId: string): void {
    const vnfStoreKey = this._sharedTreeService.getSelectedVNF() || this.getDefaultVNF(node.parent, serviceModelId);
    if (vnfStoreKey) {
      this._dialogService.addDialog(GenericFormPopupComponent, {
        type: PopupType.VF_MODULE,
        uuidData: <any>{
          serviceId: serviceModelId,
          modelName: node.data.name,
          vFModuleStoreKey: null,
          vnfStoreKey: vnfStoreKey,
          modelId: node.data.modelId,
          type: node.data.type,
          popupService: this._vfModulePopupService
        },
        node: node,
        isUpdateMode: false
      });
    } else {
      let messageBoxData: MessageBoxData = new MessageBoxData(
        "Select a parent",  // modal title
        "There are multiple instances on the right side that can contain this vf-module Please select the VNF instance, to add this vf-module to, on the right side and then click the + sign",
        <any>"warning",
        <any>"md",
        [
          {text: "Close", size: "medium", closeModal: true}
        ]);
      MessageBoxService.openModal.next(messageBoxData);
    }
  }

  getDefaultVNF(node: ITreeNode, serviceModelId: string): string {
    let keys = _.keys(_.pickBy(this._store.getState().service.serviceInstance[serviceModelId].vnfs, vnf => {
      return (vnf.originalName == node.data.name);
    }));
    return keys.length === 1 ? this._store.getState().service.serviceInstance[serviceModelId].vnfs[keys[0]].vnfStoreKey : null;
  }

  /***********************************************************
   * return number of existing instances (in all VNF's)
   * @param node - VfModule node
   * @param serviceModelId - current service id
   ************************************************************/
  getNodeCount(node: ITreeNode, serviceModelId: string): number {
    let count: number = 0;
    if (!_.isNil(this._store.getState().service.serviceInstance) && !_.isNil(this._store.getState().service.serviceInstance[serviceModelId])) {
      const vnfs = this._store.getState().service.serviceInstance[serviceModelId].vnfs;

      for (let vnfKey in vnfs) {
        count += this.countNumberOfVFModule(vnfs[vnfKey], node);
      }
      return count;
    }
    return count;
  }


  countNumberOfVFModule(vnf, node): number {
    let count = 0;
    for (let vfModuleKey in vnf['vfModules']) {
      for (let vfModule in vnf['vfModules'][vfModuleKey]) {
        if (vnf['vfModules'][vfModuleKey][vfModule]['modelInfo'].modelCustomizationId === node.data.modelUniqueId) {
          const vfModuleObj = vnf['vfModules'][vfModuleKey][vfModule];
          if (!(!_.isNil(vfModuleObj) && !_.isNil(vfModuleObj.action) && vfModuleObj.action.split('_').pop() === 'Delete')) count++;
        }
      }
    }
    return count;
  }

  getCountVFModuleOfSelectedVNF(node: ITreeNode, vnfStoreKey: string, serviceModelId: string): number {
    let count: number = 0;
    if (!_.isNil(this._store.getState().service.serviceInstance) && !_.isNil(this._store.getState().service.serviceInstance[serviceModelId])) {
      const vnfs = this._store.getState().service.serviceInstance[serviceModelId].vnfs;

      for (let vnfKey in vnfs) {
        count += this.countNumberOfVFModule(vnfs[vnfKey], node);
      }
      return count;
    }
    return count;
  }


  /***********************************************************
   * should show node icon
   * @param node - current ITrees node
   * @param serviceModelId - service id
   ************************************************************/
  showNodeIcons(node: ITreeNode, serviceModelId: string): AvailableNodeIcons {
    const selectedVNF: string = this._sharedTreeService.getSelectedVNF();
    if (selectedVNF) {
      return this.showVFModuleOnSelectedVNF(node, selectedVNF, serviceModelId);
    } else {
      const optionalSelected = this.getOptionalVNFs(serviceModelId, node.parent.data.name);
      if (optionalSelected.length === 1) {
        return this.showVFModuleOnSelectedVNF(node, optionalSelected[0].vnfStoreKey, serviceModelId);
      } else {
        return new AvailableNodeIcons(false, false);
      }
    }
  }


  showVFModuleOnSelectedVNF(node: ITreeNode, selectedVNF: string, serviceModelId: string): AvailableNodeIcons {

    if (!_.isNil(this._store.getState().service.serviceInstance[serviceModelId].vnfs[selectedVNF]) && node.parent.data.name === this._store.getState().service.serviceInstance[serviceModelId].vnfs[selectedVNF].originalName) {
      const existingVFModules = this.getCountVFModuleOfSelectedVNF(node, selectedVNF, serviceModelId);
      const reachedLimit = this.isVFModuleReachedLimit(node, this._store.getState().service.serviceHierarchy, serviceModelId, existingVFModules);
      const showAddIcon = this._sharedTreeService.shouldShowAddIcon()&& !reachedLimit;
      return new AvailableNodeIcons(showAddIcon, reachedLimit);
    }
    return new AvailableNodeIcons(false, false);

  }

  getOptionalVNFs(serviceUUID: string, vnfOriginalModelName: string): any[] {
    let result = [];
    if (!_.isNil(this._store.getState().service.serviceInstance) && !_.isNil(this._store.getState().service.serviceInstance[serviceUUID])) {
      const serviceVNFsInstances = this._store.getState().service.serviceInstance[serviceUUID].vnfs;
      for (let vnfKey in serviceVNFsInstances) {
        if (serviceVNFsInstances[vnfKey].originalName === vnfOriginalModelName) {
          serviceVNFsInstances[vnfKey].vnfStoreKey = vnfKey;
          result.push(serviceVNFsInstances[vnfKey]);
        }
      }
    }
    return result;
  }


  /************************************************
   return number of instances with action Delete
   @type: vnfs networks, vngGroups (only vfModule)
   @node : node model from the left tree
   ************************************************/
  getExistingInstancesWithDeleteMode(node, serviceModelId: string, selectedVNF: string): number {
    const existingInstances = this._store.getState().service.serviceInstance[serviceModelId].vnfs[selectedVNF].vfModules;
    let counter = 0;
    const modelUuid = node.data.uuid;

    if (!_.isNil(existingInstances)) {
      for (let instanceKey in existingInstances) {
        let size = Object.keys(existingInstances[instanceKey]).length;
        for (let i = 0; i < size; i++) {
          let vfModuleDynamicKey = Object.keys(existingInstances[instanceKey])[i];
          if (!_.isNil(existingInstances[instanceKey][vfModuleDynamicKey].action)) {
            if (existingInstances[instanceKey][vfModuleDynamicKey]['uuid'] === modelUuid && existingInstances[instanceKey][vfModuleDynamicKey].action.split('_').pop() === 'Delete') {
              counter++;
            }
          }
        }

      }
    }
    return counter;
  }

  isVFModuleReachedLimit(node: any, serviceHierarchy: any, serviceModelId: string, currentNodeCount: number): boolean {
    let maxNodes: number = 1;
    let vnfModules = serviceHierarchy[serviceModelId].vfModules;
    if (vnfModules[node.data.name]) {
      maxNodes = vnfModules[node.data.name].properties.maxCountInstances || 1;
    }
    return !(maxNodes > currentNodeCount);

  }

  getMenuAction(node: ITreeNode, serviceModelId: string): { [methodName: string]: { method: Function, visible: Function, enable: Function } } {
    return {
      edit: {
        method: (node, serviceModelId) => {
          this._iframeService.addClassOpenModal('content');
          this._dialogService.addDialog(GenericFormPopupComponent, {
            type: PopupType.VF_MODULE,
            uuidData: <any>{
              serviceId: serviceModelId,
              modelName: node.data.modelName,
              vFModuleStoreKey: node.data.dynamicModelName,
              vnfStoreKey: node.parent.data.vnfStoreKey,
              modelId: node.data.modelId,
              type: node.data.type,
              popupService: this._vfModulePopupService
            },
            node: node,
            isUpdateMode: true
          });
        },
        visible: (node) => this._sharedTreeService.shouldShowRemoveAndEdit(node),
        enable: (node) => this._sharedTreeService.shouldShowRemoveAndEdit(node),
      },
      showAuditInfo: {
        method: (node, serviceModelId) => {
          let instance = this._store.getState().service.serviceInstance[serviceModelId].vnfs[node.parent.data.vnfStoreKey].vfModules[node.data.modelName][node.data.modelVersionId];
          this._sharedTreeService.openAuditInfoModal(node, serviceModelId, instance, 'VFMODULE', this);
        },
        visible: (node) => this._sharedTreeService.shouldShowAuditInfo(node),
        enable: (node) => this._sharedTreeService.shouldShowAuditInfo(node)
      },
      remove: {
        method: (node, serviceModelId) => this._store.dispatch(removeVfModuleInstance(node.data.modelName, serviceModelId, node.parent.data.modelName, node.parent.data.vnfStoreKey, node.data.dynamicModelName)),
        visible: (node) => this._sharedTreeService.shouldShowRemoveAndEdit(node),
        enable: (node) => this._sharedTreeService.shouldShowRemoveAndEdit(node),
      },
      delete: {
        method: (node, serviceModelId) => {
          this._store.dispatch(deleteActionVfModuleInstance(node.data.dynamicModelName, node.parent.data.vnfStoreKey, serviceModelId))
        },
        visible: (node) => this._sharedTreeService.shouldShowDelete(node),
        enable: (node) => this._sharedTreeService.shouldShowDelete(node)
      },
      undoDelete: {
        method: (node, serviceModelId) => {
          this._store.dispatch(undoDeleteVfModuleInstance(node.data.dynamicModelName, node.parent.data.vnfStoreKey, serviceModelId))
        },
        visible: (node) => this._sharedTreeService.shouldShowUndoDelete(node),
        enable: (node, serviceModelId) => this._sharedTreeService.shouldShowUndoDelete(node) && this._sharedTreeService.shouldShowDelete(node.parent) && !this._sharedTreeService.isServiceOnDeleteMode(serviceModelId)
      },
      upgrade : {
        method : (node, serviceModelId) => {
          this._sharedTreeService.upgradeBottomUp(node, serviceModelId);
          this._store.dispatch(upgradeVFModule(node.data.modelName,  node.parent.data.vnfStoreKey, serviceModelId, node.data.dynamicModelName));
        },
        visible: (node,serviceModelId) => {
          return this._sharedTreeService.shouldShowUpgrade(node, serviceModelId);
        },
        enable:  (node, serviceModelId) => {
          return this._sharedTreeService.shouldShowUpgrade(node, serviceModelId);
        }
      },
      undoUpgrade: {
        method: (node, serviceModelId) => {
          this._sharedTreeService.undoUpgradeBottomUp(node, serviceModelId);
          this._store.dispatch(undoUgradeVFModule(node.data.modelName,  node.parent.data.vnfStoreKey, serviceModelId, node.data.dynamicModelName));
        },
        visible: (node) => {
          return this._sharedTreeService.shouldShowUndoUpgrade(node);
        },
        enable: (node) => {
          return this._sharedTreeService.shouldShowUndoUpgrade(node);
        }
      },
    };
  }

  updatePosition(that , node, instanceId, parentStoreKey): void {
    that.store.dispatch(updateVFModulePosition(node, instanceId, parentStoreKey));
  }


  getNodePosition(instance, deepDynamicName): number {
    return (!_.isNil(instance) && !_.isNil(instance[deepDynamicName])) ? instance[deepDynamicName].position : null;
  }

  getInfo(model, instance): ModelInformationItem[] {
    const modelInformation = !_.isEmpty(model) && !_.isEmpty(model.properties) ? [
      ModelInformationItem.createInstance("Base module", model.properties.baseModule),
      ModelInformationItem.createInstance("Min instances", !_.isNull(model.properties.minCountInstances)? String(model.properties.minCountInstances): null),
      ModelInformationItem.createInstance("Max instances", !_.isNull(model.properties.maxCountInstances)? String(model.properties.maxCountInstances): null),
      ModelInformationItem.createInstance("Initial instances count", !_.isNull(model.properties.initialCount)? String(model.properties.initialCount): null)
    ] : [];

    const instanceInfo =  [];
    const result = [modelInformation, instanceInfo];
    return _.uniq(_.flatten(result));
  }
}