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