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 StartStopRebuild Component
21 import { isNullOrUndefined } from 'util';
22 import { HttpHeaders } from '@angular/common/http';
23 import { Component, Injector, Input, OnInit } from '@angular/core';
24 import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
25 import { Router } from '@angular/router';
26 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
27 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
28 import { environment } from 'environment';
29 import { StartStopRebuild } from 'NSInstanceModel';
30 import { RestService } from 'RestService';
31 import { SharedService } from 'SharedService';
32 import { DF, VNFD } from 'VNFDModel';
33 import { VDUR, VNFInstanceDetails } from 'VNFInstanceModel';
37 * @Component takes StartStopRebuildComponent.html as template url
40 selector: 'app-start-stop-rebuild',
41 templateUrl: './StartStopRebuildComponent.html',
42 styleUrls: ['./StartStopRebuildComponent.scss']
44 export 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 */
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;
71 /** Contains vduId @public */
73 /** Items for countIndex @public */
74 public countIndex: {}[];
75 /** 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);
99 /** convenience getter for easy access to form fields */
100 get f(): FormGroup['controls'] { return this.startForm.controls; }
102 * Lifecyle Hooks the trigger before component is instantiate
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'
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]]
122 /** Getting MemberVnfIndex using VNFInstances API @public */
123 public getMemberVnfIndex(): void {
124 const vnfInstanceData: {}[] = [];
125 this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => {
126 vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => {
127 const vnfDataObj: {} =
129 VNFD: vnfData['vnfd-ref'],
130 VNFInstanceId: vnfData._id,
131 MemberIndex: vnfData['member-vnf-index-ref'],
132 NS: vnfData['nsr-id-ref'],
133 VNFID: vnfData['vnfd-id']
135 vnfInstanceData.push(vnfDataObj);
137 const nsId: string = 'NS';
138 // eslint-disable-next-line security/detect-object-injection
139 this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
140 this.nsIdFilteredData.forEach((resVNF: {}[]): void => {
141 const memberIndex: string = 'MemberIndex';
142 const vnfinstanceID: string = 'VNFInstanceId';
143 const assignMemberIndex: {} = {
144 // eslint-disable-next-line security/detect-object-injection
145 id: resVNF[memberIndex],
146 // eslint-disable-next-line security/detect-object-injection
147 vnfinstanceId: resVNF[vnfinstanceID]
149 this.memberVnfIndex.push(assignMemberIndex);
151 this.memberTypes = this.memberVnfIndex;
152 this.isLoadingResults = false;
153 }, (error: ERRORDATA): void => {
154 this.restService.handleError(error, 'get');
155 this.isLoadingResults = false;
159 /** Getting vdu-id & count-index from VNFInstance API */
160 public getVdu(id: string): void {
161 const vnfInstanceData: {}[] = [];
162 this.getFormControl('vduId').setValue(null);
163 this.getFormControl('countIndex').setValue(null);
164 if (!isNullOrUndefined(id)) {
165 this.restService.getResource(environment.VNFINSTANCES_URL + '/' + id).
166 subscribe((vnfInstanceDetail: VNFInstanceDetails[]): void => {
167 this.instanceId = id;
168 this.selectedvnfId = vnfInstanceDetail['vnfd-ref'];
169 const VDU: string = 'vdur';
170 // eslint-disable-next-line security/detect-object-injection
171 if (vnfInstanceDetail[VDU] !== undefined) {
172 // eslint-disable-next-line security/detect-object-injection
173 vnfInstanceDetail[VDU].forEach((vdu: VDUR): void => {
174 const vnfInstanceDataObj: {} =
176 'count-index': vdu['count-index'],
177 VDU: vdu['vdu-id-ref']
180 vnfInstanceData.push(vnfInstanceDataObj);
182 this.vdu = vnfInstanceData;
183 const vduName: string = 'VDU';
184 this.vduId = this.vdu.filter((vdu: {}, index: number, self: {}[]): {} =>
185 index === self.findIndex((t: {}): {} => (
186 // eslint-disable-next-line security/detect-object-injection
187 t[vduName] === vdu[vduName]
191 this.checkDay12Operation(this.selectedvnfId);
192 }, (error: ERRORDATA): void => {
193 this.restService.handleError(error, 'get');
194 this.isLoadingResults = false;
199 /** Getting count-index by filtering id */
200 public getCountIndex(id: string): void {
201 const VDU: string = 'VDU';
202 // eslint-disable-next-line security/detect-object-injection
203 this.countIndex = this.vdu.filter((vnfdData: {}[]): boolean => vnfdData[VDU] === id);
206 /** To check primitve actions from VNFR */
207 public checkDay12Operation(id: string): void {
208 const apiUrl: string = environment.VNFPACKAGES_URL + '?id=' + id;
209 this.restService.getResource(apiUrl).subscribe((vnfdInfo: VNFD[]): void => {
210 const vnfInstances: VNFD = vnfdInfo[0];
211 if (!isNullOrUndefined(vnfInstances.df)) {
212 vnfInstances.df.forEach((df: DF): void => {
213 if (df['lcm-operations-configuration'] !== undefined) {
214 if (df['lcm-operations-configuration']['operate-vnf-op-config']['day1-2'] !== undefined) {
215 this['day1-2'] = true;
218 this['day1-2'] = false;
222 }, (error: ERRORDATA): void => {
223 this.isLoadingResults = false;
224 this.restService.handleError(error, 'get');
227 /** Check Instance type is start or stop or rebuild and proceed action */
228 public instanceCheck(instanceType: string): void {
229 this.submitted = true;
230 this.sharedService.cleanForm(this.startForm);
231 if (this.startForm.invalid) { return; } // Proceed, onces form is valid
232 if (instanceType === 'start') {
233 const startPayload: StartStopRebuild = {
234 updateType: 'OPERATE_VNF',
236 vnfInstanceId: this.instanceId,
237 changeStateTo: 'start',
240 vdu_id: this.startForm.value.vduId,
241 'count-index': this.startForm.value.countIndex
245 this.startInitialization(startPayload);
246 } else if (instanceType === 'stop') {
247 const stopPayload: StartStopRebuild = {
248 updateType: 'OPERATE_VNF',
250 vnfInstanceId: this.instanceId,
251 changeStateTo: 'stop',
254 vdu_id: this.startForm.value.vduId,
255 'count-index': this.startForm.value.countIndex
259 this.startInitialization(stopPayload);
261 const rebuildPayload: StartStopRebuild = {
262 updateType: 'OPERATE_VNF',
264 vnfInstanceId: this.instanceId,
265 changeStateTo: 'rebuild',
267 'run-day1': (this['day1-2'] === true) ? true : false,
268 vdu_id: this.startForm.value.vduId,
269 'count-index': this.startForm.value.countIndex
273 this.startInitialization(rebuildPayload);
277 /** Initialize the start, stop or rebuild operation @public */
278 public startInitialization(startPayload: object): void {
279 this.isLoadingResults = true;
280 const apiURLHeader: APIURLHEADER = {
281 url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/update',
282 httpOptions: { headers: this.headers }
284 const modalData: MODALCLOSERESPONSEDATA = {
287 this.restService.postResource(apiURLHeader, startPayload).subscribe((result: {}): void => {
288 this.activeModal.close(modalData);
289 this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => {
290 // Catch Navigation Error
292 }, (error: ERRORDATA): void => {
293 this.restService.handleError(error, 'post');
294 this.isLoadingResults = false;
298 /** Used to get the AbstractControl of controlName passed @private */
299 private getFormControl(controlName: string): AbstractControl {
300 // eslint-disable-next-line security/detect-object-injection
301 return this.startForm.controls[controlName];