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';
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";
11 import {SdcUiCommon, SdcUiServices} from "onap-ui-angular";
12 import {changeInstanceCounter, duplicateBulkInstances} from "../../../shared/storeUtil/utils/general/general.actions";
13 import {IModalConfig} from "onap-ui-angular/dist/modals/models/modal-config";
16 export class DuplicateService {
18 constructor(private _logService : LogService, private _store: NgRedux<AppState>, modalService: SdcUiServices.ModalService) {
19 this.modalService = modalService;
22 numberOfDuplicates:number;
23 setNumberOfDuplicates(numberOfDuplicates: number) {
24 this.numberOfDuplicates = numberOfDuplicates;
27 currentInstanceId: string = null;
28 currentServiceId: string = null;
29 maxNumberOfDuplicate: number = 0;
30 storeKey: string = null;
32 modalService: SdcUiServices.ModalService;
33 store : NgRedux<AppState>;
34 existingNames : {[key: string] : any};
35 currentNode : ITreeNode = null;
39 canDuplicate(node: ITreeNode): boolean {
40 let reduxState = <AppState>JSON.parse(sessionStorage.getItem('reduxState'));
41 return node.data.type === 'VF' || node.data.type === 'VL';
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];
51 return max - currentExisting > 0;
57 hasMissingData(node : ITreeNode): boolean {
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){
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;
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;
87 this.currentNode = node;
91 size: SdcUiCommon.ModalSize.medium,
92 title: 'Duplicate Node',
93 type: SdcUiCommon.ModalType.custom,
95 {text: 'Duplicate', callback: this.duplicate.bind(this, this.currentNode), closeModal: true},
96 {text: 'Cancel', closeModal: true}
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];
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;
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);
117 cloneVnf(vnf : VnfInstance, originalName: string): VnfInstance {
118 let newUniqueVnf : VnfInstance = _.cloneDeep(vnf);
120 newUniqueVnf.originalName = originalName;
121 newUniqueVnf.trackById = DefaultDataGeneratorService.createRandomTrackById();
122 if (!_.isNil(vnf.instanceName)){
123 newUniqueVnf.instanceName = this.ensureUniqueNameOrGenerateOne(vnf.instanceName);
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);
133 if (!_.isNil(vfModuleModel[vfModule].volumeGroupName)){
134 newUniqueVnf.vfModules[vf_module_model_name][vfModule].volumeGroupName = this.ensureUniqueNameOrGenerateOne(vfModuleModel[vfModule].volumeGroupName);
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()] = "";
147 return uniqueInstanceName;
151 isAlreadyExists(name : string, existingNames : {[key: string] : any}){
152 return _.has(existingNames, name.toLowerCase());
155 generateNextUniqueName(name : string, existingNames : {[key: string] : any}) :string{
158 if (name.match(/^.*_[\d]{3}$/)){
159 name = name.substring(0, name.length - 4);
163 let paddingNumber : string = this.getNumberAsPaddingString(counter, suffix);
164 let candidateUniqueName = name + '_' + paddingNumber;
165 if(!this.isAlreadyExists(candidateUniqueName, existingNames)){
166 return candidateUniqueName;
172 generateUniqueStoreKey(serviceId : string, objectName : string, existing : any, newObjects: any) : string {
175 let paddingNumber : string = this.getNumberAsPaddingString(counter, this.padding);
176 const name = objectName + ':' + paddingNumber;
177 if(_.isNil(existing[name]) && _.isNil(newObjects[name])){
184 getNumberAsPaddingString(val: number, padding: string): string {
185 const str = "" + val;
186 return padding.substring(0, padding.length - str.length) + str;