2 Copyright 2020 TATA ELXSI
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 Author: SANDHYA JS (sandhya.j@tataelxsi.co.in)
19 * @file Vm Migration Component
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';
36 * @Component takes VmMigrationComponent.html as template url
39 selector: 'app-vm-migration',
40 templateUrl: './VmMigrationComponent.html',
41 styleUrls: ['./VmMigrationComponent.scss']
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 */
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);
88 /** convenience getter for easy access to form fields */
89 get f(): FormGroup['controls'] { return this.migrationForm.controls; }
91 * Lifecyle Hooks the trigger before component is instantiate
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'
102 /** Initialize Migration Forms @public */
103 public initializeForm(): void {
104 this.migrationForm = this.formBuilder.group({
105 memberVnfIndex: [null, [Validators.required]],
108 migrateToHost: [null]
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: {} =
120 VNFD: vnfData['vnfd-ref'],
121 VNFInstanceId: vnfData._id,
122 MemberIndex: vnfData['member-vnf-index-ref'],
123 NS: vnfData['nsr-id-ref']
125 vnfInstanceData.push(vnfDataObj);
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]
136 this.memberVnfIndex.push(assignMemberIndex);
138 this.memberTypes = this.memberVnfIndex;
139 this.isLoadingResults = false;
140 }, (error: ERRORDATA): void => {
141 this.restService.handleError(error, 'get');
142 this.isLoadingResults = false;
146 /** Getting vdu-id & count-index from VNFInstance API */
147 public getVdu(id: string): void {
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: {} =
162 'count-index': vdu['count-index'],
163 VDU: vdu['vdu-id-ref']
166 vnfInstanceData.push(vnfInstanceDataObj);
168 this.vdu = vnfInstanceData;
170 }, (error: ERRORDATA): void => {
171 this.restService.handleError(error, 'get');
172 this.isLoadingResults = false;
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
186 if (!isNullOrUndefined(this.migrationForm.value.migrateToHost)) {
187 migrationPayload.migrateToHost = this.migrationForm.value.migrateToHost;
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
194 } else if (!isNullOrUndefined(this.migrationForm.value.vduId)) {
195 migrationPayload.vdu = {
196 vduId: this.migrationForm.value.vduId
198 } else if (!isNullOrUndefined(this.migrationForm.value.countIndex)) {
199 migrationPayload.vdu = {
200 vduCountIndex: this.migrationForm.value.countIndex
203 this.migrationInitialization(migrationPayload);
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 }
214 const modalData: MODALCLOSERESPONSEDATA = {
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;
226 /** Used to get the AbstractControl of controlName passed @private */
227 private getFormControl(controlName: string): AbstractControl {
228 return this.migrationForm.controls[controlName];