8c6af89dae2d7a4e5965d0267f414e9bd1638137
[vid.git] / vid-webpack-master / src / app / shared / components / genericForm / formControlsServices / vfModuleGenerator / vfModule.control.generator.ts
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";
7 import {
8   CustomValidatorOptions,
9   FormControlModel,
10   ValidatorModel,
11   ValidatorOptions
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";
28
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',
36 }
37
38
39 @Injectable()
40 export class VfModuleControlGenerator {
41   aaiService: AaiService;
42   vfModuleModel: VfModule;
43   isUpdateMode: boolean;
44
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;
55   }
56
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']]);
63     }
64     return vfModuleInstance;
65   };
66
67   extractVfAccordingToVfModuleUuid(serviceId: string, UUIDData: Object): VfModule {
68     const vfModule = this.store.getState().service.serviceHierarchy[serviceId].vfModules[UUIDData['modelName']];
69     this.vfModuleModel = vfModule;
70     return vfModule;
71   }
72
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)) {
77       if (isUpdateMode) {
78         this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
79         return [];
80       }
81     }
82
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);
87
88     let result: FormControlModel[] = [];
89
90     if (!_.isNil(vfModuleModel)) {
91       result = this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, false);
92     }
93     if (this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
94       result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
95     }
96     return result;
97   }
98
99   private newVNFModel(serviceId: string, vnf: VnfInstance) {
100     const vnfModelName: string = this._sharedTreeService.modelUniqueNameOrId(vnf);
101
102     const serviceModelFromHierarchy = this.store.getState().service.serviceHierarchy[serviceId];
103     const model = this._sharedTreeService.modelByIdentifiers(serviceModelFromHierarchy, "vnfs", vnfModelName);
104     return new VNFModel(model);
105   }
106
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));
111     }
112     return result;
113   }
114
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)) {
119       if (isUpdateMode) {
120         this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
121         return [];
122       }
123     }
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));
133     }
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);
139     }
140     return result;
141   }
142
143   getSdncExtraContents() : object[] {
144     return _.compact([
145       !!this.store.getState().global.flags['FLAG_2006_VFM_SDNC_PRELOAD_FILES'] ? {
146         type: 'UPLOAD_FILE',
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)=>{
152               return response;
153             }).catch(err => {
154               return false;
155             });
156         },
157         isDisabled: (form: FormGroup): boolean => {
158           return !form.controls[SDN_C_PRE_LOAD].value;
159         },
160         onSuccess: (form: FormGroup): void => {
161           MessageModal.showMessageModal({
162             text: 'The pre-load file(s) have been uploaded successfully.',
163             type: "success",
164             title: 'Success',
165             buttons: [{type: ButtonType.success, size: 'large', text: 'OK', closeModal: true}]
166           })
167         },
168         onFailed: (form: FormGroup) : void=> {
169           MessageModal.showMessageModal({
170             text: 'Failed to upload one or more of the files, please retry.',
171             type: "error",
172             title: 'Failure',
173             buttons: [{type: ButtonType.error, size: 'large', text: 'OK', closeModal: true}]
174           })
175         }
176       } : null
177     ]);
178   }
179
180
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");
186       }
187     };
188
189     return formControlModel;
190   }
191
192   getDefaultVolumeGroupName(instance: any, isEcompGeneratedNaming: boolean): string {
193     if ((!_.isNil(instance) && instance.volumeGroupName)) {
194       return instance.volumeGroupName;
195     }
196     if (isEcompGeneratedNaming) {
197       return null;
198     }
199     return this._basicControlGenerator.getDefaultInstanceName(instance, this.vfModuleModel) + "_vol";
200   }
201
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])
206     ];
207
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;
219         if (pattern) {
220           if (!pattern.test(event['key'])) {
221             event.preventDefault();
222           }
223         }
224         return event;
225       }
226     });
227   }
228
229   private shouldVGNameBeVisible(isEcompGeneratedNaming: boolean, isALaCarte: boolean) {
230     if ((!isALaCarte && !isEcompGeneratedNaming) || isALaCarte) {
231       return true;
232     }
233     return false;
234
235   }
236 }