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";
18 import {CheckboxFormControl} from "../../../models/formControlModels/checkboxFormControl.model";
19 import {VidNotions} from "../../../models/vidNotions";
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',
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;
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]);
53 return serviceInstance;
56 getAlaCartControls(serviceId: string, dynamicInputs?: any[]) : FormControlModel[] {
57 if(_.isNil(serviceId)){
58 this._logService.error('should provide serviceId', serviceId);
61 const serviceInstance = this.getServiceInstance(serviceId);
63 let result : FormControlModel[] = [];
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));
75 this._logService.info('Generate dynamic service controls, is edit mode: ' + serviceInstance != null , result);
79 getMacroFormControls(serviceId: string, dynamicInputs?: any[]) : FormControlModel[] {
80 if(_.isNil(serviceId)){
81 this._logService.error('should provide serviceId', serviceId);
85 const serviceInstance = this.getServiceInstance(serviceId);
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));
101 if(serviceModel.isMultiStepDesign){
102 result.push(new CheckboxFormControl({
103 controlName : FormControlNames.PAUSE,
104 displayName : 'Pause on pause points',
105 dataTestId : 'Pause',
107 validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
108 value : serviceInstance ? serviceInstance.pause : null,
112 result.push(this.getProjectControl(serviceInstance, result));
113 result.push(this.getRollbackOnFailureControl(serviceInstance, result));
117 this._logService.info('Generate dynamic service controls, is edit mode: ' + serviceInstance != null , result);
121 getRollBackOnFailureOptions = () : Observable<SelectOption[]> =>{
123 new SelectOption({id: 'true', name: 'Rollback'}),
124 new SelectOption({id: 'false', name: 'Don\'t Rollback'})
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',
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();
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;
149 form.controls[FormControlNames.SUBSCRIPTION_SERVICE_TYPE].disable();
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(
169 this.aaiService.getServiceTypes.bind(this, serviceInstance.globalSubscriberId)) : ()=>{},
170 onChange : (param: string, form : FormGroup) => {
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;
183 form.controls[FormControlNames.LCPCLOUD_REGION_ID].disable();
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",
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)
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',
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),
222 isRegionAndTenantOptional = (vidNotions?: VidNotions) : boolean => {
223 return !_.isNil(vidNotions) && vidNotions.modelCategory === "Transport"
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',
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(
241 this.aaiService.getLcpRegionsAndTenants.bind(this, serviceInstance.globalSubscriberId, serviceInstance.subscriptionServiceType)) : ()=>{},
242 onChange : (param: string, form : FormGroup) => {
243 form.controls[FormControlNames.TENANT_ID].reset();
245 form.controls[FormControlNames.TENANT_ID].enable();
248 form.controls[FormControlNames.TENANT_ID].disable();
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];
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',
269 isDisabled : _.isNil(serviceInstance),
270 onInitSelectedField : serviceInstance ? ['lcpRegionsTenantsMap', serviceInstance.lcpCloudRegionId] : null,
271 onInit : serviceInstance ? this._basicControlGenerator.getSubscribeInitResult.bind(
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')],
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',
287 value : serviceInstance ? serviceInstance.aicZoneId : null,
290 onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getAicZones)
294 getPauseControl = (serviceInstance : any, controls : FormControlModel[]) :CheckboxFormControl => {
295 return new CheckboxFormControl({
296 controlName : FormControlNames.PAUSE,
297 displayName : 'Pause on pause points',
298 dataTestId : 'Pause',
300 value : serviceInstance ? serviceInstance.pause : null,
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',
314 value : serviceInstance ? serviceInstance.projectName : null,
315 onInitSelectedField : ['projectList'],
316 onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
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',
327 validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
328 value : serviceInstance ? serviceInstance.rollbackOnFailure : 'true',
329 onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)