0ec8bc4d6693bd4c5c33d2b38d1fd7fa11755ad7
[osm/NG-UI.git] / src / app / utilities / vm-migration / VmMigrationComponent.ts
1 /*
2  Copyright 2020 TATA ELXSI
3
4  Licensed under the Apache License, Version 2.0 (the 'License');
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15
16  Author: SANDHYA JS (sandhya.j@tataelxsi.co.in)
17 */
18 /**
19  * @file Vm Migration Component
20  */
21 import { HttpHeaders } from '@angular/common/http';
22 import { Component, Injector, Input, OnInit } from '@angular/core';
23 import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { Router } from '@angular/router';
25 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
26 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
27 import { environment } from 'environment';
28 import { VMMIGRATION } from 'NSInstanceModel';
29 import { RestService } from 'RestService';
30 import { SharedService } from 'SharedService';
31 import { isNullOrUndefined } from 'util';
32 import { VDUR, VNFInstanceDetails } from 'VNFInstanceModel';
33
34 /**
35  * Creating component
36  * @Component takes VmMigrationComponent.html as template url
37  */
38 @Component({
39     selector: 'app-vm-migration',
40     templateUrl: './VmMigrationComponent.html',
41     styleUrls: ['./VmMigrationComponent.scss']
42 })
43 export class VmMigrationComponent implements OnInit {
44     /** To inject services @public */
45     public injector: Injector;
46     /** Instance for active modal service @public */
47     public activeModal: NgbActiveModal;
48     /** Check the loading results @public */
49     public isLoadingResults: Boolean = false;
50     /** Give the message for the loading @public */
51     public message: string = 'PLEASEWAIT';
52     /** FormGroup instance added to the form @ html @public */
53     public migrationForm: FormGroup;
54     /** Items for the vdu-Id and count-index @public */
55     public vdu: {}[];
56     /** Selected VNFInstanceId @public */
57     public selectedvnfId: string = '';
58     /** Array holds VNFR Data filtered with nsr ID @public */
59     public nsIdFilteredData: {}[] = [];
60     /** Items for the member types @public */
61     public memberTypes: {}[];
62     /** Contains MemberVNFIndex values @public */
63     public memberVnfIndex: {}[] = [];
64     /** Contains vnfInstanceId of the selected MemberVnfIndex  @public */
65     public instanceId: string;
66     /** Form valid on submit trigger @public */
67     public submitted: boolean = false;
68     /** Input contains component objects @private */
69     @Input() private params: URLPARAMS;
70     /** FormBuilder instance added to the formBuilder @private */
71     private formBuilder: FormBuilder;
72     /** Instance of the rest service @private */
73     private restService: RestService;
74     /** Controls the header form @private */
75     private headers: HttpHeaders;
76     /** Contains all methods related to shared @private */
77     private sharedService: SharedService;
78     /** Holds the instance of AuthService class of type AuthService @private */
79     private router: Router;
80     constructor(injector: Injector) {
81         this.injector = injector;
82         this.restService = this.injector.get(RestService);
83         this.activeModal = this.injector.get(NgbActiveModal);
84         this.formBuilder = this.injector.get(FormBuilder);
85         this.sharedService = this.injector.get(SharedService);
86         this.router = this.injector.get(Router);
87     }
88     /** convenience getter for easy access to form fields */
89     get f(): FormGroup['controls'] { return this.migrationForm.controls; }
90     /**
91      * Lifecyle Hooks the trigger before component is instantiate
92      */
93     public ngOnInit(): void {
94         this.initializeForm();
95         this.getMemberVnfIndex();
96         this.headers = new HttpHeaders({
97             'Content-Type': 'application/json',
98             Accept: 'application/json',
99             'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
100         });
101     }
102     /** Initialize Migration Forms @public */
103     public initializeForm(): void {
104         this.migrationForm = this.formBuilder.group({
105             memberVnfIndex: [null, [Validators.required]],
106             vduId: [null],
107             countIndex: [null],
108             migrateToHost: [null]
109         });
110     }
111
112     /** Getting MemberVnfIndex using NSDescriptor API @public */
113     public getMemberVnfIndex(): void {
114         const vnfInstanceData: {}[] = [];
115         this.isLoadingResults = true;
116         this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => {
117             vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => {
118                 const vnfDataObj: {} =
119                 {
120                     VNFD: vnfData['vnfd-ref'],
121                     VNFInstanceId: vnfData._id,
122                     MemberIndex: vnfData['member-vnf-index-ref'],
123                     NS: vnfData['nsr-id-ref']
124                 };
125                 vnfInstanceData.push(vnfDataObj);
126             });
127             const nsId: string = 'NS';
128             this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
129             this.nsIdFilteredData.forEach((resVNF: {}[]): void => {
130                 const memberIndex: string = 'MemberIndex';
131                 const vnfinstanceID: string = 'VNFInstanceId';
132                 const assignMemberIndex: {} = {
133                     id: resVNF[memberIndex],
134                     vnfinstanceId: resVNF[vnfinstanceID]
135                 };
136                 this.memberVnfIndex.push(assignMemberIndex);
137             });
138             this.memberTypes = this.memberVnfIndex;
139             this.isLoadingResults = false;
140         }, (error: ERRORDATA): void => {
141             this.restService.handleError(error, 'get');
142             this.isLoadingResults = false;
143         });
144     }
145
146     /** Getting vdu-id & count-index from VNFInstance API */
147     public getVdu(id: string): void {
148         this.vdu = [];
149         const vnfInstanceData: {}[] = [];
150         this.instanceId = id;
151         this.getFormControl('vduId').setValue(null);
152         this.getFormControl('countIndex').setValue(null);
153         if (!isNullOrUndefined(id)) {
154             this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id).
155                 subscribe((vnfInstanceDetail: VNFInstanceDetails[]): void => {
156                     this.selectedvnfId = vnfInstanceDetail['vnfd-ref'];
157                     const VDU: string = 'vdur';
158                     if (vnfInstanceDetail[VDU] !== undefined) {
159                         vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => {
160                             const vnfInstanceDataObj: {} =
161                             {
162                                 'count-index': vdu['count-index'],
163                                 VDU: vdu['vdu-id-ref']
164
165                             };
166                             vnfInstanceData.push(vnfInstanceDataObj);
167                         });
168                         this.vdu = vnfInstanceData;
169                     }
170                 }, (error: ERRORDATA): void => {
171                     this.restService.handleError(error, 'get');
172                     this.isLoadingResults = false;
173                 });
174         }
175     }
176
177     /** Trigger VM Migration on submit */
178     public triggerMigration(): void {
179         this.submitted = true;
180         this.sharedService.cleanForm(this.migrationForm);
181         if (this.migrationForm.invalid) { return; } // Proceed, onces form is valid
182         const migrationPayload: VMMIGRATION = {
183             lcmOperationType: 'migrate',
184             vnfInstanceId: this.instanceId
185         };
186         if (!isNullOrUndefined(this.migrationForm.value.migrateToHost)) {
187             migrationPayload.migrateToHost = this.migrationForm.value.migrateToHost;
188         }
189         if (!isNullOrUndefined(this.migrationForm.value.vduId) && !isNullOrUndefined(this.migrationForm.value.countIndex)) {
190             migrationPayload.vdu = {
191                 vduId: this.migrationForm.value.vduId,
192                 vduCountIndex: this.migrationForm.value.countIndex
193             };
194         } else if (!isNullOrUndefined(this.migrationForm.value.vduId)) {
195             migrationPayload.vdu = {
196                 vduId: this.migrationForm.value.vduId
197             };
198         } else if (!isNullOrUndefined(this.migrationForm.value.countIndex)) {
199             migrationPayload.vdu = {
200                 vduCountIndex: this.migrationForm.value.countIndex
201             };
202         }
203         this.migrationInitialization(migrationPayload);
204
205     }
206
207     /** Initialize the Vm Migration operation @public */
208     public migrationInitialization(migrationPayload: object): void {
209         this.isLoadingResults = true;
210         const apiURLHeader: APIURLHEADER = {
211             url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/migrate',
212             httpOptions: { headers: this.headers }
213         };
214         const modalData: MODALCLOSERESPONSEDATA = {
215             message: 'Done'
216         };
217         this.restService.postResource(apiURLHeader, migrationPayload).subscribe((result: {}): void => {
218             this.activeModal.close(modalData);
219             this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch();
220         }, (error: ERRORDATA): void => {
221             this.restService.handleError(error, 'post');
222             this.isLoadingResults = false;
223         });
224     }
225
226     /** Used to get the AbstractControl of controlName passed @private */
227     private getFormControl(controlName: string): AbstractControl {
228         return this.migrationForm.controls[controlName];
229     }
230 }