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);
76 const networkInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getNetworkInstance(serviceId, networkStoreKey, isUpdateMode));
77 const networkModel = new NetworkModel(this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]);
78 let result: FormControlModel[] = [];
80 if (!_.isNil(networkModel)) {
81 result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming));
82 result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false));
83 result.push(this.getLcpRegionControl(serviceId, networkInstance, result));
84 result.push(this._basicControlGenerator.getLegacyRegion(networkInstance));
85 result.push(this.getTenantControl(serviceId, networkInstance, result));
86 result.push(this.getPlatformControl(networkInstance, result));
87 result.push(this.getLineOfBusinessControl(networkInstance, result));
93 getAlaCarteFormControls(serviceId: string, networkStoreKey: string, networkName: string, isUpdateMode : boolean): FormControlModel[] {
94 networkStoreKey = _.isNil(networkStoreKey) ? networkName : networkStoreKey;
95 if (_.isNil(serviceId) || _.isNil(networkStoreKey) || _.isNil(networkName)) {
96 this._logService.error('should provide serviceId, networkName, networkStoreKey', serviceId);
100 let result: FormControlModel[] = [];
101 const networkInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getNetworkInstance(serviceId, networkStoreKey, isUpdateMode));
102 const networkModel = new NetworkModel(this.store.getState().service.serviceHierarchy[serviceId].networks[networkName]);
104 if (!_.isNil(networkModel)) {
105 result.push(this.getInstanceName(networkInstance, serviceId, networkName, networkModel.isEcompGeneratedNaming));
106 result.push(this._basicControlGenerator.getProductFamilyControl(networkInstance, result, false));
107 result.push(this.getLcpRegionControl(serviceId, networkInstance, result));
108 result.push(this._basicControlGenerator.getLegacyRegion(networkInstance));
109 result.push(this.getTenantControl(serviceId, networkInstance, result));
110 result.push(this.getPlatformControl(networkInstance, result));
111 result.push(this.getLineOfBusinessControl(networkInstance, result));
112 result.push(this.getRollbackOnFailureControl(networkInstance, result));
118 isInputShouldBeShown = (inputType: any): boolean => {
119 let networkInputs = [InputType.LCP_REGION, InputType.LOB, InputType.TENANT, InputType.PRODUCT_FAMILY, InputType.PLATFORM, InputType.ROLLBACK];
120 return networkInputs.indexOf(inputType) > -1;
123 getInstanceName(instance : any, serviceId : string, networkName : string, isEcompGeneratedNaming: boolean): FormControlModel {
124 const networkModel : NetworkModel = this.store.getState().service.serviceHierarchy[serviceId].networks[networkName];
125 return this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, networkModel);
128 getLineOfBusinessControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
129 return new DropdownFormControl({
130 type: FormControlType.DROPDOWN,
131 controlName: 'lineOfBusiness',
132 displayName: 'Line of business',
133 dataTestId: 'lineOfBusiness',
134 placeHolder: 'Select Line Of Business',
136 name: "lineOfBusiness",
137 value: instance ? instance.lineOfBusiness : null,
138 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
139 onInitSelectedField: ['lineOfBusinessList'],
140 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
144 getPlatformControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
145 return new DropdownFormControl({
146 type: FormControlType.DROPDOWN,
147 controlName: 'platformName',
148 displayName: 'Platform',
149 dataTestId: 'platform',
150 placeHolder: 'Select Platform',
153 value: instance ? instance.platformName : null,
155 onInitSelectedField: ['platformList'],
156 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters)
160 getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
161 const service = this.store.getState().service.serviceInstance[serviceId];
162 const globalCustomerId: string = service.globalSubscriberId;
163 const serviceType: string = service.subscriptionServiceType;
164 return new DropdownFormControl({
165 type: FormControlType.DROPDOWN,
166 controlName: FormControlNames.TENANT_ID,
167 displayName: 'Tenant',
168 dataTestId: 'tenant',
169 placeHolder: 'Select Tenant',
171 isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId),
172 onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null,
173 value: instance ? instance.tenantId : null,
174 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
175 onInit : instance ? this._basicControlGenerator.getSubscribeInitResult.bind(
177 this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : ()=>{},
181 getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
182 const service = this.store.getState().service.serviceInstance[serviceId];
183 const globalCustomerId: string = service.globalSubscriberId;
184 const serviceType: string = service.subscriptionServiceType;
185 return new DropdownFormControl({
186 type: FormControlType.DROPDOWN,
187 controlName: 'lcpCloudRegionId',
188 displayName: 'LCP region',
189 dataTestId: 'lcpRegion',
190 placeHolder: 'Select LCP Region',
193 value: instance ? instance.lcpCloudRegionId : null,
194 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
195 onInitSelectedField: ['lcpRegionList'],
196 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(
198 this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)),
199 onChange: (param: string, form: FormGroup) => {
200 form.controls[FormControlNames.TENANT_ID].enable();
201 form.controls[FormControlNames.TENANT_ID].reset();
202 if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) {
203 this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => {
204 controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
205 if(res.lcpRegionsTenantsMap[param]){
206 controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0;
211 if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) {
212 form.controls['legacyRegion'].enable();
213 controls.find(item => item.controlName === 'legacyRegion').isVisible = true;
216 controls.find(item => item.controlName === 'legacyRegion').isVisible = false;
217 form.controls['legacyRegion'].setValue(null);
218 form.controls['legacyRegion'].reset();
219 form.controls['legacyRegion'].disable();
225 getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
226 return new DropdownFormControl({
227 type: FormControlType.DROPDOWN,
228 controlName: FormControlNames.ROLLBACK_ON_FAILURE,
229 displayName: 'Rollback on failure',
230 dataTestId: 'rollback',
231 placeHolder: 'Rollback on failure',
233 validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
234 value: instance ? instance.rollbackOnFailure : 'true',
235 onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
239 getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
241 new SelectOption({id: 'true', name: 'Rollback'}),
242 new SelectOption({id: 'false', name: 'Don\'t Rollback'})