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";
27 import {PauseStatus} from "../../../../models/serviceInstanceActions";
29 export enum FormControlNames {
30 INSTANCE_NAME = 'instanceName',
31 VOLUME_GROUP_NAME = 'volumeGroupName',
32 LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
33 LEGACY_REGION = 'legacyRegion',
34 TENANT_ID = 'tenantId',
35 ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
40 export class VfModuleControlGenerator {
41 aaiService: AaiService;
42 vfModuleModel: VfModule;
43 isUpdateMode: boolean;
45 constructor(private genericFormService: GenericFormService,
46 private _basicControlGenerator: ControlGeneratorUtil,
47 private _sharedControllersService: SharedControllersService,
48 private _sharedTreeService: SharedTreeService,
49 private store: NgRedux<AppState>,
50 private http: HttpClient,
51 private _aaiService: AaiService,
52 private _logService: LogService,
53 private _featureFlagsService:FeatureFlagsService) {
54 this.aaiService = _aaiService;
57 getVfModuleInstance = (serviceId: string, vnfStoreKey: string, UUIDData: Object, isUpdateMode: boolean): VfModuleInstance => {
58 let vfModuleInstance: VfModuleInstance = null;
59 if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] &&
60 _.has(this.store.getState().service.serviceInstance[serviceId].vnfs, vnfStoreKey) &&
61 _.has(this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules, UUIDData['modelName'])) {
62 vfModuleInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules[UUIDData['modelName']][UUIDData['vFModuleStoreKey']]);
64 return vfModuleInstance;
67 extractVfAccordingToVfModuleUuid(serviceId: string, UUIDData: Object): VfModule {
68 const vfModule = this.store.getState().service.serviceHierarchy[serviceId].vfModules[UUIDData['modelName']];
69 this.vfModuleModel = vfModule;
73 getMacroFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData: Object, isUpdateMode: boolean): FormControlModel[] {
74 this.isUpdateMode = isUpdateMode;
75 this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
76 if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
78 this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
83 const vfModuleInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode));
84 const vfModuleModel = this.vfModuleModel;
85 const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
86 const vnfModel = this.newVNFModel(serviceId, vnf);
88 let result: FormControlModel[] = [];
90 if (!_.isNil(vfModuleModel)) {
91 result = this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, false);
93 if (this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
94 result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
99 private newVNFModel(serviceId: string, vnf: VnfInstance) {
100 const vnfModelName: string = this._sharedTreeService.modelUniqueNameOrId(vnf);
102 const serviceModelFromHierarchy = this.store.getState().service.serviceHierarchy[serviceId];
103 const model = this._sharedTreeService.modelByIdentifiers(serviceModelFromHierarchy, "vnfs", vnfModelName);
104 return new VNFModel(model);
107 pushInstanceAndVGToForm(result: FormControlModel[], vfModuleElement: any, serviceId: string, vnfModel: any, isALaCarte: boolean): FormControlModel[] {
108 result.push(this.getInstanceName(vfModuleElement, serviceId, vnfModel.isEcompGeneratedNaming));
109 if (this.vfModuleModel.volumeGroupAllowed) {
110 result.push(this.getVolumeGroupData(vfModuleElement, serviceId, vnfModel.isEcompGeneratedNaming, isALaCarte));
115 getAlaCarteFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData: Object, isUpdateMode: boolean): FormControlModel[] {
116 this.isUpdateMode = isUpdateMode;
117 this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
118 if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
120 this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
124 const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
125 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, false, this.getSdncExtraContents()));
136 result.push(this._sharedControllersService.getPauseInstantiation(vfModuleInstance));
137 if (this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
138 result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
143 getSdncExtraContents() : object[] {
145 !!this.store.getState().global.flags['FLAG_2006_VFM_SDNC_PRELOAD_FILES'] ? {
147 dataTestId: 'sdnc_pereload_upload_link',
148 uploadMethod: (form: FormGroup) : Promise<boolean> => {
149 // this -> files item
150 return this._aaiService.sdncPreload().toPromise()
151 .then((response : boolean)=>{
157 isDisabled: (form: FormGroup): boolean => {
158 return !form.controls[SDN_C_PRE_LOAD].value;
160 onSuccess: (form: FormGroup): void => {
161 MessageModal.showMessageModal({
162 text: 'The pre-load file(s) have been uploaded successfully.',
165 buttons: [{type: ButtonType.success, size: 'large', text: 'OK', closeModal: true}]
168 onFailed: (form: FormGroup) : void=> {
169 MessageModal.showMessageModal({
170 text: 'Failed to upload one or more of the files, please retry.',
173 buttons: [{type: ButtonType.error, size: 'large', text: 'OK', closeModal: true}]
181 getInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): FormControlModel {
182 let formControlModel: FormControlModel = this._sharedControllersService.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, this.vfModuleModel);
183 formControlModel.onBlur = (event, form: FormGroup) => {
184 if (!_.isNil(form.controls['volumeGroupName']) && event.target.value.length > 0) {
185 form.controls['volumeGroupName'].setValue(event.target.value + "_vol");
189 return formControlModel;
192 getDefaultVolumeGroupName(instance: any, isEcompGeneratedNaming: boolean): string {
193 if ((!_.isNil(instance) && instance.volumeGroupName)) {
194 return instance.volumeGroupName;
196 if (isEcompGeneratedNaming) {
199 return this._basicControlGenerator.getDefaultInstanceName(instance, this.vfModuleModel) + "_vol";
202 getVolumeGroupData(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, isALaCarte: boolean): FormControlModel {
203 let validations: ValidatorModel[] = [
204 new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', ControlGeneratorUtil.INSTANCE_NAME_REG_EX),
205 new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'Volume Group instance name is already in use, please pick another name', [this.store, serviceId, instance && instance.volumeGroupName])
208 return new InputFormControl({
209 controlName: 'volumeGroupName',
210 displayName: 'Volume Group Name',
211 dataTestId: 'volumeGroupName',
212 validations: validations,
213 tooltip: 'When filled, VID will create a Volume Group by this name and associate with this module.\n' +
214 'When empty, the module is created without a Volume Group.',
215 isVisible: this.shouldVGNameBeVisible(isEcompGeneratedNaming, isALaCarte),
216 value: this.getDefaultVolumeGroupName(instance, isEcompGeneratedNaming),
217 onKeypress: (event) => {
218 const pattern: RegExp = ControlGeneratorUtil.INSTANCE_NAME_REG_EX;
220 if (!pattern.test(event['key'])) {
221 event.preventDefault();
229 private shouldVGNameBeVisible(isEcompGeneratedNaming: boolean, isALaCarte: boolean) {
230 if ((!isALaCarte && !isEcompGeneratedNaming) || isALaCarte) {