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 } 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 /** FormBuilder instance added to the formBuilder @private */
85 private formBuilder: FormBuilder;
87 /** Utilizes rest service for any CRUD operations @private */
88 private restService: RestService;
90 /** packages data service collections @private */
91 private dataService: DataService;
93 /** Contains tranlsate instance @private */
94 private translateService: TranslateService;
96 /** Notifier service to popup notification @private */
97 private notifierService: NotifierService;
99 /** Contains all methods related to shared @private */
100 private sharedService: SharedService;
102 /** Contains objects that is used to convert key/value pair @private */
103 private objectPrimitiveParams: {} = {};
105 constructor(injector: Injector) {
106 this.injector = injector;
107 this.restService = this.injector.get(RestService);
108 this.dataService = this.injector.get(DataService);
109 this.translateService = this.injector.get(TranslateService);
110 this.notifierService = this.injector.get(NotifierService);
111 this.sharedService = this.injector.get(SharedService);
112 this.activeModal = this.injector.get(NgbActiveModal);
113 this.formBuilder = this.injector.get(FormBuilder);
114 this.primitiveTypeList = [{ title: this.translateService.instant('VNFPRIMITIVE'), value: 'VNF_Primitive' }];
115 this.primitiveType = 'VNF_Primitive';
119 * Lifecyle Hooks the trigger before component is instantiate
121 public ngOnInit(): void {
122 /** Setting up initial value for NSD */
123 this.dataService.currentMessage.subscribe((event: NSData) => {
124 if (event.identifier !== undefined || event.identifier !== '' || event.identifier !== null) {
125 this.nsdId = event.identifier;
128 if (!isNullOrUndefined(this.params.nsConfig)) {
129 this.primitiveTypeList.push({ title: this.translateService.instant('NSPRIMITIVE'), value: 'NS_Primitive' });
131 this.initializeForm();
134 /** convenience getter for easy access to form fields */
135 get f(): FormGroup['controls'] { return this.primitiveForm.controls; }
137 /** initialize Forms @public */
138 public initializeForm(): void {
139 this.primitiveForm = this.formBuilder.group({
140 primitive: [null, [Validators.required]],
141 vnf_member_index: [null, [Validators.required]],
142 primitive_params: this.formBuilder.array([this.primitiveParamsBuilder()])
146 /** Generate primitive params @public */
147 public primitiveParamsBuilder(): FormGroup {
148 return this.formBuilder.group({
149 primitive_params_name: [null, [Validators.required]],
150 primitive_params_value: ['', [Validators.required]]
154 /** Handle FormArray Controls @public */
155 public getControls(): AbstractControl[] {
156 return (this.getFormControl('primitive_params') as FormArray).controls;
159 /** Push all primitive params on user's action @public */
160 public createPrimitiveParams(): void {
161 this.primitiveParams = this.getFormControl('primitive_params') as FormArray;
162 this.primitiveParams.push(this.primitiveParamsBuilder());
165 /** Remove primitive params on user's action @public */
166 public removePrimitiveParams(index: number): void {
167 this.primitiveParams.removeAt(index);
170 /** Execute NS Primitive @public */
171 public execNSPrimitive(): void {
172 this.submitted = true;
173 this.objectPrimitiveParams = {};
174 this.sharedService.cleanForm(this.primitiveForm);
175 if (this.primitiveForm.invalid) { return; } // Proceed, onces form is valid
176 this.primitiveForm.value.primitive_params.forEach((params: NSPrimitiveParams) => {
177 if (params.primitive_params_name !== null && params.primitive_params_value !== '') {
178 this.objectPrimitiveParams[params.primitive_params_name] = params.primitive_params_value;
181 //Prepare primitive params
182 const primitiveParamsPayLoads: {} = {
183 primitive: this.primitiveForm.value.primitive,
184 primitive_params: this.objectPrimitiveParams
186 if (this.primitiveType === 'VNF_Primitive') {
187 // tslint:disable-next-line: no-string-literal
188 primitiveParamsPayLoads['vnf_member_index'] = this.primitiveForm.value.vnf_member_index;
190 const apiURLHeader: APIURLHEADER = {
191 url: environment.NSDINSTANCES_URL + '/' + this.nsdId + '/action'
193 this.isLoadingResults = true;
194 this.restService.postResource(apiURLHeader, primitiveParamsPayLoads).subscribe((result: {}) => {
195 this.activeModal.dismiss();
196 this.notifierService.notify('success', this.translateService.instant('PAGE.NSPRIMITIVE.EXECUTEDSUCCESSFULLY'));
197 this.isLoadingResults = false;
198 }, (error: ERRORDATA) => {
199 this.isLoadingResults = false;
200 this.restService.handleError(error, 'post');
203 /** Primitive type change event @public */
204 public primitiveTypeChange(data: { value: string }): void {
205 this.primitiveList = [];
206 this.primitiveParameter = [];
207 this.initializeForm();
208 if (data.value === 'NS_Primitive') {
209 this.primitiveList = !isNullOrUndefined(this.params.nsConfig['config-primitive']) ?
210 this.params.nsConfig['config-primitive'] : [];
211 this.getFormControl('vnf_member_index').setValidators([]);
214 /** Member index change event */
215 public indexChange(data: {}): void {
217 this.getVnfdInfo(data['vnfd-id-ref']);
219 this.primitiveList = [];
220 this.getFormControl('primitive').setValue(null);
221 this.primitiveParameter = [];
224 /** Primivtive change event */
225 public primitiveChange(data: { parameter: {}[] }): void {
226 this.primitiveParameter = [];
227 const formArr: FormArray = this.getFormControl('primitive_params') as FormArray;
228 formArr.controls = [];
229 this.createPrimitiveParams();
231 this.updatePrimitive(data);
234 /** Update primitive value based on parameter */
235 private updatePrimitive(primitive: { parameter: {}[] }): void {
236 if (primitive.parameter) {
237 this.primitiveParameter = primitive.parameter;
239 this.primitiveParameter = [];
240 const formArr: AbstractControl[] = this.getControls();
241 formArr.forEach((formGp: FormGroup) => {
242 formGp.controls.primitive_params_name.setValidators([]);
243 formGp.controls.primitive_params_name.updateValueAndValidity();
244 formGp.controls.primitive_params_value.setValidators([]);
245 formGp.controls.primitive_params_value.updateValueAndValidity();
249 /** Get primivitive actions from vnfd data */
250 private getVnfdInfo(vnfdRef: string): void {
251 this.primitiveList = [];
252 this.primitiveParameter = [];
253 this.getFormControl('primitive').setValue(null);
254 const apiUrl: string = environment.VNFPACKAGES_URL + '?short-name=' + vnfdRef;
255 this.isLoadingResults = true;
256 this.restService.getResource(apiUrl)
257 .subscribe((vnfdInfo: {}) => {
258 if (vnfdInfo[0]['vnf-configuration']) {
259 this.primitiveList = vnfdInfo[0]['vnf-configuration']['config-primitive'];
261 this.isLoadingResults = false;
262 }, (error: ERRORDATA) => {
263 this.isLoadingResults = false;
264 this.restService.handleError(error, 'get');
267 /** Used to get the AbstractControl of controlName passed @private */
268 private getFormControl(controlName: string): AbstractControl {
269 return this.primitiveForm.controls[controlName];