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