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: BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file Scaling 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 { TranslateService } from '@ngx-translate/core';
27 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
28 import { environment } from 'environment';
29 import { RestService } from 'RestService';
30 import { SharedService, isNullOrUndefined } from 'SharedService';
31 import { DF, SCALING, VNFD } from 'VNFDModel';
32 import { InstanceData, VNFInstanceDetails } from 'VNFInstanceModel';
35 * @Component takes ScalingComponent.html as template url
38 selector: 'app-scaling',
39 templateUrl: './ScalingComponent.html',
40 styleUrls: ['./ScalingComponent.scss']
42 export class ScalingComponent implements OnInit {
43 /** To inject services @public */
44 public injector: Injector;
45 /** Instance for active modal service @public */
46 public activeModal: NgbActiveModal;
47 /** Check the loading results @public */
48 public isLoadingResults: Boolean = false;
49 /** Give the message for the loading @public */
50 public message: string = 'PLEASEWAIT';
51 /** Contains the scaling group descriptor names @public */
52 public scalingGroup: {}[];
53 /** Member index of the NS @public */
54 public memberVNFIndex: {}[] = [];
55 /** Items for the memberVNFIndex @public */
56 public memberTypes: {}[];
57 /** handles the selected VNFID @public */
58 public selectedVNFID: string = '';
59 /** Items for the Scale Types @public */
60 public scaleTypes: {}[];
61 /** FormGroup instance added to the form @ html @public */
62 public scalingForm: FormGroup;
63 /** Form valid on submit trigger @public */
64 public submitted: boolean = false;
65 /** Array holds VNFR Data filtered with nsr ID @private */
66 private nsIdFilteredData: {}[] = [];
67 /** FormBuilder instance added to the formBuilder @private */
68 private formBuilder: FormBuilder;
69 /** Instance of the rest service @private */
70 private restService: RestService;
71 /** Controls the header form @private */
72 private headers: HttpHeaders;
73 /** Contains tranlsate instance @private */
74 private translateService: TranslateService;
75 /** Input contains component objects @private */
76 @Input() private params: URLPARAMS;
77 /** Contains all methods related to shared @private */
78 private sharedService: SharedService;
79 /** Holds teh instance of AuthService class of type AuthService @private */
80 private router: Router;
82 constructor(injector: Injector) {
83 this.injector = injector;
84 this.restService = this.injector.get(RestService);
85 this.activeModal = this.injector.get(NgbActiveModal);
86 this.translateService = this.injector.get(TranslateService);
87 this.formBuilder = this.injector.get(FormBuilder);
88 this.sharedService = this.injector.get(SharedService);
89 this.router = this.injector.get(Router);
92 /** convenience getter for easy access to form fields */
93 get f(): FormGroup['controls'] { return this.scalingForm.controls; }
96 * Lifecyle Hooks the trigger before component is instantiate
98 public ngOnInit(): void {
99 this.initializeForm();
100 this.getmemberIndex();
102 { id: 'SCALE_OUT', name: this.translateService.instant('SCALEOUT') },
103 { id: 'SCALE_IN', name: this.translateService.instant('SCALEIN') }
105 this.headers = new HttpHeaders({
106 'Content-Type': 'application/json',
107 Accept: 'application/json',
108 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
112 /** Initialize Scaling Forms @public */
113 public initializeForm(): void {
114 this.scalingForm = this.formBuilder.group({
115 memberIndex: [null, [Validators.required]],
116 scalingname: [null, [Validators.required]],
117 scaleType: [null, [Validators.required]]
121 /** Get the member-vnf-index from NS Package -> vnf-profile @public */
122 public getmemberIndex(): void {
123 this.isLoadingResults = true;
124 const vnfInstanceData: {}[] = [];
125 this.restService.getResource(environment.VNFINSTANCES_URL).subscribe((vnfInstancesData: VNFInstanceDetails[]): void => {
126 vnfInstancesData.forEach((vnfData: VNFInstanceDetails): void => {
127 const vnfdRef: string = 'vnfd-ref';
128 const memberIndex: string = 'member-vnf-index-ref';
129 const nsrId: string = 'nsr-id-ref';
130 const vnfId: string = 'vnfd-id';
131 const vnfDataObj: {} =
133 // eslint-disable-next-line security/detect-object-injection
134 VNFD: vnfData[vnfdRef],
135 VNFInstanceId: vnfData._id,
136 // eslint-disable-next-line security/detect-object-injection
137 MemberIndex: vnfData[memberIndex],
138 // eslint-disable-next-line security/detect-object-injection
140 // eslint-disable-next-line security/detect-object-injection
141 VNFID: vnfData[vnfId]
143 vnfInstanceData.push(vnfDataObj);
145 const nsId: string = 'NS';
146 // eslint-disable-next-line security/detect-object-injection
147 this.nsIdFilteredData = vnfInstanceData.filter((vnfdData: {}[]): boolean => vnfdData[nsId] === this.params.id);
148 this.nsIdFilteredData.forEach((resVNF: InstanceData): void => {
149 const assignMemberIndex: {} = {
150 id: resVNF.MemberIndex,
151 vnfinstanceId: resVNF.VNFInstanceId,
154 this.memberVNFIndex.push(assignMemberIndex);
156 this.memberTypes = this.memberVNFIndex;
157 this.isLoadingResults = false;
158 }, (error: ERRORDATA): void => {
159 this.restService.handleError(error, 'get');
160 this.isLoadingResults = false;
164 /** Get the scaling-group-descriptor name @public */
165 public getScalingGroupDescriptorName(vnfID: string): void {
166 this.selectedVNFID = vnfID;
167 this.scalingGroup = [];
168 this.getFormControl('scalingname').setValue(null);
169 const scalingGroupDescriptorName: SCALING[] = [];
170 if (!isNullOrUndefined(this.params.data)) {
171 const vnfdPackageDetails: VNFD = this.params.data.filter((vnfdData: VNFD): boolean => vnfdData.id === vnfID)[0];
172 if (vnfdPackageDetails.df.length > 0) {
173 vnfdPackageDetails.df.forEach((df: DF): void => {
174 if (!isNullOrUndefined(df['scaling-aspect']) && df['scaling-aspect'].length > 0) {
175 df['scaling-aspect'].forEach((scalingAspect: SCALING): void => {
176 scalingGroupDescriptorName.push(scalingAspect);
180 this.scalingGroup = scalingGroupDescriptorName;
185 /** If form is valid and call scaleInstances method to initialize scaling @public */
186 public manualScalingTrigger(): void {
187 this.submitted = true;
188 this.sharedService.cleanForm(this.scalingForm);
189 if (this.scalingForm.invalid) { return; } // Proceed, onces form is valid
190 const scalingPayload: object = {
191 scaleType: 'SCALE_VNF',
194 scaleVnfType: this.scalingForm.value.scaleType,
196 'scaling-group-descriptor': this.scalingForm.value.scalingname,
197 'member-vnf-index': this.scalingForm.value.memberIndex
201 this.scaleInstances(scalingPayload);
204 /** Initialize the scaling @public */
205 public scaleInstances(scalingPayload: object): void {
206 this.isLoadingResults = true;
207 const apiURLHeader: APIURLHEADER = {
208 url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/scale',
209 httpOptions: { headers: this.headers }
211 const modalData: MODALCLOSERESPONSEDATA = {
214 this.restService.postResource(apiURLHeader, scalingPayload).subscribe((result: {}): void => {
215 this.activeModal.close(modalData);
216 this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => {
217 // Catch Navigation Error
219 }, (error: ERRORDATA): void => {
220 this.restService.handleError(error, 'post');
221 this.isLoadingResults = false;
225 /** Used to get the AbstractControl of controlName passed @private */
226 private getFormControl(controlName: string): AbstractControl {
227 // eslint-disable-next-line security/detect-object-injection
228 return this.scalingForm.controls[controlName];