blob: 644b83d405f11eab93cc3343d6d2a93cd956e4e0 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +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: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17 */
18/**
19 * @file ADD PDU Instances Component
20 */
21import { Component, Injector, Input, OnInit, Output } from '@angular/core';
22import { AbstractControl, FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
23import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
24import { TranslateService } from '@ngx-translate/core';
25import { NotifierService } from 'angular-notifier';
26import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
27import { DataService } from 'DataService';
28import { environment } from 'environment';
29import { PDUInstanceDetails } from 'PDUInstanceModel';
30import { RestService } from 'RestService';
31import { SharedService } from 'SharedService';
32import { VimAccountDetails } from 'VimAccountModel';
33
34/**
35 * Creating component
36 * @Component takes AddPDUInstancesComponent.html as template url
37 */
38@Component({
39 templateUrl: './AddPDUInstancesComponent.html',
40 styleUrls: ['./AddPDUInstancesComponent.scss']
41})
42/** Exporting a class @exports AddPDUInstancesComponent */
43export class AddPDUInstancesComponent implements OnInit {
44 /** Form valid on submit trigger @public */
45 public submitted: boolean = false;
46
47 /** To inject services @public */
48 public injector: Injector;
49
50 /** Instance for active modal service @public */
51 public activeModal: NgbActiveModal;
52
53 /** FormGroup instance added to the form @ html @public */
54 public pduInstancesForm: FormGroup;
55
56 /** Primitive params array @public */
57 public pduInterfaces: FormArray;
58
59 /** Variable set for twoway binding @public */
60 public pduInstanceId: string;
61
kumaran.m3b4814a2020-05-01 19:48:54 +053062 /** Set vim field to empty on load @public */
63 public selectedVIM: string;
64
65 /** Contains boolean value as select options for mgmt @public */
66 public mgmtState: {}[] = [{ name: 'True', value: true }, { name: 'False', value: false }];
67
68 /** Input contains Modal dialog component Instance @private */
69 @Input() public title: string;
70
71 /** Contains all the vim accounts list @public */
72 public vimAccountSelect: VimAccountDetails;
73
74 /** Check the loading results @public */
75 public isLoadingResults: boolean = false;
76
77 /** Give the message for the loading @public */
78 public message: string = 'PLEASEWAIT';
79
80 /** FormBuilder instance added to the formBuilder @private */
81 private formBuilder: FormBuilder;
82
83 /** Utilizes rest service for any CRUD operations @private */
84 private restService: RestService;
85
86 /** packages data service collections @private */
87 private dataService: DataService;
88
89 /** Contains tranlsate instance @private */
90 private translateService: TranslateService;
91
92 /** Notifier service to popup notification @private */
93 private notifierService: NotifierService;
94
95 /** Contains all methods related to shared @private */
96 private sharedService: SharedService;
97
98 constructor(injector: Injector) {
99 this.injector = injector;
100 this.restService = this.injector.get(RestService);
101 this.dataService = this.injector.get(DataService);
102 this.translateService = this.injector.get(TranslateService);
103 this.notifierService = this.injector.get(NotifierService);
104 this.sharedService = this.injector.get(SharedService);
105 this.activeModal = this.injector.get(NgbActiveModal);
106 this.formBuilder = this.injector.get(FormBuilder);
107 }
108
109 /**
110 * Lifecyle Hooks the trigger before component is instantiate
111 */
112 public ngOnInit(): void {
113 /** Setting up initial value for NSD */
114 this.dataService.currentMessage.subscribe((event: PDUInstanceDetails) => {
115 if (event.identifier !== undefined || event.identifier !== '' || event.identifier !== null) {
116 this.pduInstanceId = event.identifier;
117 }
118 });
119 this.generateVIMAccounts();
120 this.initializeForm();
121 }
122
123 /** convenience getter for easy access to form fields */
124 get f(): FormGroup['controls'] { return this.pduInstancesForm.controls; }
125
126 /** initialize Forms @public */
127 public initializeForm(): void {
128 this.pduInstancesForm = this.formBuilder.group({
129 name: ['', [Validators.required]],
130 type: ['', [Validators.required]],
131 vim_accounts: ['', [Validators.required]],
132 interfaces: this.formBuilder.array([this.interfacesBuilder()])
133 });
134 }
135
136 /** Generate interfaces fields @public */
137 public interfacesBuilder(): FormGroup {
138 return this.formBuilder.group({
139 name: ['', [Validators.required]],
140 'ip-address': ['', [Validators.required, Validators.pattern(this.sharedService.REGX_IP_PATTERN)]],
SANDHYA.JS5030d1f2022-04-26 17:59:39 +0530141 mgmt: [null, [Validators.required]],
kumaran.m3b4814a2020-05-01 19:48:54 +0530142 'vim-network-name': ['', [Validators.required]]
143 });
144 }
145
146 /** Handle FormArray Controls @public */
147 public getControls(): AbstractControl[] {
148 // tslint:disable-next-line:no-backbone-get-set-outside-model
149 return (this.pduInstancesForm.get('interfaces') as FormArray).controls;
150 }
151
152 /** Push all primitive params on user's action @public */
153 public createInterfaces(): void {
154 // tslint:disable-next-line:no-backbone-get-set-outside-model
155 this.pduInterfaces = this.pduInstancesForm.get('interfaces') as FormArray;
156 this.pduInterfaces.push(this.interfacesBuilder());
157 }
158
159 /** Remove interfaces on user's action @public */
160 public removeInterfaces(index: number): void {
161 this.pduInterfaces.removeAt(index);
162 }
163
164 /** Execute New PDU Instances @public */
165 public createPDUInstances(): void {
166 this.submitted = true;
167 this.sharedService.cleanForm(this.pduInstancesForm);
168 if (this.pduInstancesForm.invalid) { return; } // Proceed, onces form is valid
169 this.isLoadingResults = true;
170 const modalData: MODALCLOSERESPONSEDATA = {
171 message: 'Done'
172 };
173 const apiURLHeader: APIURLHEADER = {
174 url: environment.PDUINSTANCE_URL
175 };
176 this.restService.postResource(apiURLHeader, this.pduInstancesForm.value).subscribe((result: {}) => {
177 this.activeModal.close(modalData);
178 this.notifierService.notify('success', this.translateService.instant('PAGE.PDUINSTANCE.CREATEDSUCCESSFULLY'));
179 this.isLoadingResults = false;
180 }, (error: ERRORDATA) => {
181 this.restService.handleError(error, 'post');
182 this.isLoadingResults = false;
183 });
184 }
185
186 /** Generate vim accounts list @public */
187 public generateVIMAccounts(): void {
188 this.restService.getResource(environment.VIMACCOUNTS_URL).subscribe((vimData: VimAccountDetails) => {
189 this.vimAccountSelect = vimData;
190 }, (error: ERRORDATA) => {
191 this.restService.handleError(error, 'get');
192 });
193 }
194}