Merge "Do not show retain VGs checkbox in VFM Upgrade modal + tests modifications"
[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 {BasicControlGenerator} from "../basic.control.generator";
7 import * as _ from 'lodash';
8 import {Observable, of} from "rxjs";
9
10 import {
11   CustomValidatorOptions,
12   FormControlModel,
13   ValidatorModel,
14   ValidatorOptions
15 } from "../../../../models/formControlModels/formControl.model";
16 import {LogService} from "../../../../utils/log/log.service";
17 import {AppState} from "../../../../store/reducers";
18 import {FormGroup} from "@angular/forms";
19 import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model";
20 import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum";
21 import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model";
22 import {SelectOption} from "../../../../models/selectOption";
23 import {VfModuleInstance} from "../../../../models/vfModuleInstance";
24 import {VfModule} from "../../../../models/vfModule";
25 import {VNFModel} from "../../../../models/vnfModel";
26 import {VnfInstance} from "../../../../models/vnfInstance";
27 import {FileFormControl} from "../../../../models/formControlModels/fileFormControl.model";
28 import {CheckboxFormControl} from "../../../../models/formControlModels/checkboxFormControl.model";
29 import {FileUnit} from "../../../formControls/component/file/fileUnit.enum";
30 import {Constants} from "../../../../utils/constants";
31
32
33 export enum FormControlNames {
34   INSTANCE_NAME = 'instanceName',
35   VOLUME_GROUP_NAME = 'volumeGroupName',
36   LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
37   LEGACY_REGION = 'legacyRegion',
38   TENANT_ID = 'tenantId',
39   ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
40 }
41
42
43 @Injectable()
44 export class VfModuleControlGenerator {
45   aaiService: AaiService;
46   vfModuleModel: VfModule;
47   isUpdateMode : boolean;
48
49   constructor(private genericFormService: GenericFormService,
50               private _basicControlGenerator: BasicControlGenerator,
51               private store: NgRedux<AppState>,
52               private http: HttpClient,
53               private _aaiService: AaiService,
54               private _logService: LogService) {
55     this.aaiService = _aaiService;
56   }
57
58   setVFModuleStoreKey = (serviceId: string, vfModuleUuid: string) => {
59       const vfModules = this.store.getState().service.serviceHierarchy[serviceId].vfModules;
60       const vfModulesKeys = Object.keys(vfModules);
61       for(let key of  vfModulesKeys){
62         if(vfModules[key].uuid === vfModuleUuid){
63           return;
64         }
65       }
66   };
67
68
69   getVfModuleInstance = (serviceId: string, vnfStoreKey: string, UUIDData: Object, isUpdateMode: boolean): VfModuleInstance => {
70     let vfModuleInstance: VfModuleInstance = null;
71     if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] &&
72       _.has(this.store.getState().service.serviceInstance[serviceId].vnfs, vnfStoreKey) &&
73       _.has(this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules, UUIDData['modelName'])) {
74        vfModuleInstance = Object.assign({},this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules[UUIDData['modelName']][UUIDData['vFModuleStoreKey']]);
75     }
76     return vfModuleInstance;
77   };
78
79   extractVfAccordingToVfModuleUuid(serviceId: string, UUIDData: Object): VfModule {
80     const vfModule = this.store.getState().service.serviceHierarchy[serviceId].vfModules[UUIDData['modelName']];
81     this.vfModuleModel = vfModule;
82     return vfModule;
83   }
84
85   getMacroFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData : Object, isUpdateMode: boolean): FormControlModel[] {
86     this.isUpdateMode = isUpdateMode;
87     this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
88     if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
89       if(isUpdateMode){
90         this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
91         return [];
92       }
93     }
94
95     const vfModuleInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode));
96     const vfModuleModel = this.vfModuleModel;
97     const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
98     const vnfModelName: string = vnf.originalName;
99     const vnfModel = new VNFModel(this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfModelName]);
100
101     let result: FormControlModel[] = [];
102
103     if (!_.isNil(vfModuleModel)) {
104       result = this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, false);
105     }
106     if(this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
107       result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
108     }
109     return result;
110   }
111
112   pushInstanceAndVGToForm(result: FormControlModel[], vfModuleElement: any, serviceId: string, vnfModel: any, isALaCarte: boolean) :FormControlModel[]{
113     result.push(this.getInstanceName(vfModuleElement, serviceId, vnfModel.isEcompGeneratedNaming));
114     if (this.vfModuleModel.volumeGroupAllowed) {
115       result.push(this.getVolumeGroupData(vfModuleElement, serviceId, vnfModel.isEcompGeneratedNaming, isALaCarte));
116     }
117     return result;
118   }
119
120   getAlaCarteFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData : Object, isUpdateMode: boolean): FormControlModel[] {
121     this.isUpdateMode = isUpdateMode;
122     this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
123     if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
124       if(isUpdateMode){
125         this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
126         return [];
127       }
128     }
129     const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey] ;
130     const vnfModelName: string = vnf.originalName;
131     const vnfModel = new VNFModel(this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfModelName]);
132
133     const vfModuleInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode));
134     let result: FormControlModel[] = [];
135     this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, true);
136     result.push(this.getLcpRegionControl(serviceId, vfModuleInstance, result));
137     result.push(this._basicControlGenerator.getLegacyRegion(vfModuleInstance));
138     result.push(this.getTenantControl(serviceId, vfModuleInstance, result));
139     result.push(this.getRollbackOnFailureControl(vfModuleInstance, result));
140     result.push(this._basicControlGenerator.getSDNCControl(vfModuleInstance));
141     if(this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
142       result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
143     }
144     return result;
145   }
146
147   getInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): FormControlModel {
148     let formControlModel:FormControlModel = this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, this.vfModuleModel);
149     formControlModel.onBlur = (event, form : FormGroup) => {
150         if(!_.isNil(form.controls['volumeGroupName'])&& event.target.value.length > 0){
151           form.controls['volumeGroupName'].setValue(event.target.value + "_vol");
152         }
153       };
154
155     return formControlModel;
156   }
157
158   getDefaultVolumeGroupName(instance: any, isEcompGeneratedNaming: boolean): string {
159     if ((!_.isNil(instance) && instance.volumeGroupName))  {
160       return instance.volumeGroupName;
161     }
162     if (isEcompGeneratedNaming) {
163       return null;
164     }
165     return this._basicControlGenerator.getDefaultInstanceName(instance, this.vfModuleModel) + "_vol";
166   }
167
168   getVolumeGroupData(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, isALaCarte: boolean): FormControlModel {
169     let validations: ValidatorModel[] = [
170       new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', BasicControlGenerator.INSTANCE_NAME_REG_EX),
171       new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'Volume Group instance name is already in use, please pick another name', [this.store, serviceId, instance && instance.volumeGroupName])
172     ];
173     // comment out because if not provided vid won't create VG
174     // if (!isEcompGeneratedNaming)  {
175     //   validations.push(new ValidatorModel(ValidatorOptions.required, 'is required'));
176     // }
177     return new InputFormControl({
178       controlName: 'volumeGroupName',
179       displayName: 'Volume Group Name',
180       dataTestId: 'volumeGroupName',
181       // placeHolder: (!isEcompGeneratedNaming) ? 'Volume Group Name' : 'Automatically generated when not provided',
182       validations: validations,
183       tooltip : 'When filled, VID will create a Volume Group by this name and associate with this module.\n' +
184                 'When empty, the module is created without a Volume Group.',
185       isVisible: this.shouldVGNameBeVisible(isEcompGeneratedNaming,isALaCarte),
186       value: this.getDefaultVolumeGroupName(instance, isEcompGeneratedNaming),
187       onKeypress: (event) => {
188         const pattern:RegExp = BasicControlGenerator.INSTANCE_NAME_REG_EX;
189         if (pattern) {
190           if (!pattern.test(event['key'])) {
191             event.preventDefault();
192           }
193         }
194         return event;
195       }
196     });
197   }
198
199   private shouldVGNameBeVisible(isEcompGeneratedNaming: boolean, isALaCarte: boolean) {
200     if((!isALaCarte && !isEcompGeneratedNaming) || isALaCarte){
201       return true;
202     }
203     return false;
204
205   }
206
207   getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
208     const service = this.store.getState().service.serviceInstance[serviceId];
209     const globalCustomerId: string = service.globalSubscriberId;
210     const serviceType: string = service.subscriptionServiceType;
211     return new DropdownFormControl({
212       type: FormControlType.DROPDOWN,
213       controlName: FormControlNames.TENANT_ID,
214       displayName: 'Tenant',
215       dataTestId: 'tenant',
216       placeHolder: 'Select Tenant',
217       name: "tenant",
218       isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId),
219       onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null,
220       value: instance ? instance.tenantId : null,
221       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
222       onInit: instance ? this._basicControlGenerator.getSubscribeInitResult.bind(
223         this._aaiService,
224         this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : () => {
225       },
226     })
227   };
228
229   getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
230     const service = this.store.getState().service.serviceInstance[serviceId];
231     const globalCustomerId: string = service.globalSubscriberId;
232     const serviceType: string = service.subscriptionServiceType;
233     return new DropdownFormControl({
234       type: FormControlType.DROPDOWN,
235       controlName: 'lcpCloudRegionId',
236       displayName: 'LCP region',
237       dataTestId: 'lcpRegion',
238       placeHolder: 'Select LCP Region',
239       name: "lcpRegion",
240       isDisabled: false,
241       value: instance ? instance.lcpCloudRegionId : null,
242       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
243       onInitSelectedField: ['lcpRegionList'],
244       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(
245         this._aaiService,
246         this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)),
247       onChange: (param: string, form: FormGroup) => {
248         form.controls[FormControlNames.TENANT_ID].enable();
249         form.controls[FormControlNames.TENANT_ID].reset();
250         if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) {
251           this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => {
252             controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
253             if (res.lcpRegionsTenantsMap[param]) {
254               controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0;
255             }
256           }));
257         }
258
259         if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) {
260           form.controls['legacyRegion'].enable();
261           controls.find(item => item.controlName === 'legacyRegion').isVisible = true;
262
263         } else {
264           controls.find(item => item.controlName === 'legacyRegion').isVisible = false;
265           form.controls['legacyRegion'].setValue(null);
266           form.controls['legacyRegion'].reset();
267           form.controls['legacyRegion'].disable();
268         }
269       }
270     })
271   };
272
273   getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
274     return new DropdownFormControl({
275       type: FormControlType.DROPDOWN,
276       controlName: FormControlNames.ROLLBACK_ON_FAILURE,
277       displayName: 'Rollback on failure',
278       dataTestId: 'rollback',
279       isDisabled: false,
280       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
281       value: instance ? instance.rollbackOnFailure : 'true',
282       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
283     })
284   };
285
286   getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
287     return of([
288       new SelectOption({id: 'true', name: 'Rollback'}),
289       new SelectOption({id: 'false', name: 'Don\'t Rollback'})
290     ]);
291   };
292 }