1 import {Injectable} from "@angular/core";
2 import {GenericFormService} from "../../generic-form.service";
3 import {AaiService} from "../../../../services/aaiService/aai.service";
4 import {NgRedux} from "@angular-redux/store";
5 import {HttpClient} from "@angular/common/http";
6 import {BasicControlGenerator} from "../basic.control.generator";
7 import * as _ from 'lodash';
8 import {Observable, of} from "rxjs";
14 } from "../../../../models/formControlModels/formControl.model";
15 import {LogService} from "../../../../utils/log/log.service";
16 import {AppState} from "../../../../store/reducers";
17 import {FormGroup} from "@angular/forms";
18 import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model";
19 import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum";
20 import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model";
21 import {SelectOption} from "../../../../models/selectOption";
22 import {NetworkInstance} from "../../../../models/networkInstance";
23 import {NetworkModel} from "../../../../models/networkModel";
24 import {Constants} from "../../../../utils/constants";
26 export enum FormControlNames {
27 INSTANCE_NAME = 'instanceName',
28 GLOBAL_SUBSCRIBER_ID = 'globalSubscriberId',
29 PRODUCT_FAMILY_ID = 'productFamilyId',
30 LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
31 TENANT_ID = 'tenantId',
32 AICZONE_ID = 'aicZoneId',
33 ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
34 LEGACY_REGION = 'legacyRegion'
39 LCP_REGION = "lcpCloudRegionId",
41 LOB = "lineOfBusiness",
42 PLATFORM = "platformName",
43 ROLLBACK = "rollbackOnFailure",
44 PRODUCT_FAMILY = "productFamilyId",
45 VG = "volumeGroupName"
49 export class NetworkControlGenerator {
50 aaiService: AaiService;
52 constructor(private genericFormService: GenericFormService,
53 private _basicControlGenerator: BasicControlGenerator,
54 private store: NgRedux<AppState>,
55 private http: HttpClient,
56 private _aaiService: AaiService,
57 private _logService: LogService) {
58 this.aaiService = _aaiService;
61 getNetworkInstance = (serviceId: string, networkName: string, isUpdateMode : boolean): NetworkInstance => {
62 let networkInstance : NetworkInstance = null;
63 if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] && _.has(this.store.getState().service.serviceInstance[serviceId].networks, networkName)) {
64 networkInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].networks[networkName]);
66 return networkInstance;
70 getMacroFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode : boolean): FormControlModel[] {
71 networkStoreKey = _.isNil(networkStoreKey) ? networkName : networkStoreKey;
73 if (_.isNil(serviceId) || _.isNil(networkStoreKey) || _.isNil(networkName)) {
74 this._logService.error('should provide serviceId, networkName, networkStoreKey', serviceId);
78 const networkInstance = this.getNetworkInstance(serviceId, networkStoreKey, isUpdateMode);
79 const networkModel = new NetworkModel(this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]);
80 let result: FormControlModel[] = [];
82 if (!_.isNil(networkModel)) {
83 result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming));
84 result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false));
85 result.push(this.getLcpRegionControl(serviceId, networkInstance, result));
86 result.push(this._basicControlGenerator.getLegacyRegion(networkInstance));
87 result.push(this.getTenantControl(serviceId, networkInstance, result));
88 result.push(this.getPlatformControl(networkInstance, result));
89 result.push(this.getLineOfBusinessControl(networkInstance, result));
95 getAlaCarteFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode : boolean): FormControlModel[] {
96 networkStoreKey = _.isNil(networkStoreKey) ? networkName : networkStoreKey;
97 if (_.isNil(serviceId) || _.isNil(networkStoreKey) || _.isNil(networkName)) {
98 this._logService.error('should provide serviceId, networkName, networkStoreKey', serviceId);
102 let result: FormControlModel[] = [];
103 const networkInstance = this.getNetworkInstance(serviceId, networkStoreKey, isUpdateMode);
104 const networkModel = new NetworkModel(this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]);
106 if (!_.isNil(networkModel)) {
107 result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming));
108 result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false));
109 result.push(this.getLcpRegionControl(serviceId, networkInstance, result));
110 result.push(this._basicControlGenerator.getLegacyRegion(networkInstance));
111 result.push(this.getTenantControl(serviceId, networkInstance, result));
112 result.push(this.getPlatformControl(networkInstance, result));
113 result.push(this.getLineOfBusinessControl(networkInstance, result));
114 result.push(this.getRollbackOnFailureControl(networkInstance, result));
120 isInputShouldBeShown = (inputType: any): boolean => {
121 let networkInputs = [InputType.LCP_REGION, InputType.LOB, InputType.TENANT, InputType.PRODUCT_FAMILY, InputType.PLATFORM, InputType.ROLLBACK];
122 return networkInputs.indexOf(inputType) > -1;
125 getInstanceName(instance : any, serviceId : string, networkName : string, isEcompGeneratedNaming: boolean): FormControlModel {
126 const networkModel : NetworkModel = this.store.getState().service.serviceHierarchy[serviceId].networks[networkName];
127 return this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, networkModel);
130 getLineOfBusinessControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
131 return new DropdownFormControl({
132 type: FormControlType.DROPDOWN,
133 controlName: 'lineOfBusiness',
134 displayName: 'Line of business',
135 dataTestId: 'lineOfBusiness',
136 placeHolder: 'Select Line Of Business',
138 name: "lineOfBusiness",
139 value: instance ? instance.lineOfBusiness : null,
141 onInitSelectedField: ['lineOfBusinessList'],
142 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
146 getPlatformControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
147 return new DropdownFormControl({
148 type: FormControlType.DROPDOWN,
149 controlName: 'platformName',
150 displayName: 'Platform',
151 dataTestId: 'platform',
152 placeHolder: 'Select Platform',
155 value: instance ? instance.platformName : null,
156 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
157 onInitSelectedField: ['platformList'],
158 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
162 getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
163 const service = this.store.getState().service.serviceInstance[serviceId];
164 const globalCustomerId: string = service.globalSubscriberId;
165 const serviceType: string = service.subscriptionServiceType;
166 return new DropdownFormControl({
167 type: FormControlType.DROPDOWN,
168 controlName: FormControlNames.TENANT_ID,
169 displayName: 'Tenant',
170 dataTestId: 'tenant',
171 placeHolder: 'Select Tenant',
173 isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId),
174 onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null,
175 value: instance ? instance.tenantId : null,
176 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
177 onInit : instance ? this._basicControlGenerator.getSubscribeInitResult.bind(
179 this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : ()=>{},
183 getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
184 const service = this.store.getState().service.serviceInstance[serviceId];
185 const globalCustomerId: string = service.globalSubscriberId;
186 const serviceType: string = service.subscriptionServiceType;
187 return new DropdownFormControl({
188 type: FormControlType.DROPDOWN,
189 controlName: 'lcpCloudRegionId',
190 displayName: 'LCP region',
191 dataTestId: 'lcpRegion',
192 placeHolder: 'Select LCP Region',
195 value: instance ? instance.lcpCloudRegionId : null,
196 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
197 onInitSelectedField: ['lcpRegionList'],
198 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(
200 this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)),
201 onChange: (param: string, form: FormGroup) => {
202 form.controls[FormControlNames.TENANT_ID].enable();
203 form.controls[FormControlNames.TENANT_ID].reset();
204 if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) {
205 this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => {
206 controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
207 if(res.lcpRegionsTenantsMap[param]){
208 controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0;
213 if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) {
214 form.controls['legacyRegion'].enable();
215 controls.find(item => item.controlName === 'legacyRegion').isVisible = true;
218 controls.find(item => item.controlName === 'legacyRegion').isVisible = false;
219 form.controls['legacyRegion'].setValue(null);
220 form.controls['legacyRegion'].reset();
221 form.controls['legacyRegion'].disable();
227 getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
228 return new DropdownFormControl({
229 type: FormControlType.DROPDOWN,
230 controlName: FormControlNames.ROLLBACK_ON_FAILURE,
231 displayName: 'Rollback on failure',
232 dataTestId: 'rollback',
233 placeHolder: 'Rollback on failure',
235 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
236 value: instance ? instance.rollbackOnFailure : 'true',
237 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
241 getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
243 new SelectOption({id: 'true', name: 'Rollback'}),
244 new SelectOption({id: 'false', name: 'Don\'t Rollback'})