Executive primitive bug for lcm-operations-conf
[osm/NG-UI.git] / src / app / instances / ns-primitive / NSPrimitiveComponent.ts
index 1086f49..c8b7fed 100644 (file)
@@ -23,14 +23,15 @@ import { AbstractControl, FormArray, FormBuilder, FormGroup, Validators } from '
 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
 import { TranslateService } from '@ngx-translate/core';
 import { NotifierService } from 'angular-notifier';
-import { APIURLHEADER, ERRORDATA, URLPARAMS } from 'CommonModel';
+import { APIURLHEADER, ERRORDATA, PRIMITIVEDATA, PRIMITIVETYPES, URLPARAMS } from 'CommonModel';
 import { DataService } from 'DataService';
 import { environment } from 'environment';
-import { NSData, VDUPRIMITIVELEVEL } from 'NSDModel';
+import { KDUPRIMITIVELEVEL, NSData, VDUPRIMITIVELEVEL, VNFPROFILE } from 'NSDModel';
 import { NSPrimitiveParams } from 'NSInstanceModel';
 import { RestService } from 'RestService';
 import { SharedService } from 'SharedService';
 import { isNullOrUndefined } from 'util';
+import { CONFIGPRIMITIVE, DF, VDUCONFIG, VDUPROFILE, VNFCONFIG, VNFD } from 'VNFDModel';
 
 /**
  * Creating component
@@ -76,13 +77,16 @@ export class NSPrimitiveComponent implements OnInit {
     public primitiveList: {}[];
 
     /** Contains objects that is used to hold types of primitive @public */
-    public primitiveTypeList: {}[] = [];
+    public primitiveTypeList: PRIMITIVETYPES[] = [];
 
     /** Model value used to hold selected primitive type @public */
     public primitiveType: string;
 
     /** Contains list of VDU primitive lists @public */
-    public vduList: {}[];
+    public vduList: VDUPROFILE[];
+
+    /** Contains list of KDU primitive lists @public */
+    public kduList: {}[];
 
     /** FormBuilder instance added to the formBuilder @private */
     private formBuilder: FormBuilder;
@@ -126,16 +130,23 @@ export class NSPrimitiveComponent implements OnInit {
             {
                 title: this.translateService.instant('VDUPRIMITIVE'),
                 value: 'VDU_Primitive'
+            },
+            {
+                title: this.translateService.instant('KDUPRIMITIVE'),
+                value: 'KDU_Primitive'
             }
         ];
     }
 
