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