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";
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";
23 import {CheckboxFormControl} from "../../../models/formControlModels/checkboxFormControl.model";
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',
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;
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]);
57 return serviceInstance;
60 getAlaCartControls(serviceId: string, dynamicInputs?: any[]) : FormControlModel[] {
61 if(_.isNil(serviceId)){
62 this._logService.error('should provide serviceId', serviceId);
65 const serviceInstance = this.getServiceInstance(serviceId);
67 let result : FormControlModel[] = [];
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));
79 this._logService.info('Generate dynamic service controls, is edit mode: ' + serviceInstance != null , result);
83 getMacroFormControls(serviceId: string, dynamicInputs?: any[]) : FormControlModel[] {
84 if(_.isNil(serviceId)){
85 this._logService.error('should provide serviceId', serviceId);
89 const serviceInstance = this.getServiceInstance(serviceId);
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));
103 if(serviceModel.isMultiStepDesign){
104 result.push(new CheckboxFormControl({
105 controlName : FormControlNames.PAUSE,
106 displayName : 'Pause on pause points',
107 dataTestId : 'Pause',
109 validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
110 value : serviceInstance ? serviceInstance.pause : null,
114 result.push(this.getProjectControl(serviceInstance, result));
115 result.push(this.getRollbackOnFailureControl(serviceInstance, result));
119 this._logService.info('Generate dynamic service controls, is edit mode: ' + serviceInstance != null , result);
123 getRollBackOnFailureOptions = () : Observable<SelectOption[]> =>{
125 new SelectOption({id: 'true', name: 'Rollback'}),
126 new SelectOption({id: 'false', name: 'Don\'t Rollback'})
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',
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();
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;
151 form.controls[FormControlNames.SUBSCRIPTION_SERVICE_TYPE].disable();
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(
171 this.aaiService.getServiceTypes.bind(this, serviceInstance.globalSubscriberId)) : ()=>{},
172 onChange : (param: string, form : FormGroup) => {
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;
185 form.controls[FormControlNames.LCPCLOUD_REGION_ID].disable();
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",
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)
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',
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),
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',
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(
238 this.aaiService.getLcpRegionsAndTenants.bind(this, serviceInstance.globalSubscriberId, serviceInstance.subscriptionServiceType)) : ()=>{},
239 onChange : (param: string, form : FormGroup) => {
240 form.controls[FormControlNames.TENANT_ID].reset();
242 form.controls[FormControlNames.TENANT_ID].enable();
245 form.controls[FormControlNames.TENANT_ID].disable();
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];
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',
266 isDisabled : _.isNil(serviceInstance),
267 onInitSelectedField :serviceInstance ? ['lcpRegionsTenantsMap', serviceInstance.lcpCloudRegionId] : null,
268 onInit : serviceInstance ? this._basicControlGenerator.getSubscribeInitResult.bind(
270 this.aaiService.getLcpRegionsAndTenants.bind(this, serviceInstance.globalSubscriberId, serviceInstance.subscriptionServiceType)) : ()=>{},
271 value : serviceInstance ? serviceInstance.tenantId : null,
272 validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
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',
284 value : serviceInstance ? serviceInstance.aicZoneId : null,
287 onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getAicZones)
291 getPauseControl = (serviceInstance : any, controls : FormControlModel[]) :CheckboxFormControl => {
292 return new CheckboxFormControl({
293 controlName : FormControlNames.PAUSE,
294 displayName : 'Pause on pause points',
295 dataTestId : 'Pause',
297 value : serviceInstance ? serviceInstance.pause : null,
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',
311 value : serviceInstance ? serviceInstance.projectName : null,
312 onInitSelectedField : ['projectList'],
313 onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
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',
324 validations : [new ValidatorModel(ValidatorOptions.required, 'is required')],
325 value : serviceInstance ? serviceInstance.rollbackOnFailure : 'true',
326 onInit : this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)