1 import {Injectable} from "@angular/core";
2 import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model";
3 import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum";
8 } from "../../../../models/formControlModels/formControl.model";
9 import {NgRedux} from "@angular-redux/store";
10 import {AppState} from "../../../../store/reducers";
11 import {AaiService} from "../../../../services/aaiService/aai.service";
12 import {ControlGeneratorUtil, SDN_C_PRE_LOAD} from "../control.generator.util.service";
13 import * as _ from "lodash";
14 import {FormGroup} from "@angular/forms";
15 import {Constants} from "../../../../utils/constants";
16 import {CheckboxFormControl} from "../../../../models/formControlModels/checkboxFormControl.model";
17 import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model";
18 import {NodeModel} from "../../../../models/nodeModel";
21 export class SharedControllersService {
22 constructor(private _store : NgRedux<AppState>,
23 private _aaiService : AaiService,
24 private _basicControlGenerator : ControlGeneratorUtil){}
27 getLineOfBusinessControl = (instance?: any): DropdownFormControl => {
28 return new DropdownFormControl({
29 type: FormControlType.DROPDOWN,
30 controlName: 'lineOfBusiness',
31 displayName: 'Line of business',
32 dataTestId: 'lineOfBusiness',
33 placeHolder: 'Select Line Of Business',
35 name: "lineOfBusiness",
36 value: instance ? instance.lineOfBusiness : null,
37 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
38 onInitSelectedField: ['lineOfBusinessList'],
39 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
43 getTenantControl = (serviceId: string, instance?: any): DropdownFormControl => {
44 const service = this._store.getState().service.serviceInstance[serviceId];
45 const globalCustomerId: string = service.globalSubscriberId;
46 const serviceType: string = service.subscriptionServiceType;
47 return new DropdownFormControl({
48 type: FormControlType.DROPDOWN,
49 controlName: 'tenantId',
50 displayName: 'Tenant',
52 placeHolder: 'Select Tenant',
54 isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId),
55 onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null,
56 value: instance ? instance.tenantId : null,
57 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
58 onInit: instance ? this._basicControlGenerator.getSubscribeInitResult.bind(
60 this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : () => {
65 getRollbackOnFailureControl = (instance?: any): DropdownFormControl => {
66 return new DropdownFormControl({
67 type: FormControlType.DROPDOWN,
68 controlName: 'rollbackOnFailure',
69 displayName: 'Rollback on failure',
70 dataTestId: 'rollback',
71 placeHolder: 'Rollback on failure',
73 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
74 value: instance ? instance.rollbackOnFailure : 'true',
75 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._basicControlGenerator.getRollBackOnFailureOptions)
79 getLegacyRegion(instance: any): FormControlModel {
80 return new InputFormControl({
81 controlName: 'legacyRegion',
82 displayName: 'Legacy Region',
83 dataTestId: 'lcpRegionText',
84 placeHolder: 'Type Legacy Region',
86 isVisible: this._basicControlGenerator.isLegacyRegionShouldBeVisible(instance),
87 isDisabled : _.isNil(instance) ? true : Constants.LegacyRegion.MEGA_REGION.indexOf(instance.lcpCloudRegionId),
88 value: instance ? instance.legacyRegion : null
92 getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
93 const service = this._store.getState().service.serviceInstance[serviceId];
94 const globalCustomerId: string = service.globalSubscriberId;
95 const serviceType: string = service.subscriptionServiceType;
96 return new DropdownFormControl({
97 type: FormControlType.DROPDOWN,
98 controlName: 'lcpCloudRegionId',
99 displayName: 'LCP region',
100 dataTestId: 'lcpRegion',
101 placeHolder: 'Select LCP Region',
104 value: instance ? instance.lcpCloudRegionId : null,
105 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
106 onInitSelectedField: ['lcpRegionList'],
107 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(
109 this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)),
110 onChange: (param: string, form: FormGroup) => {
111 form.controls['tenantId'].enable();
112 form.controls['tenantId'].reset();
113 if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) {
114 this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => {
115 controls.find(item => item.controlName === 'tenantId')['options$'] = res.lcpRegionsTenantsMap[param];
116 if (res.lcpRegionsTenantsMap[param]) {
117 controls.find(item => item.controlName === 'tenantId')['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0;
122 if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) {
123 form.controls['legacyRegion'].enable();
124 controls.find(item => item.controlName === 'legacyRegion').isVisible = true;
127 controls.find(item => item.controlName === 'legacyRegion').isVisible = false;
128 form.controls['legacyRegion'].setValue(null);
129 form.controls['legacyRegion'].reset();
130 form.controls['legacyRegion'].disable();
136 getSDNCControl = (instance: any, extraContents? : object[]): FormControlModel => {
137 return new CheckboxFormControl({
138 controlName: SDN_C_PRE_LOAD,
139 displayName: 'SDN-C pre-load',
140 dataTestId: 'sdncPreLoad',
141 value: instance ? instance.sdncPreLoad : false,
142 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
147 getProductFamilyControl = (instance : any, controls : FormControlModel[], isMandatory?: boolean) : DropdownFormControl => {
148 return new DropdownFormControl({
149 type : FormControlType.DROPDOWN,
150 controlName : 'productFamilyId',
151 displayName : 'Product family',
152 dataTestId : 'productFamily',
153 placeHolder : 'Select Product Family',
155 name : "product-family-select",
156 value : instance ? instance.productFamilyId : null,
157 validations : _.isNil(isMandatory) || isMandatory === true ? [new ValidatorModel(ValidatorOptions.required, 'is required')]: [],
158 onInit : this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getProductFamilies),
162 getInstanceNameController(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, model: NodeModel): FormControlModel {
163 let validations: ValidatorModel[] = this._basicControlGenerator.createValidationsForInstanceName(instance, serviceId, isEcompGeneratedNaming);
164 return new InputFormControl({
165 controlName: 'instanceName',
166 displayName: 'Instance name',
167 dataTestId: 'instanceName',
168 placeHolder: (!isEcompGeneratedNaming) ? 'Instance name' : 'Automatically generated when not provided',
169 validations: validations,
171 value : (!isEcompGeneratedNaming || (!_.isNil(instance) && !_.isNil(instance.instanceName) && instance.instanceName !== ""))
172 ? this._basicControlGenerator.getDefaultInstanceName(instance, model) : null,
173 onKeypress : (event) => {
174 const pattern:RegExp = ControlGeneratorUtil.INSTANCE_NAME_REG_EX;
176 if(!pattern.test(event['key'])){
177 event.preventDefault();
185 getInstanceName(instance : any, serviceId : string, isEcompGeneratedNaming: boolean): FormControlModel {
186 let formControlModel:FormControlModel = this.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, new NodeModel());
187 formControlModel.value = instance ? instance.instanceName : null;
188 return formControlModel;