From 6f900cc45d7dd7f97430812b86b5c1d1693c8ae3 Mon Sep 17 00:00:00 2001 From: Ittay Stern Date: Wed, 29 Aug 2018 17:01:32 +0300 Subject: merge from ecomp a88f0072 - Modern UI Issue-ID: VID-378 Change-Id: Ibcb23dd27f550cf32ce2fe0239f0f496ae014ff6 Signed-off-by: Ittay Stern --- .../duplicate/duplicate.service.ts | 189 +++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 vid-webpack-master/src/app/drawingBoard/service-planning/duplicate/duplicate.service.ts (limited to 'vid-webpack-master/src/app/drawingBoard/service-planning/duplicate/duplicate.service.ts') diff --git a/vid-webpack-master/src/app/drawingBoard/service-planning/duplicate/duplicate.service.ts b/vid-webpack-master/src/app/drawingBoard/service-planning/duplicate/duplicate.service.ts new file mode 100644 index 000000000..e695edf86 --- /dev/null +++ b/vid-webpack-master/src/app/drawingBoard/service-planning/duplicate/duplicate.service.ts @@ -0,0 +1,189 @@ +import { Injectable } from '@angular/core'; +import { ITreeNode } from 'angular-tree-component/dist/defs/api'; +import { AppState } from '../../../shared/store/reducers'; +import { LogService } from '../../../shared/utils/log/log.service'; +import { NgRedux } from '@angular-redux/store'; +import {VnfInstance} from "../../../shared/models/vnfInstance"; +import {VfModuleMap} from "../../../shared/models/vfModulesMap"; +import * as _ from "lodash"; +import {DefaultDataGeneratorService} from "../../../shared/services/defaultDataServiceGenerator/default.data.generator.service"; +import {TypeNodeInformation} from "../typeNodeInformation.model"; +import { SdcUiServices} from "onap-ui-angular"; +import { SdcUiCommon} from "onap-ui-angular"; +import {changeInstanceCounter, duplicateBulkInstances} from "../../../shared/storeUtil/utils/general/general.actions"; +import {IModalConfig} from "onap-ui-angular/dist/modals/models/modal-config"; + +@Injectable() +export class DuplicateService { + + constructor(private _logService : LogService, private _store: NgRedux, modalService: SdcUiServices.ModalService) { + this.modalService = modalService; + } + + numberOfDuplicates:number; + setNumberOfDuplicates(numberOfDuplicates: number) { + this.numberOfDuplicates = numberOfDuplicates; + } + + currentInstanceId: string = null; + currentServiceId: string = null; + maxNumberOfDuplicate: number = 0; + storeKey: string = null; + padding = '0000'; + modalService: SdcUiServices.ModalService; + store : NgRedux; + existingNames : {[key: string] : any}; + currentNode : ITreeNode = null; + + + + canDuplicate(node: ITreeNode): boolean { + let reduxState = JSON.parse(sessionStorage.getItem('reduxState')); + return reduxState.global.flags['FLAG_DUPLICATE_VNF'] && (node.data.type === 'VF' || node.data.type === 'VL'); + } + + isEnabled(node: ITreeNode, store: NgRedux, serviceId : string): boolean { + if(!_.isNil(node) && !_.isNil(node.data.menuActions['duplicate'])){ + if(this.hasMissingData(node)) return false; + const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(node); + const max : number = store.getState().service.serviceHierarchy[serviceId][typeNodeInformation.hierarchyName][node.data.modelName].properties['max_instances'] || 1; + const currentExisting: number = store.getState().service.serviceInstance[serviceId][typeNodeInformation.existingMappingCounterName][node.data.modelUniqueId]; + + return max - currentExisting > 0; + }else { + return false; + } + } + + hasMissingData(node : ITreeNode): boolean { + if(!_.isNil(node)){ + if(node.data.missingData) return true; + if(!_.isNil(node.data.children)){ + for(let child of node.data.children) { + if(child.missingData){ + return true; + } + } + } + + } + return false; + } + + getRemainsInstance(modelId : string, modelName : string, serviceId : string, store: NgRedux, node : ITreeNode) : number { + const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(node); + const properties = store.getState().service.serviceHierarchy[serviceId][typeNodeInformation.hierarchyName][modelName].properties; + const currentExisting : number = store.getState().service.serviceInstance[serviceId][typeNodeInformation.existingMappingCounterName][modelId]; + return (!_.isNil(properties) && !_.isNil(properties['max_instances'])) ? properties['max_instances'] - currentExisting : null; + } + + + + openDuplicateModal(currentServiceId: string, currentUuid: string, currentId: string, storeKey : string, numberOfDuplicate: number, _store : NgRedux, node: ITreeNode): IModalConfig { + this.currentInstanceId = currentId; + this.currentServiceId = currentServiceId; + this.maxNumberOfDuplicate = this.getRemainsInstance(currentUuid, currentId, currentServiceId, _store, node); + this.storeKey = storeKey; + this.store = _store; + this.currentNode = node; + + + return { + size: SdcUiCommon.ModalSize.medium, + title: 'Duplicate Node', + type: SdcUiCommon.ModalType.custom, + buttons: [ + {text: 'Duplicate', callback: this.duplicate.bind(this, this.currentNode), closeModal: true}, + {text: 'Cancel', closeModal: true} + ] + }; + } + + duplicate(node : ITreeNode): void { + const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(node); + this.existingNames = this.store.getState().service.serviceInstance[this.currentServiceId].existingNames; + const toClone = this.store.getState().service.serviceInstance[this.currentServiceId][typeNodeInformation.hierarchyName][this.storeKey]; + let newObjects = {}; + for(let i = 0; i < this.numberOfDuplicates; i++) { + const uniqueStoreKey = this.generateUniqueStoreKey(this.currentServiceId, this.currentInstanceId, this.store.getState().service.serviceInstance[this.currentServiceId][typeNodeInformation.hierarchyName], newObjects); + const clone = this.cloneVnf(toClone, this.currentInstanceId); + newObjects[uniqueStoreKey] = clone; + } + this.store.dispatch(duplicateBulkInstances(this.currentServiceId, newObjects, this.existingNames, node)); + this.store.dispatch(changeInstanceCounter(toClone.modelInfo.modelUniqueId, this.currentServiceId, this.numberOfDuplicates, node)); + this._logService.info("Duplicate " + this.storeKey + " serviceId: " + this.currentServiceId + "number of duplicate: " + this.numberOfDuplicates, toClone); + } + + + cloneVnf(vnf : VnfInstance, originalName: string): VnfInstance { + let newUniqueVnf : VnfInstance = _.cloneDeep(vnf); + + newUniqueVnf.originalName = originalName; + newUniqueVnf.trackById = DefaultDataGeneratorService.createRandomTrackById(); + if (!_.isNil(vnf.instanceName)){ + newUniqueVnf.instanceName = this.ensureUniqueNameOrGenerateOne(vnf.instanceName); + } + + for (let vf_module_model_name in vnf.vfModules) { + const vfModuleModel: VfModuleMap = vnf.vfModules[vf_module_model_name]; + for (let vfModule in vfModuleModel) { + newUniqueVnf.vfModules[vf_module_model_name][vfModule].trackById = DefaultDataGeneratorService.createRandomTrackById(); + if (!_.isNil(vfModuleModel[vfModule].instanceName)){ + newUniqueVnf.vfModules[vf_module_model_name][vfModule].instanceName = this.ensureUniqueNameOrGenerateOne(vfModuleModel[vfModule].instanceName); + } + if (!_.isNil(vfModuleModel[vfModule].volumeGroupName)){ + newUniqueVnf.vfModules[vf_module_model_name][vfModule].volumeGroupName = this.ensureUniqueNameOrGenerateOne(vfModuleModel[vfModule].volumeGroupName); + } + } + } + return newUniqueVnf; + } + + ensureUniqueNameOrGenerateOne(instanceName){ + let uniqueInstanceName = instanceName; + if (this.isAlreadyExists(instanceName, this.existingNames)) { + uniqueInstanceName = this.generateNextUniqueName(instanceName, this.existingNames); + this.existingNames[uniqueInstanceName.toLowerCase()] = ""; + } + return uniqueInstanceName; + } + + + isAlreadyExists(name : string, existingNames : {[key: string] : any}){ + return _.has(existingNames, name.toLowerCase()); + } + + generateNextUniqueName(name : string, existingNames : {[key: string] : any}) :string{ + let suffix = "000"; + let counter = 1; + if (name.match(/^.*_[\d]{3}$/)){ + name = name.substring(0, name.length - 4); + } + + while(true){ + let paddingNumber : string = this.getNumberAsPaddingString(counter, suffix); + let candidateUniqueName = name + '_' + paddingNumber; + if(!this.isAlreadyExists(candidateUniqueName, existingNames)){ + return candidateUniqueName; + } + counter++; + } + } + + generateUniqueStoreKey(serviceId : string, objectName : string, existing : any, newObjects: any) : string { + let counter = 1; + while(true){ + let paddingNumber : string = this.getNumberAsPaddingString(counter, this.padding); + const name = objectName + ':' + paddingNumber; + if(_.isNil(existing[name]) && _.isNil(newObjects[name])){ + return name; + } + counter++; + } + } + + getNumberAsPaddingString(val: number, padding: string): string { + const str = "" + val; + return padding.substring(0, padding.length - str.length) + str; + } +} -- cgit 1.2.3-korg