merge from ecomp a88f0072 - Modern UI
[vid.git] / vid-webpack-master / src / app / shared / components / genericForm / formControlsServices / networkGenerator / network.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   FormControlModel,
12   ValidatorModel,
13   ValidatorOptions
14 } from "../../../../models/formControlModels/formControl.model";
15 import {LogService} from "../../../../utils/log/log.service";
16 import {AppState} from "../../../../store/reducers";
17 import {FormGroup} from "@angular/forms";
18 import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model";
19 import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum";
20 import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model";
21 import {SelectOption} from "../../../../models/selectOption";
22 import {NetworkInstance} from "../../../../models/networkInstance";
23 import {NetworkModel} from "../../../../models/networkModel";
24 import {Constants} from "../../../../utils/constants";
25
26 export enum FormControlNames {
27   INSTANCE_NAME = 'instanceName',
28   GLOBAL_SUBSCRIBER_ID = 'globalSubscriberId',
29   PRODUCT_FAMILY_ID = 'productFamilyId',
30   LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
31   TENANT_ID = 'tenantId',
32   AICZONE_ID = 'aicZoneId',
33   ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
34   LEGACY_REGION = 'legacyRegion'
35 }
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 NetworkControlGenerator {
50   aaiService: AaiService;
51
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   getNetworkInstance = (serviceId: string, networkName: string, isUpdateMode : boolean): NetworkInstance => {
62     let networkInstance : NetworkInstance = null;
63     if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] && _.has(this.store.getState().service.serviceInstance[serviceId].networks, networkName)) {
64       networkInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].networks[networkName]);
65     }
66     return networkInstance;
67   };
68
69
70   getMacroFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode : boolean): FormControlModel[] {
71     networkStoreKey = _.isNil(networkStoreKey) ? networkName : networkStoreKey;
72
73     if (_.isNil(serviceId) || _.isNil(networkStoreKey) || _.isNil(networkName)) {
74       this._logService.error('should provide serviceId, networkName, networkStoreKey', serviceId);
75       return [];
76     }
77
78     const networkInstance = this.getNetworkInstance(serviceId, networkStoreKey, isUpdateMode);
79     const networkModel = new NetworkModel(this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]);
80     let result: FormControlModel[] = [];
81
82     if (!_.isNil(networkModel)) {
83       result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming));
84       result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false));
85       result.push(this.getLcpRegionControl(serviceId, networkInstance, result));
86       result.push(this._basicControlGenerator.getLegacyRegion(networkInstance));
87       result.push(this.getTenantControl(serviceId, networkInstance, result));
88       result.push(this.getPlatformControl(networkInstance, result));
89       result.push(this.getLineOfBusinessControl(networkInstance, result));
90     }
91     return result;
92
93   }
94
95   getAlaCarteFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode : boolean): FormControlModel[] {
96     networkStoreKey = _.isNil(networkStoreKey) ? networkName : networkStoreKey;
97     if (_.isNil(serviceId) || _.isNil(networkStoreKey) || _.isNil(networkName)) {
98       this._logService.error('should provide serviceId, networkName, networkStoreKey', serviceId);
99       return [];
100     }
101
102     let result: FormControlModel[] = [];
103     const networkInstance = this.getNetworkInstance(serviceId, networkStoreKey, isUpdateMode);
104     const networkModel = new NetworkModel(this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]);
105
106     if (!_.isNil(networkModel)) {
107       result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming));
108       result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false));
109       result.push(this.getLcpRegionControl(serviceId, networkInstance, result));
110       result.push(this._basicControlGenerator.getLegacyRegion(networkInstance));
111       result.push(this.getTenantControl(serviceId, networkInstance, result));
112       result.push(this.getPlatformControl(networkInstance, result));
113       result.push(this.getLineOfBusinessControl(networkInstance, result));
114       result.push(this.getRollbackOnFailureControl(networkInstance, result));
115     }
116     return result;
117
118   }
119
120   isInputShouldBeShown = (inputType: any): boolean => {
121     let networkInputs = [InputType.LCP_REGION, InputType.LOB, InputType.TENANT, InputType.PRODUCT_FAMILY, InputType.PLATFORM, InputType.ROLLBACK];
122     return networkInputs.indexOf(inputType) > -1;
123   };
124
125   getInstanceName(instance : any, serviceId : string, networkName : string, isEcompGeneratedNaming: boolean): FormControlModel {
126     const networkModel : NetworkModel = this.store.getState().service.serviceHierarchy[serviceId].networks[networkName];
127     return this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, networkModel);
128   }
129
130   getLineOfBusinessControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
131     return new DropdownFormControl({
132       type: FormControlType.DROPDOWN,
133       controlName: 'lineOfBusiness',
134       displayName: 'Line of business',
135       dataTestId: 'lineOfBusiness',
136       placeHolder: 'Select Line Of Business',
137       isDisabled: false,
138       name: "lineOfBusiness",
139       value: instance ? instance.lineOfBusiness : null,
140       validations: [],
141       onInitSelectedField: ['lineOfBusinessList'],
142       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
143     })
144   };
145
146   getPlatformControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
147     return new DropdownFormControl({
148       type: FormControlType.DROPDOWN,
149       controlName: 'platformName',
150       displayName: 'Platform',
151       dataTestId: 'platform',
152       placeHolder: 'Select Platform',
153       isDisabled: false,
154       name: "platform",
155       value: instance ? instance.platformName : null,
156       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
157       onInitSelectedField: ['platformList'],
158       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
159     })
160   };
161
162   getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
163     const service = this.store.getState().service.serviceInstance[serviceId];
164     const globalCustomerId: string = service.globalSubscriberId;
165     const serviceType: string = service.subscriptionServiceType;
166     return new DropdownFormControl({
167       type: FormControlType.DROPDOWN,
168       controlName: FormControlNames.TENANT_ID,
169       displayName: 'Tenant',
170       dataTestId: 'tenant',
171       placeHolder: 'Select Tenant',
172       name: "tenant",
173       isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId),
174       onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null,
175       value: instance ? instance.tenantId : null,
176       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
177       onInit : instance ? this._basicControlGenerator.getSubscribeInitResult.bind(
178         this._aaiService,
179         this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : ()=>{},
180     })
181   };
182
183   getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
184     const service = this.store.getState().service.serviceInstance[serviceId];
185     const globalCustomerId: string = service.globalSubscriberId;
186     const serviceType: string = service.subscriptionServiceType;
187     return new DropdownFormControl({
188       type: FormControlType.DROPDOWN,
189       controlName: 'lcpCloudRegionId',
190       displayName: 'LCP region',
191       dataTestId: 'lcpRegion',
192       placeHolder: 'Select LCP Region',
193       name: "lcpRegion",
194       isDisabled: false,
195       value: instance ? instance.lcpCloudRegionId : null,
196       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
197       onInitSelectedField: ['lcpRegionList'],
198       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(
199         this._aaiService,
200         this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)),
201       onChange: (param: string, form: FormGroup) => {
202         form.controls[FormControlNames.TENANT_ID].enable();
203         form.controls[FormControlNames.TENANT_ID].reset();
204         if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) {
205           this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => {
206             controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
207             if(res.lcpRegionsTenantsMap[param]){
208               controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0;
209             }
210           }));
211         }
212
213         if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) {
214           form.controls['legacyRegion'].enable();
215           controls.find(item => item.controlName === 'legacyRegion').isVisible = true;
216
217         } else {
218           controls.find(item => item.controlName === 'legacyRegion').isVisible = false;
219           form.controls['legacyRegion'].setValue(null);
220           form.controls['legacyRegion'].reset();
221           form.controls['legacyRegion'].disable();
222         }
223       }
224     })
225   };
226
227   getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
228     return new DropdownFormControl({
229       type: FormControlType.DROPDOWN,
230       controlName: FormControlNames.ROLLBACK_ON_FAILURE,
231       displayName: 'Rollback on failure',
232       dataTestId: 'rollback',
233       placeHolder: 'Rollback on failure',
234       isDisabled: false,
235       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
236       value: instance ? instance.rollbackOnFailure : 'true',
237       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
238     })
239   };
240
241   getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
242     return of([
243       new SelectOption({id: 'true', name: 'Rollback'}),
244       new SelectOption({id: 'false', name: 'Don\'t Rollback'})
245     ]);
246   };
247 }