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: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file NS Instance Primitive Component
21 import { Component, Injector, Input, OnInit } from '@angular/core';
22 import { AbstractControl, FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
23 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
24 import { TranslateService } from '@ngx-translate/core';
25 import { NotifierService } from 'angular-notifier';
26 import { APIURLHEADER, ERRORDATA, URLPARAMS } from 'CommonModel';
27 import { DataService } from 'DataService';
28 import { environment } from 'environment';
29 import { NSData, VDUPRIMITIVELEVEL } from 'NSDModel';
30 import { NSPrimitiveParams } from 'NSInstanceModel';
31 import { RestService } from 'RestService';
32 import { SharedService } from 'SharedService';
33 import { isNullOrUndefined } from 'util';
37 * @Component takes NSPrimitiveComponent.html as template url
40 templateUrl: './NSPrimitiveComponent.html',
41 styleUrls: ['./NSPrimitiveComponent.scss']
43 /** Exporting a class @exports NSPrimitiveComponent */
44 export class NSPrimitiveComponent implements OnInit {
45 /** Form valid on submit trigger @public */
46 public submitted: boolean = false;
48 /** To inject services @public */
49 public injector: Injector;
51 /** Instance for active modal service @public */
52 public activeModal: NgbActiveModal;
54 /** FormGroup instance added to the form @ html @public */
55 public primitiveForm: FormGroup;
57 /** Primitive params array @public */
58 public primitiveParams: FormArray;
60 /** Variable set for twoway binding @public */
63 /** Check the loading results @public */
64 public isLoadingResults: boolean = false;
66 /** Give the message for the loading @public */
67 public message: string = 'PLEASEWAIT';
69 /** Contains list of primitive parameter @public */
70 public primitiveParameter: {}[] = [];
72 /** Input contains component objects @public */
73 @Input() public params: URLPARAMS;
75 /** Contains list of primitive actions @public */
76 public primitiveList: {}[];
78 /** Contains objects that is used to hold types of primitive @public */
79 public primitiveTypeList: {}[] = [];
81 /** Model value used to hold selected primitive type @public */
82 public primitiveType: string;
84 /** Contains list of VDU primitive lists @public */
87 /** FormBuilder instance added to the formBuilder @private */
88 private formBuilder: FormBuilder;
90 /** Utilizes rest service for any CRUD operations @private */
91 private restService: RestService;
93 /** packages data service collections @private */
94 private dataService: DataService;
96 /** Contains tranlsate instance @private */
97 private translateService: TranslateService;
99 /** Notifier service to popup notification @private */
100 private notifierService: NotifierService;
102 /** Contains all methods related to shared @private */
103 private sharedService: SharedService;
105 /** Contains objects that is used to convert key/value pair @private */
106 private objectPrimitiveParams: {} = {};
108 constructor(injector: Injector) {
109 this.injector = injector;
110 this.restService = this.injector.get(RestService);
111 this.dataService = this.injector.get(DataService);
112 this.translateService = this.injector.get(TranslateService);
113 this.notifierService = this.injector.get(NotifierService);
114 this.sharedService = this.injector.get(SharedService);
115 this.activeModal = this.injector.get(NgbActiveModal);
116 this.formBuilder = this.injector.get(FormBuilder);
117 this.primitiveTypeList = [
119 title: this.translateService.instant('VNFPRIMITIVE'),
120 value: 'VNF_Primitive'
123 title: this.translateService.instant('VDUPRIMITIVE'),
124 value: 'VDU_Primitive'
127 this.primitiveType = 'VNF_Primitive';
131 * Lifecyle Hooks the trigger before component is instantiate
133 public ngOnInit(): void {
134 /** Setting up initial value for NSD */
135 this.dataService.currentMessage.subscribe((event: NSData) => {
136 if (event.identifier !== undefined || event.identifier !== '' || event.identifier !== null) {
137 this.nsdId = event.identifier;
140 if (!isNullOrUndefined(this.params.nsConfig)) {
141 this.primitiveTypeList.push({ title: this.translateService.instant('NSPRIMITIVE'), value: 'NS_Primitive' });
143 this.initializeForm();
146 /** convenience getter for easy access to form fields */
147 get f(): FormGroup['controls'] { return this.primitiveForm.controls; }
149 /** initialize Forms @public */
150 public initializeForm(): void {
151 this.primitiveForm = this.formBuilder.group({
152 primitive: [null, [Validators.required]],
153 vnf_member_index: [null, [Validators.required]],
154 vdu_id: [null, [Validators.required]],
155 primitive_params: this.formBuilder.array([this.primitiveParamsBuilder()])
159 /** Generate primitive params @public */
160 public primitiveParamsBuilder(): FormGroup {
161 return this.formBuilder.group({
162 primitive_params_name: [null, [Validators.required]],
163 primitive_params_value: ['', [Validators.required]]
167 /** Handle FormArray Controls @public */
168 public getControls(): AbstractControl[] {
169 return (this.getFormControl('primitive_params') as FormArray).controls;
172 /** Push all primitive params on user's action @public */
173 public createPrimitiveParams(): void {
174 this.primitiveParams = this.getFormControl('primitive_params') as FormArray;
175 this.primitiveParams.push(this.primitiveParamsBuilder());
178 /** Remove primitive params on user's action @public */
179 public removePrimitiveParams(index: number): void {
180 this.primitiveParams.removeAt(index);
183 /** Execute NS Primitive @public */
184 public execNSPrimitive(): void {
185 this.submitted = true;
186 this.objectPrimitiveParams = {};
187 this.sharedService.cleanForm(this.primitiveForm);
188 if (this.primitiveForm.invalid) { return; } // Proceed, onces form is valid
189 this.primitiveForm.value.primitive_params.forEach((params: NSPrimitiveParams) => {
190 if (params.primitive_params_name !== null && params.primitive_params_value !== '') {
191 this.objectPrimitiveParams[params.primitive_params_name] = params.primitive_params_value;
194 //Prepare primitive params
195 const primitiveParamsPayLoads: {} = {
196 primitive: this.primitiveForm.value.primitive,
197 primitive_params: this.objectPrimitiveParams
199 if (this.primitiveType === 'VNF_Primitive') {
200 // tslint:disable-next-line: no-string-literal
201 primitiveParamsPayLoads['vnf_member_index'] = this.primitiveForm.value.vnf_member_index;
203 if (this.primitiveType === 'VDU_Primitive') {
204 // tslint:disable-next-line: no-string-literal
205 primitiveParamsPayLoads['vnf_member_index'] = this.primitiveForm.value.vnf_member_index;
206 // tslint:disable-next-line: no-string-literal
207 primitiveParamsPayLoads['vdu_id'] = this.primitiveForm.value.vdu_id;
209 const apiURLHeader: APIURLHEADER = {
210 url: environment.NSDINSTANCES_URL + '/' + this.nsdId + '/action'
212 this.isLoadingResults = true;
213 this.restService.postResource(apiURLHeader, primitiveParamsPayLoads).subscribe((result: {}) => {
214 this.activeModal.dismiss();
215 this.notifierService.notify('success', this.translateService.instant('PAGE.NSPRIMITIVE.EXECUTEDSUCCESSFULLY'));
216 this.isLoadingResults = false;
217 }, (error: ERRORDATA) => {
218 this.isLoadingResults = false;
219 this.restService.handleError(error, 'post');
222 /** Primitive type change event @public */
223 public primitiveTypeChange(data: { value: string }): void {
224 this.primitiveList = [];
225 this.primitiveParameter = [];
226 this.initializeForm();
227 if (data.value === 'NS_Primitive') {
228 this.primitiveList = !isNullOrUndefined(this.params.nsConfig['config-primitive']) ?
229 this.params.nsConfig['config-primitive'] : [];
230 this.getFormControl('vnf_member_index').setValidators([]);
231 this.getFormControl('vdu_id').setValidators([]);
234 /** Member index change event */
235 public indexChange(data: {}, getType?: string): void {
236 this.getFormControl('vdu_id').setValue(null);
238 this.getVnfdInfo(data['vnfd-id-ref'], getType);
240 this.primitiveList = [];
241 this.getFormControl('primitive').setValue(null);
242 this.primitiveParameter = [];
245 /** Get VDU Primitive from selected VDU id/name change event */
246 public getVDUPrimitive(data: {}): void {
247 this.primitiveList = data['vdu-configuration']['config-primitive'];
249 /** Primivtive change event */
250 public primitiveChange(data: { parameter: {}[] }): void {
251 this.primitiveParameter = [];
252 const formArr: FormArray = this.getFormControl('primitive_params') as FormArray;
253 formArr.controls = [];
254 this.createPrimitiveParams();
256 this.updatePrimitive(data);
259 /** Generate vdu section @public */
260 public generateVDUData(vduData: VDUPRIMITIVELEVEL): VDUPRIMITIVELEVEL {
264 'vdu-configuration': vduData['vdu-configuration']
267 /** Update primitive value based on parameter */
268 private updatePrimitive(primitive: { parameter: {}[] }): void {
269 if (primitive.parameter) {
270 this.primitiveParameter = primitive.parameter;
272 this.primitiveParameter = [];
273 const formArr: AbstractControl[] = this.getControls();
274 formArr.forEach((formGp: FormGroup) => {
275 formGp.controls.primitive_params_name.setValidators([]);
276 formGp.controls.primitive_params_name.updateValueAndValidity();
277 formGp.controls.primitive_params_value.setValidators([]);
278 formGp.controls.primitive_params_value.updateValueAndValidity();
282 /** Get primivitive actions from vnfd data */
283 private getVnfdInfo(vnfdRef: string, getType?: string): void {
284 this.primitiveList = [];
285 this.primitiveParameter = [];
286 this.getFormControl('primitive').setValue(null);
287 const apiUrl: string = environment.VNFPACKAGES_URL + '?short-name=' + vnfdRef;
288 this.isLoadingResults = true;
289 this.restService.getResource(apiUrl)
290 .subscribe((vnfdInfo: {}) => {
291 if (vnfdInfo[0]['vnf-configuration']) {
292 this.getFormControl('vdu_id').setValidators([]);
293 this.primitiveList = vnfdInfo[0]['vnf-configuration']['config-primitive'];
295 if (getType === 'VDU_Primitive') {
297 vnfdInfo[0].vdu.forEach((vduData: VDUPRIMITIVELEVEL) => {
298 if (vduData['vdu-configuration']) {
299 const vduDataObj: VDUPRIMITIVELEVEL = this.generateVDUData(vduData);
300 this.vduList.push(vduDataObj);
304 this.isLoadingResults = false;
305 }, (error: ERRORDATA) => {
306 this.isLoadingResults = false;
307 this.restService.handleError(error, 'get');
310 /** Used to get the AbstractControl of controlName passed @private */
311 private getFormControl(controlName: string): AbstractControl {
312 return this.primitiveForm.controls[controlName];