1 import {Injectable} from "@angular/core";
2 import {GenericFormService} from "../../generic-form.service";
3 import {AaiService} from "../../../../services/aaiService/aai.service";
4 import {NgRedux} from "@angular-redux/store";
5 import {HttpClient} from "@angular/common/http";
6 import {ControlGeneratorUtil, SDN_C_PRE_LOAD} from "../control.generator.util.service";
8 CustomValidatorOptions,
12 } from "../../../../models/formControlModels/formControl.model";
13 import {LogService} from "../../../../utils/log/log.service";
14 import {AppState} from "../../../../store/reducers";
15 import {FormGroup} from "@angular/forms";
16 import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model";
17 import {VfModuleInstance} from "../../../../models/vfModuleInstance";
18 import {VfModule} from "../../../../models/vfModule";
19 import {VNFModel} from "../../../../models/vnfModel";
20 import {VnfInstance} from "../../../../models/vnfInstance";
21 import * as _ from 'lodash';
22 import {SharedControllersService} from "../sharedControlles/shared.controllers.service";
23 import {MessageModal} from "../../../messageModal/message-modal.service";
24 import {ButtonType} from "../../../customModal/models/button.type";
25 import {SharedTreeService} from "../../../../../drawingBoard/service-planning/objectsToTree/shared.tree.service";
26 import {FeatureFlagsService, Features} from "../../../../services/featureFlag/feature-flags.service";
28 export enum FormControlNames {
29 INSTANCE_NAME = 'instanceName',
30 VOLUME_GROUP_NAME = 'volumeGroupName',
31 LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
32 LEGACY_REGION = 'legacyRegion',
33 TENANT_ID = 'tenantId',
34 ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
39 export class VfModuleControlGenerator {
40 aaiService: AaiService;
41 vfModuleModel: VfModule;
42 isUpdateMode: boolean;
44 constructor(private genericFormService: GenericFormService,
45 private _basicControlGenerator: ControlGeneratorUtil,
46 private _sharedControllersService: SharedControllersService,
47 private _sharedTreeService: SharedTreeService,
48 private store: NgRedux<AppState>,
49 private http: HttpClient,
50 private _aaiService: AaiService,
51 private _logService: LogService,
52 private _featureFlagsService:FeatureFlagsService) {
53 this.aaiService = _aaiService;
56 getVfModuleInstance = (serviceId: string, vnfStoreKey: string, UUIDData: Object, isUpdateMode: boolean): VfModuleInstance => {
57 let vfModuleInstance: VfModuleInstance = null;
58 if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] &&
59 _.has(this.store.getState().service.serviceInstance[serviceId].vnfs, vnfStoreKey) &&
60 _.has(this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules, UUIDData['modelName'])) {
61 vfModuleInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules[UUIDData['modelName']][UUIDData['vFModuleStoreKey']]);
63 return vfModuleInstance;
66 extractVfAccordingToVfModuleUuid(serviceId: string, UUIDData: Object): VfModule {
67 const vfModule = this.store.getState().service.serviceHierarchy[serviceId].vfModules[UUIDData['modelName']];
68 this.vfModuleModel = vfModule;
72 getMacroFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData: Object, isUpdateMode: boolean): FormControlModel[] {
73 this.isUpdateMode = isUpdateMode;
74 this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
75 if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
77 this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
82 const vfModuleInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode));
83 const vfModuleModel = this.vfModuleModel;
84 const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
85 const vnfModel = this.newVNFModel(serviceId, vnf);
87 let result: FormControlModel[] = [];
89 if (!_.isNil(vfModuleModel)) {
90 result = this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, false);
92 if (this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
93 result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
98 private newVNFModel(serviceId: string, vnf: VnfInstance) {
99 const vnfModelName: string = this._sharedTreeService.modelUniqueNameOrId(vnf);
101 const serviceModelFromHierarchy = this.store.getState().service.serviceHierarchy[serviceId];
102 const model = this._sharedTreeService.modelByIdentifiers(serviceModelFromHierarchy, "vnfs", vnfModelName);
103 return new VNFModel(model);
106 pushInstanceAndVGToForm(result: FormControlModel[], vfModuleElement: any, serviceId: string, vnfModel: any, isALaCarte: boolean): FormControlModel[] {
107 result.push(this.getInstanceName(vfModuleElement, serviceId, vnfModel.isEcompGeneratedNaming));
108 if (this.vfModuleModel.volumeGroupAllowed) {
109 result.push(this.getVolumeGroupData(vfModuleElement, serviceId, vnfModel.isEcompGeneratedNaming, isALaCarte));
114 getAlaCarteFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData: Object, isUpdateMode: boolean): FormControlModel[] {
115 this.isUpdateMode = isUpdateMode;
116 this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
117 if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
119 this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
123 const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
124 const vnfModel = this.newVNFModel(serviceId, vnf);
126 const vfModuleInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode));
127 let result: FormControlModel[] = [];
128 this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, true);
129 if( !this._featureFlagsService.getFlagState(Features.FLAG_2006_VFMODULE_TAKES_TENANT_AND_REGION_FROM_VNF)) {
130 result.push(this._sharedControllersService.getLcpRegionControl(serviceId, vfModuleInstance, result));
131 result.push(this._sharedControllersService.getLegacyRegion(vfModuleInstance));
132 result.push(this._sharedControllersService.getTenantControl(serviceId, vfModuleInstance));
134 result.push(this._sharedControllersService.getRollbackOnFailureControl(vfModuleInstance));
135 result.push(this._sharedControllersService.getSDNCControl(vfModuleInstance, this.getSdncExtraContents()));
136 if (this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
137 result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
142 getSdncExtraContents() : object[] {
144 !!this.store.getState().global.flags['FLAG_2006_VFM_SDNC_PRELOAD_FILES'] ? {
146 dataTestId: 'sdnc_pereload_upload_link',
147 uploadMethod: (form: FormGroup) : Promise<boolean> => {
148 // this -> files item
149 return this._aaiService.sdncPreload().toPromise()
150 .then((response : boolean)=>{
156 isDisabled: (form: FormGroup): boolean => {
157 return !form.controls[SDN_C_PRE_LOAD].value;
159 onSuccess: (form: FormGroup): void => {
160 MessageModal.showMessageModal({
161 text: 'The pre-load file(s) have been uploaded successfully.',
164 buttons: [{type: ButtonType.success, size: 'large', text: 'OK', closeModal: true}]
167 onFailed: (form: FormGroup) : void=> {
168 MessageModal.showMessageModal({
169 text: 'Failed to upload one or more of the files, please retry.',
172 buttons: [{type: ButtonType.error, size: 'large', text: 'OK', closeModal: true}]
180 getInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): FormControlModel {
181 let formControlModel: FormControlModel = this._sharedControllersService.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, this.vfModuleModel);
182 formControlModel.onBlur = (event, form: FormGroup) => {
183 if (!_.isNil(form.controls['volumeGroupName']) && event.target.value.length > 0) {
184 form.controls['volumeGroupName'].setValue(event.target.value + "_vol");
188 return formControlModel;
191 getDefaultVolumeGroupName(instance: any, isEcompGeneratedNaming: boolean): string {
192 if ((!_.isNil(instance) && instance.volumeGroupName)) {
193 return instance.volumeGroupName;
195 if (isEcompGeneratedNaming) {
198 return this._basicControlGenerator.getDefaultInstanceName(instance, this.vfModuleModel) + "_vol";
201 getVolumeGroupData(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, isALaCarte: boolean): FormControlModel {
202 let validations: ValidatorModel[] = [
203 new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', ControlGeneratorUtil.INSTANCE_NAME_REG_EX),
204 new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'Volume Group instance name is already in use, please pick another name', [this.store, serviceId, instance && instance.volumeGroupName])
207 return new InputFormControl({
208 controlName: 'volumeGroupName',
209 displayName: 'Volume Group Name',
210 dataTestId: 'volumeGroupName',
211 validations: validations,
212 tooltip: 'When filled, VID will create a Volume Group by this name and associate with this module.\n' +
213 'When empty, the module is created without a Volume Group.',
214 isVisible: this.shouldVGNameBeVisible(isEcompGeneratedNaming, isALaCarte),
215 value: this.getDefaultVolumeGroupName(instance, isEcompGeneratedNaming),
216 onKeypress: (event) => {
217 const pattern: RegExp = ControlGeneratorUtil.INSTANCE_NAME_REG_EX;
219 if (!pattern.test(event['key'])) {
220 event.preventDefault();
228 private shouldVGNameBeVisible(isEcompGeneratedNaming: boolean, isALaCarte: boolean) {
229 if ((!isALaCarte && !isEcompGeneratedNaming) || isALaCarte) {