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