Feature 11038: Enhancement of Vertical Scale Feature and merge in update API
[osm/NG-UI.git] / src / app / utilities / ns-update / NsUpdateComponent.ts
index 80773e2..d5eb443 100644 (file)
@@ -28,10 +28,10 @@ import { APIURLHEADER, CONFIGCONSTANT, ERRORDATA, MODALCLOSERESPONSEDATA, URLPAR
 import { environment } from 'environment';
 import { NSUPDATE, TERMINATEVNF } from 'NSInstanceModel';
 import { RestService } from 'RestService';
+import { isNullOrUndefined } from 'SharedService';
 import { SharedService } from 'SharedService';
-import { isNullOrUndefined } from 'util';
 import { VNFD } from 'VNFDModel';
-import { VNFInstanceDetails } from 'VNFInstanceModel';
+import { InstanceData, VDUDETAIL, VDUR, VNFInstanceDetails } from 'VNFInstanceModel';
 import { WarningComponent } from 'WarningComponent';
 
 /**
@@ -66,6 +66,8 @@ export class NsUpdateComponent implements OnInit {
     public memberIndexValue: string;
     /** Form Check vnfdId Section @public */
     public vnfdIdShow: boolean = false;
+    /** Form Check vduId Section @public */
+    public vduIdShow: boolean = false;
     /** Contains MemberVNFIndex values @public */
     public memberVnfIndex: {}[] = [];
     /** Contains MemberVNFIndex content @public */
@@ -84,6 +86,10 @@ export class NsUpdateComponent implements OnInit {
     public instanceId: string;
     /** Selected VNFInstanceId @public */
     public selectedvnfId: string = '';
+    /** Items for vduId & countIndex @public */
+    public vdu: {}[];
+    /** Contains vduId @public */
+    public vduId: {};
     /** Input contains component objects @private */
     @Input() private params: URLPARAMS;
     /** FormBuilder instance added to the formBuilder @private */
@@ -117,6 +123,10 @@ export class NsUpdateComponent implements OnInit {
             {
                 title: this.translateService.instant('REMOVEVNF'),
                 value: 'REMOVE_VNF'
+            },
+            {
+                title: this.translateService.instant('VERTICALSCALE'),
+                value: 'VERTICAL_SCALE'
             }
         ];
     }
@@ -140,33 +150,43 @@ export class NsUpdateComponent implements OnInit {
         this.nsUpdateForm = this.formBuilder.group({
             updateType: [null, [Validators.required]],
             memberVnfIndex: [null, [Validators.required]],
-            vnfdId: [null, [Validators.required]]
+            vnfdId: [null, [Validators.required]],
+            vduId: [null, [Validators.required]],
+            countIndex: [null, [Validators.required]]
         });
     }
 
     /** Getting MemberVnfIndex using NSDescriptor API @public */
     public getMemberVnfIndex(): void {
+        this.isLoadingResults = true;
         const vnfInstanceData: {}[] = [];
         this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => {
             vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => {
+                const vnfdRef: string = 'vnfd-ref';
+                const memberIndex: string = 'member-vnf-index-ref';
+                const nsrId: string = 'nsr-id-ref';
+                const vnfId: string = 'vnfd-id';
                 const vnfDataObj: {} =
                 {
-                    VNFD: vnfData['vnfd-ref'],
+                    // eslint-disable-next-line security/detect-object-injection
+                    VNFD: vnfData[vnfdRef],
                     VNFInstanceId: vnfData._id,
-                    MemberIndex: vnfData['member-vnf-index-ref'],
-                    NS: vnfData['nsr-id-ref'],
-                    VNFID: vnfData['vnfd-id']
+                    // eslint-disable-next-line security/detect-object-injection
+                    MemberIndex: vnfData[memberIndex],
+                    // eslint-disable-next-line security/detect-object-injection
+                    NS: vnfData[nsrId],
+                    // eslint-disable-next-line security/detect-object-injection
+                    VNFID: vnfData[vnfId]
                 };
                 vnfInstanceData.push(vnfDataObj);
             });
             const nsId: string = 'NS';
+            // eslint-disable-next-line security/detect-object-injection
             this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
-            this.nsIdFilteredData.forEach((resVNF: {}[]): void => {
-                const memberIndex: string = 'MemberIndex';
-                const vnfinstanceID: string = 'VNFInstanceId';
+            this.nsIdFilteredData.forEach((resVNF: InstanceData): void => {
                 const assignMemberIndex: {} = {
-                    id: resVNF[memberIndex],
-                    vnfinstanceId: resVNF[vnfinstanceID]
+                    id: resVNF.MemberIndex,
+                    vnfinstanceId: resVNF.VNFInstanceId
                 };
                 this.memberVnfIndex.push(assignMemberIndex);
             });
@@ -184,26 +204,69 @@ export class NsUpdateComponent implements OnInit {
      */
     public getSelectedVNF(id: string): void {
         this.instanceId = id;
+        this.getVdu(this.instanceId);
         let memberIndexFilteredData: {}[] = [];
         const memberIndex: string = 'MemberIndex';
         memberIndexFilteredData = this.nsIdFilteredData.filter((vnfdData: {}[]): boolean =>
+            // eslint-disable-next-line security/detect-object-injection
             vnfdData[memberIndex] === this.memberIndexValue);
         const vnfId: string = 'VNFID';
         const selectedvnfId: string = 'VNFD';
         this.selectedVnf = memberIndexFilteredData;
         for (const data of memberIndexFilteredData) {
+            // eslint-disable-next-line security/detect-object-injection
             this.vnfID = data[vnfId];
+            // eslint-disable-next-line security/detect-object-injection
             this.selectedvnfId = data[selectedvnfId];
         }
     }
 
+    /** Getting vdu-id & count-index from API */
+    public getVdu(id: string): void {
+        const vnfInstanceData: {}[] = [];
+        this.getFormControl('vduId').setValue(null);
+        this.getFormControl('countIndex').setValue(null);
+        if (!isNullOrUndefined(id)) {
+            this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id).
+                subscribe((vnfInstanceDetail: VNFInstanceDetails): void => {
+                    this.instanceId = id;
+                    this.selectedvnfId = vnfInstanceDetail['vnfd-ref'];
+                    if (!isNullOrUndefined(vnfInstanceDetail.vdur)) {
+                        vnfInstanceDetail.vdur.forEach((vdu: VDUR): void => {
+                            const vnfInstanceDataObj: {} =
+                            {
+                                'count-index': vdu['count-index'],
+                                VDU: vdu['vdu-id-ref']
+
+                            };
+                            vnfInstanceData.push(vnfInstanceDataObj);
+                        });
+                        this.vdu = vnfInstanceData;
+                        this.vduId = this.vdu.filter((vdu: VDUDETAIL, index: number, self: {}[]): {} =>
+                            index === self.findIndex((t: VDUDETAIL): {} => (
+                                t.VDU === vdu.VDU
+                            ))
+                        );
+                    }
+                }, (error: ERRORDATA): void => {
+                    this.restService.handleError(error, 'get');
+                    this.isLoadingResults = false;
+                });
+        }
+    }
+
     /** Trigger NsUpdate on submit */
     public triggerNsUpdate(): void {
         this.submitted = true;
         this.sharedService.cleanForm(this.nsUpdateForm);
         if (this.nsUpdateForm.invalid) { return; } // Proceed, onces form is valid
         this.vnfdId = this.nsUpdateForm.value.vnfdId;
-        this.checkUpdateType();
+        if (this.terminateVnf === 'VERTICAL_SCALE') {
+            this.getFormControl('countIndex').enable();
+            this.onSubmit();
+        } else {
+            this.checkUpdateType();
+        }
     }
 
     /** Ns Update on submit */
@@ -216,7 +279,7 @@ export class NsUpdateComponent implements OnInit {
                 removeVnfInstanceId: this.instanceId
             };
             this.nsUpdateInitialization(nsUpdateTerminatePayload);
-        } else {
+        } else if (this.terminateVnf === 'CHANGE_VNFPKG') {
             const nsUpdatePayload: NSUPDATE = {
                 lcmOperationType: 'update',
                 updateType: this.nsUpdateForm.value.updateType,
@@ -227,6 +290,19 @@ export class NsUpdateComponent implements OnInit {
                 }
             };
             this.nsUpdateInitialization(nsUpdatePayload);
+        } else if (this.terminateVnf === 'VERTICAL_SCALE') {
+            const nsUpdatePayload: NSUPDATE = {
+                lcmOperationType: 'update',
+                updateType: this.nsUpdateForm.value.updateType,
+                nsInstanceId: this.params.id,
+                verticalScaleVnf: {
+                    vnfInstanceId: this.instanceId,
+                    vnfdId: this.nsUpdateForm.value.vnfdId,
+                    countIndex: 0,
+                    vduId: this.nsUpdateForm.value.vduId
+                }
+            };
+            this.nsUpdateInitialization(nsUpdatePayload);
         }
     }
 
@@ -237,7 +313,8 @@ export class NsUpdateComponent implements OnInit {
         this.isLoadingResults = true;
         if (this.nsUpdateForm.value.updateType === 'CHANGE_VNFPKG') {
             this.checkVersion();
-        } else {
+        } else if (this.nsUpdateForm.value.updateType === 'REMOVE_VNF') {
+            // eslint-disable-next-line security/detect-non-literal-fs-filename
             const modalRef: NgbModalRef = this.modalService.open(WarningComponent, { backdrop: 'static' });
             modalRef.componentInstance.heading = this.translateService.instant('TERMINATEVNF');
             modalRef.componentInstance.confirmationMessage = this.translateService.instant('TERMINATEVNFCONTENT');
@@ -246,8 +323,9 @@ export class NsUpdateComponent implements OnInit {
                 if (result.message === CONFIGCONSTANT.done) {
                     this.onSubmit();
                 }
-            }).catch((): void => { //empty
-             });
+            }).catch((): void => {
+                // Catch Navigation Error
+            });
         }
         this.isLoadingResults = false;
     }
