Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[osm/NG-UI.git] / src / app / instances / pdu-instances / add-pdu-instances / AddPDUInstancesComponent.ts
1 /*
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  */
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';
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 */
43 export 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
62     /** 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)]],
141             mgmt: [null, [Validators.required]],
142             'vim-network-name': ['', [Validators.required]]
143         });
144     }
145
146     /** Handle FormArray Controls @public */
147     public getControls(): AbstractControl[] {
148         return (this.pduInstancesForm.get('interfaces') as FormArray).controls;
149     }
150
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());
155     }
156
157     /** Remove interfaces on user's action @public */
158     public removeInterfaces(index: number): void {
159         this.pduInterfaces.removeAt(index);
160     }
161
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 = {
169             message: 'Done'
170         };
171         const apiURLHeader: APIURLHEADER = {
172             url: environment.PDUINSTANCE_URL
173         };
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;
181         });
182     }
183
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');
190         });
191     }
192 }