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 { 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 { TranslateService } from '@ngx-translate/core';
28 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
29 import { environment } from 'environment';
30 import { DF as NSDF, VNFPROFILE } from 'NSDModel';
31 import { RestService } from 'RestService';
32 import { SharedService } from 'SharedService';
33 import { DF, SCALING, VNFD } from 'VNFDModel';
37 * @Component takes ScalingComponent.html as template url
40 selector: 'app-scaling',
41 templateUrl: './ScalingComponent.html',
42 styleUrls: ['./ScalingComponent.scss']
44 export class ScalingComponent 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 /** Contains the scaling group descriptor names @public */
54 public scalingGroup: {}[];
55 /** Member index of the NS @public */
56 public memberVNFIndex: {}[];
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 /** FormBuilder instance added to the formBuilder @private */
66 private formBuilder: FormBuilder;
67 /** Instance of the rest service @private */
68 private restService: RestService;
69 /** Controls the header form @private */
70 private headers: HttpHeaders;
71 /** Contains tranlsate instance @private */
72 private translateService: TranslateService;
73 /** Input contains component objects @private */
74 @Input() private params: URLPARAMS;
75 /** Contains all methods related to shared @private */
76 private sharedService: SharedService;
77 /** Holds teh instance of AuthService class of type AuthService @private */
78 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.translateService = this.injector.get(TranslateService);
85 this.formBuilder = this.injector.get(FormBuilder);
86 this.sharedService = this.injector.get(SharedService);
87 this.router = this.injector.get(Router);
90 /** convenience getter for easy access to form fields */
91 get f(): FormGroup['controls'] { return this.scalingForm.controls; }
94 * Lifecyle Hooks the trigger before component is instantiate
96 public ngOnInit(): void {
97 this.initializeForm();
98 this.getmemberIndex();
100 { id: 'SCALE_OUT', name: this.translateService.instant('SCALEOUT') },
101 { id: 'SCALE_IN', name: this.translateService.instant('SCALEIN') }
103 this.headers = new HttpHeaders({
104 'Content-Type': 'application/json',
105 Accept: 'application/json',
106 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
110 /** Initialize Scaling Forms @public */
111 public initializeForm(): void {
112 this.scalingForm = this.formBuilder.group({
113 memberIndex: [null, [Validators.required]],
114 scalingname: [null, [Validators.required]],
115 scaleType: [null, [Validators.required]]
119 /** Get the member-vnf-index from NS Package -> vnf-profile @public */
120 public getmemberIndex(): void {
121 if (this.params.nsd.df.length > 0) {
122 const getconstituentVNFD: {}[] = [];
123 this.params.nsd.df.forEach((data: NSDF): void => {
124 data['vnf-profile'].forEach((vnfProfile: VNFPROFILE): void => {
125 const assignMemberIndex: {} = {
127 name: vnfProfile['vnfd-id']
129 getconstituentVNFD.push(assignMemberIndex);
132 this.memberVNFIndex = getconstituentVNFD;
136 /** Get the scaling-group-descriptor name @public */
137 public getScalingGroupDescriptorName(vnfID: string): void {
138 this.selectedVNFID = vnfID;
139 this.scalingGroup = [];
140 this.getFormControl('scalingname').setValue(null);
141 const scalingGroupDescriptorName: SCALING[] = [];
142 if (!isNullOrUndefined(this.params.data)) {
143 const vnfdPackageDetails: VNFD = this.params.data.filter((vnfdData: VNFD): boolean => vnfdData.id === vnfID)[0];
144 if (vnfdPackageDetails.df.length > 0) {
145 vnfdPackageDetails.df.forEach((df: DF): void => {
146 if (!isNullOrUndefined(df['scaling-aspect']) && df['scaling-aspect'].length > 0) {
147 df['scaling-aspect'].forEach((scalingAspect: SCALING): void => {
148 scalingGroupDescriptorName.push(scalingAspect);
152 this.scalingGroup = scalingGroupDescriptorName;
157 /** If form is valid and call scaleInstances method to initialize scaling @public */
158 public manualScalingTrigger(): void {
159 this.submitted = true;
160 this.sharedService.cleanForm(this.scalingForm);
161 if (this.scalingForm.invalid) { return; } // Proceed, onces form is valid
162 const scalingPayload: object = {
163 scaleType: 'SCALE_VNF',
166 scaleVnfType: this.scalingForm.value.scaleType,
168 'scaling-group-descriptor': this.scalingForm.value.scalingname,
169 'member-vnf-index': this.scalingForm.value.memberIndex
173 this.scaleInstances(scalingPayload);
176 /** Initialize the scaling @public */
177 public scaleInstances(scalingPayload: object): void {
178 this.isLoadingResults = true;
179 const apiURLHeader: APIURLHEADER = {
180 url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/scale',
181 httpOptions: { headers: this.headers }
183 const modalData: MODALCLOSERESPONSEDATA = {
186 this.restService.postResource(apiURLHeader, scalingPayload).subscribe((result: {}): void => {
187 this.activeModal.close(modalData);
188 this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch((): void => {
189 // Catch Navigation Error
191 }, (error: ERRORDATA): void => {
192 this.restService.handleError(error, 'post');
193 this.isLoadingResults = false;
197 /** Used to get the AbstractControl of controlName passed @private */
198 private getFormControl(controlName: string): AbstractControl {
199 // eslint-disable-next-line security/detect-object-injection
200 return this.scalingForm.controls[controlName];