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 SDN Controller Component.
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';
34 * @Component takes NewSDNControllerComponent.html as template url
37 templateUrl: './NewSDNControllerComponent.html',
38 styleUrls: ['./NewSDNControllerComponent.scss']
40 /** Exporting a class @exports NewSDNControllerComponent */
41 export class NewSDNControllerComponent implements OnInit {
42 /** To inject services @public */
43 public injector: Injector;
45 /** Set SDN Type select to empty @public */
46 public sdnTypeMod: string = null;
48 /** Setting SDN types in array @public */
49 public sdnType: TYPESECTION[];
51 /** New SDN controller form controls using formgroup @public */
52 public sdnControllerForm: FormGroup;
54 /** Form submission Add */
55 public submitted: boolean = false;
57 /** Instance for active modal service @public */
58 public activeModal: NgbActiveModal;
60 /** Check the loading results for loader status @public */
61 public isLoadingResults: boolean = false;
63 /** Give the message for the loading @public */
64 public message: string = 'PLEASEWAIT';
66 /** Instance of the rest service @private */
67 private restService: RestService;
69 /** Controls the header form @private */
70 private headers: HttpHeaders;
72 /** FormBuilder instance added to the formBuilder @private */
73 private formBuilder: FormBuilder;
75 /** Notifier service to popup notification @private */
76 private notifierService: NotifierService;
78 /** Contains tranlsate instance @private */
79 private translateService: TranslateService;
81 /** Contains all methods related to shared @private */
82 private sharedService: SharedService;
84 constructor(injector: Injector) {
85 this.injector = injector;
86 this.restService = this.injector.get(RestService);
87 this.formBuilder = this.injector.get(FormBuilder);
88 this.notifierService = this.injector.get(NotifierService);
89 this.translateService = this.injector.get(TranslateService);
90 this.activeModal = this.injector.get(NgbActiveModal);
91 this.sharedService = this.injector.get(SharedService);
93 /** Initializing Form Action */
94 this.sdnControllerForm = this.formBuilder.group({
95 name: ['', Validators.required],
96 type: ['', Validators.required],
97 user: ['', Validators.required],
98 password: ['', Validators.required],
99 ip: ['', Validators.pattern(this.sharedService.REGX_IP_PATTERN)],
100 port: ['', Validators.pattern(this.sharedService.REGX_PORT_PATTERN)],
101 dpid: ['', Validators.pattern(this.sharedService.REGX_DPID_PATTERN)],
106 /** convenience getter for easy access to form fields */
107 get f(): FormGroup['controls'] { return this.sdnControllerForm.controls; }
110 * Lifecyle Hooks the trigger before component is instantiate
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'
121 /** On modal submit sdnControllerFormSubmit will called @public */
122 public sdnControllerFormSubmit(): void {
123 this.submitted = true;
124 const modalData: MODALCLOSERESPONSEDATA = {
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 }
134 if (this.sdnControllerForm.value.port) {
135 this.sdnControllerForm.value.port = +this.sdnControllerForm.value.port;
137 if (this.sdnControllerForm.value.version === '') {
138 this.sdnControllerForm.value.version = undefined;
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;