blob: 994012913da49a819085c49bebeabe0ee0e4afb9 [file] [log] [blame]
SANDHYA.JS3d81a282022-05-02 08:25:39 +05301/*
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 StartStopRebuild Component
20 */
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +053021import { isNullOrUndefined } from 'util';
SANDHYA.JS3d81a282022-05-02 08:25:39 +053022import { HttpHeaders } from '@angular/common/http';
23import { Component, Injector, Input, OnInit } from '@angular/core';
24import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
25import { Router } from '@angular/router';
26import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
27import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
28import { environment } from 'environment';
29import { StartStopRebuild } from 'NSInstanceModel';
30import { RestService } from 'RestService';
31import { SharedService } from 'SharedService';
SANDHYA.JS3d81a282022-05-02 08:25:39 +053032import { DF, VNFD } from 'VNFDModel';
SANDHYA.JSc7e64622023-10-12 11:05:48 +053033import { InstanceData, VDUR, VNFInstanceDetails } from 'VNFInstanceModel';
SANDHYA.JS3d81a282022-05-02 08:25:39 +053034
35/**
36 * Creating component
37 * @Component takes StartStopRebuildComponent.html as template url
38 */
39@Component({
40 selector: 'app-start-stop-rebuild',
41 templateUrl: './StartStopRebuildComponent.html',
42 styleUrls: ['./StartStopRebuildComponent.scss']
43})
44export class StartStopRebuildComponent implements OnInit {
45 /** To inject services @public */
46 public injector: Injector;
47 /** Instance for active modal service @public */
48 public activeModal: NgbActiveModal;
49 /** Check the loading results @public */
50 public isLoadingResults: Boolean = false;
51 /** Give the message for the loading @public */
52 public message: string = 'PLEASEWAIT';
53 /** FormGroup instance added to the form @ html @public */
54 public startForm: FormGroup;
55 /** Items for the memberVNFIndex @public */
56 public memberTypes: {}[];
57 /** Contains MemberVNFIndex values @public */
58 public memberVnfIndex: {}[] = [];
59 /** Contains vnfInstanceId of the selected MemberVnfIndex @public */
60 public instanceId: string;
61 /** Items for vduId & countIndex @public */
62 public vdu: {}[];
63 /** Selected VNFInstanceId @public */
64 public selectedvnfId: string = '';
65 /** Check day1-2 operation @public */
66 public 'day1-2': boolean;
67 /** Array holds VNFR Data filtered with nsr ID @public */
68 public nsIdFilteredData: {}[] = [];
69 /** Form valid on submit trigger @public */
70 public submitted: boolean = false;
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +053071 /** Contains vduId @public */
72 public vduId: {};
73 /** Items for countIndex @public */
74 public countIndex: {}[];
SANDHYA.JS3d81a282022-05-02 08:25:39 +053075 /** Input contains Modal dialog component Instance @public */
76 @Input() public instanceType: string;
77 /** Input contains Modal dialog component Instance @public */
78 @Input() public instanceTitle: string;
79 /** Input contains component objects @private */
80 @Input() private params: URLPARAMS;
81 /** FormBuilder instance added to the formBuilder @private */
82 private formBuilder: FormBuilder;
83 /** Instance of the rest service @private */
84 private restService: RestService;
85 /** Controls the header form @private */
86 private headers: HttpHeaders;
87 /** Contains all methods related to shared @private */
88 private sharedService: SharedService;
89 /** Holds the instance of AuthService class of type AuthService @private */
90 private router: Router;
91 constructor(injector: Injector) {
92 this.injector = injector;
93 this.restService = this.injector.get(RestService);
94 this.activeModal = this.injector.get(NgbActiveModal);
95 this.formBuilder = this.injector.get(FormBuilder);
96 this.sharedService = this.injector.get(SharedService);
97 this.router = this.injector.get(Router);
98 }
99 /** convenience getter for easy access to form fields */
100 get f(): FormGroup['controls'] { return this.startForm.controls; }
101 /**
102 * Lifecyle Hooks the trigger before component is instantiate
103 */
104 public ngOnInit(): void {
105 this.initializeForm();
106 this.getMemberVnfIndex();
107 this.headers = new HttpHeaders({
108 'Content-Type': 'application/json',
109 Accept: 'application/json',
110 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
111 });
112 }
113 /** Initialize start, stop or rebuild Forms @public */
114 public initializeForm(): void {
115 this.startForm = this.formBuilder.group({
116 memberVnfIndex: [null, [Validators.required]],
117 vduId: [null, [Validators.required]],
118 countIndex: [null, [Validators.required]]
119 });
120 }
121
122 /** Getting MemberVnfIndex using VNFInstances API @public */
123 public getMemberVnfIndex(): void {
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530124 this.isLoadingResults = true;
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530125 const vnfInstanceData: {}[] = [];
126 this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => {
127 vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => {
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530128 const vnfdRef: string = 'vnfd-ref';
129 const memberIndex: string = 'member-vnf-index-ref';
130 const nsrId: string = 'nsr-id-ref';
131 const vnfId: string = 'vnfd-id';
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530132 const vnfDataObj: {} =
133 {
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530134 // eslint-disable-next-line security/detect-object-injection
135 VNFD: vnfData[vnfdRef],
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530136 VNFInstanceId: vnfData._id,
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530137 // eslint-disable-next-line security/detect-object-injection
138 MemberIndex: vnfData[memberIndex],
139 // eslint-disable-next-line security/detect-object-injection
140 NS: vnfData[nsrId],
141 // eslint-disable-next-line security/detect-object-injection
142 VNFID: vnfData[vnfId]
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530143 };
144 vnfInstanceData.push(vnfDataObj);
145 });
146 const nsId: string = 'NS';
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530147 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530148 this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530149 this.nsIdFilteredData.forEach((resVNF: InstanceData): void => {
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530150 const assignMemberIndex: {} = {
SANDHYA.JSc7e64622023-10-12 11:05:48 +0530151 id: resVNF.MemberIndex,
152 vnfinstanceId: resVNF.VNFInstanceId
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530153 };
154 this.memberVnfIndex.push(assignMemberIndex);
155 });
156 this.memberTypes = this.memberVnfIndex;
157 this.isLoadingResults = false;
158 }, (error: ERRORDATA): void => {
159 this.restService.handleError(error, 'get');
160 this.isLoadingResults = false;
161 });
162 }
163
164 /** Getting vdu-id & count-index from VNFInstance API */
165 public getVdu(id: string): void {
166 const vnfInstanceData: {}[] = [];
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530167 this.getFormControl('vduId').setValue(null);
168 this.getFormControl('countIndex').setValue(null);
169 if (!isNullOrUndefined(id)) {
170 this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id).
171 subscribe((vnfInstanceDetail: VNFInstanceDetails[]): void => {
172 this.instanceId = id;
173 this.selectedvnfId = vnfInstanceDetail['vnfd-ref'];
174 const VDU: string = 'vdur';
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530175 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530176 if (vnfInstanceDetail[VDU] !== undefined) {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530177 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530178 vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => {
179 const vnfInstanceDataObj: {} =
180 {
181 'count-index': vdu['count-index'],
182 VDU: vdu['vdu-id-ref']
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530183 };
184 vnfInstanceData.push(vnfInstanceDataObj);
185 });
186 this.vdu = vnfInstanceData;
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530187 const vduName: string = 'VDU';
188 this.vduId = this.vdu.filter((vdu: {}, index: number, self: {}[]): {} =>
189 index === self.findIndex((t: {}): {} => (
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530190 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530191 t[vduName] === vdu[vduName]
192 ))
193 );
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530194 }
195 this.checkDay12Operation(this.selectedvnfId);
196 }, (error: ERRORDATA): void => {
197 this.restService.handleError(error, 'get');
198 this.isLoadingResults = false;
199 });
200 }
201 }
202
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530203 /** Getting count-index by filtering id */
204 public getCountIndex(id: string): void {
205 const VDU: string = 'VDU';
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530206 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JSbc5d33e2022-08-25 08:19:13 +0530207 this.countIndex = this.vdu.filter((vnfdData: {}[]): boolean => vnfdData[VDU] === id);
208 }
209
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530210 /** To check primitve actions from VNFR */
211 public checkDay12Operation(id: string): void {
212 const apiUrl: string = environment.VNFPACKAGES_URL + '?id=' + id;
213 this.restService.getResource(apiUrl).subscribe((vnfdInfo: VNFD[]): void => {
214 const vnfInstances: VNFD = vnfdInfo[0];
215 if (!isNullOrUndefined(vnfInstances.df)) {
216 vnfInstances.df.forEach((df: DF): void => {
217 if (df['lcm-operations-configuration'] !== undefined) {
218 if (df['lcm-operations-configuration']['operate-vnf-op-config']['day1-2'] !== undefined) {
219 this['day1-2'] = true;
220 }
221 } else {
222 this['day1-2'] = false;
223 }
224 });
225 }
226 }, (error: ERRORDATA): void => {
227 this.isLoadingResults = false;
228 this.restService.handleError(error, 'get');
229 });
230 }
231 /** Check Instance type is start or stop or rebuild and proceed action */
232 public instanceCheck(instanceType: string): void {
233 this.submitted = true;
234 this.sharedService.cleanForm(this.startForm);
235 if (this.startForm.invalid) { return; } // Proceed, onces form is valid
236 if (instanceType === 'start') {
237 const startPayload: StartStopRebuild = {
238 updateType: 'OPERATE_VNF',
239 operateVnfData: {
240 vnfInstanceId: this.instanceId,
241 changeStateTo: 'start',
242 additionalParam: {
243 'run-day1': false,
244 vdu_id: this.startForm.value.vduId,
245 'count-index': this.startForm.value.countIndex
246 }
247 }
248 };
249 this.startInitialization(startPayload);
250 } else if (instanceType === 'stop') {
251 const stopPayload: StartStopRebuild = {
252 updateType: 'OPERATE_VNF',
253 operateVnfData: {
254 vnfInstanceId: this.instanceId,
255 changeStateTo: 'stop',
256 additionalParam: {
257 'run-day1': false,
258 vdu_id: this.startForm.value.vduId,
259 'count-index': this.startForm.value.countIndex
260 }
261 }
262 };
263 this.startInitialization(stopPayload);
264 } else {
265 const rebuildPayload: StartStopRebuild = {
266 updateType: 'OPERATE_VNF',
267 operateVnfData: {
268 vnfInstanceId: this.instanceId,
269 changeStateTo: 'rebuild',
270 additionalParam: {
271 'run-day1': (this['day1-2'] === true) ? true : false,
272 vdu_id: this.startForm.value.vduId,
273 'count-index': this.startForm.value.countIndex
274 }
275 }
276 };
277 this.startInitialization(rebuildPayload);
278 }
279 }
280
281 /** Initialize the start, stop or rebuild operation @public */
282 public startInitialization(startPayload: object): void {
283 this.isLoadingResults = true;
284 const apiURLHeader: APIURLHEADER = {
285 url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/update',
286 httpOptions: { headers: this.headers }
287 };
288 const modalData: MODALCLOSERESPONSEDATA = {
289 message: 'Done'
290 };
291 this.restService.postResource(apiURLHeader, startPayload).subscribe((result: {}): void => {
292 this.activeModal.close(modalData);
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530293 this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => {
294 // Catch Navigation Error
295 });
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530296 }, (error: ERRORDATA): void => {
297 this.restService.handleError(error, 'post');
298 this.isLoadingResults = false;
299 });
300 }
301
302 /** Used to get the AbstractControl of controlName passed @private */
303 private getFormControl(controlName: string): AbstractControl {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530304 // eslint-disable-next-line security/detect-object-injection
SANDHYA.JS3d81a282022-05-02 08:25:39 +0530305 return this.startForm.controls[controlName];
306 }
307}