rework admin table
[portal/ric-dashboard.git] / webapp-frontend / src / app / ran-control / ran-connection-dialog.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 import { Component, OnInit, Inject } from '@angular/core';
21 import { FormGroup, FormControl, Validators } from '@angular/forms';
22 import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
23 import { E2ManagerService } from '../services/e2-mgr/e2-mgr.service';
24 import { NotificationService } from '../services/ui/notification.service';
25 import { ErrorDialogService } from '../services/ui/error-dialog.service';
26 import { E2SetupRequest } from '../interfaces/e2-mgr.types';
27 import { HttpErrorResponse } from '@angular/common/http';
28
29 @Component({
30     selector: 'app-signal-ranconnect-dialog',
31     templateUrl: './ran-connection-dialog.component.html',
32     styleUrls: ['./ran-connection-dialog.component.css']
33 })
34
35 export class RANConnectionDialogComponent implements OnInit {
36
37     public ranDialogForm: FormGroup;
38
39     constructor(
40         private dialogRef: MatDialogRef<RANConnectionDialogComponent>,
41         private service: E2ManagerService,
42         private errorService: ErrorDialogService,
43         private notifService: NotificationService,
44         @Inject(MAT_DIALOG_DATA) public data: E2SetupRequest) {
45     }
46
47     ngOnInit() {
48         const namePattern = /^([A-Z]){4}([0-9]){6}$/;
49         // tslint:disable-next-line:max-line-length
50         const ipPattern = /((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0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)?\s*$))/;
51         const portPattern = /^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
52         this.ranDialogForm = new FormGroup({
53             ranType: new FormControl('endc'),
54             ranName: new FormControl('', [Validators.required, Validators.pattern(namePattern)]),
55             ranIp: new FormControl('', [Validators.required, Validators.pattern(ipPattern)]),
56             ranPort: new FormControl('', [Validators.required, Validators.pattern(portPattern)])
57         });
58
59     }
60
61     onCancel() {
62         this.dialogRef.close();
63     }
64
65     public setupConnection = (ranFormValue) => {
66         if (this.ranDialogForm.valid) {
67             this.executeSetupConnection(ranFormValue);
68         }
69     }
70
71     private executeSetupConnection = (ranFormValue) => {
72         let httpErrRes: HttpErrorResponse;
73         const aboutError = 'RAN Connection Failed: ';
74         const setupRequest: E2SetupRequest = {
75             ranName: ranFormValue.ranName,
76             ranIp: ranFormValue.ranIp,
77             ranPort: ranFormValue.ranPort
78         };
79         if (ranFormValue.ranType === 'endc') {
80             this.service.endcSetup(setupRequest).subscribe(
81                 (response: any) => {
82                     this.notifService.success('Endc connect succeeded!');
83                     this.dialogRef.close();
84                 },
85                 (error => {
86                     httpErrRes = error;
87                     this.errorService.displayError(aboutError + httpErrRes.message);
88                 })
89             );
90         } else {
91             this.service.x2Setup(setupRequest).subscribe(
92                 (response: any) => {
93                     this.notifService.success('X2 connect succeeded!');
94                     this.dialogRef.close();
95                 },
96                 (error => {
97                     httpErrRes = error;
98                     this.errorService.displayError(aboutError + httpErrRes.message);
99                 })
100             );
101         }
102     }
103
104     public hasError(controlName: string, errorName: string) {
105         if (this.ranDialogForm.controls[controlName].hasError(errorName)) {
106           return true;
107         }
108         return false;
109     }
110
111     public validateControl(controlName: string) {
112         if (this.ranDialogForm.controls[controlName].invalid && this.ranDialogForm.controls[controlName].touched) {
113             return true;
114         }
115         return false;
116     }
117
118 } // class AppRANConnectDialog