New VIM Design with the config implemented.
[osm/NG-UI.git] / src / app / sdn-controller / new-sdn-controller / NewSDNControllerComponent.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 SDN Controller Component.
20  */
21 import { HttpHeaders } from '@angular/common/http';
22 import { Component, Injector, OnInit } from '@angular/core';
23 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25 import { TranslateService } from '@ngx-translate/core';
26 import { NotifierService } from 'angular-notifier';
27 import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, SDN_TYPES, TYPESECTION } from 'CommonModel';
28 import { environment } from 'environment';
29 import { RestService } from 'RestService';
30 import { SharedService } from 'SharedService';
31
32 /**
33  * Creating component
34  * @Component takes NewSDNControllerComponent.html as template url
35  */
36 @Component({
37   templateUrl: './NewSDNControllerComponent.html',
38   styleUrls: ['./NewSDNControllerComponent.scss']
39 })
40 /** Exporting a class @exports NewSDNControllerComponent */
41 export class NewSDNControllerComponent implements OnInit {
42   /** To inject services @public */
43   public injector: Injector;
44
45   /** Set SDN Type select to empty @public */
46   public sdnTypeMod: string = null;
47
48   /** Setting SDN types in array @public */
49   public sdnType: TYPESECTION[];
50
51   /** New SDN controller form controls using formgroup @public */
52   public sdnControllerForm: FormGroup;
53
54   /** Form submission Add */
55   public submitted: boolean = false;
56
57   /** Instance for active modal service @public */
58   public activeModal: NgbActiveModal;
59
60   /** Check the loading results for loader status @public */
61   public isLoadingResults: boolean = false;
62
63   /** Give the message for the loading @public */
64   public message: string = 'PLEASEWAIT';
65
66   /** Contains all methods related to shared @private */
67   public sharedService: SharedService;
68
69   /** Instance of the rest service @private */
70   private restService: RestService;
71
72   /** Controls the header form @private */
73   private headers: HttpHeaders;
74
75   /** FormBuilder instance added to the formBuilder @private */
76   private formBuilder: FormBuilder;
77
78   /** Notifier service to popup notification @private */
79   private notifierService: NotifierService;
80
81   /** Contains tranlsate instance @private */
82   private translateService: TranslateService;
83
84   /** convenience getter for easy access to form fields */
85   get f(): FormGroup['controls'] { return this.sdnControllerForm.controls; }
86
87   constructor(injector: Injector) {
88     this.injector = injector;
89     this.restService = this.injector.get(RestService);
90     this.formBuilder = this.injector.get(FormBuilder);
91     this.notifierService = this.injector.get(NotifierService);
92     this.translateService = this.injector.get(TranslateService);
93     this.activeModal = this.injector.get(NgbActiveModal);
94     this.sharedService = this.injector.get(SharedService);
95
96     /** Initializing Form Action */
97     this.sdnControllerForm = this.formBuilder.group({
98       name: ['', Validators.required],
99       type: ['', Validators.required],
100       user: ['', Validators.required],
101       password: ['', Validators.required],
102       ip: ['', Validators.pattern(this.sharedService.REGX_IP_PATTERN)],
103       port: ['', Validators.pattern(this.sharedService.REGX_PORT_PATTERN)],
104       dpid: ['', Validators.pattern(this.sharedService.REGX_DPID_PATTERN)],
105       version: ['']
106     });
107   }
108
109   /**
110    * Lifecyle Hooks the trigger before component is instantiate
111    */
112   public ngOnInit(): void {
113     this.sdnType = SDN_TYPES;
114     this.headers = new HttpHeaders({
115       Accept: 'application/json',
116       'Content-Type': 'application/json',
117       'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
118     });
119   }
120
121   /** On modal submit sdnControllerFormSubmit will called @public */
122   public sdnControllerFormSubmit(): void {
123     this.submitted = true;
124     const modalData: MODALCLOSERESPONSEDATA = {
125       message: 'Done'
126     };
127     this.sharedService.cleanForm(this.sdnControllerForm);
128     if (!this.sdnControllerForm.invalid) {
129       this.isLoadingResults = true;
130       const apiURLHeader: APIURLHEADER = {
131         url: environment.SDNCONTROLLER_URL,
132         httpOptions: { headers: this.headers }
133       };
134       if (this.sdnControllerForm.value.port) {
135         this.sdnControllerForm.value.port = +this.sdnControllerForm.value.port;
136       }
137       if (this.sdnControllerForm.value.version === '') {
138         this.sdnControllerForm.value.version = undefined;
139       }
140       this.restService.postResource(apiURLHeader, this.sdnControllerForm.value)
141         .subscribe((result: {}) => {
142           this.activeModal.close(modalData);
143           this.isLoadingResults = false;
144           this.notifierService.notify('success', this.translateService.instant('PAGE.SDNCONTROLLER.CREATEDSUCCESSFULLY'));
145         }, (error: ERRORDATA) => {
146           this.restService.handleError(error, 'post');
147           this.isLoadingResults = false;
148         });
149     }
150   }
151 }