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";
24 selector: 'vnf-instance-details',
25 templateUrl: 'vnf-instance-details.html',
26 styleUrls: ['vnf-instance-details.scss'],
27 providers: [AaiService]
30 export class VnfInstanceDetailsComponent implements OnInit {
31 @ViewChild('vnfForm') vnfForm: 'VnfForm';
34 set vnfModel(vnfModel: VNFModel) {
35 this._vnfModel = vnfModel;
36 this.updateFormGroupControlsFromVNFModel();
38 @Input() vnfInstance: any;
39 @Input() serviceInstance: ServiceInstance;
40 @Input() dynamicInputs;
41 @Input() modelName: string;
42 @Input() serviceUuid: string;
43 @Input() userProvidedNaming: boolean;
46 set modelType(modelType: string) {
47 this._modelType = modelType;
48 this.updateFormGroupControlsFromVNFModel();
51 @Input() parentModelName: string;
52 @Input() isNewVfModule : boolean;
55 @Output() onSubmitClick: EventEmitter<any> = new EventEmitter<any>();
56 @Output() onServiceInstanceNameChanged : EventEmitter<boolean> = new EventEmitter<boolean>();
57 @Output() onVolumeGroupNameChanged : EventEmitter<boolean> = new EventEmitter<boolean>();
59 @Output() onDataChanged: EventEmitter<any> = new EventEmitter<any>();
60 @select(['service','productFamilies'])
61 readonly productFamilies : Observable<SelectOptionInterface[]>;
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;
71 constructor(private _aaiService: AaiService, private store: NgRedux<AppState>,
72 private _vnfInstanceDetailsService : VnfInstanceDetailsService) {
73 this.store.subscribe(() => {
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());
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];
91 if (this._modelType === 'VF') {
92 exist = vnfInputs.indexOf(inputType) > -1;
95 exist = vfInputs.indexOf(inputType) > -1;
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])))
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));
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));
125 const tenantDisabled = !this.vnfInstance.lcpCloudRegionId;
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),
138 else if (this._modelType === ServiceNodeTypes.VFmodule) {
139 const vfInstance = <VfModuleInstance>this.vnfInstance;
140 this.instanceFormGroup = new FormGroup({
144 this.instanceFormGroup.valueChanges.subscribe(()=> {
145 this.checkForUniqueInstanceName();
146 this.onDataChanged.next();
149 this.updateFormGroupControlsFromVNFModel();
152 private getParentVnfModel(): VNFModel {
153 const rawModel = _.get(this.store.getState().service.serviceHierarchy[this.serviceUuid], ['vnfs', this.parentModelName]);
154 return new VNFModel(rawModel);
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){
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));
177 this.vnfPopupDataModel.platforms = service.categoryParameters.platformList;
178 this.vnfPopupDataModel.lineOfBusinesses = service.categoryParameters.lineOfBusinessList;
179 this.onDataChanged.next();
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();
193 setDisabledState(val, field: string): void {
195 this.instanceFormGroup.controls[field].enable();
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);
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;
215 resetTenantSelection() {
216 this.instanceFormGroup.controls.tenantId.setValue(undefined);
219 checkForUniqueInstanceName() {
220 let currentName = !isNullOrUndefined(this.instanceFormGroup.get('instanceName')) ? this.instanceFormGroup.get('instanceName').value : null;
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);
226 this.isNotUniqueInstanceName = false;
227 this.onServiceInstanceNameChanged.emit(false);
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);
237 this.isNotUniqueVolumeGroupName = false;
238 this.onVolumeGroupNameChanged.emit(false);
242 onSubmit(formValues): void {
243 formValues.modelInfo = new ModelInfo(this._vnfModel);
244 if (this._modelType === 'VFmodule') {
245 let dynamicFields: { [dynamicField: string]: string; };
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];
254 formValues.instanceParams = [];
255 formValues.instanceParams.push(dynamicFields);
256 if(this.isNewVfModule){
257 this.store.dispatch(createVFModuleInstance(formValues, this.modelName, this.serviceUuid));
259 this.store.dispatch(updateVFModuleInstance(formValues, this.modelName, this.serviceUuid));
264 formValues.isUserProvidedNaming = this.userProvidedNaming;
265 this.store.dispatch(updateVNFInstance(formValues, this.modelName, this.serviceUuid));
267 window.parent.postMessage({
268 eventId: 'submitIframe',
270 serviceModelId: this.serviceUuid
273 this.onSubmitClick.emit(this.serviceUuid);