539f848d5d0b7f9166d57426a98d92268afaa66f
[vid.git] /
1 import {Injectable} from "@angular/core";
2 import {GenericFormService} from "../generic-form.service";
3 import {NgRedux} from "@angular-redux/store";
4 import {AppState} from "../../../store/reducers";
5 import {
6   FormControlModel,
7   ValidatorModel,
8   ValidatorOptions
9 } from "../../../models/formControlModels/formControl.model";
10 import {DropdownFormControl} from "../../../models/formControlModels/dropdownFormControl.model";
11 import * as _ from 'lodash';
12 import {BasicControlGenerator} from "./basic.control.generator";
13 import {AaiService} from "../../../services/aaiService/aai.service";
14 import {FormGroup} from "@angular/forms";
15 import {FormControlType} from "../../../models/formControlModels/formControlTypes.enum";
16 import {HttpClient} from "@angular/common/http";
17 import {SelectOption} from "../../../models/selectOption";
18 import {Observable} from "rxjs";
19 import {LogService} from "../../../utils/log/log.service";
20 import {ServiceModel} from "../../../models/serviceModel";
21 import {of} from "rxjs";
22
23 import {CheckboxFormControl} from "../../../models/formControlModels/checkboxFormControl.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 @Injectable()
40 export class  ServiceControlGenerator {
41   aaiService : AaiService;
42   constructor(private genericFormService : GenericFormService,
43               private _basicControlGenerator : BasicControlGenerator,
44               private store: NgRedux<AppState>,
45               private http: HttpClient,
46               private _aaiService : AaiService,
47               private _logService : LogService){
48     this.aaiService = _aaiService;
49   }
50
51   getServiceInstance = (serviceId : string) : any => {
52     let serviceInstance = null;
53     if (_.has(this.store.getState().service.serviceInstance, serviceId)) {
54       serviceInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId]);
55     }
56
57     return serviceInstance;
58   };
59
60   getAlaCartControls(serviceId: string, dynamicInputs?: any[]) : FormControlModel[] {
61     if(_.isNil(serviceId)){
62       this._logService.error('should provide serviceId', serviceId);
63       return [];
64     }
65     const serviceInstance = this.getServiceInstance(serviceId);
66
67     let result : FormControlModel[] = [];
68
69     const serviceModel = new ServiceModel(this.store.getState().service.serviceHierarchy[serviceId]);
70     if(!_.isNil(serviceModel)){
71       result.push(this._basicControlGenerator.getInstanceName(serviceInstance, serviceId, serviceModel.isEcompGeneratedNaming));
72       result.push(this.getGlobalSubscriberControl(serviceInstance, result));
73       result.push(this.getServiceTypeControl(serviceInstance, result, false));
74       result.push(this.getOwningEntityControl(serviceInstance, result));
75       result.push(this.getProjectControl(serviceInstance, result));
76       result.push(this.getRollbackOnFailureControl(serviceInstance, result));
77     }
78
79     this._logService.info('Generate dynamic service controls, is edit mode: ' + serviceInstance != null , result);
80     return result;
81   }
82
83   getMacroFormControls(serviceId: string, dynamicInputs?: any[]) : FormControlModel[] {
84     if(_.isNil(serviceId)){
85       this._logService.error('should provide serviceId', serviceId);
86       return [];
87     }
88
89     const serviceInstance = this.getServiceInstance(serviceId);
90
91     let result : FormControlModel[] = [];
92     const serviceModel = new ServiceModel(this.store.getState().service.serviceHierarchy[serviceId]);
93     if(!_.isNil(serviceModel)){
94       result.push(this._basicControlGenerator.getInstanceName(serviceInstance, serviceId, serviceModel.isEcompGeneratedNaming));
95       result.push(this.getGlobalSubscriberControl(serviceInstance, result));
96       result.push(this.getServiceTypeControl(serviceInstance, result, true));
97       result.push(this.getOwningEntityControl(serviceInstance, result));
98       result.push(this.getProductFamilyControl(serviceInstance, result));
99       result.push(this.getLcpRegionControl(serviceInstance, result));
100       result.push(this.getTenantControl(serviceInstance, result),);
101       result.push(this.getAICZoneControl(serviceInstance, result));
102
103       if(serviceModel.isMultiStepDesign){
104         result.push(new CheckboxFormControl({
105           controlName : FormControlNames.PAUSE,
106           displayName : 'Pause on pause points',
107           dataTestId : 'Pause',
108           isDisabled : false,
109           validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
110           value : serviceInstance ? serviceInstance.pause : null,
111         }));
112       }
113
114       result.push(this.getProjectControl(serviceInstance, result));
115       result.push(this.getRollbackOnFailureControl(serviceInstance, result));
116     }
117
118
119     this._logService.info('Generate dynamic service controls, is edit mode: ' + serviceInstance != null , result);
120     return result;
121   }
122
123   getRollBackOnFailureOptions = () : Observable<SelectOption[]> =>{
124     return of([
125       new SelectOption({id: 'true', name: 'Rollback'}),
126       new SelectOption({id: 'false', name: 'Don\'t Rollback'})
127     ]);
128   };
129
130   getGlobalSubscriberControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl  => {
131     return new DropdownFormControl({
132       type : FormControlType.DROPDOWN,
133       controlName : FormControlNames.GLOBAL_SUBSCRIBER_ID,
134       displayName : 'Subscriber name',
135       dataTestId : 'subscriberName',
136       placeHolder : 'Select Subscriber Name',
137       isDisabled : false,
138       name : "subscriber-name-select",
139       value : serviceInstance ? serviceInstance.globalSubscriberId : null,
140       validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
141       onInit : this._basicControlGenerator.getSubscribeInitResult.bind(this._aaiService, this.aaiService.getSubscribers),
142       onChange :  (param: string, form : FormGroup) => {
143         form.controls[FormControlNames.SUBSCRIPTION_SERVICE_TYPE].reset();
144         if(!_.isNil(param)){
145           form.controls[FormControlNames.SUBSCRIPTION_SERVICE_TYPE].enable();
146           this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getServiceTypes(param).subscribe(res =>{
147             controls.find(item => item.controlName === FormControlNames.SUBSCRIPTION_SERVICE_TYPE)['options$'] = res;
148           }));
149         }
150         else {
151           form.controls[FormControlNames.SUBSCRIPTION_SERVICE_TYPE].disable();
152         }
153       }
154     })
155   };
156
157   getServiceTypeControl = (serviceInstance : any, controls : FormControlModel[], isMacro?: boolean) : DropdownFormControl => {
158     return new DropdownFormControl({
159       type : FormControlType.DROPDOWN,
160       controlName : FormControlNames.SUBSCRIPTION_SERVICE_TYPE,
161       displayName : 'Service type',
162       dataTestId : 'serviceType',
163       placeHolder : 'Select Service Type',
164       selectedField : 'name',
165       name : "service-type",
166       isDisabled : _.isNil(serviceInstance),
167       value : serviceInstance ? serviceInstance.subscriptionServiceType : null,
168       validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
169       onInit : serviceInstance ? this._basicControlGenerator.getSubscribeInitResult.bind(
170         this._aaiService,
171         this.aaiService.getServiceTypes.bind(this, serviceInstance.globalSubscriberId)) : ()=>{},
172       onChange :  (param: string, form : FormGroup) => {
173         if(isMacro){
174           form.controls[FormControlNames.LCPCLOUD_REGION_ID].reset();
175           if(!_.isNil(param)) {
176             form.controls[FormControlNames.LCPCLOUD_REGION_ID].enable();
177             const globalCustomerId: string = form.controls[FormControlNames.GLOBAL_SUBSCRIBER_ID].value;
178             if (!_.isNil(globalCustomerId)) {
179               this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, param).subscribe(res => {
180                 controls.find(item => item.controlName === FormControlNames.LCPCLOUD_REGION_ID)['options$'] = res.lcpRegionList;
181               }));
182             }
183           }
184           else {
185             form.controls[FormControlNames.LCPCLOUD_REGION_ID].disable();
186           }
187         }
188
189       }
190     })
191   };
192
193   getOwningEntityControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
194     return new DropdownFormControl({
195       type : FormControlType.DROPDOWN,
196       controlName : FormControlNames.OWNING_ENTITY_ID,
197       displayName : 'Owning entity',
198       dataTestId : 'owningEntity',
199       placeHolder : 'Select Owning Entity',
200       name :"owningEntity",
201       isDisabled : false,
202       validations : [new ValidatorModel(ValidatorOptions.required, 'is required'),],
203       onInitSelectedField : ['owningEntityList'],
204       value : serviceInstance ? serviceInstance.owningEntityId : null,
205       onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
206     })
207   };
208
209   getProductFamilyControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
210     return new DropdownFormControl({
211       type : FormControlType.DROPDOWN,
212       controlName : FormControlNames.PRODUCT_FAMILY_ID,
213       displayName : 'Product family',
214       dataTestId : 'productFamily',
215       placeHolder : 'Select Product Family',
216       isDisabled : false,
217       name : "product-family-select",
218       value : serviceInstance ? serviceInstance.productFamilyId : null,
219       validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
220       onInit : this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getProductFamilies),
221     })
222   };
223
224   getLcpRegionControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
225     return new DropdownFormControl({
226       type : FormControlType.DROPDOWN,
227       controlName : FormControlNames.LCPCLOUD_REGION_ID,
228       displayName : 'LCP region',
229       dataTestId : 'lcpRegion',
230       placeHolder : 'Select LCP Region',
231       name : "lcpRegion",
232       isDisabled : _.isNil(serviceInstance),
233       value : serviceInstance ? serviceInstance.lcpCloudRegionId : null,
234       validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
235       onInitSelectedField : ['lcpRegionList'],
236       onInit : serviceInstance ? this._basicControlGenerator.getSubscribeInitResult.bind(
237         this._aaiService,
238         this.aaiService.getLcpRegionsAndTenants.bind(this, serviceInstance.globalSubscriberId, serviceInstance.subscriptionServiceType)) : ()=>{},
239       onChange :  (param: string, form : FormGroup) => {
240         form.controls[FormControlNames.TENANT_ID].reset();
241         if(param) {
242           form.controls[FormControlNames.TENANT_ID].enable();
243         }
244         else {
245           form.controls[FormControlNames.TENANT_ID].disable();
246         }
247         const globalCustomerId : string = form.controls[FormControlNames.GLOBAL_SUBSCRIBER_ID].value;
248         const serviceType : string = form.controls[FormControlNames.SUBSCRIPTION_SERVICE_TYPE].value;
249         if(!_.isNil(globalCustomerId) && !_.isNil(serviceType)){
250           this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res =>{
251             controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
252           }));
253         }
254       }
255     })
256   };
257
258   getTenantControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
259     return  new DropdownFormControl({
260       type : FormControlType.DROPDOWN,
261       controlName : FormControlNames.TENANT_ID,
262       displayName : 'Tenant',
263       dataTestId : 'tenant',
264       placeHolder : 'Select Tenant',
265       name : "tenant",
266       isDisabled : _.isNil(serviceInstance),
267       onInitSelectedField :serviceInstance ?  ['lcpRegionsTenantsMap', serviceInstance.lcpCloudRegionId] : null,
268       onInit : serviceInstance ? this._basicControlGenerator.getSubscribeInitResult.bind(
269         this._aaiService,
270         this.aaiService.getLcpRegionsAndTenants.bind(this, serviceInstance.globalSubscriberId, serviceInstance.subscriptionServiceType)) : ()=>{},
271       value : serviceInstance ? serviceInstance.tenantId : null,
272       validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
273     })
274   };
275
276   getAICZoneControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
277     return new DropdownFormControl({
278       type : FormControlType.DROPDOWN,
279       controlName : FormControlNames.AICZONE_ID,
280       displayName : 'AIC zone',
281       dataTestId : 'aic_zone',
282       placeHolder : 'Select AIC zone',
283       name : "aicZone",
284       value : serviceInstance ? serviceInstance.aicZoneId : null,
285       isDisabled : false,
286       validations : [],
287       onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getAicZones)
288     })
289   };
290
291   getPauseControl = (serviceInstance : any, controls : FormControlModel[]) :CheckboxFormControl => {
292     return  new CheckboxFormControl({
293       controlName : FormControlNames.PAUSE,
294       displayName : 'Pause on pause points',
295       dataTestId : 'Pause',
296       isDisabled : false,
297       value : serviceInstance ? serviceInstance.pause : null,
298     })
299   };
300
301   getProjectControl = (serviceInstance : any, controls : FormControlModel[]) :DropdownFormControl =>{
302     return new DropdownFormControl({
303       type : FormControlType.DROPDOWN,
304       controlName : FormControlNames.PROJECT_NAME,
305       displayName : 'Project',
306       dataTestId : 'project',
307       placeHolder : 'Select Project',
308       name : "project",
309       isDisabled : false,
310       validations : [],
311       value : serviceInstance ? serviceInstance.projectName : null,
312       onInitSelectedField : ['projectList'],
313       onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
314     })
315   };
316
317   getRollbackOnFailureControl = (serviceInstance : any, controls : FormControlModel[]) : DropdownFormControl => {
318     return new DropdownFormControl({
319       type : FormControlType.DROPDOWN,
320       controlName : FormControlNames.ROLLBACK_ON_FAILURE,
321       displayName : 'Rollback on failure',
322       dataTestId : 'rollback',
323       isDisabled : false,
324       validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
325       value : serviceInstance ? serviceInstance.rollbackOnFailure : 'true',
326       onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
327     })
328   };
329 }
330
331