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()));
137 if(this._featureFlagsService.getFlagState(Features.FLAG_2008_PAUSE_INSTANTIATION_ON_VFMODULE_POPUP)){
138 console.log(Features.FLAG_2008_PAUSE_INSTANTIATION_ON_VFMODULE_POPUP);
139 result.push(this._sharedControllersService.getPauseInstantiation(vfModuleInstance));
141 if (this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
142 result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
147 getSdncExtraContents() : object[] {
149 !!this.store.getState().global.flags['FLAG_2006_VFM_SDNC_PRELOAD_FILES'] ? {
151 dataTestId: 'sdnc_pereload_upload_link',
152 uploadMethod: (form: FormGroup) : Promise<boolean> => {
153 // this -> files item
154 return this._aaiService.sdncPreload().toPromise()
155 .then((response : boolean)=>{
161 isDisabled: (form: FormGroup): boolean => {
162 return !form.controls[SDN_C_PRE_LOAD].value;
164 onSuccess: (form: FormGroup): void => {
165 MessageModal.showMessageModal({
166 text: 'The pre-load file(s) have been uploaded successfully.',
169 buttons: [{type: ButtonType.success, size: 'large', text: 'OK', closeModal: true}]
172 onFailed: (form: FormGroup) : void=> {
173 MessageModal.showMessageModal({
174 text: 'Failed to upload one or more of the files, please retry.',
177 buttons: [{type: ButtonType.error, size: 'large', text: 'OK', closeModal: true}]
185 getInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): FormControlModel {
186 let formControlModel: FormControlModel = this._sharedControllersService.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, this.vfModuleModel);
187 formControlModel.onBlur = (event, form: FormGroup) => {
188 if (!_.isNil(form.controls['volumeGroupName']) && event.target.value.length > 0) {
189 form.controls['volumeGroupName'].setValue(event.target.value + "_vol");
193 return formControlModel;
196 getDefaultVolumeGroupName(instance: any, isEcompGeneratedNaming: boolean): string {
197 if ((!_.isNil(instance) && instance.volumeGroupName)) {
198 return instance.volumeGroupName;
200 if (isEcompGeneratedNaming) {
203 return this._basicControlGenerator.getDefaultInstanceName(instance, this.vfModuleModel) + "_vol";
206 getVolumeGroupData(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, isALaCarte: boolean): FormControlModel {
207 let validations: ValidatorModel[] = [
208 new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', ControlGeneratorUtil.INSTANCE_NAME_REG_EX),
209 new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'Volume Group instance name is already in use, please pick another name', [this.store, serviceId, instance && instance.volumeGroupName])
212 return new InputFormControl({
213 controlName: 'volumeGroupName',
214 displayName: 'Volume Group Name',
215 dataTestId: 'volumeGroupName',
216 validations: validations,
217 tooltip: 'When filled, VID will create a Volume Group by this name and associate with this module.\n' +
218 'When empty, the module is created without a Volume Group.',
219 isVisible: this.shouldVGNameBeVisible(isEcompGeneratedNaming, isALaCarte),
220 value: this.getDefaultVolumeGroupName(instance, isEcompGeneratedNaming),
221 onKeypress: (event) => {
222 const pattern: RegExp = ControlGeneratorUtil.INSTANCE_NAME_REG_EX;
224 if (!pattern.test(event['key'])) {
225 event.preventDefault();
233 private shouldVGNameBeVisible(isEcompGeneratedNaming: boolean, isALaCarte: boolean) {
234 if ((!isALaCarte && !isEcompGeneratedNaming) || isALaCarte) {