When opening new VNF modal the modal should be empty.
[vid.git] / vid-webpack-master / src / app / shared / components / genericForm / formControlsServices / vnfGenerator / vnf.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 {
8   FormControlModel,
9   ValidatorModel,
10   ValidatorOptions
11 } from "../../../../models/formControlModels/formControl.model";
12 import {LogService} from "../../../../utils/log/log.service";
13 import {VNFModel} from "../../../../models/vnfModel";
14 import {AppState} from "../../../../store/reducers";
15 import {FormGroup} from "@angular/forms";
16 import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model";
17 import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum";
18 import {Observable, of} from "rxjs";
19 import {SelectOption} from "../../../../models/selectOption";
20 import * as _ from 'lodash';
21 import {Constants} from "../../../../utils/constants";
22 import {MultiselectFormControl} from "../../../../models/formControlModels/multiselectFormControl.model";
23 import {MultiSelectItem} from "../../../formControls/component/multiselect/multiselect.model";
24
25 export enum FormControlNames {
26   INSTANCE_NAME = 'instanceName',
27   GLOBAL_SUBSCRIBER_ID = 'globalSubscriberId',
28   SUBSCRIPTION_SERVICE_TYPE = 'subscriptionServiceType',
29   PRODUCT_FAMILY_ID = 'productFamilyId',
30   LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
31   TENANT_ID = 'tenantId',
32   AICZONE_ID = 'aicZoneId',
33   PROJECT_NAME = 'projectName',
34   OWNING_ENTITY_ID = 'owningEntityId',
35   ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
36   PAUSE = 'pause'
37 }
38
39 enum InputType {
40   LCP_REGION = "lcpCloudRegionId",
41   TENANT = "tenantId",
42   LOB = "lineOfBusiness",
43   PLATFORM = "platformName",
44   ROLLBACK = "rollbackOnFailure",
45   PRODUCT_FAMILY = "productFamilyId",
46   VG = "volumeGroupName"
47 }
48
49 @Injectable()
50 export class VnfControlGenerator {
51   aaiService: AaiService;
52   constructor(private genericFormService: GenericFormService,
53               private _basicControlGenerator: BasicControlGenerator,
54               private store: NgRedux<AppState>,
55               private http: HttpClient,
56               private _aaiService: AaiService,
57               private _logService: LogService) {
58     this.aaiService = _aaiService;
59   }
60
61   getVnfInstance = (serviceId: string, vnfStoreKey: string): any => {
62     let vnfInstance = null;
63     if (this.store.getState().service.serviceInstance[serviceId] && _.has(this.store.getState().service.serviceInstance[serviceId].vnfs, vnfStoreKey)) {
64       vnfInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey]);
65     }
66     return vnfInstance;
67   };
68
69   getMacroFormControls(serviceId: string, vnfStoreKey: string, vnfName: string, dynamicInputs?: any[]): FormControlModel[] {
70     vnfStoreKey = _.isNil(vnfStoreKey) ? vnfName : vnfStoreKey;
71
72     if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vnfName)) {
73       this._logService.error('should provide serviceId, vnfName, vnfStoreKey', serviceId);
74       return [];
75     }
76     const vnfInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store,this.getVnfInstance(serviceId, vnfStoreKey));
77     const vnfModel = new VNFModel(this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfName]);
78     let result: FormControlModel[] = [];
79     const flags = this.store.getState().global.flags;
80
81     if (!_.isNil(vnfModel)) {
82       result.push(this.getInstanceName(vnfInstance, serviceId, vnfName, vnfModel.isEcompGeneratedNaming));
83       result.push(this._basicControlGenerator.getProductFamilyControl(vnfInstance, result, false));
84       result.push(this.getLcpRegionControl(serviceId, vnfInstance, result));
85       result.push(this._basicControlGenerator.getLegacyRegion(vnfInstance));
86       result.push(this.getTenantControl(serviceId, vnfInstance, result));
87       result.push(this.getPlatformMultiselectControl(vnfInstance, result, flags['FLAG_2002_VNF_PLATFORM_MULTI_SELECT']));
88       result.push(this.getLineOfBusinessControl(vnfInstance, result));
89     }
90     return result;
91   }
92
93   getAlaCarteFormControls(serviceId: string, vnfStoreKey: string, vnfName: string, dynamicInputs?: any[]): FormControlModel[] {
94     vnfStoreKey = _.isNil(vnfStoreKey) ? vnfName : vnfStoreKey;
95     if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vnfName)) {
96       this._logService.error('should provide serviceId, vnfName, vnfStoreKey', serviceId);
97       return [];
98     }
99
100     let result: FormControlModel[] = [];
101     const vnfInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store,this.getVnfInstance(serviceId, vnfStoreKey));
102     const vnfModel = new VNFModel(this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfName]);
103
104     if (!_.isNil(vnfModel)) {
105       const flags = this.store.getState().global.flags;
106       result.push(this.getInstanceName(vnfInstance, serviceId, vnfName, vnfModel.isEcompGeneratedNaming));
107       result.push(this._basicControlGenerator.getProductFamilyControl(vnfInstance, result, false));
108       result.push(this.getLcpRegionControl(serviceId, vnfInstance, result));
109       result.push(this._basicControlGenerator.getLegacyRegion(vnfInstance));
110       result.push(this.getTenantControl(serviceId, vnfInstance, result));
111       result.push(this.getPlatformMultiselectControl(vnfInstance, result, flags['FLAG_2002_VNF_PLATFORM_MULTI_SELECT']));
112       result.push(this.getLineOfBusinessControl(vnfInstance, result));
113       result.push(this.getRollbackOnFailureControl(vnfInstance, result));
114     }
115     return result;
116   }
117
118   isInputShouldBeShown = (inputType: any): boolean => {
119     let vnfInputs = [InputType.LCP_REGION, InputType.LOB, InputType.TENANT, InputType.PRODUCT_FAMILY, InputType.PLATFORM, InputType.ROLLBACK];
120     return vnfInputs.indexOf(inputType) > -1;
121   };
122
123   getInstanceName(instance : any, serviceId : string, vnfName : string, isEcompGeneratedNaming: boolean): FormControlModel {
124     const vnfModel : VNFModel = this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfName];
125     return this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, vnfModel);
126   }
127
128   getLineOfBusinessControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
129     return new DropdownFormControl({
130       type: FormControlType.DROPDOWN,
131       controlName: 'lineOfBusiness',
132       displayName: 'Line of business',
133       dataTestId: 'lineOfBusiness',
134       placeHolder: 'Select Line Of Business',
135       isDisabled: false,
136       name: "lineOfBusiness",
137       value: instance ? instance.lineOfBusiness : null,
138       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
139       onInitSelectedField: ['lineOfBusinessList'],
140       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
141     })
142   };
143
144
145
146   getPlatformMultiselectControl = (instance: any, controls: FormControlModel[], isMultiSelected: boolean) : MultiselectFormControl => {
147     return new MultiselectFormControl({
148       type: FormControlType.MULTI_SELECT ,
149       controlName: 'platformName',
150       displayName: 'Platform',
151       dataTestId: 'multi-selectPlatform',
152       selectedFieldName :  'name' ,
153       ngValue :  'name',
154       placeHolder: 'Select Platform',
155       isDisabled: false,
156       name: "platform",
157       value: instance ? instance.platformName : '',
158       limitSelection : isMultiSelected ? 1000 : 1,
159       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
160       onInitSelectedField: ['platformList'],
161       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters),
162       onChange : (param: MultiSelectItem[], form: FormGroup) => {
163         form.controls['platformName'].setValue(param.map((multiSelectItem: MultiSelectItem)=>{
164           return multiSelectItem.itemName
165         }).join(','));
166       },
167       convertOriginalDataToArray : (value?: string) => {
168         if(_.isNil(value)) return [];
169         return value.split(',');
170       }
171     });
172   };
173
174   getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
175     const service = this.store.getState().service.serviceInstance[serviceId];
176     const globalCustomerId: string = service.globalSubscriberId;
177     const serviceType: string = service.subscriptionServiceType;
178     return new DropdownFormControl({
179       type: FormControlType.DROPDOWN,
180       controlName: FormControlNames.TENANT_ID,
181       displayName: 'Tenant',
182       dataTestId: 'tenant',
183       placeHolder: 'Select Tenant',
184       name: "tenant",
185       isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId),
186       onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null,
187       value: instance ? instance.tenantId : null,
188       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
189       onInit : instance ? this._basicControlGenerator.getSubscribeInitResult.bind(
190         this._aaiService,
191         this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : ()=>{},
192     })
193   };
194
195   getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
196     const service = this.store.getState().service.serviceInstance[serviceId];
197     const globalCustomerId: string = service.globalSubscriberId;
198     const serviceType: string = service.subscriptionServiceType;
199     return new DropdownFormControl({
200       type: FormControlType.DROPDOWN,
201       controlName: 'lcpCloudRegionId',
202       displayName: 'LCP region',
203       dataTestId: 'lcpRegion',
204       placeHolder: 'Select LCP Region',
205       name: "lcpRegion",
206       isDisabled: false,
207       value: instance ? instance.lcpCloudRegionId : null,
208       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
209       onInitSelectedField: ['lcpRegionList'],
210       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(
211         this._aaiService,
212         this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)),
213       onChange: (param: string, form: FormGroup) => {
214         form.controls[FormControlNames.TENANT_ID].enable();
215         form.controls[FormControlNames.TENANT_ID].reset();
216         if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) {
217           this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => {
218             controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
219             if(res.lcpRegionsTenantsMap[param]){
220               controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0;
221             }
222           }));
223         }
224
225         if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) {
226           form.controls['legacyRegion'].enable();
227           controls.find(item => item.controlName === 'legacyRegion').isVisible = true;
228
229         } else {
230           controls.find(item => item.controlName === 'legacyRegion').isVisible = false;
231           form.controls['legacyRegion'].setValue(null);
232           form.controls['legacyRegion'].reset();
233           form.controls['legacyRegion'].disable();
234         }
235       }
236     })
237   };
238
239   getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
240     return new DropdownFormControl({
241       type: FormControlType.DROPDOWN,
242       controlName: FormControlNames.ROLLBACK_ON_FAILURE,
243       displayName: 'Rollback on failure',
244       dataTestId: 'rollback',
245       placeHolder: 'Rollback on failure',
246       isDisabled: false,
247       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
248       value: instance ? instance.rollbackOnFailure : 'true',
249       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
250     })
251   };
252
253   getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
254     return of([
255       new SelectOption({id: 'true', name: 'Rollback'}),
256       new SelectOption({id: 'false', name: 'Don\'t Rollback'})
257     ]);
258   };
259 }