19c85a1ff2d348f5d6a923ecb45def8ffed3f481
[vid.git] /
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     const networkInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getNetworkInstance(serviceId, networkStoreKey, isUpdateMode));
77     const networkModel = new NetworkModel(this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]);
78     let result: FormControlModel[] = [];
79
80     if (!_.isNil(networkModel)) {
81       result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming));
82       result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false));
83       result.push(this.getLcpRegionControl(serviceId, networkInstance, result));
84       result.push(this._basicControlGenerator.getLegacyRegion(networkInstance));
85       result.push(this.getTenantControl(serviceId, networkInstance, result));
86       result.push(this.getPlatformControl(networkInstance, result));
87       result.push(this.getLineOfBusinessControl(networkInstance, result));
88     }
89     return result;
90
91   }
92
93   getAlaCarteFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode : boolean): FormControlModel[] {
94     networkStoreKey = _.isNil(networkStoreKey) ? networkName : networkStoreKey;
95     if (_.isNil(serviceId) || _.isNil(networkStoreKey) || _.isNil(networkName)) {
96       this._logService.error('should provide serviceId, networkName, networkStoreKey', serviceId);
97       return [];
98     }
99
100     let result: FormControlModel[] = [];
101     const networkInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getNetworkInstance(serviceId, networkStoreKey, isUpdateMode));
102     const networkModel = new NetworkModel(this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]);
103
104     if (!_.isNil(networkModel)) {
105       result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming));
106       result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false));
107       result.push(this.getLcpRegionControl(serviceId, networkInstance, result));
108       result.push(this._basicControlGenerator.getLegacyRegion(networkInstance));
109       result.push(this.getTenantControl(serviceId, networkInstance, result));
110       result.push(this.getPlatformControl(networkInstance, result));
111       result.push(this.getLineOfBusinessControl(networkInstance, result));
112       result.push(this.getRollbackOnFailureControl(networkInstance, result));
113     }
114     return result;
115
116   }
117
118   isInputShouldBeShown = (inputType: any): boolean => {
119     let networkInputs = [InputType.LCP_REGION, InputType.LOB, InputType.TENANT, InputType.PRODUCT_FAMILY, InputType.PLATFORM, InputType.ROLLBACK];
120     return networkInputs.indexOf(inputType) > -1;
121   };
122
123   getInstanceName(instance : any, serviceId : string, networkName : string, isEcompGeneratedNaming: boolean): FormControlModel {
124     const networkModel : NetworkModel = this.store.getState().service.serviceHierarchy[serviceId].networks[networkName];
125     return this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, networkModel);
126   }
127
128   getLineOfBusinessControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
129     return new DropdownFormControl({
130       type: FormControlType.DROPDOWN,
131       controlName: 'lineOfBusiness',
132       displayName: 'Line of business',
133       dataTestId: 'lineOfBusiness',
134       placeHolder: 'Select Line Of Business',
135       isDisabled: false,
136       name: "lineOfBusiness",
137       value: instance ? instance.lineOfBusiness : null,
138       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
139       onInitSelectedField: ['lineOfBusinessList'],
140       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
141     })
142   };
143
144   getPlatformControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
145     return new DropdownFormControl({
146       type: FormControlType.DROPDOWN,
147       controlName: 'platformName',
148       displayName: 'Platform',
149       dataTestId: 'platform',
150       placeHolder: 'Select Platform',
151       isDisabled: false,
152       name: "platform",
153       value: instance ? instance.platformName : null,
154       validations: [],
155       onInitSelectedField: ['platformList'],
156       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
157     })
158   };
159
160   getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
161     const service = this.store.getState().service.serviceInstance[serviceId];
162     const globalCustomerId: string = service.globalSubscriberId;
163     const serviceType: string = service.subscriptionServiceType;
164     return new DropdownFormControl({
165       type: FormControlType.DROPDOWN,
166       controlName: FormControlNames.TENANT_ID,
167       displayName: 'Tenant',
168       dataTestId: 'tenant',
169       placeHolder: 'Select Tenant',
170       name: "tenant",
171       isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId),
172       onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null,
173       value: instance ? instance.tenantId : null,
174       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
175       onInit : instance ? this._basicControlGenerator.getSubscribeInitResult.bind(
176         this._aaiService,
177         this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : ()=>{},
178     })
179   };
180
181   getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
182     const service = this.store.getState().service.serviceInstance[serviceId];
183     const globalCustomerId: string = service.globalSubscriberId;
184     const serviceType: string = service.subscriptionServiceType;
185     return new DropdownFormControl({
186       type: FormControlType.DROPDOWN,
187       controlName: 'lcpCloudRegionId',
188       displayName: 'LCP region',
189       dataTestId: 'lcpRegion',
190       placeHolder: 'Select LCP Region',
191       name: "lcpRegion",
192       isDisabled: false,
193       value: instance ? instance.lcpCloudRegionId : null,
194       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
195       onInitSelectedField: ['lcpRegionList'],
196       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(
197         this._aaiService,
198         this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)),
199       onChange: (param: string, form: FormGroup) => {
200         form.controls[FormControlNames.TENANT_ID].enable();
201         form.controls[FormControlNames.TENANT_ID].reset();
202         if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) {
203           this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => {
204             controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
205             if(res.lcpRegionsTenantsMap[param]){
206               controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0;
207             }
208           }));
209         }
210
211         if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) {
212           form.controls['legacyRegion'].enable();
213           controls.find(item => item.controlName === 'legacyRegion').isVisible = true;
214
215         } else {
216           controls.find(item => item.controlName === 'legacyRegion').isVisible = false;
217           form.controls['legacyRegion'].setValue(null);
218           form.controls['legacyRegion'].reset();
219           form.controls['legacyRegion'].disable();
220         }
221       }
222     })
223   };
224
225   getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
226     return new DropdownFormControl({
227       type: FormControlType.DROPDOWN,
228       controlName: FormControlNames.ROLLBACK_ON_FAILURE,
229       displayName: 'Rollback on failure',
230       dataTestId: 'rollback',
231       placeHolder: 'Rollback on failure',
232       isDisabled: false,
233       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
234       value: instance ? instance.rollbackOnFailure : 'true',
235       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
236     })
237   };
238
239   getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
240     return of([
241       new SelectOption({id: 'true', name: 'Rollback'}),
242       new SelectOption({id: 'false', name: 'Don\'t Rollback'})
243     ]);
244   };
245 }