+    /** convenience getter for easy access to form fields */
+    get f(): FormGroup['controls'] { return this.primitiveForm.controls; }
+
     /**
      * Lifecyle Hooks the trigger before component is instantiate
      */
     public ngOnInit(): void {
         /** Setting up initial value for NSD */
-        this.dataService.currentMessage.subscribe((event: NSData) => {
+        this.dataService.currentMessage.subscribe((event: NSData): void => {
             if (event.identifier !== undefined || event.identifier !== '' || event.identifier !== null) {
                 this.nsdId = event.identifier;
             }
@@ -143,15 +154,13 @@ export class NSPrimitiveComponent implements OnInit {
         this.initializeForm();
     }
 
-    /** convenience getter for easy access to form fields */
-    get f(): FormGroup['controls'] { return this.primitiveForm.controls; }
-
     /** initialize Forms @public */
     public initializeForm(): void {
         this.primitiveForm = this.formBuilder.group({
             primitive: [null, [Validators.required]],
-            vnf_member_index: [null, [Validators.required]],
+            member_vnf_index: [null, [Validators.required]],
             vdu_id: [null, [Validators.required]],
+            kdu_name: [null, [Validators.required]],
             primitive_params: this.formBuilder.array([this.primitiveParamsBuilder()])
         });
     }
@@ -180,13 +189,13 @@ export class NSPrimitiveComponent implements OnInit {
         this.primitiveParams.removeAt(index);
     }
 
-    /** Execute NS Primitive @public */
-    public execNSPrimitive(): void {
+    /** Execute Primitive @public */
+    public execPrimitive(): void {
         this.submitted = true;
         this.objectPrimitiveParams = {};
         this.sharedService.cleanForm(this.primitiveForm);
         if (this.primitiveForm.invalid) { return; } // Proceed, onces form is valid
-        this.primitiveForm.value.primitive_params.forEach((params: NSPrimitiveParams) => {
+        this.primitiveForm.value.primitive_params.forEach((params: NSPrimitiveParams): void => {
             if (params.primitive_params_name !== null && params.primitive_params_value !== '') {
                 this.objectPrimitiveParams[params.primitive_params_name] = params.primitive_params_value;
             }
@@ -198,60 +207,67 @@ export class NSPrimitiveComponent implements OnInit {
         };
         if (this.primitiveType === 'VNF_Primitive') {
             // tslint:disable-next-line: no-string-literal
-            primitiveParamsPayLoads['vnf_member_index'] = this.primitiveForm.value.vnf_member_index;
+            primitiveParamsPayLoads['member_vnf_index'] = this.primitiveForm.value.member_vnf_index;
         }
         if (this.primitiveType === 'VDU_Primitive') {
             // tslint:disable-next-line: no-string-literal
-            primitiveParamsPayLoads['vnf_member_index'] = this.primitiveForm.value.vnf_member_index;
+            primitiveParamsPayLoads['member_vnf_index'] = this.primitiveForm.value.member_vnf_index;
             // tslint:disable-next-line: no-string-literal
             primitiveParamsPayLoads['vdu_id'] = this.primitiveForm.value.vdu_id;
         }
+        if (this.primitiveType === 'KDU_Primitive') {
+            // tslint:disable-next-line: no-string-literal
+            primitiveParamsPayLoads['member_vnf_index'] = this.primitiveForm.value.member_vnf_index;
+            // tslint:disable-next-line: no-string-literal
+            primitiveParamsPayLoads['kdu_name'] = this.primitiveForm.value.kdu_name;
+        }
         const apiURLHeader: APIURLHEADER = {
             url: environment.NSDINSTANCES_URL + '/' + this.nsdId + '/action'
         };
         this.isLoadingResults = true;
-        this.restService.postResource(apiURLHeader, primitiveParamsPayLoads).subscribe((result: {}) => {
+        this.restService.postResource(apiURLHeader, primitiveParamsPayLoads).subscribe((result: {}): void => {
             this.activeModal.dismiss();
             this.notifierService.notify('success', this.translateService.instant('PAGE.NSPRIMITIVE.EXECUTEDSUCCESSFULLY'));
             this.isLoadingResults = false;
-        }, (error: ERRORDATA) => {
+        }, (error: ERRORDATA): void => {
             this.isLoadingResults = false;
             this.restService.handleError(error, 'post');
         });
     }
     /** Primitive type change event @public */
-    public primitiveTypeChange(data: { value: string }): void {
+    public primitiveTypeChange(data: PRIMITIVETYPES): void {
         this.primitiveList = [];
         this.primitiveParameter = [];
         this.initializeForm();
         if (data.value === 'NS_Primitive') {
             this.getNSInfo(this.params.name);
-            this.getFormControl('vnf_member_index').setValidators([]);
-            this.getFormControl('vnf_member_index').updateValueAndValidity();
-            this.getFormControl('vdu_id').setValidators([]);
-            this.getFormControl('vdu_id').updateValueAndValidity();
-        } else if (data.value === 'VNF_Primitive') {
-            this.getFormControl('vdu_id').setValidators([]);
-            this.getFormControl('vdu_id').updateValueAndValidity();
+            this.setUpdateValueandValidation('member_vnf_index');
+        }
+        if (data.value === 'VNF_Primitive' || data.value === 'KDU_Primitive' || data.value === 'NS_Primitive') {
+            this.setUpdateValueandValidation('vdu_id');
+        }
+        if (data.value === 'VDU_Primitive' || data.value === 'VNF_Primitive' || data.value === 'NS_Primitive') {
+            this.setUpdateValueandValidation('kdu_name');
         }
     }
     /** Member index change event */
-    public indexChange(data: {}, getType?: string): void {
+    public indexChange(data: VNFPROFILE, getType?: string): void {
         this.getFormControl('vdu_id').setValue(null);
-        if (data) {
-            this.getVnfdInfo(data['vnfd-id-ref'], getType);
+        this.getFormControl('kdu_name').setValue(null);
+        if (data['vnfd-id'] !== null) {
+            this.getVnfdInfo(data['vnfd-id'], getType);
         } else {
             this.primitiveList = [];
             this.getFormControl('primitive').setValue(null);
             this.primitiveParameter = [];
         }
     }
-    /** Get VDU Primitive from selected VDU id/name change event */
-    public getVDUPrimitive(data: {}): void {
-        this.primitiveList = data['vdu-configuration']['config-primitive'];
+    /** Get VDU/KDU primitive List for the selected event @public */
+    public getPrimitiveList(data: {}, selectedType: string): void {
+        this.primitiveList = data[selectedType + '-configuration']['config-primitive'];
     }
     /** Primivtive change event */
-    public primitiveChange(data: { parameter: {}[] }): void {
+    public primitiveChange(data: PRIMITIVEDATA): void {
         this.primitiveParameter = [];
         const formArr: FormArray = this.getFormControl('primitive_params') as FormArray;
         formArr.controls = [];
@@ -261,21 +277,34 @@ export class NSPrimitiveComponent implements OnInit {
         }
     }
     /** Generate vdu section @public */
-    public generateVDUData(vduData: VDUPRIMITIVELEVEL): VDUPRIMITIVELEVEL {
+    public generateVDUData(vduConfig: VDUCONFIG): VDUPROFILE {
         return {
-            id: vduData.id,
-            name: vduData.name,
-            'vdu-configuration': vduData['vdu-configuration']
+            id: vduConfig.id,
+            name: vduConfig.id,
+            'vdu-configuration': vduConfig
         };
     }
+    /** Generate kdu section @public */
+    public generateKDUData(kduData: KDUPRIMITIVELEVEL, kduConfig: VDUCONFIG): KDUPRIMITIVELEVEL {
+        return {
+            name: kduData.name,
+            'juju-bundle': kduData['juju-bundle'],
+            'kdu-configuration': kduConfig
+        };
+    }
+    /** Used to set the validation and value and update the validation and value @public */
+    public setUpdateValueandValidation(formName: string): void {
+        this.getFormControl(formName).setValidators([]);
+        this.getFormControl(formName).updateValueAndValidity();
+    }
     /** Update primitive value based on parameter */
-    private updatePrimitive(primitive: { parameter: {}[] }): void {
+    private updatePrimitive(primitive: PRIMITIVEDATA): void {
         if (primitive.parameter) {
             this.primitiveParameter = primitive.parameter;
         } else {
             this.primitiveParameter = [];
             const formArr: AbstractControl[] = this.getControls();
-            formArr.forEach((formGp: FormGroup) => {
+            formArr.forEach((formGp: FormGroup): void => {
                 formGp.controls.primitive_params_name.setValidators([]);
                 formGp.controls.primitive_params_name.updateValueAndValidity();
                 formGp.controls.primitive_params_value.setValidators([]);
@@ -288,26 +317,60 @@ export class NSPrimitiveComponent implements OnInit {
         this.primitiveList = [];
         this.primitiveParameter = [];
         this.getFormControl('primitive').setValue(null);
-        const apiUrl: string = environment.VNFPACKAGES_URL + '?short-name=' + vnfdRef;
+        const apiUrl: string = environment.VNFPACKAGES_URL + '?id=' + vnfdRef;
         this.isLoadingResults = true;
-        this.restService.getResource(apiUrl)
-            .subscribe((vnfdInfo: {}) => {
-                if (vnfdInfo[0]['vnf-configuration'] !== undefined && vnfdInfo[0]['vnf-configuration']) {
+        this.restService.getResource(apiUrl).subscribe((vnfdInfo: VNFD[]): void => {
+                const vnfInstances: VNFD = vnfdInfo[0];
+                if (!isNullOrUndefined(vnfInstances.df)) {
                     this.getFormControl('vdu_id').setValidators([]);
-                    this.primitiveList = vnfdInfo[0]['vnf-configuration']['config-primitive'];
-                }
-                if (getType === 'VDU_Primitive') {
-                    this.vduList = [];
-                    this.primitiveList = [];
-                    vnfdInfo[0].vdu.forEach((vduData: VDUPRIMITIVELEVEL) => {
-                        if (vduData['vdu-configuration']) {
-                            const vduDataObj: VDUPRIMITIVELEVEL = this.generateVDUData(vduData);
-                            this.vduList.push(vduDataObj);
+                    this.getFormControl('kdu_name').setValidators([]);
+                    vnfInstances.df.forEach((df: DF): void => {
+                        if (df['lcm-operations-configuration'] !== undefined) {
+                            if (df['lcm-operations-configuration']['operate-vnf-op-config'] !== undefined) {
+                                const day12Operation: VDUCONFIG[] = df['lcm-operations-configuration']['operate-vnf-op-config']['day1-2'];
+                                if (day12Operation !== undefined) {
+                                    const vnfprimitiveList: VNFCONFIG = day12Operation
+                                        .filter((itemData: VNFCONFIG): boolean => itemData.id === vnfInstances.id)[0];
+                                    if (vnfprimitiveList !== undefined) {
+                                        this.primitiveList = vnfprimitiveList['config-primitive'];
+                                    }
+                                    /** VDU Primitive */
+                                    if (getType === 'VDU_Primitive') {
+                                        this.kduList = [];
+                                        this.vduList = [];
+                                        this.primitiveList = [];
+                                        df['vdu-profile'].forEach((vduProfile: VDUPROFILE): void => {
+                                            day12Operation.forEach((element: VDUCONFIG): void => {
+                                                if (element.id === vduProfile.id){
+                                                    const vduDataObj: VDUPROFILE = this.generateVDUData(element);
+                                                    this.vduList.push(vduDataObj);
+                                                }
+                                            });
+                                        });
+                                    }
+                                    /** KDU Primitive */
+                                    if (getType === 'KDU_Primitive') {
+                                        this.kduList = [];
+                                        this.vduList = [];
+                                        this.primitiveList = [];
+                                        if (!isNullOrUndefined(vnfInstances.kdu)) {
+                                            vnfInstances.kdu.forEach((kduData: KDUPRIMITIVELEVEL): void => {
+                                                day12Operation.forEach((element: VDUCONFIG): void => {
+                                                    if (element.id === kduData.name){
+                                                        const kduDataObj: KDUPRIMITIVELEVEL = this.generateKDUData(kduData, element);
+                                                        this.kduList.push(kduDataObj);
+                                                    }
+                                                });
+                                            });
+                                        }
+                                    }
+                                }
+                            }
                         }
                     });
                 }
                 this.isLoadingResults = false;
-            }, (error: ERRORDATA) => {
+            }, (error: ERRORDATA): void => {
                 this.isLoadingResults = false;
                 this.restService.handleError(error, 'get');
             });
@@ -317,18 +380,18 @@ export class NSPrimitiveComponent implements OnInit {
         this.primitiveList = [];
         this.primitiveParameter = [];
         this.getFormControl('primitive').setValue(null);
-        const apiUrl: string = environment.NSDESCRIPTORS_URL + '?short-name=' + nsdRef;
+        const apiUrl: string = environment.NSDESCRIPTORS_URL + '?id=' + nsdRef;
         this.isLoadingResults = true;
         this.restService.getResource(apiUrl)
-            .subscribe((nsdInfo: {}) => {
+            .subscribe((nsdInfo: {}): void => {
                 if (!isNullOrUndefined(nsdInfo[0]['ns-configuration'])) {
                     this.primitiveList = !isNullOrUndefined(nsdInfo[0]['ns-configuration']['config-primitive']) ?
-                    nsdInfo[0]['ns-configuration']['config-primitive'] : [];
+                        nsdInfo[0]['ns-configuration']['config-primitive'] : [];
                 } else {
                     this.primitiveList = [];
                 }
                 this.isLoadingResults = false;
-            }, (error: ERRORDATA) => {
+            }, (error: ERRORDATA): void => {
                 this.isLoadingResults = false;
                 this.restService.handleError(error, 'get');
             });