blob: 930eb862248b7b1f91b193a6dbf724b2a9f86922 [file] [log] [blame]
Barath Kumar R07698ab2021-03-30 11:50:42 +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: BARATH KUMAR R (barath.r@tataelxsi.co.in)
17*/
18/**
19 * @file Scaling Component
20 */
21import { HttpHeaders } from '@angular/common/http';
22import { Component, Injector, Input, OnInit } from '@angular/core';
23import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
24import { Router } from '@angular/router';
25import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
26import { TranslateService } from '@ngx-translate/core';
27import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, URLPARAMS } from 'CommonModel';
28import { environment } from 'environment';
29import { DF as NSDF, VNFPROFILE } from 'NSDModel';
30import { RestService } from 'RestService';
31import { SharedService } from 'SharedService';
32import { isNullOrUndefined } from 'util';
33import { DF, SCALING, VNFD } from 'VNFDModel';
34
35/**
36 * Creating component
37 * @Component takes ScalingComponent.html as template url
38 */
39@Component({
40 selector: 'app-scaling',
41 templateUrl: './ScalingComponent.html',
42 styleUrls: ['./ScalingComponent.scss']
43})
44export 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;
79
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);
88 }
89
90 /** convenience getter for easy access to form fields */
91 get f(): FormGroup['controls'] { return this.scalingForm.controls; }
92
93 /**
94 * Lifecyle Hooks the trigger before component is instantiate
95 */
96 public ngOnInit(): void {
97 this.initializeForm();
98 this.getmemberIndex();
99 this.scaleTypes = [
100 { id: 'SCALE_OUT', name: this.translateService.instant('SCALEOUT') },
101 { id: 'SCALE_IN', name: this.translateService.instant('SCALEIN') }
102 ];
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'
107 });
108 }
109
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]]
116 });
117 }
118
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: {} = {
126 id: vnfProfile.id,
127 name: vnfProfile['vnfd-id']
128 };
129 getconstituentVNFD.push(assignMemberIndex);
130 });
131 });
132 this.memberVNFIndex = getconstituentVNFD;
133 }
134 }
135
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 console.log(this.params.data);
144 const vnfdPackageDetails: VNFD = this.params.data.filter((vnfdData: VNFD): boolean => vnfdData.id === vnfID)[0];
145 if (vnfdPackageDetails.df.length > 0) {
146 vnfdPackageDetails.df.forEach((df: DF): void => {
147 if (!isNullOrUndefined(df['scaling-aspect']) && df['scaling-aspect'].length > 0) {
148 df['scaling-aspect'].forEach((scalingAspect: SCALING): void => {
149 scalingGroupDescriptorName.push(scalingAspect);
150 });
151 }
152 });
153 this.scalingGroup = scalingGroupDescriptorName;
154 }
155 }
156 }
157
158 /** If form is valid and call scaleInstances method to initialize scaling @public */
159 public manualScalingTrigger(): void {
160 this.submitted = true;
161 this.sharedService.cleanForm(this.scalingForm);
162 if (this.scalingForm.invalid) { return; } // Proceed, onces form is valid
163 const scalingPayload: object = {
164 scaleType: 'SCALE_VNF',
165 scaleVnfData:
166 {
167 scaleVnfType: this.scalingForm.value.scaleType,
168 scaleByStepData: {
169 'scaling-group-descriptor': this.scalingForm.value.scalingname,
170 'member-vnf-index': this.scalingForm.value.memberIndex
171 }
172 }
173 };
174 this.scaleInstances(scalingPayload);
175 }
176
177 /** Initialize the scaling @public */
178 public scaleInstances(scalingPayload: object): void {
179 this.isLoadingResults = true;
180 const apiURLHeader: APIURLHEADER = {
181 url: environment.NSDINSTANCES_URL + '/' + this.params.id + '/scale',
182 httpOptions: { headers: this.headers }
183 };
184 const modalData: MODALCLOSERESPONSEDATA = {
185 message: 'Done'
186 };
187 this.restService.postResource(apiURLHeader, scalingPayload).subscribe((result: {}): void => {
188 this.activeModal.close(modalData);
189 this.router.navigate(['/instances/ns/history-operations/' + this.params.id]).catch();
190 }, (error: ERRORDATA): void => {
191 this.restService.handleError(error, 'post');
192 this.isLoadingResults = false;
193 });
194 }
195
196 /** Used to get the AbstractControl of controlName passed @private */
197 private getFormControl(controlName: string): AbstractControl {
198 return this.scalingForm.controls[controlName];
199 }
200}