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