VNF's LCP regions found by Line-of-business (and owning-entity)
[vid.git] / vid-webpack-master / src / app / shared / components / genericForm / formControlsServices / vnfGenerator / vnf.control.generator.ts
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 {ControlGeneratorUtil} from "../control.generator.util.service";
7 import {
8   FormControlModel,
9   ValidatorModel,
10   ValidatorOptions
11 } from "../../../../models/formControlModels/formControl.model";
12 import {LogService} from "../../../../utils/log/log.service";
13 import {VNFModel} from "../../../../models/vnfModel";
14 import {AppState} from "../../../../store/reducers";
15 import {FormGroup} from "@angular/forms";
16 import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum";
17 import * as _ from 'lodash';
18 import {MultiselectFormControl} from "../../../../models/formControlModels/multiselectFormControl.model";
19 import {MultiSelectItem} from "../../../formControls/component/multiselect/multiselect.model";
20 import {SharedControllersService} from "../sharedControlles/shared.controllers.service";
21 import {FeatureFlagsService, Features} from "../../../../services/featureFlag/feature-flags.service";
22
23 @Injectable()
24 export class VnfControlGenerator {
25   aaiService: AaiService;
26   constructor(private genericFormService: GenericFormService,
27               private _basicControlGenerator: ControlGeneratorUtil,
28               private _sharedControllersService : SharedControllersService,
29               private _featureFlagsService: FeatureFlagsService,
30               private store: NgRedux<AppState>,
31               private http: HttpClient,
32               private _aaiService: AaiService,
33               private _logService: LogService) {
34     this.aaiService = _aaiService;
35   }
36
37   getVnfInstance = (serviceId: string, vnfStoreKey: string): any => {
38     let vnfInstance = null;
39     if (this.store.getState().service.serviceInstance[serviceId] && _.has(this.store.getState().service.serviceInstance[serviceId].vnfs, vnfStoreKey)) {
40       vnfInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey]);
41     }
42     return vnfInstance;
43   };
44
45   getMacroFormControls(serviceId: string, vnfStoreKey: string, vnfName: string, dynamicInputs?: any[]): FormControlModel[] {
46     vnfStoreKey = _.isNil(vnfStoreKey) ? vnfName : vnfStoreKey;
47
48     if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vnfName)) {
49       this._logService.error('should provide serviceId, vnfName, vnfStoreKey', serviceId);
50       return [];
51     }
52     const vnfInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store,this.getVnfInstance(serviceId, vnfStoreKey));
53     const vnfModel = new VNFModel(this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfName]);
54     let result: FormControlModel[] = [];
55     const flags = this.store.getState().global.flags;
56
57     if (!_.isNil(vnfModel)) {
58       result.push(this.getInstanceName(vnfInstance, serviceId, vnfName, vnfModel.isEcompGeneratedNaming));
59       result.push(this._sharedControllersService.getProductFamilyControl(vnfInstance, result, true));
60       result.push(this._sharedControllersService.getLcpRegionControl(serviceId, vnfInstance, result));
61       result.push(this._sharedControllersService.getLegacyRegion(vnfInstance));
62       result.push(this._sharedControllersService.getTenantControl(serviceId, vnfInstance));
63       result.push(this.getPlatformMultiselectControl(vnfInstance, result, flags['FLAG_2002_VNF_PLATFORM_MULTI_SELECT']));
64       result.push(this._sharedControllersService.getLineOfBusinessControl(vnfInstance));
65     }
66     return result;
67   }
68
69   getAlaCarteFormControls(serviceId: string, vnfStoreKey: string, vnfName: string, dynamicInputs?: any[]): FormControlModel[] {
70     vnfStoreKey = _.isNil(vnfStoreKey) ? vnfName : vnfStoreKey;
71     if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vnfName)) {
72       this._logService.error('should provide serviceId, vnfName, vnfStoreKey', serviceId);
73       return [];
74     }
75
76     let result: FormControlModel[] = [];
77     const vnfInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store,this.getVnfInstance(serviceId, vnfStoreKey));
78     const vnfModel = new VNFModel(this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfName]);
79
80     if (!_.isNil(vnfModel)) {
81       const isMultiSelected =
82         this._featureFlagsService.getFlagState(Features.FLAG_2002_VNF_PLATFORM_MULTI_SELECT);
83       const lcpRegionsByLineOFBusiness =
84         this._featureFlagsService.getFlagState(Features.FLAG_2006_LCP_REGIONS_BY_LINE_OF_BUSINESS);
85
86       result.push(this.getInstanceName(vnfInstance, serviceId, vnfName, vnfModel.isEcompGeneratedNaming));
87       result.push(this._sharedControllersService.getProductFamilyControl(vnfInstance, result, true));
88
89       if (lcpRegionsByLineOFBusiness) {
90         result.push(this._sharedControllersService.getLineOfBusinessByOwningEntityControl(vnfInstance, serviceId, result));
91         result.push(this._sharedControllersService.getLcpRegionByLineOfBusinessControl(serviceId, vnfInstance, result));
92         result.push(this._sharedControllersService.getLegacyRegion(vnfInstance));
93         result.push(this._sharedControllersService.getTenantByLcpRegionControl(serviceId, vnfInstance));
94         result.push(this.getPlatformMultiselectControl(vnfInstance, result, isMultiSelected));
95       } else {
96         result.push(this._sharedControllersService.getLcpRegionControl(serviceId, vnfInstance, result));
97         result.push(this._sharedControllersService.getLegacyRegion(vnfInstance));
98         result.push(this._sharedControllersService.getTenantControl(serviceId, vnfInstance));
99         result.push(this.getPlatformMultiselectControl(vnfInstance, result, isMultiSelected));
100         result.push(this._sharedControllersService.getLineOfBusinessControl(vnfInstance));
101       }
102
103       result.push(this._sharedControllersService.getRollbackOnFailureControl(vnfInstance));
104     }
105     return result;
106   }
107
108   getInstanceName(instance : any, serviceId : string, vnfName : string, isEcompGeneratedNaming: boolean): FormControlModel {
109     const vnfModel : VNFModel = this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfName];
110     return this._sharedControllersService.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, vnfModel);
111   }
112
113   getPlatformMultiselectControl = (instance: any, controls: FormControlModel[], isMultiSelected: boolean) : MultiselectFormControl => {
114     return new MultiselectFormControl({
115       type: FormControlType.MULTI_SELECT ,
116       controlName: 'platformName',
117       displayName: 'Platform',
118       dataTestId: 'multi-selectPlatform',
119       selectedFieldName :  'name' ,
120       ngValue :  'name',
121       placeHolder: 'Select Platform',
122       isDisabled: false,
123       name: "platform",
124       value: instance ? instance.platformName : '',
125       limitSelection : isMultiSelected ? 1000 : 1,
126       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
127       onInitSelectedField: ['platformList'],
128       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this._aaiService.getCategoryParameters),
129       onChange : (param: MultiSelectItem[], form: FormGroup) => {
130         form.controls['platformName'].setValue(param.map((multiSelectItem: MultiSelectItem)=>{
131           return multiSelectItem.itemName
132         }).join(','));
133       },
134       convertOriginalDataToArray : (value?: string) => {
135         if(_.isNil(value)) return [];
136         return value.split(',');
137       }
138     });
139   };
140 }