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 {SelectOption} from "../../../../models/selectOption";
21 import {NetworkInstance} from "../../../../models/networkInstance";
22 import {NetworkModel} from "../../../../models/networkModel";
23 import {Constants} from "../../../../utils/constants";
25 export enum FormControlNames {
26 INSTANCE_NAME = 'instanceName',
27 GLOBAL_SUBSCRIBER_ID = 'globalSubscriberId',
28 PRODUCT_FAMILY_ID = 'productFamilyId',
29 LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
30 TENANT_ID = 'tenantId',
31 AICZONE_ID = 'aicZoneId',
32 ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
33 LEGACY_REGION = 'legacyRegion'
38 LCP_REGION = "lcpCloudRegionId",
40 LOB = "lineOfBusiness",
41 PLATFORM = "platformName",
42 ROLLBACK = "rollbackOnFailure",
43 PRODUCT_FAMILY = "productFamilyId",
44 VG = "volumeGroupName"
48 export class NetworkControlGenerator {
49 aaiService: AaiService;
51 constructor(private genericFormService: GenericFormService,
52 private _basicControlGenerator: BasicControlGenerator,
53 private store: NgRedux<AppState>,
54 private http: HttpClient,
55 private _aaiService: AaiService,
56 private _logService: LogService) {
57 this.aaiService = _aaiService;
60 getNetworkInstance = (serviceId: string, networkName: string, isUpdateMode : boolean): NetworkInstance => {
61 let networkInstance : NetworkInstance = null;
62 if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] && _.has(this.store.getState().service.serviceInstance[serviceId].networks, networkName)) {
63 networkInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].networks[networkName]);
65 return networkInstance;
69 getMacroFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode : boolean): FormControlModel[] {
70 networkStoreKey = _.isNil(networkStoreKey) ? networkName : networkStoreKey;
72 if (_.isNil(serviceId) || _.isNil(networkStoreKey) || _.isNil(networkName)) {
73 this._logService.error('should provide serviceId, networkName, networkStoreKey', serviceId);
77 const networkInstance = this.getNetworkInstance(serviceId, networkStoreKey, isUpdateMode);
78 const networkModel = new NetworkModel(this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]);
79 let result: FormControlModel[] = [];
81 if (!_.isNil(networkModel)) {
82 result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming));
83 result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false));
84 result.push(this.getLcpRegionControl(serviceId, networkInstance, result));
85 result.push(this._basicControlGenerator.getLegacyRegion(networkInstance));
86 result.push(this.getTenantControl(serviceId, networkInstance, result));
87 result.push(this.getPlatformControl(networkInstance, result));
88 result.push(this.getLineOfBusinessControl(networkInstance, result));
94 getAlaCarteFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode : boolean): FormControlModel[] {
95 networkStoreKey = _.isNil(networkStoreKey) ? networkName : networkStoreKey;
96 if (_.isNil(serviceId) || _.isNil(networkStoreKey) || _.isNil(networkName)) {
97 this._logService.error('should provide serviceId, networkName, networkStoreKey', serviceId);
101 let result: FormControlModel[] = [];
102 const networkInstance = this.getNetworkInstance(serviceId, networkStoreKey, isUpdateMode);
103 const networkModel = new NetworkModel(this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]);
105 if (!_.isNil(networkModel)) {
106 result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming));
107 result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false));
108 result.push(this.getLcpRegionControl(serviceId, networkInstance, result));
109 result.push(this._basicControlGenerator.getLegacyRegion(networkInstance));
110 result.push(this.getTenantControl(serviceId, networkInstance, result));
111 result.push(this.getPlatformControl(networkInstance, result));
112 result.push(this.getLineOfBusinessControl(networkInstance, result));
113 result.push(this.getRollbackOnFailureControl(networkInstance, result));
119 isInputShouldBeShown = (inputType: any): boolean => {
120 let networkInputs = [InputType.LCP_REGION, InputType.LOB, InputType.TENANT, InputType.PRODUCT_FAMILY, InputType.PLATFORM, InputType.ROLLBACK];
121 return networkInputs.indexOf(inputType) > -1;
124 getInstanceName(instance : any, serviceId : string, networkName : string, isEcompGeneratedNaming: boolean): FormControlModel {
125 const networkModel : NetworkModel = this.store.getState().service.serviceHierarchy[serviceId].networks[networkName];
126 return this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, networkModel);
129 getLineOfBusinessControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
130 return new DropdownFormControl({
131 type: FormControlType.DROPDOWN,
132 controlName: 'lineOfBusiness',
133 displayName: 'Line of business',
134 dataTestId: 'lineOfBusiness',
135 placeHolder: 'Select Line Of Business',
137 name: "lineOfBusiness",
138 value: instance ? instance.lineOfBusiness : null,
139 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
140 onInitSelectedField: ['lineOfBusinessList'],
141 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
145 getPlatformControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
146 return new DropdownFormControl({
147 type: FormControlType.DROPDOWN,
148 controlName: 'platformName',
149 displayName: 'Platform',
150 dataTestId: 'platform',
151 placeHolder: 'Select Platform',
154 value: instance ? instance.platformName : null,
156 onInitSelectedField: ['platformList'],
157 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
161 getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
162 const service = this.store.getState().service.serviceInstance[serviceId];
163 const globalCustomerId: string = service.globalSubscriberId;
164 const serviceType: string = service.subscriptionServiceType;
165 return new DropdownFormControl({
166 type: FormControlType.DROPDOWN,
167 controlName: FormControlNames.TENANT_ID,
168 displayName: 'Tenant',
169 dataTestId: 'tenant',
170 placeHolder: 'Select Tenant',
172 isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId),
173 onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null,
174 value: instance ? instance.tenantId : null,
175 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
176 onInit : instance ? this._basicControlGenerator.getSubscribeInitResult.bind(
178 this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : ()=>{},
182 getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
183 const service = this.store.getState().service.serviceInstance[serviceId];
184 const globalCustomerId: string = service.globalSubscriberId;
185 const serviceType: string = service.subscriptionServiceType;
186 return new DropdownFormControl({
187 type: FormControlType.DROPDOWN,
188 controlName: 'lcpCloudRegionId',
189 displayName: 'LCP region',
190 dataTestId: 'lcpRegion',
191 placeHolder: 'Select LCP Region',
194 value: instance ? instance.lcpCloudRegionId : null,
195 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
196 onInitSelectedField: ['lcpRegionList'],
197 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(
199 this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)),
200 onChange: (param: string, form: FormGroup) => {
201 form.controls[FormControlNames.TENANT_ID].enable();
202 form.controls[FormControlNames.TENANT_ID].reset();
203 if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) {
204 this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => {
205 controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
206 if(res.lcpRegionsTenantsMap[param]){
207 controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0;
212 if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) {
213 form.controls['legacyRegion'].enable();
214 controls.find(item => item.controlName === 'legacyRegion').isVisible = true;
217 controls.find(item => item.controlName === 'legacyRegion').isVisible = false;
218 form.controls['legacyRegion'].setValue(null);
219 form.controls['legacyRegion'].reset();
220 form.controls['legacyRegion'].disable();
226 getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
227 return new DropdownFormControl({
228 type: FormControlType.DROPDOWN,
229 controlName: FormControlNames.ROLLBACK_ON_FAILURE,
230 displayName: 'Rollback on failure',
231 dataTestId: 'rollback',
232 placeHolder: 'Rollback on failure',
234 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
235 value: instance ? instance.rollbackOnFailure : 'true',
236 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
240 getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
242 new SelectOption({id: 'true', name: 'Rollback'}),
243 new SelectOption({id: 'false', name: 'Don\'t Rollback'})