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 ADD PDU Instances Component
21 import { Component, Injector, Input, OnInit, Output } 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, MODALCLOSERESPONSEDATA } from 'CommonModel';
27 import { DataService } from 'DataService';
28 import { environment } from 'environment';
29 import { PDUInstanceDetails } from 'PDUInstanceModel';
30 import { RestService } from 'RestService';
31 import { SharedService } from 'SharedService';
32 import { VimAccountDetails } from 'VimAccountModel';
36 * @Component takes AddPDUInstancesComponent.html as template url
39 templateUrl: './AddPDUInstancesComponent.html',
40 styleUrls: ['./AddPDUInstancesComponent.scss']
42 /** Exporting a class @exports AddPDUInstancesComponent */
43 export class AddPDUInstancesComponent implements OnInit {
44 /** Form valid on submit trigger @public */
45 public submitted: boolean = false;
47 /** To inject services @public */
48 public injector: Injector;
50 /** Instance for active modal service @public */
51 public activeModal: NgbActiveModal;
53 /** FormGroup instance added to the form @ html @public */
54 public pduInstancesForm: FormGroup;
56 /** Primitive params array @public */
57 public pduInterfaces: FormArray;
59 /** Variable set for twoway binding @public */
60 public pduInstanceId: string;
62 /** Set vim field to empty on load @public */
63 public selectedVIM: string;
65 /** Contains boolean value as select options for mgmt @public */
66 public mgmtState: {}[] = [{ name: 'True', value: true }, { name: 'False', value: false }];
68 /** Input contains Modal dialog component Instance @private */
69 @Input() public title: string;
71 /** Contains all the vim accounts list @public */
72 public vimAccountSelect: VimAccountDetails;
74 /** Check the loading results @public */
75 public isLoadingResults: boolean = false;
77 /** Give the message for the loading @public */
78 public message: string = 'PLEASEWAIT';
80 /** FormBuilder instance added to the formBuilder @private */
81 private formBuilder: FormBuilder;
83 /** Utilizes rest service for any CRUD operations @private */
84 private restService: RestService;
86 /** packages data service collections @private */
87 private dataService: DataService;
89 /** Contains tranlsate instance @private */
90 private translateService: TranslateService;
92 /** Notifier service to popup notification @private */
93 private notifierService: NotifierService;
95 /** Contains all methods related to shared @private */
96 private sharedService: SharedService;
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);
110 * Lifecyle Hooks the trigger before component is instantiate
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;
119 this.generateVIMAccounts();
120 this.initializeForm();
123 /** convenience getter for easy access to form fields */
124 get f(): FormGroup['controls'] { return this.pduInstancesForm.controls; }
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()])
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)]],
141 mgmt: [null, [Validators.required]],
142 'vim-network-name': ['', [Validators.required]]
146 /** Handle FormArray Controls @public */
147 public getControls(): AbstractControl[] {
148 return (this.pduInstancesForm.get('interfaces') as FormArray).controls;
151 /** Push all primitive params on user's action @public */
152 public createInterfaces(): void {
153 this.pduInterfaces = this.pduInstancesForm.get('interfaces') as FormArray;
154 this.pduInterfaces.push(this.interfacesBuilder());
157 /** Remove interfaces on user's action @public */
158 public removeInterfaces(index: number): void {
159 this.pduInterfaces.removeAt(index);
162 /** Execute New PDU Instances @public */
163 public createPDUInstances(): void {
164 this.submitted = true;
165 this.sharedService.cleanForm(this.pduInstancesForm);
166 if (this.pduInstancesForm.invalid) { return; } // Proceed, onces form is valid
167 this.isLoadingResults = true;
168 const modalData: MODALCLOSERESPONSEDATA = {
171 const apiURLHeader: APIURLHEADER = {
172 url: environment.PDUINSTANCE_URL
174 this.restService.postResource(apiURLHeader, this.pduInstancesForm.value).subscribe((result: {}) => {
175 this.activeModal.close(modalData);
176 this.notifierService.notify('success', this.translateService.instant('PAGE.PDUINSTANCE.CREATEDSUCCESSFULLY'));
177 this.isLoadingResults = false;
178 }, (error: ERRORDATA) => {
179 this.restService.handleError(error, 'post');
180 this.isLoadingResults = false;
184 /** Generate vim accounts list @public */
185 public generateVIMAccounts(): void {
186 this.restService.getResource(environment.VIMACCOUNTS_URL).subscribe((vimData: VimAccountDetails) => {
187 this.vimAccountSelect = vimData;
188 }, (error: ERRORDATA) => {
189 this.restService.handleError(error, 'get');