ad8712161f13a7dbc2144c6904079c90386c09f8
[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
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 { HttpErrorResponse, HttpResponse } from '@angular/common/http';
21 import { Component, OnInit } from '@angular/core';
22 import { FormControl, FormGroup, Validators } from '@angular/forms';
23 import { MatDialogRef } from '@angular/material/dialog';
24 import { Observable } from 'rxjs';
25 import { finalize } from 'rxjs/operators';
26 import { E2SetupRequest, RanDialogFormData } from '../interfaces/e2-mgr.types';
27 import { E2ManagerService } from '../services/e2-mgr/e2-mgr.service';
28 import { ErrorDialogService } from '../services/ui/error-dialog.service';
29 import { LoadingDialogService } from '../services/ui/loading-dialog.service';
30 import { NotificationService } from '../services/ui/notification.service';
31
32 @Component({
33   selector: 'rd-ran-control-connect-dialog',
34   templateUrl: './ran-connection-dialog.component.html',
35   styleUrls: ['./ran-connection-dialog.component.scss']
36 })
37
38 export class RanControlConnectDialogComponent implements OnInit {
39
40   public ranDialogForm: FormGroup;
41   public processing = false;
42
43   constructor(
44     private dialogRef: MatDialogRef<RanControlConnectDialogComponent>,
45     private service: E2ManagerService,
46     private errorService: ErrorDialogService,
47     private loadingDialogService: LoadingDialogService,
48     private notifService: NotificationService) {
49     // opens with empty fields; accepts no data to display
50   }
51
52   ngOnInit() {
53     const namePattern = /^([A-Z0-9])+$/;
54     // tslint:disable-next-line:max-line-length
55     const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0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?$))/;
56     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])$/;
57     this.ranDialogForm = new FormGroup({
58       ranType: new FormControl('endc'),
59       ranName: new FormControl('', [Validators.required, Validators.pattern(namePattern)]),
60       ranIp: new FormControl('', [Validators.required, Validators.pattern(ipPattern)]),
61       ranPort: new FormControl('', [Validators.required, Validators.pattern(portPattern)])
62     });
63   }
64
65   onCancel() {
66     this.dialogRef.close(false);
67   }
68
69   setupConnection = (ranFormValue: RanDialogFormData) => {
70     if (!this.ranDialogForm.valid) {
71       // should never happen
72       return;
73     }
74     this.processing = true;
75     const setupRequest: E2SetupRequest = {
76       ranName: ranFormValue.ranName.trim(),
77       ranIp: ranFormValue.ranIp.trim(),
78       ranPort: ranFormValue.ranPort.trim()
79     };
80     this.loadingDialogService.startLoading('Setting up connection');
81     let observable: Observable<HttpResponse<Object>>;
82     if (ranFormValue.ranType === 'endc') {
83       observable = this.service.endcSetup(setupRequest);
84     } else {
85       observable = this.service.x2Setup(setupRequest);
86     }
87     observable
88       .pipe(
89         finalize(() => this.loadingDialogService.stopLoading())
90       )
91       .subscribe(
92         (response: any) => {
93           this.processing = false;
94           this.notifService.success('Connect request sent!');
95           this.dialogRef.close(true);
96         },
97         ((her: HttpErrorResponse) => {
98           this.processing = false;
99           // the error field carries the server's response
100           let msg = her.message;
101           if (her.error && her.error.message) {
102             msg = her.error.message;
103           }
104           this.errorService.displayError('Connect request failed: ' + msg);
105           // keep the dialog open
106         })
107       );
108   }
109
110   hasError(controlName: string, errorName: string) {
111     if (this.ranDialogForm.controls[controlName].hasError(errorName)) {
112       return true;
113     }
114     return false;
115   }
116
117   validateControl(controlName: string) {
118     if (this.ranDialogForm.controls[controlName].invalid && this.ranDialogForm.controls[controlName].touched) {
119       return true;
120     }
121     return false;
122   }
123
124 }