725e44293f5ab8ef687611b44fbbaa0b08719508
[vid.git] /
1 import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
2 import {FormControl, FormGroup, Validators} from "@angular/forms";
3 import {VNFPopupDataModel} from './vnfPopupDataModel';
4 import {AaiService} from '../../../services/aaiService/aai.service';
5 import { createVFModuleInstance, updateVFModuleInstance, updateVNFInstance } from '../../../service.actions';
6 import {VnfInstance} from "../../../shared/models/vnfInstance";
7 import {ServiceInstance} from "../../../shared/models/serviceInstance";
8 import {VNFModel} from "../../../shared/models/vnfModel";
9 import {InputType} from "../../../shared/models/inputTypes";
10 import {ModelInfo} from "../../../shared/models/modelInfo";
11 import {VfModuleInstance} from "../../../shared/models/vfModuleInstance";
12 import {NgRedux, select} from "@angular-redux/store";
13 import {AppState} from "../../../store/reducers";
14 import {SelectOptionInterface} from "../../../shared/models/selectOption";
15 import {Observable} from "rxjs/Observable";
16 import {loadProductFamiliesAction} from "../../../services/aaiService/aai.actions";
17 import {VnfInstanceDetailsService} from "./vnf-instance-details.service";
18 import {isNullOrUndefined} from 'util';
19 import {NumbersLettersUnderscoreValidator} from '../../../shared/components/validators/numbersLettersUnderscore/numbersLettersUnderscore.validator';
20 import * as _ from "lodash";
21 import {ServiceNodeTypes} from "../../../shared/models/ServiceNodeTypes";
22
23 @Component({
24   selector: 'vnf-instance-details',
25   templateUrl: 'vnf-instance-details.html',
26   styleUrls: ['vnf-instance-details.scss'],
27   providers: [AaiService]
28 })
29
30 export class VnfInstanceDetailsComponent implements OnInit {
31   @ViewChild('vnfForm') vnfForm: 'VnfForm';
32   _vnfModel: VNFModel;
33   @Input ()
34   set vnfModel(vnfModel: VNFModel) {
35     this._vnfModel = vnfModel;
36     this.updateFormGroupControlsFromVNFModel();
37   }
38   @Input() vnfInstance: any;
39   @Input() serviceInstance: ServiceInstance;
40   @Input() dynamicInputs;
41   @Input() modelName: string;
42   @Input() serviceUuid: string;
43   @Input() userProvidedNaming: boolean;
44   _modelType: string;
45   @Input()
46   set modelType(modelType: string) {
47     this._modelType = modelType;
48     this.updateFormGroupControlsFromVNFModel();
49   }
50
51   @Input() parentModelName: string;
52   @Input() isNewVfModule : boolean;
53
54
55   @Output() onSubmitClick: EventEmitter<any> = new EventEmitter<any>();
56   @Output() onServiceInstanceNameChanged :  EventEmitter<boolean> = new EventEmitter<boolean>();
57   @Output() onVolumeGroupNameChanged :  EventEmitter<boolean> = new EventEmitter<boolean>();
58
59 @Output() onDataChanged: EventEmitter<any> = new EventEmitter<any>();
60   @select(['service','productFamilies'])
61   readonly productFamilies : Observable<SelectOptionInterface[]>;
62
63   vnfPopupDataModel: VNFPopupDataModel = new VNFPopupDataModel();
64   lcpRegionsThatEnableLegacyRegionField = ['AAIAIC25', 'rdm3', 'rdm5a'];
65   shouldShowLegacyRegion: boolean;
66   instanceFormGroup: FormGroup = null;
67   inputType = InputType;
68   isNotUniqueInstanceName : boolean = false;
69   isNotUniqueVolumeGroupName : boolean = false;
70
71   constructor(private _aaiService: AaiService, private store: NgRedux<AppState>,
72               private _vnfInstanceDetailsService : VnfInstanceDetailsService) {
73     this.store.subscribe(() => {
74       this.updateFormData()
75     });
76   }
77
78   ngOnInit() {
79     this.updateFormGroup();
80     this.subscribeToFormChanges();
81     this._aaiService.getCategoryParameters(null).subscribe();
82     this._aaiService.getLcpRegionsAndTenants(this.serviceInstance.globalSubscriberId, this.serviceInstance.subscriptionServiceType).subscribe();
83     this.updateLegacyRegionVisibility();
84     this.store.dispatch(loadProductFamiliesAction());
85   }
86
87   isInputShouldBeShown(inputType: any) {
88     let vnfInputs = [InputType.LCP_REGION, InputType.LOB, InputType.TENANT, InputType.PRODUCT_FAMILY, InputType.PLATFORM, InputType.ROLLBACK];
89     let vfInputs = [InputType.VG];
90     let exist = false;
91     if (this._modelType === 'VF') {
92       exist = vnfInputs.indexOf(inputType) > -1;
93     }
94     else {
95       exist = vfInputs.indexOf(inputType) > -1;
96     }
97     return exist;
98   }
99
100   updateFormGroupControlsFromVNFModel() {
101     if (this._vnfModel && this._modelType) {
102       if (this._modelType === ServiceNodeTypes.VF) {
103         const vnfInstance = <VnfInstance>this.vnfInstance;
104         if (this.instanceFormGroup && this.userProvidedNaming
105           && !this.instanceFormGroup.get('instanceName')) {
106           const initialInstanceName = vnfInstance.instanceName || (!isNullOrUndefined(this._vnfModel.name) ? this._vnfModel.name.replace(/[-]/g, "") : this._vnfModel.name);
107           this.instanceFormGroup.addControl('instanceName', new FormControl(initialInstanceName, Validators.compose([Validators.required, NumbersLettersUnderscoreValidator.valid])))
108         }
109       }
110       else if (this._modelType === ServiceNodeTypes.VFmodule) {
111         const vfInstance = <VfModuleInstance>this.vnfInstance;
112         if (this.instanceFormGroup && this.userProvidedNaming && !this.instanceFormGroup.get('instanceName')) {
113           this.instanceFormGroup.addControl('instanceName', new FormControl(vfInstance.instanceName, Validators.required));
114
115           let vfModule = this.extractVfAccordingToVfModuleUuid(this.store.getState(), this._vnfModel.uuid);
116           if (vfModule.volumeGroupAllowed && !this.instanceFormGroup.get('volumeGroupName')) {
117             this.instanceFormGroup.addControl('volumeGroupName', new FormControl(vfInstance.volumeGroupName));
118           }
119         }
120       }
121     }
122   }
123
124   updateFormGroup() {
125     const tenantDisabled = !this.vnfInstance.lcpCloudRegionId;
126
127     if (this._modelType === ServiceNodeTypes.VF) {
128       const vnfInstance = <VnfInstance>this.vnfInstance;
129       this.instanceFormGroup = new FormGroup({
130         productFamilyId: new FormControl(vnfInstance.productFamilyId),
131         lcpCloudRegionId: new FormControl(vnfInstance.lcpCloudRegionId, Validators.required),
132         tenantId: new FormControl({value: vnfInstance.tenantId, disabled: tenantDisabled}, Validators.required),
133         legacyRegion: new FormControl(vnfInstance.legacyRegion),
134         lineOfBusiness: new FormControl(vnfInstance.lineOfBusiness),
135         platformName: new FormControl(vnfInstance.platformName, Validators.required),
136       });
137     }
138     else if (this._modelType === ServiceNodeTypes.VFmodule) {
139       const vfInstance = <VfModuleInstance>this.vnfInstance;
140       this.instanceFormGroup = new FormGroup({
141       });
142     }
143
144     this.instanceFormGroup.valueChanges.subscribe(()=> {
145       this.checkForUniqueInstanceName();
146       this.onDataChanged.next();
147     });
148
149     this.updateFormGroupControlsFromVNFModel();
150   }
151
152   private getParentVnfModel(): VNFModel {
153     const rawModel = _.get(this.store.getState().service.serviceHierarchy[this.serviceUuid], ['vnfs', this.parentModelName]);
154     return new VNFModel(rawModel);
155   }
156
157   extractVfAccordingToVfModuleUuid(state : any,vfModuleUuid : string) {
158     const vnfs = this.store.getState().service.serviceHierarchy[this.serviceUuid].vnfs;
159     const vnfsArray = Object.values(vnfs);
160     for (let i = 0; i<vnfsArray.length;i++){
161       let vfModules = Object.values(vnfsArray[i].vfModules);
162       for (let j = 0; j<vfModules.length;j++){
163         if (vfModules[j].uuid === vfModuleUuid){
164           return vfModules[j];
165         }
166       }
167     }
168   }
169
170   updateFormData() {
171     let service = this.store.getState().service;
172     this.vnfPopupDataModel.lcpRegions = service.lcpRegionsAndTenants.lcpRegionList;
173     if (this.vnfInstance && this.vnfInstance.lcpCloudRegionId) {
174       this.vnfPopupDataModel.tenants = service.lcpRegionsAndTenants.lcpRegionsTenantsMap[this.vnfInstance.lcpCloudRegionId];
175       console.log('setting vnf instances tenant: ' + JSON.stringify(this.vnfPopupDataModel.tenants));
176     }
177     this.vnfPopupDataModel.platforms = service.categoryParameters.platformList;
178     this.vnfPopupDataModel.lineOfBusinesses = service.categoryParameters.lineOfBusinessList;
179     this.onDataChanged.next();
180   }
181
182   subscribeToFormChanges(): void {
183     if (this.instanceFormGroup.get('lcpCloudRegionId') !== null) {
184       this.instanceFormGroup.get('lcpCloudRegionId').valueChanges.subscribe(val => {
185         this.setDisabledState(val, 'tenantId');
186         this.updateTenantList(val);
187         this.updateLegacyRegionVisibility();
188         this.onDataChanged.next();
189       });
190     }
191   }
192
193   setDisabledState(val, field: string): void {
194     if (val) {
195       this.instanceFormGroup.controls[field].enable();
196     }
197   }
198
199   updateLegacyRegionVisibility() {
200     if (this.instanceFormGroup.get('lcpCloudRegionId') !== null) {
201       this.shouldShowLegacyRegion = this.lcpRegionsThatEnableLegacyRegionField.indexOf(this.instanceFormGroup.get('lcpCloudRegionId').value) > -1;
202       if (!this.shouldShowLegacyRegion) {
203         this.instanceFormGroup.controls.legacyRegion.setValue(undefined);
204       }
205     }
206   }
207
208   updateTenantList(cloudRegionId) {
209     this.resetTenantSelection();
210     const tenantsForCloudRegionId = this.store.getState().service.lcpRegionsAndTenants.lcpRegionsTenantsMap[cloudRegionId];
211     console.log('tenants for selected cloud region id: ' + JSON.stringify(tenantsForCloudRegionId));
212     this.vnfPopupDataModel.tenants = tenantsForCloudRegionId;
213   }
214
215   resetTenantSelection() {
216     this.instanceFormGroup.controls.tenantId.setValue(undefined);
217   }
218
219   checkForUniqueInstanceName() {
220     let currentName = !isNullOrUndefined(this.instanceFormGroup.get('instanceName')) ? this.instanceFormGroup.get('instanceName').value : null;
221
222     if(currentName && !this._vnfInstanceDetailsService.isUnique(this.store.getState().service.serviceInstance, this.serviceUuid, currentName, currentName === this.serviceInstance.instanceName) && this.userProvidedNaming){
223       this.isNotUniqueInstanceName = true;
224       this.onServiceInstanceNameChanged.emit(true);
225     }else {
226       this.isNotUniqueInstanceName = false;
227       this.onServiceInstanceNameChanged.emit(false);
228     }
229   }
230
231   checkForUniqueGroupName(){
232     let currentName = this.instanceFormGroup.get('volumeGroupName').value;
233     if( !this._vnfInstanceDetailsService.isUnique(this.store.getState().service.serviceInstance, this.serviceUuid, currentName, currentName === this.serviceInstance['volumeGroupName'])){
234       this.isNotUniqueVolumeGroupName = true;
235       this.onVolumeGroupNameChanged.emit(true);
236     }else {
237       this.isNotUniqueVolumeGroupName = false;
238       this.onVolumeGroupNameChanged.emit(false);
239     }
240   }
241
242   onSubmit(formValues): void {
243     formValues.modelInfo = new ModelInfo(this._vnfModel);
244     if (this._modelType === 'VFmodule') {
245       let dynamicFields: { [dynamicField: string]: string; };
246       dynamicFields = {};
247       if(!_.isEmpty(this.dynamicInputs)) {
248         this.dynamicInputs.map(function (x) {
249           let dynamicField: string = x.id;
250           dynamicFields[dynamicField] = formValues[dynamicField];
251           delete formValues[dynamicField];
252         });
253       }
254       formValues.instanceParams = [];
255       formValues.instanceParams.push(dynamicFields);
256       if(this.isNewVfModule){
257         this.store.dispatch(createVFModuleInstance(formValues, this.modelName, this.serviceUuid));
258       }else {
259         this.store.dispatch(updateVFModuleInstance(formValues, this.modelName, this.serviceUuid));
260       }
261
262     }
263     else {
264       formValues.isUserProvidedNaming = this.userProvidedNaming;
265       this.store.dispatch(updateVNFInstance(formValues, this.modelName, this.serviceUuid));
266     }
267     window.parent.postMessage({
268       eventId: 'submitIframe',
269       data: {
270         serviceModelId: this.serviceUuid
271       }
272     }, "*");
273     this.onSubmitClick.emit(this.serviceUuid);
274   }
275 }