From 4c87ecaa8cce40f078f8540a15928e7e805e4ef7 Mon Sep 17 00:00:00 2001 From: Yoav Schneiderman Date: Wed, 18 Dec 2019 21:04:38 +0200 Subject: Refactor Generic Generators Issue-ID: VID-731 Signed-off-by: Yoav Schneiderman Change-Id: Ib851ff4056d64e12eaf415c5d689c832196ec8b0 Signed-off-by: Yoav Schneiderman --- .../basic.control.generator.spec.ts | 108 ------- .../basic.control.generator.ts | 310 --------------------- .../control.generator.util.service.spec.ts | 112 ++++++++ .../control.generator.util.service.ts | 252 +++++++++++++++++ .../network.control.generator.spec.ts | 6 +- .../networkGenerator/network.control.generator.ts | 181 ++---------- .../service.control.generator.spec.ts | 6 +- .../service.control.generator.ts | 10 +- .../shared.controllers.service.spec.ts | 263 +++++++++++++++++ .../sharedControlles/shared.controllers.service.ts | 189 +++++++++++++ .../vfModule.control.generator.spec.ts | 6 +- .../vfModule.control.generator.ts | 137 +-------- .../vnfGenerator/vnf.control.generator.spec.ts | 18 +- .../vnfGenerator/vnf.control.generator.ts | 173 ++---------- .../vnfGroup.control.generator.spec.ts | 6 +- .../vnfGroup.control.generator.ts | 24 +- 16 files changed, 909 insertions(+), 892 deletions(-) delete mode 100644 vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts delete mode 100644 vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts create mode 100644 vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/control.generator.util.service.spec.ts create mode 100644 vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/control.generator.util.service.ts create mode 100644 vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.spec.ts create mode 100644 vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts (limited to 'vid-webpack-master/src/app/shared/components/genericForm') diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts deleted file mode 100644 index 7e2d3f942..000000000 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.spec.ts +++ /dev/null @@ -1,108 +0,0 @@ -import {getTestBed, TestBed} from '@angular/core/testing'; -import {AaiService} from "../../../services/aaiService/aai.service"; -import {FormControlModel} from "../../../models/formControlModels/formControl.model"; -import {FeatureFlagsService} from "../../../services/featureFlag/feature-flags.service"; -import {BasicControlGenerator} from "./basic.control.generator"; -import {NgRedux} from '@angular-redux/store'; -import each from "jest-each"; -import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; -import {FileFormControl} from "../../../models/formControlModels/fileFormControl.model"; - -class MockAppStore {} - -class MockFeatureFlagsService {} - -describe('Basic Control Generator', () => { - let injector; - let service: BasicControlGenerator; - let httpMock: HttpTestingController; - - - beforeAll(done => (async () => { - TestBed.configureTestingModule({ - imports: [HttpClientTestingModule], - providers: [BasicControlGenerator, - AaiService, - {provide:FeatureFlagsService, useClass: MockFeatureFlagsService}, - {provide: NgRedux, useClass: MockAppStore}] - }); - await TestBed.compileComponents(); - - injector = getTestBed(); - service = injector.get(BasicControlGenerator); - httpMock = injector.get(HttpTestingController); - - })().then(done).catch(done.fail)); - - - test('getlegacyRegion with AAIAIC25 - isVisible true', () => { - const instance = {lcpCloudRegionId : 'AAIAIC25'}; - const legacyRegionControl: FormControlModel = service.getLegacyRegion(instance); - expect(legacyRegionControl.isVisible).toBeTruthy(); - }); - - test('getlegacyRegion without AAIAIC25 - isVisible false', () => { - const instance = {lcpCloudRegionId : 'olson3'}; - const legacyRegionControl: FormControlModel = service.getLegacyRegion(instance); - expect(legacyRegionControl.isVisible).toBeFalsy(); - }); - - test('sdn-preload checkbox is visible', () => { - const instance = {}; - const sdncPreload: FormControlModel = service.getSDNCControl(instance); - expect (sdncPreload.displayName).toEqual('SDN-C pre-load'); - expect (sdncPreload.value).toBeFalsy(); - }); - - test('given instance, get supp file from getSupplementaryFile ', () => { - const instance = {}; - const suppFileForInstance: FileFormControl = service.getSupplementaryFile(instance); - expect(suppFileForInstance.isVisible).toBeTruthy(); - expect(suppFileForInstance.hiddenFile.length).toBeGreaterThanOrEqual(1); - expect(suppFileForInstance.hiddenFile[0].validations[0].validatorName).toEqual("isFileTooBig"); - }); - - test('concatSupplementaryFile add SupplementaryFile control and hidden file', () => { - - //given - const instance = {}; - const controls = [service.getLegacyRegion(instance)]; - expect(controls).toHaveLength(1); - - //when - const result = service.concatSupplementaryFile(controls, instance); - - //then - expect(controls).toHaveLength(1); //original controls remain the same - - expect(result.map((control) => {return control.controlName})).toEqual([ - "legacyRegion", - "supplementaryFile", - "supplementaryFile_hidden", - "supplementaryFile_hidden_content" - ]); - }); - each([ - [null, false], - [{}, true] - ]). - test('retrieveInstanceIfUpdateMode returns %s if update mode is %s', (expected, isUpdateModalMode) => { - //given - const store= { - getState() { - return { - global: { - isUpdateModalMode - } - }}}; - const instance = {}; - - //when - let retrievedInstance = service.retrieveInstanceIfUpdateMode (store, instance); - - //then - expect(retrievedInstance).toEqual(expected); - - }); - -}); diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts deleted file mode 100644 index aff33982f..000000000 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/basic.control.generator.ts +++ /dev/null @@ -1,310 +0,0 @@ -import {Injectable} from "@angular/core"; -import {DropdownFormControl} from "../../../models/formControlModels/dropdownFormControl.model"; -import {FormGroup} from "@angular/forms"; -import {CustomValidatorOptions, FormControlModel, ValidatorModel, ValidatorOptions} from "../../../models/formControlModels/formControl.model"; -import {InputFormControl} from "../../../models/formControlModels/inputFormControl.model"; -import {AppState} from "../../../store/reducers"; -import {NgRedux} from "@angular-redux/store"; -import {NumberFormControl} from "../../../models/formControlModels/numberFormControl.model"; -import {FormControlType} from "../../../models/formControlModels/formControlTypes.enum"; -import {FileFormControl} from "../../../models/formControlModels/fileFormControl.model"; -import {SelectOption} from "../../../models/selectOption"; -import * as _ from 'lodash'; -import {DynamicInputLabelPipe} from "../../../pipes/dynamicInputLabel/dynamic-input-label.pipe"; -import {AaiService} from "../../../services/aaiService/aai.service"; -import {FormGeneralErrorsService} from "../../formGeneralErrors/formGeneralErrors.service"; -import {Observable, of} from "rxjs"; -import {NodeModel} from "../../../models/nodeModel"; -import {Constants} from "../../../utils/constants"; -import {FileUnit} from "../../formControls/component/file/fileUnit.enum"; -import {CheckboxFormControl} from "../../../models/formControlModels/checkboxFormControl.model"; - -export const SUPPLEMENTARY_FILE = 'supplementaryFile'; -export const SDN_C_PRE_LOAD = 'sdncPreLoad'; - -@Injectable() -export class BasicControlGenerator { - - public static readonly INSTANCE_NAME_REG_EX:RegExp = /^[a-zA-Z0-9._-]*$/; - public static readonly GENERATED_NAME_REG_EX:RegExp = /[^a-zA-Z0-9._-]/g; - - constructor(private _store : NgRedux, - private _aaiService : AaiService){} - getSubscribeResult(subscribeFunction : Function, control : DropdownFormControl) : Observable{ - return subscribeFunction(this).subscribe((res) => { - control.options$ = res; - control.hasEmptyOptions = res.length === 0; - FormGeneralErrorsService.checkForErrorTrigger.next(); - return of(res); - }); - } - - getSubscribeInitResult(subscribeFunction : Function, control : DropdownFormControl, form : FormGroup) : Observable{ - return subscribeFunction(this).subscribe((res) => { - if(!_.isNil(control['onInitSelectedField'])){ - let result = res; - for(let key of control['onInitSelectedField']){ - result = !_.isNil(result[key]) ? result[key] : []; - } - control.options$ = result; - control.hasEmptyOptions = _.isNil(result) || result.length === 0; - } else{ - control.options$ = !_.isNil(res) ? res : []; - control.hasEmptyOptions = _.isNil(res) || res.length === 0; - } - - FormGeneralErrorsService.checkForErrorTrigger.next(); - return of(res); - }); - } - - getInstanceNameController(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, model: NodeModel): FormControlModel { - let validations: ValidatorModel[] = this.createValidationsForInstanceName(instance, serviceId, isEcompGeneratedNaming); - return new InputFormControl({ - controlName: 'instanceName', - displayName: 'Instance name', - dataTestId: 'instanceName', - placeHolder: (!isEcompGeneratedNaming) ? 'Instance name' : 'Automatically generated when not provided', - validations: validations, - isVisible : true, - value : (!isEcompGeneratedNaming || (!_.isNil(instance) && !_.isNil(instance.instanceName))) - ? this.getDefaultInstanceName(instance, model) : null, - onKeypress : (event) => { - const pattern:RegExp = BasicControlGenerator.INSTANCE_NAME_REG_EX; - if(pattern){ - if(!pattern.test(event['key'])){ - event.preventDefault(); - } - } - return event; - } - }); - } - - getInstanceName(instance : any, serviceId : string, isEcompGeneratedNaming: boolean): FormControlModel { - let formControlModel:FormControlModel = this.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, new NodeModel()); - formControlModel.value = instance ? instance.instanceName : null; - return formControlModel; - } - - isLegacyRegionShouldBeVisible(instance : any) : boolean { - if(!_.isNil(instance) && !_.isNil(instance.lcpCloudRegionId)) { - return Constants.LegacyRegion.MEGA_REGION.indexOf(instance.lcpCloudRegionId) !== -1; - } - return false; - } - - getLegacyRegion(instance: any): FormControlModel { - return new InputFormControl({ - controlName: 'legacyRegion', - displayName: 'Legacy Region', - dataTestId: 'lcpRegionText', - placeHolder: 'Type Legacy Region', - validations: [], - isVisible: this.isLegacyRegionShouldBeVisible(instance), - isDisabled : _.isNil(instance) ? true : Constants.LegacyRegion.MEGA_REGION.indexOf(instance.lcpCloudRegionId), - value: instance ? instance.legacyRegion : null - }); - } - - private createValidationsForInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): ValidatorModel[] { - let validations: ValidatorModel[] = [ - new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', BasicControlGenerator.INSTANCE_NAME_REG_EX), - new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'some error', [this._store, serviceId, instance && instance.instanceName]) - ]; - if (!isEcompGeneratedNaming) { - validations.push(new ValidatorModel(ValidatorOptions.required, 'is required')); - } - return validations; - } - - getInputsOptions = (options: any[]) : Observable =>{ - let optionList: SelectOption[] = []; - options.forEach((option) => { - optionList.push(new SelectOption({ - id: option.id || option.name, - name: option.name - })); - }); - return of(optionList); - }; - - getProductFamilyControl = (instance : any, controls : FormControlModel[], isMandatory?: boolean) : DropdownFormControl => { - return new DropdownFormControl({ - type : FormControlType.DROPDOWN, - controlName : 'productFamilyId', - displayName : 'Product family', - dataTestId : 'productFamily', - placeHolder : 'Select Product Family', - isDisabled : false, - name : "product-family-select", - value : instance ? instance.productFamilyId : null, - validations : _.isNil(isMandatory) || isMandatory === true ? [new ValidatorModel(ValidatorOptions.required, 'is required')]: [], - onInit : this.getSubscribeResult.bind(this, this._aaiService.getProductFamilies), - }) - }; - - - - getDynamicInputsByType(dynamicInputs : any, serviceModelId : string, storeKey : string, type: string ) : FormControlModel[] { - let result : FormControlModel[] = []; - if(dynamicInputs) { - let nodeInstance = null; - if (_.has(this._store.getState().service.serviceInstance[serviceModelId][type], storeKey)) { - nodeInstance = Object.assign({}, this._store.getState().service.serviceInstance[serviceModelId][type][storeKey]); - } - result = this.getDynamicInputs(dynamicInputs, nodeInstance); - } - return result; - } - - - getServiceDynamicInputs(dynamicInputs : any, serviceModelId : string) : FormControlModel[] { - let result: FormControlModel[] = []; - if (dynamicInputs) { - let serviceInstance = null; - if (_.has(this._store.getState().service.serviceInstance, serviceModelId)) { - serviceInstance = Object.assign({}, this._store.getState().service.serviceInstance[serviceModelId]); - } - result = this.getDynamicInputs(dynamicInputs, serviceInstance); - } - return result; - } - - getDynamicInputs(dynamicInputs : any, instance :any) : FormControlModel[]{ - let result : FormControlModel[] = []; - if(dynamicInputs) { - dynamicInputs.forEach((input)=> { - let validations: ValidatorModel[] = []; - if(input.isRequired) { - validations.push(new ValidatorModel(ValidatorOptions.required, 'is required')) - } - if(input.minLength) { - validations.push(new ValidatorModel(ValidatorOptions.minLength, '', input.minLength)) - } - if(input.maxLength) { - validations.push(new ValidatorModel(ValidatorOptions.maxLength, '', input.maxLength)) - } - - let dynamicInputLabelPipe: DynamicInputLabelPipe = new DynamicInputLabelPipe(); - let data:any = { - controlName: input.name, - displayName: dynamicInputLabelPipe.transform(input.name).slice(0, -1), - dataTestId: input.id, - placeHolder: input.prompt, - tooltip: input.description, - validations: validations, - isVisible: input.isVisible, - value: !_.isNil(instance) && !_.isNil(instance.instanceParams) && instance.instanceParams.length > 0 ? instance.instanceParams[0][input.name] : input.value - }; - - switch (input.type) { - case 'select' : - case 'boolean' :{ - data.value = data.value || input.optionList.filter((option) => option.isDefault ? option.id || option.name: null); - data.onInit = this.getSubscribeInitResult.bind(null, this.getInputsOptions.bind(this, input.optionList)); - result.push(new DropdownFormControl(data)); - break; - } - case 'checkbox': { - data.type = FormControlType.CHECKBOX; - result.push(new FormControlModel(data)); - break; - } - case 'number': { - data.min = input.min; - data.max = input.max; - result.push(new NumberFormControl(data)); - break; - } - case 'file': { - result.push(new FileFormControl(data)); - break; - } - default: { - result.push(new InputFormControl(data)); - } - } - }) - } - - return result; - } - - getDefaultInstanceName(instance: any, model: NodeModel) : string { - const initialInstanceName = (!_.isNil(instance) && instance.instanceName) || (!_.isNil(model.name) ? model.name.replace(BasicControlGenerator.GENERATED_NAME_REG_EX, "") : model.name); - return initialInstanceName; - } - - concatSupplementaryFile(originalArray: FormControlModel[], vfModuleInstance): FormControlModel[] { - let suppFileInput: FileFormControl = (this.getSupplementaryFile(vfModuleInstance)); - return originalArray.concat([suppFileInput], suppFileInput.hiddenFile); - } - - getSDNCControl = (instance: any): FormControlModel => { - return new CheckboxFormControl({ - controlName: SDN_C_PRE_LOAD, - displayName: 'SDN-C pre-load', - dataTestId: 'sdncPreLoad', - value: instance ? instance.sdncPreLoad : false, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')] - }) - }; - - getSupplementaryFile(instance: any): FileFormControl { - return new FileFormControl({ - controlName: SUPPLEMENTARY_FILE, - displayName: 'Supplementary Data File (JSON format)', - dataTestId: 'SupplementaryFile', - placeHolder: 'Choose file', - selectedFile: !_.isNil(instance) ? instance.supplementaryFileName: null, - isVisible: true, - acceptedExtentions: "application/json", - hiddenFile : [new InputFormControl({ - controlName: SUPPLEMENTARY_FILE + "_hidden", - isVisible: false, - validations: [new ValidatorModel(CustomValidatorOptions.isFileTooBig, "File size exceeds 5MB.", [FileUnit.MB, 5])] - }), - new InputFormControl({ - controlName: SUPPLEMENTARY_FILE + "_hidden_content", - isVisible: false, - validations: [new ValidatorModel(CustomValidatorOptions.isValidJson, - "File is invalid, please make sure a legal JSON file is uploaded using name:value pairs.",[]), - new ValidatorModel(CustomValidatorOptions.isStringContainTags, - "File is invalid, please remove tags <>.",[])], - value: !_.isNil(instance) ? (instance.supplementaryFile_hidden_content): null, - }) - ], - onDelete : this.getOnDeleteForSupplementaryFile(), - onChange : this.getOnChangeForSupplementaryFile() - }) - }; - - retrieveInstanceIfUpdateMode(store: NgRedux, instance: any): any{ - return store.getState().global.isUpdateModalMode ? instance : null; - } - - private getOnDeleteForSupplementaryFile() { - return (form: FormGroup) => { - form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(null); - form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null); - }; - } - - private getOnChangeForSupplementaryFile() { - return (files: FileList, form: FormGroup) => { - if (files.length > 0) { - const file = files.item(0); - let reader = new FileReader(); - reader.onload = function (event) { - form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(reader.result); - form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(file); - }; - reader.readAsText(file); - } else { - form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(null); - form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null); - } - }; - } -} diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/control.generator.util.service.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/control.generator.util.service.spec.ts new file mode 100644 index 000000000..9aefa98b8 --- /dev/null +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/control.generator.util.service.spec.ts @@ -0,0 +1,112 @@ +import {getTestBed, TestBed} from '@angular/core/testing'; +import {AaiService} from "../../../services/aaiService/aai.service"; +import {FeatureFlagsService} from "../../../services/featureFlag/feature-flags.service"; +import {ControlGeneratorUtil} from "./control.generator.util.service"; +import {NgRedux} from '@angular-redux/store'; +import each from "jest-each"; +import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; +import {FileFormControl} from "../../../models/formControlModels/fileFormControl.model"; +import {AppState} from "../../../store/reducers"; +import {SelectOption} from "../../../models/selectOption"; +import {SharedControllersService} from "./sharedControlles/shared.controllers.service"; + +describe('Control Generator Util', () => { + let injector; + let service: ControlGeneratorUtil; + let sharedControllersService : SharedControllersService; + let httpMock: HttpTestingController; + let store: NgRedux; + + + beforeAll(done => (async () => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [ControlGeneratorUtil, + SharedControllersService, + AaiService, + {provide:FeatureFlagsService, useClass: MockFeatureFlagsService}, + {provide: NgRedux, useClass: MockAppStore}] + }); + await TestBed.compileComponents(); + + injector = getTestBed(); + service = injector.get(ControlGeneratorUtil); + httpMock = injector.get(HttpTestingController); + sharedControllersService = injector.get(SharedControllersService); + store = injector.get(NgRedux); + + })().then(done).catch(done.fail)); + + + test('given instance, get supp file from getSupplementaryFile ', () => { + const instance = {}; + const suppFileForInstance: FileFormControl = service.getSupplementaryFile(instance); + expect(suppFileForInstance.isVisible).toBeTruthy(); + expect(suppFileForInstance.hiddenFile.length).toBeGreaterThanOrEqual(1); + expect(suppFileForInstance.hiddenFile[0].validations[0].validatorName).toEqual("isFileTooBig"); + }); + + test('concatSupplementaryFile add SupplementaryFile control and hidden file', () => { + + //given + const instance = {}; + const controls = [sharedControllersService.getLegacyRegion(instance)]; + expect(controls).toHaveLength(1); + + //when + const result = service.concatSupplementaryFile(controls, instance); + + //then + expect(controls).toHaveLength(1); //original controls remain the same + + expect(result.map((control) => {return control.controlName})).toEqual([ + "legacyRegion", + "supplementaryFile", + "supplementaryFile_hidden", + "supplementaryFile_hidden_content" + ]); + }); + each([ + [null, false], + [{}, true] + ]). + test('retrieveInstanceIfUpdateMode returns %s if update mode is %s', (expected, isUpdateModalMode) => { + //given + const store= { + getState() { + return { + global: { + isUpdateModalMode + } + }}}; + const instance = {}; + + //when + let retrievedInstance = service.retrieveInstanceIfUpdateMode (store, instance); + + //then + expect(retrievedInstance).toEqual(expected); + + }); + + test('getRollBackOnFailureOptions', async (done)=> { + service.getRollBackOnFailureOptions().subscribe((rollBackOnFailureOptions : SelectOption[])=>{ + expect(rollBackOnFailureOptions[0].id).toEqual('true'); + expect(rollBackOnFailureOptions[0].name).toEqual('Rollback'); + expect(rollBackOnFailureOptions[1].id).toEqual('false'); + expect(rollBackOnFailureOptions[1].name).toEqual('Don\'t Rollback'); + done(); + }); + }); + + +}); + + +class MockAppStore { + getState() { + return {} + } +} + +class MockFeatureFlagsService {} diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/control.generator.util.service.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/control.generator.util.service.ts new file mode 100644 index 000000000..08575bcad --- /dev/null +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/control.generator.util.service.ts @@ -0,0 +1,252 @@ +import {Injectable} from "@angular/core"; +import {DropdownFormControl} from "../../../models/formControlModels/dropdownFormControl.model"; +import {FormGroup} from "@angular/forms"; +import { + CustomValidatorOptions, + FormControlModel, + ValidatorModel, + ValidatorOptions +} from "../../../models/formControlModels/formControl.model"; +import {InputFormControl} from "../../../models/formControlModels/inputFormControl.model"; +import {AppState} from "../../../store/reducers"; +import {NgRedux} from "@angular-redux/store"; +import {NumberFormControl} from "../../../models/formControlModels/numberFormControl.model"; +import {FormControlType} from "../../../models/formControlModels/formControlTypes.enum"; +import {FileFormControl} from "../../../models/formControlModels/fileFormControl.model"; +import {SelectOption} from "../../../models/selectOption"; +import {DynamicInputLabelPipe} from "../../../pipes/dynamicInputLabel/dynamic-input-label.pipe"; +import {FormGeneralErrorsService} from "../../formGeneralErrors/formGeneralErrors.service"; +import {Observable, of} from "rxjs"; +import {NodeModel} from "../../../models/nodeModel"; +import {Constants} from "../../../utils/constants"; +import {FileUnit} from "../../formControls/component/file/fileUnit.enum"; +import * as _ from 'lodash'; + +export const SUPPLEMENTARY_FILE = 'supplementaryFile'; +export const SDN_C_PRE_LOAD = 'sdncPreLoad'; + +@Injectable() +export class ControlGeneratorUtil { + + public static readonly INSTANCE_NAME_REG_EX: RegExp = /^[a-zA-Z0-9._-]*$/; + public static readonly GENERATED_NAME_REG_EX: RegExp = /[^a-zA-Z0-9._-]/g; + + constructor(private _store: NgRedux) { + } + + getSubscribeResult(subscribeFunction: Function, control: DropdownFormControl): Observable { + return subscribeFunction(this).subscribe((res) => { + control.options$ = res; + control.hasEmptyOptions = res.length === 0; + FormGeneralErrorsService.checkForErrorTrigger.next(); + return of(res); + }); + } + + getSubscribeInitResult(subscribeFunction: Function, control: DropdownFormControl, form: FormGroup): Observable { + return subscribeFunction(this).subscribe((res) => { + if (!_.isNil(control['onInitSelectedField'])) { + let result = res; + for (let key of control['onInitSelectedField']) { + result = !_.isNil(result[key]) ? result[key] : []; + } + control.options$ = result; + control.hasEmptyOptions = _.isNil(result) || result.length === 0; + } else { + control.options$ = !_.isNil(res) ? res : []; + control.hasEmptyOptions = _.isNil(res) || res.length === 0; + } + + FormGeneralErrorsService.checkForErrorTrigger.next(); + return of(res); + }); + } + + isLegacyRegionShouldBeVisible(instance: any): boolean { + if (!_.isNil(instance) && !_.isNil(instance.lcpCloudRegionId)) { + return Constants.LegacyRegion.MEGA_REGION.indexOf(instance.lcpCloudRegionId) !== -1; + } + return false; + } + + createValidationsForInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): ValidatorModel[] { + let validations: ValidatorModel[] = [ + new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', ControlGeneratorUtil.INSTANCE_NAME_REG_EX), + new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'some error', [this._store, serviceId, instance && instance.instanceName]) + ]; + if (!isEcompGeneratedNaming) { + validations.push(new ValidatorModel(ValidatorOptions.required, 'is required')); + } + return validations; + } + + getInputsOptions = (options: any[]): Observable => { + let optionList: SelectOption[] = []; + options.forEach((option) => { + optionList.push(new SelectOption({ + id: option.id || option.name, + name: option.name + })); + }); + return of(optionList); + }; + + getDynamicInputsByType(dynamicInputs: any, serviceModelId: string, storeKey: string, type: string): FormControlModel[] { + let result: FormControlModel[] = []; + if (dynamicInputs) { + let nodeInstance = null; + if (_.has(this._store.getState().service.serviceInstance[serviceModelId][type], storeKey)) { + nodeInstance = Object.assign({}, this._store.getState().service.serviceInstance[serviceModelId][type][storeKey]); + } + result = this.getDynamicInputs(dynamicInputs, nodeInstance); + } + return result; + } + + getServiceDynamicInputs(dynamicInputs: any, serviceModelId: string): FormControlModel[] { + let result: FormControlModel[] = []; + if (dynamicInputs) { + let serviceInstance = null; + if (_.has(this._store.getState().service.serviceInstance, serviceModelId)) { + serviceInstance = Object.assign({}, this._store.getState().service.serviceInstance[serviceModelId]); + } + result = this.getDynamicInputs(dynamicInputs, serviceInstance); + } + return result; + } + + getDynamicInputs(dynamicInputs: any, instance: any): FormControlModel[] { + let result: FormControlModel[] = []; + if (dynamicInputs) { + dynamicInputs.forEach((input) => { + let validations: ValidatorModel[] = []; + if (input.isRequired) { + validations.push(new ValidatorModel(ValidatorOptions.required, 'is required')) + } + if (input.minLength) { + validations.push(new ValidatorModel(ValidatorOptions.minLength, '', input.minLength)) + } + if (input.maxLength) { + validations.push(new ValidatorModel(ValidatorOptions.maxLength, '', input.maxLength)) + } + + let dynamicInputLabelPipe: DynamicInputLabelPipe = new DynamicInputLabelPipe(); + let data: any = { + controlName: input.name, + displayName: dynamicInputLabelPipe.transform(input.name).slice(0, -1), + dataTestId: input.id, + placeHolder: input.prompt, + tooltip: input.description, + validations: validations, + isVisible: input.isVisible, + value: !_.isNil(instance) && !_.isNil(instance.instanceParams) && instance.instanceParams.length > 0 ? instance.instanceParams[0][input.name] : input.value + }; + + switch (input.type) { + case 'select' : + case 'boolean' : { + data.value = data.value || input.optionList.filter((option) => option.isDefault ? option.id || option.name : null); + data.onInit = this.getSubscribeInitResult.bind(null, this.getInputsOptions.bind(this, input.optionList)); + result.push(new DropdownFormControl(data)); + break; + } + case 'checkbox': { + data.type = FormControlType.CHECKBOX; + result.push(new FormControlModel(data)); + break; + } + case 'number': { + data.min = input.min; + data.max = input.max; + result.push(new NumberFormControl(data)); + break; + } + case 'file': { + result.push(new FileFormControl(data)); + break; + } + default: { + result.push(new InputFormControl(data)); + } + } + }) + } + + return result; + } + + getDefaultInstanceName(instance: any, model: NodeModel): string { + const initialInstanceName = (!_.isNil(instance) && instance.instanceName) || (!_.isNil(model.name) ? model.name.replace(ControlGeneratorUtil.GENERATED_NAME_REG_EX, "") : model.name); + return initialInstanceName; + } + + concatSupplementaryFile(originalArray: FormControlModel[], vfModuleInstance): FormControlModel[] { + let suppFileInput: FileFormControl = (this.getSupplementaryFile(vfModuleInstance)); + return originalArray.concat([suppFileInput], suppFileInput.hiddenFile); + } + + getSupplementaryFile(instance: any): FileFormControl { + return new FileFormControl({ + controlName: SUPPLEMENTARY_FILE, + displayName: 'Supplementary Data File (JSON format)', + dataTestId: 'SupplementaryFile', + placeHolder: 'Choose file', + selectedFile: !_.isNil(instance) ? instance.supplementaryFileName : null, + isVisible: true, + acceptedExtentions: "application/json", + hiddenFile: [new InputFormControl({ + controlName: SUPPLEMENTARY_FILE + "_hidden", + isVisible: false, + validations: [new ValidatorModel(CustomValidatorOptions.isFileTooBig, "File size exceeds 5MB.", [FileUnit.MB, 5])] + }), + new InputFormControl({ + controlName: SUPPLEMENTARY_FILE + "_hidden_content", + isVisible: false, + validations: [new ValidatorModel(CustomValidatorOptions.isValidJson, + "File is invalid, please make sure a legal JSON file is uploaded using name:value pairs.", []), + new ValidatorModel(CustomValidatorOptions.isStringContainTags, + "File is invalid, please remove tags <>.", [])], + value: !_.isNil(instance) ? (instance.supplementaryFile_hidden_content) : null, + }) + ], + onDelete: this.getOnDeleteForSupplementaryFile(), + onChange: this.getOnChangeForSupplementaryFile() + }) + }; + + retrieveInstanceIfUpdateMode(store: NgRedux, instance: any): any { + return store.getState().global.isUpdateModalMode ? instance : null; + } + + private getOnDeleteForSupplementaryFile() { + return (form: FormGroup) => { + form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(null); + form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null); + }; + } + + private getOnChangeForSupplementaryFile() { + return (files: FileList, form: FormGroup) => { + if (files.length > 0) { + const file = files.item(0); + let reader = new FileReader(); + reader.onload = function (event) { + form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(reader.result); + form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(file); + }; + reader.readAsText(file); + } else { + form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(null); + form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null); + } + }; + } + + getRollBackOnFailureOptions = (): Observable => { + return of([ + new SelectOption({id: 'true', name: 'Rollback'}), + new SelectOption({id: 'false', name: 'Don\'t Rollback'}) + ]); + }; + +} diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/networkGenerator/network.control.generator.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/networkGenerator/network.control.generator.spec.ts index 0bb278a8e..b5277b48d 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/networkGenerator/network.control.generator.spec.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/networkGenerator/network.control.generator.spec.ts @@ -1,7 +1,7 @@ import {getTestBed, TestBed} from '@angular/core/testing'; import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; import {NgRedux} from '@angular-redux/store'; -import {BasicControlGenerator} from "../basic.control.generator"; +import {ControlGeneratorUtil} from "../control.generator.util.service"; import {AaiService} from "../../../../services/aaiService/aai.service"; import {GenericFormService} from "../../generic-form.service"; import {FormBuilder} from "@angular/forms"; @@ -9,6 +9,7 @@ import {LogService} from "../../../../utils/log/log.service"; import {FormControlNames, NetworkControlGenerator} from "./network.control.generator"; import {FormControlModel, ValidatorModel, ValidatorOptions} from "../../../../models/formControlModels/formControl.model"; import {FeatureFlagsService} from "../../../../services/featureFlag/feature-flags.service"; +import {SharedControllersService} from "../sharedControlles/shared.controllers.service"; class MockAppStore { getState(){ @@ -1812,7 +1813,8 @@ describe('Network Control Generator', () => { imports: [HttpClientTestingModule], providers: [NetworkControlGenerator, GenericFormService, - BasicControlGenerator, + ControlGeneratorUtil, + SharedControllersService, AaiService, FormBuilder, LogService, diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/networkGenerator/network.control.generator.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/networkGenerator/network.control.generator.ts index 19c85a1ff..a7f16db4b 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/networkGenerator/network.control.generator.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/networkGenerator/network.control.generator.ts @@ -3,53 +3,33 @@ import {GenericFormService} from "../../generic-form.service"; import {AaiService} from "../../../../services/aaiService/aai.service"; import {NgRedux} from "@angular-redux/store"; import {HttpClient} from "@angular/common/http"; -import {BasicControlGenerator} from "../basic.control.generator"; +import {ControlGeneratorUtil} from "../control.generator.util.service"; import * as _ from 'lodash'; -import {Observable, of} from "rxjs"; -import { - FormControlModel, - ValidatorModel, - ValidatorOptions -} from "../../../../models/formControlModels/formControl.model"; +import {FormControlModel,} from "../../../../models/formControlModels/formControl.model"; import {LogService} from "../../../../utils/log/log.service"; import {AppState} from "../../../../store/reducers"; -import {FormGroup} from "@angular/forms"; import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model"; import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum"; -import {SelectOption} from "../../../../models/selectOption"; import {NetworkInstance} from "../../../../models/networkInstance"; import {NetworkModel} from "../../../../models/networkModel"; -import {Constants} from "../../../../utils/constants"; +import {SharedControllersService} from "../sharedControlles/shared.controllers.service"; export enum FormControlNames { INSTANCE_NAME = 'instanceName', - GLOBAL_SUBSCRIBER_ID = 'globalSubscriberId', PRODUCT_FAMILY_ID = 'productFamilyId', LCPCLOUD_REGION_ID = 'lcpCloudRegionId', - TENANT_ID = 'tenantId', - AICZONE_ID = 'aicZoneId', - ROLLBACK_ON_FAILURE = 'rollbackOnFailure', - LEGACY_REGION = 'legacyRegion' + ROLLBACK_ON_FAILURE = 'rollbackOnFailure' } -enum InputType { - LCP_REGION = "lcpCloudRegionId", - TENANT = "tenantId", - LOB = "lineOfBusiness", - PLATFORM = "platformName", - ROLLBACK = "rollbackOnFailure", - PRODUCT_FAMILY = "productFamilyId", - VG = "volumeGroupName" -} - @Injectable() export class NetworkControlGenerator { aaiService: AaiService; constructor(private genericFormService: GenericFormService, - private _basicControlGenerator: BasicControlGenerator, + private _basicControlGenerator: ControlGeneratorUtil, + private _sharedControllersService: SharedControllersService, private store: NgRedux, private http: HttpClient, private _aaiService: AaiService, @@ -57,8 +37,8 @@ export class NetworkControlGenerator { this.aaiService = _aaiService; } - getNetworkInstance = (serviceId: string, networkName: string, isUpdateMode : boolean): NetworkInstance => { - let networkInstance : NetworkInstance = null; + getNetworkInstance = (serviceId: string, networkName: string, isUpdateMode: boolean): NetworkInstance => { + let networkInstance: NetworkInstance = null; if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] && _.has(this.store.getState().service.serviceInstance[serviceId].networks, networkName)) { networkInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].networks[networkName]); } @@ -66,7 +46,7 @@ export class NetworkControlGenerator { }; - getMacroFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode : boolean): FormControlModel[] { + getMacroFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode: boolean): FormControlModel[] { networkStoreKey = _.isNil(networkStoreKey) ? networkName : networkStoreKey; if (_.isNil(serviceId) || _.isNil(networkStoreKey) || _.isNil(networkName)) { @@ -79,18 +59,18 @@ export class NetworkControlGenerator { if (!_.isNil(networkModel)) { result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming)); - result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false)); - result.push(this.getLcpRegionControl(serviceId, networkInstance, result)); - result.push(this._basicControlGenerator.getLegacyRegion(networkInstance)); - result.push(this.getTenantControl(serviceId, networkInstance, result)); - result.push(this.getPlatformControl(networkInstance, result)); - result.push(this.getLineOfBusinessControl(networkInstance, result)); + result.push(this._sharedControllersService.getProductFamilyControl(networkInstance, result, false)); + result.push(this._sharedControllersService.getLcpRegionControl(serviceId, networkInstance, result)); + result.push(this._sharedControllersService.getLegacyRegion(networkInstance)); + result.push(this._sharedControllersService.getTenantControl(serviceId, networkInstance)); + result.push(this.getPlatformControl(networkInstance)); + result.push(this._sharedControllersService.getLineOfBusinessControl(networkInstance)); } return result; } - getAlaCarteFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode : boolean): FormControlModel[] { + getAlaCarteFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode: boolean): FormControlModel[] { networkStoreKey = _.isNil(networkStoreKey) ? networkName : networkStoreKey; if (_.isNil(serviceId) || _.isNil(networkStoreKey) || _.isNil(networkName)) { this._logService.error('should provide serviceId, networkName, networkStoreKey', serviceId); @@ -103,45 +83,24 @@ export class NetworkControlGenerator { if (!_.isNil(networkModel)) { result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming)); - result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false)); - result.push(this.getLcpRegionControl(serviceId, networkInstance, result)); - result.push(this._basicControlGenerator.getLegacyRegion(networkInstance)); - result.push(this.getTenantControl(serviceId, networkInstance, result)); - result.push(this.getPlatformControl(networkInstance, result)); - result.push(this.getLineOfBusinessControl(networkInstance, result)); - result.push(this.getRollbackOnFailureControl(networkInstance, result)); + result.push(this._sharedControllersService.getProductFamilyControl(networkInstance, result, false)); + result.push(this._sharedControllersService.getLcpRegionControl(serviceId, networkInstance, result)); + result.push(this._sharedControllersService.getLegacyRegion(networkInstance)); + result.push(this._sharedControllersService.getTenantControl(serviceId, networkInstance)); + result.push(this.getPlatformControl(networkInstance)); + result.push(this._sharedControllersService.getLineOfBusinessControl(networkInstance)); + result.push(this._sharedControllersService.getRollbackOnFailureControl(networkInstance)); } return result; - } - isInputShouldBeShown = (inputType: any): boolean => { - let networkInputs = [InputType.LCP_REGION, InputType.LOB, InputType.TENANT, InputType.PRODUCT_FAMILY, InputType.PLATFORM, InputType.ROLLBACK]; - return networkInputs.indexOf(inputType) > -1; - }; - getInstanceName(instance : any, serviceId : string, networkName : string, isEcompGeneratedNaming: boolean): FormControlModel { - const networkModel : NetworkModel = this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]; - return this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, networkModel); + getInstanceName(instance: any, serviceId: string, networkName: string, isEcompGeneratedNaming: boolean): FormControlModel { + const networkModel: NetworkModel = this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]; + return this._sharedControllersService.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, networkModel); } - getLineOfBusinessControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => { - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: 'lineOfBusiness', - displayName: 'Line of business', - dataTestId: 'lineOfBusiness', - placeHolder: 'Select Line Of Business', - isDisabled: false, - name: "lineOfBusiness", - value: instance ? instance.lineOfBusiness : null, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - onInitSelectedField: ['lineOfBusinessList'], - onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters) - }) - }; - - getPlatformControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => { + getPlatformControl = (instance: any): DropdownFormControl => { return new DropdownFormControl({ type: FormControlType.DROPDOWN, controlName: 'platformName', @@ -156,90 +115,4 @@ export class NetworkControlGenerator { onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters) }) }; - - getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => { - const service = this.store.getState().service.serviceInstance[serviceId]; - const globalCustomerId: string = service.globalSubscriberId; - const serviceType: string = service.subscriptionServiceType; - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: FormControlNames.TENANT_ID, - displayName: 'Tenant', - dataTestId: 'tenant', - placeHolder: 'Select Tenant', - name: "tenant", - isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId), - onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null, - value: instance ? instance.tenantId : null, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - onInit : instance ? this._basicControlGenerator.getSubscribeInitResult.bind( - this._aaiService, - this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : ()=>{}, - }) - }; - - getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => { - const service = this.store.getState().service.serviceInstance[serviceId]; - const globalCustomerId: string = service.globalSubscriberId; - const serviceType: string = service.subscriptionServiceType; - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: 'lcpCloudRegionId', - displayName: 'LCP region', - dataTestId: 'lcpRegion', - placeHolder: 'Select LCP Region', - name: "lcpRegion", - isDisabled: false, - value: instance ? instance.lcpCloudRegionId : null, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - onInitSelectedField: ['lcpRegionList'], - onInit: this._basicControlGenerator.getSubscribeInitResult.bind( - this._aaiService, - this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)), - onChange: (param: string, form: FormGroup) => { - form.controls[FormControlNames.TENANT_ID].enable(); - form.controls[FormControlNames.TENANT_ID].reset(); - if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) { - this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => { - controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param]; - if(res.lcpRegionsTenantsMap[param]){ - controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0; - } - })); - } - - if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) { - form.controls['legacyRegion'].enable(); - controls.find(item => item.controlName === 'legacyRegion').isVisible = true; - - } else { - controls.find(item => item.controlName === 'legacyRegion').isVisible = false; - form.controls['legacyRegion'].setValue(null); - form.controls['legacyRegion'].reset(); - form.controls['legacyRegion'].disable(); - } - } - }) - }; - - getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => { - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: FormControlNames.ROLLBACK_ON_FAILURE, - displayName: 'Rollback on failure', - dataTestId: 'rollback', - placeHolder: 'Rollback on failure', - isDisabled: false, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - value: instance ? instance.rollbackOnFailure : 'true', - onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions) - }) - }; - - getRollBackOnFailureOptions = (): Observable => { - return of([ - new SelectOption({id: 'true', name: 'Rollback'}), - new SelectOption({id: 'false', name: 'Don\'t Rollback'}) - ]); - }; } diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/service.control.generator.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/service.control.generator.spec.ts index 6bcec09c4..88a8a53d6 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/service.control.generator.spec.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/service.control.generator.spec.ts @@ -2,7 +2,7 @@ import {getTestBed, TestBed} from '@angular/core/testing'; import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; import {NgRedux} from '@angular-redux/store'; import {FormControlNames, ServiceControlGenerator} from "./service.control.generator"; -import {BasicControlGenerator} from "./basic.control.generator"; +import {ControlGeneratorUtil} from "./control.generator.util.service"; import {AaiService} from "../../../services/aaiService/aai.service"; import {GenericFormService} from "../generic-form.service"; import {FormBuilder} from "@angular/forms"; @@ -13,6 +13,7 @@ import {DropdownFormControl} from "../../../models/formControlModels/dropdownFor import {FeatureFlagsService} from "../../../services/featureFlag/feature-flags.service"; import each from 'jest-each'; import {VidNotions} from "../../../models/vidNotions"; +import {SharedControllersService} from "./sharedControlles/shared.controllers.service"; class MockAppStore { getState() { @@ -912,7 +913,8 @@ describe('Service Control Generator', () => { imports: [HttpClientTestingModule], providers: [ServiceControlGenerator, GenericFormService, - BasicControlGenerator, + ControlGeneratorUtil, + SharedControllersService, AaiService, FormBuilder, LogService, diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/service.control.generator.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/service.control.generator.ts index 12054a8a7..da13b0be4 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/service.control.generator.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/service.control.generator.ts @@ -5,7 +5,7 @@ import {AppState} from "../../../store/reducers"; import {FormControlModel, ValidatorModel, ValidatorOptions} from "../../../models/formControlModels/formControl.model"; import {DropdownFormControl} from "../../../models/formControlModels/dropdownFormControl.model"; import * as _ from 'lodash'; -import {BasicControlGenerator} from "./basic.control.generator"; +import {ControlGeneratorUtil} from "./control.generator.util.service"; import {AaiService} from "../../../services/aaiService/aai.service"; import {FormGroup} from "@angular/forms"; import {FormControlType} from "../../../models/formControlModels/formControlTypes.enum"; @@ -17,6 +17,7 @@ import {ServiceModel} from "../../../models/serviceModel"; import {CheckboxFormControl} from "../../../models/formControlModels/checkboxFormControl.model"; import {VidNotions} from "../../../models/vidNotions"; +import {SharedControllersService} from "./sharedControlles/shared.controllers.service"; export enum FormControlNames { INSTANCE_NAME = 'instanceName', @@ -36,7 +37,8 @@ export enum FormControlNames { export class ServiceControlGenerator { aaiService : AaiService; constructor(private genericFormService : GenericFormService, - private _basicControlGenerator : BasicControlGenerator, + private _basicControlGenerator : ControlGeneratorUtil, + private _sharedControllersService : SharedControllersService, private store: NgRedux, private http: HttpClient, private _aaiService : AaiService, @@ -64,7 +66,7 @@ export class ServiceControlGenerator { const serviceModel = new ServiceModel(this.store.getState().service.serviceHierarchy[serviceId]); if(!_.isNil(serviceModel)){ - result.push(this._basicControlGenerator.getInstanceName(serviceInstance, serviceId, serviceModel.isEcompGeneratedNaming)); + result.push(this._sharedControllersService.getInstanceName(serviceInstance, serviceId, serviceModel.isEcompGeneratedNaming)); result.push(this.getGlobalSubscriberControl(serviceInstance, result)); result.push(this.getServiceTypeControl(serviceInstance, result, false)); result.push(this.getOwningEntityControl(serviceInstance, result)); @@ -87,7 +89,7 @@ export class ServiceControlGenerator { let result : FormControlModel[] = []; const serviceModel = new ServiceModel(this.store.getState().service.serviceHierarchy[serviceId]); if(!_.isNil(serviceModel)){ - result.push(this._basicControlGenerator.getInstanceName(serviceInstance, serviceId, serviceModel.isEcompGeneratedNaming)); + result.push(this._sharedControllersService.getInstanceName(serviceInstance, serviceId, serviceModel.isEcompGeneratedNaming)); result.push(this.getGlobalSubscriberControl(serviceInstance, result)); result.push(this.getServiceTypeControl(serviceInstance, result, true)); result.push(this.getOwningEntityControl(serviceInstance, result)); diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.spec.ts new file mode 100644 index 000000000..58ee33b53 --- /dev/null +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.spec.ts @@ -0,0 +1,263 @@ +import * as _ from "lodash"; +import {getTestBed, TestBed} from '@angular/core/testing'; +import {SharedControllersService} from "./shared.controllers.service"; +import {HttpClientTestingModule, HttpTestingController} from "@angular/common/http/testing"; +import {AppState} from "../../../../store/reducers"; +import {NgRedux} from "@angular-redux/store"; +import {AaiService} from "../../../../services/aaiService/aai.service"; +import {FeatureFlagsService} from "../../../../services/featureFlag/feature-flags.service"; +import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model"; +import {FormControlModel, ValidatorOptions} from "../../../../models/formControlModels/formControl.model"; +import {ControlGeneratorUtil} from "../control.generator.util.service"; + + + +describe('Shared Controllers Service', () => { + let injector; + let service: SharedControllersService; + let httpMock: HttpTestingController; + let store: NgRedux; + let basicControlGenerator : ControlGeneratorUtil; + + beforeAll(done => (async () => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [SharedControllersService, + AaiService, + ControlGeneratorUtil, + {provide:FeatureFlagsService, useClass: MockFeatureFlagsService}, + {provide: NgRedux, useClass: MockAppStore}] + }); + await TestBed.compileComponents(); + + injector = getTestBed(); + service = injector.get(SharedControllersService); + basicControlGenerator = injector.get(ControlGeneratorUtil); + httpMock = injector.get(HttpTestingController); + store = injector.get(NgRedux); + + })().then(done).catch(done.fail)); + + + + + + test('getLineOfBusinessControl', ()=> { + const lineOfBusinessControl :DropdownFormControl = service.getLineOfBusinessControl(); + expect(lineOfBusinessControl.name).toEqual('lineOfBusiness'); + expect(lineOfBusinessControl.controlName).toEqual('lineOfBusiness'); + expect(lineOfBusinessControl.displayName).toEqual('Line of business'); + expect(lineOfBusinessControl.dataTestId).toEqual('lineOfBusiness'); + expect(lineOfBusinessControl.placeHolder).toEqual('Select Line Of Business'); + expect(lineOfBusinessControl.onInitSelectedField).toEqual(['lineOfBusinessList']); + expect(lineOfBusinessControl.onInit).toBeDefined(); + expect(lineOfBusinessControl.value).toBeNull(); + expect(lineOfBusinessControl.validations.find((validation)=> validation.validatorName === ValidatorOptions.required)).toBeDefined(); + expect(lineOfBusinessControl.isDisabled).toBeFalsy(); + }); + + test('getTenantControl', ()=> { + const serviceId : string = Object.keys(store.getState().service.serviceInstance)[0]; + const vnfs = store.getState().service.serviceInstance[serviceId].vnfs; + const currentVnf = vnfs[Object.keys(vnfs)[0]]; + + const tanantControl :DropdownFormControl = service.getTenantControl(serviceId, currentVnf); + expect(tanantControl.name).toEqual('tenant'); + expect(tanantControl.controlName).toEqual('tenantId'); + expect(tanantControl.displayName).toEqual('Tenant'); + expect(tanantControl.dataTestId).toEqual('tenant'); + expect(tanantControl.placeHolder).toEqual('Select Tenant'); + expect(tanantControl.onInitSelectedField).toEqual(['lcpRegionsTenantsMap', currentVnf.lcpCloudRegionId]); + expect(tanantControl.onInit).toBeDefined(); + expect(tanantControl.validations.find((validation)=> validation.validatorName === ValidatorOptions.required)).toBeDefined(); + expect(tanantControl.isDisabled).toEqual(_.isNil(currentVnf.lcpCloudRegionId)); + }); + + + test('getRollbackOnFailureControl', ()=> { + const rollbackOnFailureControl :DropdownFormControl = service.getRollbackOnFailureControl(); + expect(rollbackOnFailureControl.controlName).toEqual('rollbackOnFailure'); + expect(rollbackOnFailureControl.displayName).toEqual('Rollback on failure'); + expect(rollbackOnFailureControl.dataTestId).toEqual('rollback'); + expect(rollbackOnFailureControl.placeHolder).toEqual('Rollback on failure'); + expect(rollbackOnFailureControl.onInit).toBeDefined(); + expect(rollbackOnFailureControl.validations.find((validation)=> validation.validatorName === ValidatorOptions.required)).toBeDefined(); + expect(rollbackOnFailureControl.isDisabled).toBeFalsy(); + }); + + test('getLcpRegionControl', ()=> { + const serviceId : string = Object.keys(store.getState().service.serviceInstance)[0]; + const vnfs = store.getState().service.serviceInstance[serviceId].vnfs; + const currentVnf = vnfs[Object.keys(vnfs)[0]]; + const lcpRegionControl :DropdownFormControl = service.getLcpRegionControl(serviceId, currentVnf, []); + expect(lcpRegionControl.controlName).toEqual('lcpCloudRegionId'); + expect(lcpRegionControl.displayName).toEqual('LCP region'); + expect(lcpRegionControl.dataTestId).toEqual('lcpRegion'); + expect(lcpRegionControl.placeHolder).toEqual('Select LCP Region'); + expect(lcpRegionControl.onInit).toBeDefined(); + expect(lcpRegionControl.onChange).toBeDefined(); + expect(lcpRegionControl.validations.find((validation)=> validation.validatorName === ValidatorOptions.required)).toBeDefined(); + expect(lcpRegionControl.isDisabled).toBeFalsy(); + }); + + test('sdn-preload checkbox is visible', () => { + const instance = {}; + const sdncPreload: FormControlModel = service.getSDNCControl(instance); + expect (sdncPreload.displayName).toEqual('SDN-C pre-load'); + expect (sdncPreload.value).toBeFalsy(); + }); + + test('getlegacyRegion with AAIAIC25 - isVisible true', () => { + const instance = {lcpCloudRegionId : 'AAIAIC25'}; + const legacyRegionControl: FormControlModel = service.getLegacyRegion(instance); + expect(legacyRegionControl.isVisible).toBeTruthy(); + }); + + test('getlegacyRegion without AAIAIC25 - isVisible false', () => { + const instance = {lcpCloudRegionId : 'olson3'}; + const legacyRegionControl: FormControlModel = service.getLegacyRegion(instance); + expect(legacyRegionControl.isVisible).toBeFalsy(); + }); +}); + +class MockAppStore { + getState() { + return { + "global": { + "flags": { + "FLAG_NETWORK_TO_ASYNC_INSTANTIATION": false, + "FLAG_SHOW_ASSIGNMENTS": true, + "FLAG_FABRIC_CONFIGURATION_ASSIGNMENTS": true, + "FLAG_SHOW_VERIFY_SERVICE": false, + "FLAG_SERVICE_MODEL_CACHE": true, + "FLAG_ADD_MSO_TESTAPI_FIELD": true + } + }, + "service": { + "serviceHierarchy": { + "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc": { + "service": { + "uuid": "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc", + "invariantUuid": "e49fbd11-e60c-4a8e-b4bf-30fbe8f4fcc0", + "name": "ComplexService", + "version": "1.0", + "toscaModelURL": null, + "category": "Emanuel", + "serviceType": "", + "serviceRole": "", + "description": "ComplexService", + "serviceEcompNaming": "false", + "instantiationType": "Macro", + "inputs": {} + }, + "vnfs": { + "VF_vGeraldine 0": { + "uuid": "d6557200-ecf2-4641-8094-5393ae3aae60", + "invariantUuid": "4160458e-f648-4b30-a176-43881ffffe9e", + "description": "VSP_vGeraldine", + "name": "VF_vGeraldine", + "version": "2.0", + "customizationUuid": "91415b44-753d-494c-926a-456a9172bbb9", + "inputs": {}, + "commands": {}, + "properties": { + "max_instances": 10, + "min_instances": 1, + + }, + "type": "VF", + "modelCustomizationName": "VF_vGeraldine 0", + "vfModules": { + "vf_vgeraldine0..VfVgeraldine..vflorence_vlc..module-1": { + "uuid": "522159d5-d6e0-4c2a-aa44-5a542a12a830", + "invariantUuid": "98a7c88b-b577-476a-90e4-e25a5871e02b", + "customizationUuid": "55b1be94-671a-403e-a26c-667e9c47d091", + "description": null, + "name": "VfVgeraldine..vflorence_vlc..module-1", + "version": "2", + "modelCustomizationName": "VfVgeraldine..vflorence_vlc..module-1", + "properties": { + "minCountInstances": 0, + "maxCountInstances": null, + "initialCount": 0, + "vfModuleLabel": "vflorence_vlc" + }, + "inputs": {}, + "volumeGroupAllowed": true + } + } + } + } + } + }, + "serviceInstance": { + "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc": { + "vnfs": { + "2017-388_PASQUALE-vPE 0": { + "action": "Create", + "inMaint": false, + "rollbackOnFailure": "true", + "originalName": "2017-388_PASQUALE-vPE 0", + "isMissingData": false, + "trackById": "eymgwlevh54", + "vfModules": {}, + "vnfStoreKey": "2017-388_PASQUALE-vPE 0", + "uuid": "afacccf6-397d-45d6-b5ae-94c39734b168", + "productFamilyId": "e433710f-9217-458d-a79d-1c7aff376d89", + "lcpCloudRegionId": "AAIAIC25", + "tenantId": "092eb9e8e4b7412e8787dd091bc58e86", + "lineOfBusiness": "ONAP", + "platformName": "platform", + "modelInfo": { + "modelInvariantId": "72e465fe-71b1-4e7b-b5ed-9496118ff7a8", + "modelVersionId": "afacccf6-397d-45d6-b5ae-94c39734b168", + "modelName": "2017-388_PASQUALE-vPE", + "modelVersion": "4.0", + "modelCustomizationId": "b3c76f73-eeb5-4fb6-9d31-72a889f1811c", + "modelCustomizationName": "2017-388_PASQUALE-vPE 0", + "uuid": "afacccf6-397d-45d6-b5ae-94c39734b168", + "modelUniqueId": "b3c76f73-eeb5-4fb6-9d31-72a889f1811c" + }, + "instanceName": "2017-388_PASQUALE-vPEAjXzainstanceName", + "legacyRegion": "some legacy region", + "instanceParams": [ + { + "vnf_config_template_version": "17.2", + "bandwidth_units": "Gbps", + "bandwidth": "10", + "AIC_CLLI": "ATLMY8GA", + "ASN": "AV_vPE", + "vnf_instance_name": "mtnj309me6" + } + ] + } + }, + "service": { + "vidNotions": { + "instantiationUI": "serviceWithVRF", + "modelCategory": "other", + "viewEditUI": "serviceWithVRF", + "instantiationType": "ALaCarte" + }, + "uuid": "4117a0b6-e234-467d-b5b9-fe2f68c8b0fc", + "invariantUuid": "7ee41ce4-4827-44b0-a48e-2707a59905d2", + "name": "VRF Service for Test", + "version": "1.0", + "toscaModelURL": null, + "category": "Network L4+", + "serviceType": "INFRASTRUCTURE", + "serviceRole": "Configuration", + "description": "xxx", + "serviceEcompNaming": "true", + "instantiationType": "A-La-Carte", + "inputs": {} + }, + "isALaCarte": true + } + } + } + } + } +} + +class MockFeatureFlagsService {} diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts new file mode 100644 index 000000000..e82ea5d85 --- /dev/null +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/sharedControlles/shared.controllers.service.ts @@ -0,0 +1,189 @@ +import {Injectable} from "@angular/core"; +import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model"; +import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum"; +import { + FormControlModel, + ValidatorModel, + ValidatorOptions +} from "../../../../models/formControlModels/formControl.model"; +import {NgRedux} from "@angular-redux/store"; +import {AppState} from "../../../../store/reducers"; +import {AaiService} from "../../../../services/aaiService/aai.service"; +import {ControlGeneratorUtil, SDN_C_PRE_LOAD} from "../control.generator.util.service"; +import * as _ from "lodash"; +import {FormGroup} from "@angular/forms"; +import {Constants} from "../../../../utils/constants"; +import {CheckboxFormControl} from "../../../../models/formControlModels/checkboxFormControl.model"; +import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model"; +import {NodeModel} from "../../../../models/nodeModel"; + +@Injectable() +export class SharedControllersService { + constructor(private _store : NgRedux, + private _aaiService : AaiService, + private _basicControlGenerator : ControlGeneratorUtil){} + + + getLineOfBusinessControl = (instance?: any): DropdownFormControl => { + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'lineOfBusiness', + displayName: 'Line of business', + dataTestId: 'lineOfBusiness', + placeHolder: 'Select Line Of Business', + isDisabled: false, + name: "lineOfBusiness", + value: instance ? instance.lineOfBusiness : null, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + onInitSelectedField: ['lineOfBusinessList'], + onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters) + }) + }; + + getTenantControl = (serviceId: string, instance?: any): DropdownFormControl => { + const service = this._store.getState().service.serviceInstance[serviceId]; + const globalCustomerId: string = service.globalSubscriberId; + const serviceType: string = service.subscriptionServiceType; + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'tenantId', + displayName: 'Tenant', + dataTestId: 'tenant', + placeHolder: 'Select Tenant', + name: 'tenant', + isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId), + onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null, + value: instance ? instance.tenantId : null, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + onInit: instance ? this._basicControlGenerator.getSubscribeInitResult.bind( + this._aaiService, + this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : () => { + } + }) + }; + + getRollbackOnFailureControl = (instance?: any): DropdownFormControl => { + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'rollbackOnFailure', + displayName: 'Rollback on failure', + dataTestId: 'rollback', + placeHolder: 'Rollback on failure', + isDisabled: false, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + value: instance ? instance.rollbackOnFailure : 'true', + onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._basicControlGenerator.getRollBackOnFailureOptions) + }) + }; + + getLegacyRegion(instance: any): FormControlModel { + return new InputFormControl({ + controlName: 'legacyRegion', + displayName: 'Legacy Region', + dataTestId: 'lcpRegionText', + placeHolder: 'Type Legacy Region', + validations: [], + isVisible: this._basicControlGenerator.isLegacyRegionShouldBeVisible(instance), + isDisabled : _.isNil(instance) ? true : Constants.LegacyRegion.MEGA_REGION.indexOf(instance.lcpCloudRegionId), + value: instance ? instance.legacyRegion : null + }); + } + + getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => { + const service = this._store.getState().service.serviceInstance[serviceId]; + const globalCustomerId: string = service.globalSubscriberId; + const serviceType: string = service.subscriptionServiceType; + return new DropdownFormControl({ + type: FormControlType.DROPDOWN, + controlName: 'lcpCloudRegionId', + displayName: 'LCP region', + dataTestId: 'lcpRegion', + placeHolder: 'Select LCP Region', + name: "lcpRegion", + isDisabled: false, + value: instance ? instance.lcpCloudRegionId : null, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], + onInitSelectedField: ['lcpRegionList'], + onInit: this._basicControlGenerator.getSubscribeInitResult.bind( + this._aaiService, + this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)), + onChange: (param: string, form: FormGroup) => { + form.controls['tenantId'].enable(); + form.controls['tenantId'].reset(); + if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) { + this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => { + controls.find(item => item.controlName === 'tenantId')['options$'] = res.lcpRegionsTenantsMap[param]; + if (res.lcpRegionsTenantsMap[param]) { + controls.find(item => item.controlName === 'tenantId')['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0; + } + })); + } + + if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) { + form.controls['legacyRegion'].enable(); + controls.find(item => item.controlName === 'legacyRegion').isVisible = true; + + } else { + controls.find(item => item.controlName === 'legacyRegion').isVisible = false; + form.controls['legacyRegion'].setValue(null); + form.controls['legacyRegion'].reset(); + form.controls['legacyRegion'].disable(); + } + } + }) + }; + + getSDNCControl = (instance: any): FormControlModel => { + return new CheckboxFormControl({ + controlName: SDN_C_PRE_LOAD, + displayName: 'SDN-C pre-load', + dataTestId: 'sdncPreLoad', + value: instance ? instance.sdncPreLoad : false, + validations: [new ValidatorModel(ValidatorOptions.required, 'is required')] + }) + }; + + getProductFamilyControl = (instance : any, controls : FormControlModel[], isMandatory?: boolean) : DropdownFormControl => { + return new DropdownFormControl({ + type : FormControlType.DROPDOWN, + controlName : 'productFamilyId', + displayName : 'Product family', + dataTestId : 'productFamily', + placeHolder : 'Select Product Family', + isDisabled : false, + name : "product-family-select", + value : instance ? instance.productFamilyId : null, + validations : _.isNil(isMandatory) || isMandatory === true ? [new ValidatorModel(ValidatorOptions.required, 'is required')]: [], + onInit : this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getProductFamilies), + }) + }; + + getInstanceNameController(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, model: NodeModel): FormControlModel { + let validations: ValidatorModel[] = this._basicControlGenerator.createValidationsForInstanceName(instance, serviceId, isEcompGeneratedNaming); + return new InputFormControl({ + controlName: 'instanceName', + displayName: 'Instance name', + dataTestId: 'instanceName', + placeHolder: (!isEcompGeneratedNaming) ? 'Instance name' : 'Automatically generated when not provided', + validations: validations, + isVisible : true, + value : (!isEcompGeneratedNaming || (!_.isNil(instance) && !_.isNil(instance.instanceName))) + ? this._basicControlGenerator.getDefaultInstanceName(instance, model) : null, + onKeypress : (event) => { + const pattern:RegExp = ControlGeneratorUtil.INSTANCE_NAME_REG_EX; + if(pattern){ + if(!pattern.test(event['key'])){ + event.preventDefault(); + } + } + return event; + } + }); + } + + getInstanceName(instance : any, serviceId : string, isEcompGeneratedNaming: boolean): FormControlModel { + let formControlModel:FormControlModel = this.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, new NodeModel()); + formControlModel.value = instance ? instance.instanceName : null; + return formControlModel; + } +} diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.spec.ts index deb1a784a..f3d32b9bb 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.spec.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.spec.ts @@ -1,7 +1,7 @@ import {getTestBed, TestBed} from '@angular/core/testing'; import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; import {NgRedux} from '@angular-redux/store'; -import {BasicControlGenerator, SDN_C_PRE_LOAD} from "../basic.control.generator"; +import {ControlGeneratorUtil, SDN_C_PRE_LOAD} from "../control.generator.util.service"; import {AaiService} from "../../../../services/aaiService/aai.service"; import {GenericFormService} from "../../generic-form.service"; import {FormBuilder} from "@angular/forms"; @@ -11,6 +11,7 @@ import {FormControlNames, VfModuleControlGenerator} from "./vfModule.control.gen import {FeatureFlagsService} from "../../../../services/featureFlag/feature-flags.service"; import {VfModuleInstance} from "../../../../models/vfModuleInstance"; import {VfModule} from "../../../../models/vfModule"; +import {SharedControllersService} from "../sharedControlles/shared.controllers.service"; class MockAppStore { getState() { @@ -919,7 +920,8 @@ describe('VFModule Control Generator', () => { imports: [HttpClientTestingModule], providers: [VfModuleControlGenerator, GenericFormService, - BasicControlGenerator, + SharedControllersService, + ControlGeneratorUtil, AaiService, FormBuilder, LogService, diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts index 011f43447..60ffc3e96 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts @@ -3,10 +3,7 @@ import {GenericFormService} from "../../generic-form.service"; import {AaiService} from "../../../../services/aaiService/aai.service"; import {NgRedux} from "@angular-redux/store"; import {HttpClient} from "@angular/common/http"; -import {BasicControlGenerator} from "../basic.control.generator"; -import * as _ from 'lodash'; -import {Observable, of} from "rxjs"; - +import {ControlGeneratorUtil} from "../control.generator.util.service"; import { CustomValidatorOptions, FormControlModel, @@ -16,19 +13,13 @@ import { import {LogService} from "../../../../utils/log/log.service"; import {AppState} from "../../../../store/reducers"; import {FormGroup} from "@angular/forms"; -import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model"; -import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum"; import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model"; -import {SelectOption} from "../../../../models/selectOption"; import {VfModuleInstance} from "../../../../models/vfModuleInstance"; import {VfModule} from "../../../../models/vfModule"; import {VNFModel} from "../../../../models/vnfModel"; import {VnfInstance} from "../../../../models/vnfInstance"; -import {FileFormControl} from "../../../../models/formControlModels/fileFormControl.model"; -import {CheckboxFormControl} from "../../../../models/formControlModels/checkboxFormControl.model"; -import {FileUnit} from "../../../formControls/component/file/fileUnit.enum"; -import {Constants} from "../../../../utils/constants"; - +import * as _ from 'lodash'; +import {SharedControllersService} from "../sharedControlles/shared.controllers.service"; export enum FormControlNames { INSTANCE_NAME = 'instanceName', @@ -47,7 +38,8 @@ export class VfModuleControlGenerator { isUpdateMode : boolean; constructor(private genericFormService: GenericFormService, - private _basicControlGenerator: BasicControlGenerator, + private _basicControlGenerator: ControlGeneratorUtil, + private _sharedControllersService: SharedControllersService, private store: NgRedux, private http: HttpClient, private _aaiService: AaiService, @@ -55,17 +47,6 @@ export class VfModuleControlGenerator { this.aaiService = _aaiService; } - setVFModuleStoreKey = (serviceId: string, vfModuleUuid: string) => { - const vfModules = this.store.getState().service.serviceHierarchy[serviceId].vfModules; - const vfModulesKeys = Object.keys(vfModules); - for(let key of vfModulesKeys){ - if(vfModules[key].uuid === vfModuleUuid){ - return; - } - } - }; - - getVfModuleInstance = (serviceId: string, vnfStoreKey: string, UUIDData: Object, isUpdateMode: boolean): VfModuleInstance => { let vfModuleInstance: VfModuleInstance = null; if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] && @@ -133,11 +114,11 @@ export class VfModuleControlGenerator { const vfModuleInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode)); let result: FormControlModel[] = []; this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, true); - result.push(this.getLcpRegionControl(serviceId, vfModuleInstance, result)); - result.push(this._basicControlGenerator.getLegacyRegion(vfModuleInstance)); - result.push(this.getTenantControl(serviceId, vfModuleInstance, result)); - result.push(this.getRollbackOnFailureControl(vfModuleInstance, result)); - result.push(this._basicControlGenerator.getSDNCControl(vfModuleInstance)); + result.push(this._sharedControllersService.getLcpRegionControl(serviceId, vfModuleInstance, result)); + result.push(this._sharedControllersService.getLegacyRegion(vfModuleInstance)); + result.push(this._sharedControllersService.getTenantControl(serviceId, vfModuleInstance)); + result.push(this._sharedControllersService.getRollbackOnFailureControl(vfModuleInstance)); + result.push(this._sharedControllersService.getSDNCControl(vfModuleInstance)); if(this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) { result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance); } @@ -145,7 +126,7 @@ export class VfModuleControlGenerator { } getInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): FormControlModel { - let formControlModel:FormControlModel = this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, this.vfModuleModel); + let formControlModel:FormControlModel = this._sharedControllersService.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, this.vfModuleModel); formControlModel.onBlur = (event, form : FormGroup) => { if(!_.isNil(form.controls['volumeGroupName'])&& event.target.value.length > 0){ form.controls['volumeGroupName'].setValue(event.target.value + "_vol"); @@ -167,25 +148,21 @@ export class VfModuleControlGenerator { getVolumeGroupData(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, isALaCarte: boolean): FormControlModel { let validations: ValidatorModel[] = [ - new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', BasicControlGenerator.INSTANCE_NAME_REG_EX), + new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', ControlGeneratorUtil.INSTANCE_NAME_REG_EX), new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'Volume Group instance name is already in use, please pick another name', [this.store, serviceId, instance && instance.volumeGroupName]) ]; - // comment out because if not provided vid won't create VG - // if (!isEcompGeneratedNaming) { - // validations.push(new ValidatorModel(ValidatorOptions.required, 'is required')); - // } + return new InputFormControl({ controlName: 'volumeGroupName', displayName: 'Volume Group Name', dataTestId: 'volumeGroupName', - // placeHolder: (!isEcompGeneratedNaming) ? 'Volume Group Name' : 'Automatically generated when not provided', validations: validations, tooltip : 'When filled, VID will create a Volume Group by this name and associate with this module.\n' + 'When empty, the module is created without a Volume Group.', isVisible: this.shouldVGNameBeVisible(isEcompGeneratedNaming,isALaCarte), value: this.getDefaultVolumeGroupName(instance, isEcompGeneratedNaming), onKeypress: (event) => { - const pattern:RegExp = BasicControlGenerator.INSTANCE_NAME_REG_EX; + const pattern:RegExp = ControlGeneratorUtil.INSTANCE_NAME_REG_EX; if (pattern) { if (!pattern.test(event['key'])) { event.preventDefault(); @@ -203,90 +180,4 @@ export class VfModuleControlGenerator { return false; } - - getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => { - const service = this.store.getState().service.serviceInstance[serviceId]; - const globalCustomerId: string = service.globalSubscriberId; - const serviceType: string = service.subscriptionServiceType; - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: FormControlNames.TENANT_ID, - displayName: 'Tenant', - dataTestId: 'tenant', - placeHolder: 'Select Tenant', - name: "tenant", - isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId), - onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null, - value: instance ? instance.tenantId : null, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - onInit: instance ? this._basicControlGenerator.getSubscribeInitResult.bind( - this._aaiService, - this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : () => { - }, - }) - }; - - getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => { - const service = this.store.getState().service.serviceInstance[serviceId]; - const globalCustomerId: string = service.globalSubscriberId; - const serviceType: string = service.subscriptionServiceType; - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: 'lcpCloudRegionId', - displayName: 'LCP region', - dataTestId: 'lcpRegion', - placeHolder: 'Select LCP Region', - name: "lcpRegion", - isDisabled: false, - value: instance ? instance.lcpCloudRegionId : null, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - onInitSelectedField: ['lcpRegionList'], - onInit: this._basicControlGenerator.getSubscribeInitResult.bind( - this._aaiService, - this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)), - onChange: (param: string, form: FormGroup) => { - form.controls[FormControlNames.TENANT_ID].enable(); - form.controls[FormControlNames.TENANT_ID].reset(); - if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) { - this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => { - controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param]; - if (res.lcpRegionsTenantsMap[param]) { - controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0; - } - })); - } - - if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) { - form.controls['legacyRegion'].enable(); - controls.find(item => item.controlName === 'legacyRegion').isVisible = true; - - } else { - controls.find(item => item.controlName === 'legacyRegion').isVisible = false; - form.controls['legacyRegion'].setValue(null); - form.controls['legacyRegion'].reset(); - form.controls['legacyRegion'].disable(); - } - } - }) - }; - - getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => { - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: FormControlNames.ROLLBACK_ON_FAILURE, - displayName: 'Rollback on failure', - dataTestId: 'rollback', - isDisabled: false, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - value: instance ? instance.rollbackOnFailure : 'true', - onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions) - }) - }; - - getRollBackOnFailureOptions = (): Observable => { - return of([ - new SelectOption({id: 'true', name: 'Rollback'}), - new SelectOption({id: 'false', name: 'Don\'t Rollback'}) - ]); - }; } diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGenerator/vnf.control.generator.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGenerator/vnf.control.generator.spec.ts index 28d49d51b..018130eec 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGenerator/vnf.control.generator.spec.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGenerator/vnf.control.generator.spec.ts @@ -2,17 +2,16 @@ import {getTestBed, TestBed} from '@angular/core/testing'; import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; import {NgRedux} from '@angular-redux/store'; import {FormControlNames} from "../service.control.generator"; -import {BasicControlGenerator} from "../basic.control.generator"; +import {ControlGeneratorUtil} from "../control.generator.util.service"; import {AaiService} from "../../../../services/aaiService/aai.service"; import {GenericFormService} from "../../generic-form.service"; import {FormBuilder} from "@angular/forms"; import {FormControlModel, ValidatorModel, ValidatorOptions} from "../../../../models/formControlModels/formControl.model"; import {LogService} from "../../../../utils/log/log.service"; import {VnfControlGenerator} from "./vnf.control.generator"; -import {Observable} from "rxjs"; -import {SelectOption} from "../../../../models/selectOption"; import {FeatureFlagsService} from "../../../../services/featureFlag/feature-flags.service"; import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum"; +import {SharedControllersService} from "../sharedControlles/shared.controllers.service"; class MockAppStore { getState(){ @@ -918,7 +917,8 @@ describe('VNF Control Generator', () => { imports: [HttpClientTestingModule], providers: [VnfControlGenerator, GenericFormService, - BasicControlGenerator, + ControlGeneratorUtil, + SharedControllersService, AaiService, FormBuilder, LogService, @@ -1053,15 +1053,5 @@ describe('VNF Control Generator', () => { const instanceNameValidator: ValidatorModel = instanceNameControl.validations.find(val => val.validatorName === ValidatorOptions.pattern); expect(instanceNameValidator.validatorArg).toEqual(/^[a-zA-Z0-9._-]*$/); }); - - test('rollback should return observable of true, false', () => { - let result : Observable = service.getRollBackOnFailureOptions(); - result.subscribe((val)=>{ - expect(val).toEqual([ - new SelectOption({id: 'true', name: 'Rollback'}), - new SelectOption({id: 'false', name: 'Don\'t Rollback'}) - ]); - }); - }); }); diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGenerator/vnf.control.generator.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGenerator/vnf.control.generator.ts index c45fa968f..14b31b4b7 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGenerator/vnf.control.generator.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGenerator/vnf.control.generator.ts @@ -3,54 +3,24 @@ import {GenericFormService} from "../../generic-form.service"; import {AaiService} from "../../../../services/aaiService/aai.service"; import {NgRedux} from "@angular-redux/store"; import {HttpClient} from "@angular/common/http"; -import {BasicControlGenerator} from "../basic.control.generator"; -import { - FormControlModel, - ValidatorModel, - ValidatorOptions -} from "../../../../models/formControlModels/formControl.model"; +import {ControlGeneratorUtil} from "../control.generator.util.service"; +import {FormControlModel, ValidatorModel, ValidatorOptions} from "../../../../models/formControlModels/formControl.model"; import {LogService} from "../../../../utils/log/log.service"; import {VNFModel} from "../../../../models/vnfModel"; import {AppState} from "../../../../store/reducers"; import {FormGroup} from "@angular/forms"; -import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model"; import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum"; -import {Observable, of} from "rxjs"; -import {SelectOption} from "../../../../models/selectOption"; import * as _ from 'lodash'; -import {Constants} from "../../../../utils/constants"; import {MultiselectFormControl} from "../../../../models/formControlModels/multiselectFormControl.model"; import {MultiSelectItem} from "../../../formControls/component/multiselect/multiselect.model"; - -export enum FormControlNames { - INSTANCE_NAME = 'instanceName', - GLOBAL_SUBSCRIBER_ID = 'globalSubscriberId', - SUBSCRIPTION_SERVICE_TYPE = 'subscriptionServiceType', - PRODUCT_FAMILY_ID = 'productFamilyId', - LCPCLOUD_REGION_ID = 'lcpCloudRegionId', - TENANT_ID = 'tenantId', - AICZONE_ID = 'aicZoneId', - PROJECT_NAME = 'projectName', - OWNING_ENTITY_ID = 'owningEntityId', - ROLLBACK_ON_FAILURE = 'rollbackOnFailure', - PAUSE = 'pause' -} - -enum InputType { - LCP_REGION = "lcpCloudRegionId", - TENANT = "tenantId", - LOB = "lineOfBusiness", - PLATFORM = "platformName", - ROLLBACK = "rollbackOnFailure", - PRODUCT_FAMILY = "productFamilyId", - VG = "volumeGroupName" -} +import {SharedControllersService} from "../sharedControlles/shared.controllers.service"; @Injectable() export class VnfControlGenerator { aaiService: AaiService; constructor(private genericFormService: GenericFormService, - private _basicControlGenerator: BasicControlGenerator, + private _basicControlGenerator: ControlGeneratorUtil, + private _sharedControllersService : SharedControllersService, private store: NgRedux, private http: HttpClient, private _aaiService: AaiService, @@ -80,12 +50,12 @@ export class VnfControlGenerator { if (!_.isNil(vnfModel)) { result.push(this.getInstanceName(vnfInstance, serviceId, vnfName, vnfModel.isEcompGeneratedNaming)); - result.push(this._basicControlGenerator.getProductFamilyControl(vnfInstance, result, false)); - result.push(this.getLcpRegionControl(serviceId, vnfInstance, result)); - result.push(this._basicControlGenerator.getLegacyRegion(vnfInstance)); - result.push(this.getTenantControl(serviceId, vnfInstance, result)); + result.push(this._sharedControllersService.getProductFamilyControl(vnfInstance, result, false)); + result.push(this._sharedControllersService.getLcpRegionControl(serviceId, vnfInstance, result)); + result.push(this._sharedControllersService.getLegacyRegion(vnfInstance)); + result.push(this._sharedControllersService.getTenantControl(serviceId, vnfInstance)); result.push(this.getPlatformMultiselectControl(vnfInstance, result, flags['FLAG_2002_VNF_PLATFORM_MULTI_SELECT'])); - result.push(this.getLineOfBusinessControl(vnfInstance, result)); + result.push(this._sharedControllersService.getLineOfBusinessControl(vnfInstance)); } return result; } @@ -104,45 +74,22 @@ export class VnfControlGenerator { if (!_.isNil(vnfModel)) { const flags = this.store.getState().global.flags; result.push(this.getInstanceName(vnfInstance, serviceId, vnfName, vnfModel.isEcompGeneratedNaming)); - result.push(this._basicControlGenerator.getProductFamilyControl(vnfInstance, result, false)); - result.push(this.getLcpRegionControl(serviceId, vnfInstance, result)); - result.push(this._basicControlGenerator.getLegacyRegion(vnfInstance)); - result.push(this.getTenantControl(serviceId, vnfInstance, result)); + result.push(this._sharedControllersService.getProductFamilyControl(vnfInstance, result, false)); + result.push(this._sharedControllersService.getLcpRegionControl(serviceId, vnfInstance, result)); + result.push(this._sharedControllersService.getLegacyRegion(vnfInstance)); + result.push(this._sharedControllersService.getTenantControl(serviceId, vnfInstance)); result.push(this.getPlatformMultiselectControl(vnfInstance, result, flags['FLAG_2002_VNF_PLATFORM_MULTI_SELECT'])); - result.push(this.getLineOfBusinessControl(vnfInstance, result)); - result.push(this.getRollbackOnFailureControl(vnfInstance, result)); + result.push(this._sharedControllersService.getLineOfBusinessControl(vnfInstance)); + result.push(this._sharedControllersService.getRollbackOnFailureControl(vnfInstance)); } return result; } - isInputShouldBeShown = (inputType: any): boolean => { - let vnfInputs = [InputType.LCP_REGION, InputType.LOB, InputType.TENANT, InputType.PRODUCT_FAMILY, InputType.PLATFORM, InputType.ROLLBACK]; - return vnfInputs.indexOf(inputType) > -1; - }; - getInstanceName(instance : any, serviceId : string, vnfName : string, isEcompGeneratedNaming: boolean): FormControlModel { const vnfModel : VNFModel = this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfName]; - return this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, vnfModel); + return this._sharedControllersService.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, vnfModel); } - getLineOfBusinessControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => { - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: 'lineOfBusiness', - displayName: 'Line of business', - dataTestId: 'lineOfBusiness', - placeHolder: 'Select Line Of Business', - isDisabled: false, - name: "lineOfBusiness", - value: instance ? instance.lineOfBusiness : null, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - onInitSelectedField: ['lineOfBusinessList'], - onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters) - }) - }; - - - getPlatformMultiselectControl = (instance: any, controls: FormControlModel[], isMultiSelected: boolean) : MultiselectFormControl => { return new MultiselectFormControl({ type: FormControlType.MULTI_SELECT , @@ -170,90 +117,4 @@ export class VnfControlGenerator { } }); }; - - getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => { - const service = this.store.getState().service.serviceInstance[serviceId]; - const globalCustomerId: string = service.globalSubscriberId; - const serviceType: string = service.subscriptionServiceType; - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: FormControlNames.TENANT_ID, - displayName: 'Tenant', - dataTestId: 'tenant', - placeHolder: 'Select Tenant', - name: "tenant", - isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId), - onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null, - value: instance ? instance.tenantId : null, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - onInit : instance ? this._basicControlGenerator.getSubscribeInitResult.bind( - this._aaiService, - this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : ()=>{}, - }) - }; - - getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => { - const service = this.store.getState().service.serviceInstance[serviceId]; - const globalCustomerId: string = service.globalSubscriberId; - const serviceType: string = service.subscriptionServiceType; - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: 'lcpCloudRegionId', - displayName: 'LCP region', - dataTestId: 'lcpRegion', - placeHolder: 'Select LCP Region', - name: "lcpRegion", - isDisabled: false, - value: instance ? instance.lcpCloudRegionId : null, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - onInitSelectedField: ['lcpRegionList'], - onInit: this._basicControlGenerator.getSubscribeInitResult.bind( - this._aaiService, - this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)), - onChange: (param: string, form: FormGroup) => { - form.controls[FormControlNames.TENANT_ID].enable(); - form.controls[FormControlNames.TENANT_ID].reset(); - if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) { - this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => { - controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param]; - if(res.lcpRegionsTenantsMap[param]){ - controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0; - } - })); - } - - if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) { - form.controls['legacyRegion'].enable(); - controls.find(item => item.controlName === 'legacyRegion').isVisible = true; - - } else { - controls.find(item => item.controlName === 'legacyRegion').isVisible = false; - form.controls['legacyRegion'].setValue(null); - form.controls['legacyRegion'].reset(); - form.controls['legacyRegion'].disable(); - } - } - }) - }; - - getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => { - return new DropdownFormControl({ - type: FormControlType.DROPDOWN, - controlName: FormControlNames.ROLLBACK_ON_FAILURE, - displayName: 'Rollback on failure', - dataTestId: 'rollback', - placeHolder: 'Rollback on failure', - isDisabled: false, - validations: [new ValidatorModel(ValidatorOptions.required, 'is required')], - value: instance ? instance.rollbackOnFailure : 'true', - onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions) - }) - }; - - getRollBackOnFailureOptions = (): Observable => { - return of([ - new SelectOption({id: 'true', name: 'Rollback'}), - new SelectOption({id: 'false', name: 'Don\'t Rollback'}) - ]); - }; } diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGroupGenerator/vnfGroup.control.generator.spec.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGroupGenerator/vnfGroup.control.generator.spec.ts index 81cfd9614..c485dc04e 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGroupGenerator/vnfGroup.control.generator.spec.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGroupGenerator/vnfGroup.control.generator.spec.ts @@ -2,7 +2,7 @@ import {getTestBed, TestBed} from '@angular/core/testing'; import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; import {NgRedux} from '@angular-redux/store'; import {FormControlNames} from "../service.control.generator"; -import {BasicControlGenerator} from "../basic.control.generator"; +import {ControlGeneratorUtil} from "../control.generator.util.service"; import {AaiService} from "../../../../services/aaiService/aai.service"; import {GenericFormService} from "../../generic-form.service"; import {FormBuilder} from "@angular/forms"; @@ -12,6 +12,7 @@ import {VnfGroupControlGenerator} from "./vnfGroup.control.generator"; import {Observable} from "rxjs"; import {SelectOption} from "../../../../models/selectOption"; import {FeatureFlagsService} from "../../../../services/featureFlag/feature-flags.service"; +import {SharedControllersService} from "../sharedControlles/shared.controllers.service"; class MockAppStore { getState(){ @@ -217,7 +218,8 @@ describe('VNF Group Control Generator', () => { imports: [HttpClientTestingModule], providers: [VnfGroupControlGenerator, GenericFormService, - BasicControlGenerator, + ControlGeneratorUtil, + SharedControllersService, AaiService, FormBuilder, LogService, diff --git a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGroupGenerator/vnfGroup.control.generator.ts b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGroupGenerator/vnfGroup.control.generator.ts index 45f5ffa63..0fee0c223 100644 --- a/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGroupGenerator/vnfGroup.control.generator.ts +++ b/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vnfGroupGenerator/vnfGroup.control.generator.ts @@ -1,7 +1,7 @@ import {Injectable} from "@angular/core"; import {AaiService} from "../../../../services/aaiService/aai.service"; import {NgRedux} from "@angular-redux/store"; -import {BasicControlGenerator} from "../basic.control.generator"; +import {ControlGeneratorUtil} from "../control.generator.util.service"; import { FormControlModel, ValidatorModel, @@ -15,6 +15,7 @@ import {SelectOption} from "../../../../models/selectOption"; import {VnfGroupModel} from "../../../../models/vnfGroupModel"; import * as _ from 'lodash'; import {Observable, of} from "rxjs"; +import {SharedControllersService} from "../sharedControlles/shared.controllers.service"; export enum FormControlNames { @@ -22,14 +23,12 @@ export enum FormControlNames { ROLLBACK_ON_FAILURE = 'rollbackOnFailure', } -enum InputType { - ROLLBACK = "rollbackOnFailure" -} - @Injectable() export class VnfGroupControlGenerator { aaiService: AaiService; - constructor(private _basicControlGenerator: BasicControlGenerator, + + constructor(private _basicControlGenerator: ControlGeneratorUtil, + private _sharedControllersService: SharedControllersService, private store: NgRedux, private _aaiService: AaiService, private _logService: LogService) { @@ -80,19 +79,14 @@ export class VnfGroupControlGenerator { return result; } - isInputShouldBeShown = (inputType: any): boolean => { - let vnfGroupInputs = [InputType.ROLLBACK]; - return vnfGroupInputs.indexOf(inputType) > -1; - }; - - getDefaultInstanceName(instance : any, serviceId : string, vnfGroupName : string) : string { + getDefaultInstanceName(instance: any, serviceId: string, vnfGroupName: string): string { const vnfGroupModel: VnfGroupModel = this.store.getState().service.serviceHierarchy[serviceId].vnfGroups[vnfGroupName]; return this._basicControlGenerator.getDefaultInstanceName(instance, vnfGroupModel); } - getInstanceName(instance : any, serviceId : string, vnfGroupName : string, isEcompGeneratedNaming: boolean): FormControlModel { - const vnfGroupModel : VnfGroupModel = this.store.getState().service.serviceHierarchy[serviceId].vnfGroups[vnfGroupName]; - return this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, vnfGroupModel); + getInstanceName(instance: any, serviceId: string, vnfGroupName: string, isEcompGeneratedNaming: boolean): FormControlModel { + const vnfGroupModel: VnfGroupModel = this.store.getState().service.serviceHierarchy[serviceId].vnfGroups[vnfGroupName]; + return this._sharedControllersService.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, vnfGroupModel); } getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => { -- cgit 1.2.3-korg