@@ -272,11 +350,14 @@ export class NsUpdateComponent implements OnInit {
                 let vnfIdFilteredData: {}[] = [];
                 const vnfID: string = 'VNFID';
                 const version: string = 'version';
+                // eslint-disable-next-line security/detect-object-injection
                 vnfIdFilteredData = vnfDetails.filter((vnfdData: {}[]): boolean => vnfdData[vnfID] === this.vnfID);
                 for (const data of vnfIdFilteredData) {
+                    // eslint-disable-next-line security/detect-object-injection
                     this.vnfversion = data[version];
                 }
                 if (this.version === this.vnfversion) {
+                    // eslint-disable-next-line security/detect-non-literal-fs-filename
                     const modalRef: NgbModalRef = this.modalService.open(WarningComponent, { backdrop: 'static' });
                     modalRef.componentInstance.heading = this.translateService.instant('UPDATENS');
                     modalRef.componentInstance.confirmationMessage = this.translateService.instant('GENERICCONTENT');
@@ -286,9 +367,10 @@ export class NsUpdateComponent implements OnInit {
                             this.onSubmit();
                         }
                     }).catch((): void => { //empty
-                     }
+                    }
                     );
                 } else {
+                    // eslint-disable-next-line security/detect-non-literal-fs-filename
                     const modalRef: NgbModalRef = this.modalService.open(WarningComponent, { backdrop: 'static' });
                     modalRef.componentInstance.heading = this.translateService.instant('REDEPLOY');
                     modalRef.componentInstance.confirmationMessage = this.translateService.instant('REDEPLOYCONTENT');
@@ -298,7 +380,7 @@ export class NsUpdateComponent implements OnInit {
                             this.onSubmit();
                         }
                     }).catch((): void => { //empty
-                     });
+                    });
                 }
             }, (error: ERRORDATA): void => {
                 this.restService.handleError(error, 'get');
@@ -323,7 +405,9 @@ export class NsUpdateComponent implements OnInit {
         };
         this.restService.postResource(apiURLHeader, nsUpdatePayload).subscribe((result: {}): void => {
             this.activeModal.close(modalData);
-            this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch();
+            this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => {
+                // Catch Navigation Error
+            });
         }, (error: ERRORDATA): void => {
             this.restService.handleError(error, 'post');
             this.isLoadingResults = false;
@@ -335,15 +419,33 @@ export class NsUpdateComponent implements OnInit {
         this.terminateVnf = value;
         if (this.terminateVnf === 'REMOVE_VNF') {
             this.vnfdIdShow = true;
+            this.vduIdShow = false;
             this.getFormControl('vnfdId').disable();
-        } else {
+            this.getFormControl('vduId').disable();
+            this.getFormControl('countIndex').disable();
+        } else if (this.terminateVnf === 'CHANGE_VNFPKG') {
             this.vnfdIdShow = false;
+            this.vduIdShow = false;
             this.getFormControl('vnfdId').enable();
+            this.getFormControl('vduId').disable();
+            this.getFormControl('countIndex').disable();
+        } else if (this.terminateVnf === 'VERTICAL_SCALE') {
+            this.vnfdIdShow = false;
+            this.vduIdShow = true;
+            this.getFormControl('vnfdId').enable();
+            this.getFormControl('vduId').enable();
+            this.getFormControl('countIndex').disable();
         }
     }
 
+    /** Getting count-index by filtering id  */
+    public getCountIndex(): void {
+        this.getFormControl('countIndex').patchValue('0');
+    }
+
     /** Used to get the AbstractControl of controlName passed @private */
     private getFormControl(controlName: string): AbstractControl {
+        // eslint-disable-next-line security/detect-object-injection
         return this.nsUpdateForm.controls[controlName];
     }
 }