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";
27 export enum FormControlNames {
28 INSTANCE_NAME = 'instanceName',
29 VOLUME_GROUP_NAME = 'volumeGroupName',
30 LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
31 LEGACY_REGION = 'legacyRegion',
32 TENANT_ID = 'tenantId',
33 ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
38 export class VfModuleControlGenerator {
39 aaiService: AaiService;
40 vfModuleModel: VfModule;
41 isUpdateMode: boolean;
43 constructor(private genericFormService: GenericFormService,
44 private _basicControlGenerator: ControlGeneratorUtil,
45 private _sharedControllersService: SharedControllersService,
46 private _sharedTreeService: SharedTreeService,
47 private store: NgRedux<AppState>,
48 private http: HttpClient,
49 private _aaiService: AaiService,
50 private _logService: LogService) {
51 this.aaiService = _aaiService;
54 getVfModuleInstance = (serviceId: string, vnfStoreKey: string, UUIDData: Object, isUpdateMode: boolean): VfModuleInstance => {
55 let vfModuleInstance: VfModuleInstance = null;
56 if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] &&
57 _.has(this.store.getState().service.serviceInstance[serviceId].vnfs, vnfStoreKey) &&
58 _.has(this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules, UUIDData['modelName'])) {
59 vfModuleInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules[UUIDData['modelName']][UUIDData['vFModuleStoreKey']]);
61 return vfModuleInstance;
64 extractVfAccordingToVfModuleUuid(serviceId: string, UUIDData: Object): VfModule {
65 const vfModule = this.store.getState().service.serviceHierarchy[serviceId].vfModules[UUIDData['modelName']];
66 this.vfModuleModel = vfModule;
70 getMacroFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData: Object, isUpdateMode: boolean): FormControlModel[] {
71 this.isUpdateMode = isUpdateMode;
72 this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
73 if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
75 this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
80 const vfModuleInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode));
81 const vfModuleModel = this.vfModuleModel;
82 const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
83 const vnfModel = this.newVNFModel(serviceId, vnf);
85 let result: FormControlModel[] = [];
87 if (!_.isNil(vfModuleModel)) {
88 result = this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, false);
90 if (this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
91 result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
96 private newVNFModel(serviceId: string, vnf: VnfInstance) {
97 const vnfModelName: string = vnf.originalName;
99 const serviceModelFromHierarchy = this.store.getState().service.serviceHierarchy[serviceId];
100 const model = this._sharedTreeService.modelByIdentifier(serviceModelFromHierarchy, "vnfs", vnfModelName);
101 return new VNFModel(model);
104 pushInstanceAndVGToForm(result: FormControlModel[], vfModuleElement: any, serviceId: string, vnfModel: any, isALaCarte: boolean): FormControlModel[] {
105 result.push(this.getInstanceName(vfModuleElement, serviceId, vnfModel.isEcompGeneratedNaming));
106 if (this.vfModuleModel.volumeGroupAllowed) {
107 result.push(this.getVolumeGroupData(vfModuleElement, serviceId, vnfModel.isEcompGeneratedNaming, isALaCarte));
112 getAlaCarteFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData: Object, isUpdateMode: boolean): FormControlModel[] {
113 this.isUpdateMode = isUpdateMode;
114 this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
115 if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
117 this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
121 const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
122 const vnfModel = this.newVNFModel(serviceId, vnf);
124 const vfModuleInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode));
125 let result: FormControlModel[] = [];
126 this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, true);
127 result.push(this._sharedControllersService.getLcpRegionControl(serviceId, vfModuleInstance, result));
128 result.push(this._sharedControllersService.getLegacyRegion(vfModuleInstance));
129 result.push(this._sharedControllersService.getTenantControl(serviceId, vfModuleInstance));
130 result.push(this._sharedControllersService.getRollbackOnFailureControl(vfModuleInstance));
131 result.push(this._sharedControllersService.getSDNCControl(vfModuleInstance, this.getSdncExtraContents()));
132 if (this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
133 result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
138 getSdncExtraContents() : object[] {
140 !!this.store.getState().global.flags['FLAG_2006_VFM_SDNC_PRELOAD_FILES'] ? {
142 dataTestId: 'sdnc_pereload_upload_link',
143 uploadMethod: (form: FormGroup) : Promise<boolean> => {
144 // this -> files item
145 return this._aaiService.sdncPreload().toPromise()
146 .then((response : boolean)=>{
152 isDisabled: (form: FormGroup): boolean => {
153 return !form.controls[SDN_C_PRE_LOAD].value;
155 onSuccess: (form: FormGroup): void => {
156 MessageModal.showMessageModal({
157 text: 'The pre-load file(s) have been uploaded successfully.',
160 buttons: [{type: ButtonType.success, size: 'large', text: 'OK', closeModal: true}]
163 onFailed: (form: FormGroup) : void=> {
164 MessageModal.showMessageModal({
165 text: 'Failed to upload one or more of the files, please retry.',
168 buttons: [{type: ButtonType.error, size: 'large', text: 'OK', closeModal: true}]
176 getInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): FormControlModel {
177 let formControlModel: FormControlModel = this._sharedControllersService.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, this.vfModuleModel);
178 formControlModel.onBlur = (event, form: FormGroup) => {
179 if (!_.isNil(form.controls['volumeGroupName']) && event.target.value.length > 0) {
180 form.controls['volumeGroupName'].setValue(event.target.value + "_vol");
184 return formControlModel;
187 getDefaultVolumeGroupName(instance: any, isEcompGeneratedNaming: boolean): string {
188 if ((!_.isNil(instance) && instance.volumeGroupName)) {
189 return instance.volumeGroupName;
191 if (isEcompGeneratedNaming) {
194 return this._basicControlGenerator.getDefaultInstanceName(instance, this.vfModuleModel) + "_vol";
197 getVolumeGroupData(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, isALaCarte: boolean): FormControlModel {
198 let validations: ValidatorModel[] = [
199 new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', ControlGeneratorUtil.INSTANCE_NAME_REG_EX),
200 new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'Volume Group instance name is already in use, please pick another name', [this.store, serviceId, instance && instance.volumeGroupName])
203 return new InputFormControl({
204 controlName: 'volumeGroupName',
205 displayName: 'Volume Group Name',
206 dataTestId: 'volumeGroupName',
207 validations: validations,
208 tooltip: 'When filled, VID will create a Volume Group by this name and associate with this module.\n' +
209 'When empty, the module is created without a Volume Group.',
210 isVisible: this.shouldVGNameBeVisible(isEcompGeneratedNaming, isALaCarte),
211 value: this.getDefaultVolumeGroupName(instance, isEcompGeneratedNaming),
212 onKeypress: (event) => {
213 const pattern: RegExp = ControlGeneratorUtil.INSTANCE_NAME_REG_EX;
215 if (!pattern.test(event['key'])) {
216 event.preventDefault();
224 private shouldVGNameBeVisible(isEcompGeneratedNaming: boolean, isALaCarte: boolean) {
225 if ((!isALaCarte && !isEcompGeneratedNaming) || isALaCarte) {