Merge "Changing VNF platform to multiselect + cypress + API"
[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
77     const vnfInstance = this.getVnfInstance(serviceId, vnfStoreKey);
78     const vnfModel = new VNFModel(this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfName]);
79     let result: FormControlModel[] = [];
80     const flags = this.store.getState().global.flags;
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.getPlatformMultiselectControl(vnfInstance, result, flags['FLAG_2002_VNF_PLATFORM_MULTI_SELECT']));
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.getPlatformMultiselectControl(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   getPlatformMultiselectControl = (instance: any, controls: FormControlModel[], isMultiSelected: boolean) : MultiselectFormControl => {
148     return new MultiselectFormControl({
149       type: FormControlType.MULTI_SELECT ,
150       controlName: 'platformName',
151       displayName: 'Platform',
152       dataTestId: 'multi-selectPlatform',
153       selectedFieldName :  'name' ,
154       ngValue :  'name',
155       placeHolder: 'Select Platform',
156       isDisabled: false,
157       name: "platform",
158       value: instance ? instance.platformName : '',
159       limitSelection : isMultiSelected ? 1000 : 1,
160       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
161       onInitSelectedField: ['platformList'],
162       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters),
163       onChange : (param: MultiSelectItem[], form: FormGroup) => {
164         form.controls['platformName'].setValue(param.map((multiSelectItem: MultiSelectItem)=>{
165           return multiSelectItem.itemName
166         }).join(','));
167       },
168       convertOriginalDataToArray : (value?: string) => {
169         if(_.isNil(value)) return [];
170         return value.split(',');
171       }
172     });
173   };
174
175   getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
176     const service = this.store.getState().service.serviceInstance[serviceId];
177     const globalCustomerId: string = service.globalSubscriberId;
178     const serviceType: string = service.subscriptionServiceType;
179     return new DropdownFormControl({
180       type: FormControlType.DROPDOWN,
181       controlName: FormControlNames.TENANT_ID,
182       displayName: 'Tenant',
183       dataTestId: 'tenant',
184       placeHolder: 'Select Tenant',
185       name: "tenant",
186       isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId),
187       onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null,
188       value: instance ? instance.tenantId : null,
189       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
190       onInit : instance ? this._basicControlGenerator.getSubscribeInitResult.bind(
191         this._aaiService,
192         this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : ()=>{},
193     })
194   };
195
196   getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
197     const service = this.store.getState().service.serviceInstance[serviceId];
198     const globalCustomerId: string = service.globalSubscriberId;
199     const serviceType: string = service.subscriptionServiceType;
200     return new DropdownFormControl({
201       type: FormControlType.DROPDOWN,
202       controlName: 'lcpCloudRegionId',
203       displayName: 'LCP region',
204       dataTestId: 'lcpRegion',
205       placeHolder: 'Select LCP Region',
206       name: "lcpRegion",
207       isDisabled: false,
208       value: instance ? instance.lcpCloudRegionId : null,
209       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
210       onInitSelectedField: ['lcpRegionList'],
211       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(
212         this._aaiService,
213         this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)),
214       onChange: (param: string, form: FormGroup) => {
215         form.controls[FormControlNames.TENANT_ID].enable();
216         form.controls[FormControlNames.TENANT_ID].reset();
217         if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) {
218           this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => {
219             controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
220             if(res.lcpRegionsTenantsMap[param]){
221               controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0;
222             }
223           }));
224         }
225
226         if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) {
227           form.controls['legacyRegion'].enable();
228           controls.find(item => item.controlName === 'legacyRegion').isVisible = true;
229
230         } else {
231           controls.find(item => item.controlName === 'legacyRegion').isVisible = false;
232           form.controls['legacyRegion'].setValue(null);
233           form.controls['legacyRegion'].reset();
234           form.controls['legacyRegion'].disable();
235         }
236       }
237     })
238   };
239
240   getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
241     return new DropdownFormControl({
242       type: FormControlType.DROPDOWN,
243       controlName: FormControlNames.ROLLBACK_ON_FAILURE,
244       displayName: 'Rollback on failure',
245       dataTestId: 'rollback',
246       placeHolder: 'Rollback on failure',
247       isDisabled: false,
248       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
249       value: instance ? instance.rollbackOnFailure : 'true',
250       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
251     })
252   };
253
254   getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
255     return of([
256       new SelectOption({id: 'true', name: 'Rollback'}),
257       new SelectOption({id: 'false', name: 'Don\'t Rollback'})
258     ]);
259   };
260 }