Ittay Stern | f792671 | 2019-07-07 19:23:03 +0300 | [diff] [blame^] | 1 | import {Injectable} from '@angular/core'; |
| 2 | import {ITreeNode} from 'angular-tree-component/dist/defs/api'; |
| 3 | import {AppState} from '../../../shared/store/reducers'; |
| 4 | import {LogService} from '../../../shared/utils/log/log.service'; |
| 5 | import {NgRedux} from '@angular-redux/store'; |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 6 | import {VnfInstance} from "../../../shared/models/vnfInstance"; |
| 7 | import {VfModuleMap} from "../../../shared/models/vfModulesMap"; |
| 8 | import * as _ from "lodash"; |
| 9 | import {DefaultDataGeneratorService} from "../../../shared/services/defaultDataServiceGenerator/default.data.generator.service"; |
| 10 | import {TypeNodeInformation} from "../typeNodeInformation.model"; |
Ittay Stern | f792671 | 2019-07-07 19:23:03 +0300 | [diff] [blame^] | 11 | import {SdcUiCommon, SdcUiServices} from "onap-ui-angular"; |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 12 | import {changeInstanceCounter, duplicateBulkInstances} from "../../../shared/storeUtil/utils/general/general.actions"; |
| 13 | import {IModalConfig} from "onap-ui-angular/dist/modals/models/modal-config"; |
| 14 | |
| 15 | @Injectable() |
| 16 | export class DuplicateService { |
| 17 | |
| 18 | constructor(private _logService : LogService, private _store: NgRedux<AppState>, modalService: SdcUiServices.ModalService) { |
| 19 | this.modalService = modalService; |
| 20 | } |
| 21 | |
| 22 | numberOfDuplicates:number; |
| 23 | setNumberOfDuplicates(numberOfDuplicates: number) { |
| 24 | this.numberOfDuplicates = numberOfDuplicates; |
| 25 | } |
| 26 | |
| 27 | currentInstanceId: string = null; |
| 28 | currentServiceId: string = null; |
| 29 | maxNumberOfDuplicate: number = 0; |
| 30 | storeKey: string = null; |
| 31 | padding = '0000'; |
| 32 | modalService: SdcUiServices.ModalService; |
| 33 | store : NgRedux<AppState>; |
| 34 | existingNames : {[key: string] : any}; |
| 35 | currentNode : ITreeNode = null; |
| 36 | |
| 37 | |
| 38 | |
| 39 | canDuplicate(node: ITreeNode): boolean { |
| 40 | let reduxState = <AppState>JSON.parse(sessionStorage.getItem('reduxState')); |
Ittay Stern | f792671 | 2019-07-07 19:23:03 +0300 | [diff] [blame^] | 41 | return node.data.type === 'VF' || node.data.type === 'VL'; |
Ittay Stern | 6f900cc | 2018-08-29 17:01:32 +0300 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | isEnabled(node: ITreeNode, store: NgRedux<AppState>, serviceId : string): boolean { |
| 45 | if(!_.isNil(node) && !_.isNil(node.data.menuActions['duplicate'])){ |
| 46 | if(this.hasMissingData(node)) return false; |
| 47 | const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(node); |
| 48 | const max : number = store.getState().service.serviceHierarchy[serviceId][typeNodeInformation.hierarchyName][node.data.modelName].properties['max_instances'] || 1; |
| 49 | const currentExisting: number = store.getState().service.serviceInstance[serviceId][typeNodeInformation.existingMappingCounterName][node.data.modelUniqueId]; |
| 50 | |
| 51 | return max - currentExisting > 0; |
| 52 | }else { |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | hasMissingData(node : ITreeNode): boolean { |
| 58 | if(!_.isNil(node)){ |
| 59 | if(node.data.missingData) return true; |
| 60 | if(!_.isNil(node.data.children)){ |
| 61 | for(let child of node.data.children) { |
| 62 | if(child.missingData){ |
| 63 | return true; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | getRemainsInstance(modelId : string, modelName : string, serviceId : string, store: NgRedux<AppState>, node : ITreeNode) : number { |
| 73 | const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(node); |
| 74 | const properties = store.getState().service.serviceHierarchy[serviceId][typeNodeInformation.hierarchyName][modelName].properties; |
| 75 | const currentExisting : number = store.getState().service.serviceInstance[serviceId][typeNodeInformation.existingMappingCounterName][modelId]; |
| 76 | return (!_.isNil(properties) && !_.isNil(properties['max_instances'])) ? properties['max_instances'] - currentExisting : null; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | openDuplicateModal(currentServiceId: string, currentUuid: string, currentId: string, storeKey : string, numberOfDuplicate: number, _store : NgRedux<AppState>, node: ITreeNode): IModalConfig { |
| 82 | this.currentInstanceId = currentId; |
| 83 | this.currentServiceId = currentServiceId; |
| 84 | this.maxNumberOfDuplicate = this.getRemainsInstance(currentUuid, currentId, currentServiceId, _store, node); |
| 85 | this.storeKey = storeKey; |
| 86 | this.store = _store; |
| 87 | this.currentNode = node; |
| 88 | |
| 89 | |
| 90 | return { |
| 91 | size: SdcUiCommon.ModalSize.medium, |
| 92 | title: 'Duplicate Node', |
| 93 | type: SdcUiCommon.ModalType.custom, |
| 94 | buttons: [ |
| 95 | {text: 'Duplicate', callback: this.duplicate.bind(this, this.currentNode), closeModal: true}, |
| 96 | {text: 'Cancel', closeModal: true} |
| 97 | ] |
| 98 | }; |
| 99 | } |
| 100 | |
| 101 | duplicate(node : ITreeNode): void { |
| 102 | const typeNodeInformation : TypeNodeInformation = new TypeNodeInformation(node); |
| 103 | this.existingNames = this.store.getState().service.serviceInstance[this.currentServiceId].existingNames; |
| 104 | const toClone = this.store.getState().service.serviceInstance[this.currentServiceId][typeNodeInformation.hierarchyName][this.storeKey]; |
| 105 | let newObjects = {}; |
| 106 | for(let i = 0; i < this.numberOfDuplicates; i++) { |
| 107 | const uniqueStoreKey = this.generateUniqueStoreKey(this.currentServiceId, this.currentInstanceId, this.store.getState().service.serviceInstance[this.currentServiceId][typeNodeInformation.hierarchyName], newObjects); |
| 108 | const clone = this.cloneVnf(toClone, this.currentInstanceId); |
| 109 | newObjects[uniqueStoreKey] = clone; |
| 110 | } |
| 111 | this.store.dispatch(duplicateBulkInstances(this.currentServiceId, newObjects, this.existingNames, node)); |
| 112 | this.store.dispatch(changeInstanceCounter(toClone.modelInfo.modelUniqueId, this.currentServiceId, this.numberOfDuplicates, node)); |
| 113 | this._logService.info("Duplicate " + this.storeKey + " serviceId: " + this.currentServiceId + "number of duplicate: " + this.numberOfDuplicates, toClone); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | cloneVnf(vnf : VnfInstance, originalName: string): VnfInstance { |
| 118 | let newUniqueVnf : VnfInstance = _.cloneDeep(vnf); |
| 119 | |
| 120 | newUniqueVnf.originalName = originalName; |
| 121 | newUniqueVnf.trackById = DefaultDataGeneratorService.createRandomTrackById(); |
| 122 | if (!_.isNil(vnf.instanceName)){ |
| 123 | newUniqueVnf.instanceName = this.ensureUniqueNameOrGenerateOne(vnf.instanceName); |
| 124 | } |
| 125 | |
| 126 | for (let vf_module_model_name in vnf.vfModules) { |
| 127 | const vfModuleModel: VfModuleMap = vnf.vfModules[vf_module_model_name]; |
| 128 | for (let vfModule in vfModuleModel) { |
| 129 | newUniqueVnf.vfModules[vf_module_model_name][vfModule].trackById = DefaultDataGeneratorService.createRandomTrackById(); |
| 130 | if (!_.isNil(vfModuleModel[vfModule].instanceName)){ |
| 131 | newUniqueVnf.vfModules[vf_module_model_name][vfModule].instanceName = this.ensureUniqueNameOrGenerateOne(vfModuleModel[vfModule].instanceName); |
| 132 | } |
| 133 | if (!_.isNil(vfModuleModel[vfModule].volumeGroupName)){ |
| 134 | newUniqueVnf.vfModules[vf_module_model_name][vfModule].volumeGroupName = this.ensureUniqueNameOrGenerateOne(vfModuleModel[vfModule].volumeGroupName); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | return newUniqueVnf; |
| 139 | } |
| 140 | |
| 141 | ensureUniqueNameOrGenerateOne(instanceName){ |
| 142 | let uniqueInstanceName = instanceName; |
| 143 | if (this.isAlreadyExists(instanceName, this.existingNames)) { |
| 144 | uniqueInstanceName = this.generateNextUniqueName(instanceName, this.existingNames); |
| 145 | this.existingNames[uniqueInstanceName.toLowerCase()] = ""; |
| 146 | } |
| 147 | return uniqueInstanceName; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | isAlreadyExists(name : string, existingNames : {[key: string] : any}){ |
| 152 | return _.has(existingNames, name.toLowerCase()); |
| 153 | } |
| 154 | |
| 155 | generateNextUniqueName(name : string, existingNames : {[key: string] : any}) :string{ |
| 156 | let suffix = "000"; |
| 157 | let counter = 1; |
| 158 | if (name.match(/^.*_[\d]{3}$/)){ |
| 159 | name = name.substring(0, name.length - 4); |
| 160 | } |
| 161 | |
| 162 | while(true){ |
| 163 | let paddingNumber : string = this.getNumberAsPaddingString(counter, suffix); |
| 164 | let candidateUniqueName = name + '_' + paddingNumber; |
| 165 | if(!this.isAlreadyExists(candidateUniqueName, existingNames)){ |
| 166 | return candidateUniqueName; |
| 167 | } |
| 168 | counter++; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | generateUniqueStoreKey(serviceId : string, objectName : string, existing : any, newObjects: any) : string { |
| 173 | let counter = 1; |
| 174 | while(true){ |
| 175 | let paddingNumber : string = this.getNumberAsPaddingString(counter, this.padding); |
| 176 | const name = objectName + ':' + paddingNumber; |
| 177 | if(_.isNil(existing[name]) && _.isNil(newObjects[name])){ |
| 178 | return name; |
| 179 | } |
| 180 | counter++; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | getNumberAsPaddingString(val: number, padding: string): string { |
| 185 | const str = "" + val; |
| 186 | return padding.substring(0, padding.length - str.length) + str; |
| 187 | } |
| 188 | } |