Reorganize dashboard into subfolders
[portal/ric-dashboard.git] / dashboard / 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, Inject, OnInit } from '@angular/core';
22 import { FormControl, FormGroup, Validators } from '@angular/forms';
23 import { MatDialogRef, MAT_DIALOG_DATA } 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     @Inject(MAT_DIALOG_DATA) private data) {
50     // opens with empty fields; accepts no data to display
51   }
52
53   ngOnInit() {
54     const namePattern = /^([A-Z0-9])+$/;
55     // tslint:disable-next-line:max-line-length
56     const ipPattern = /((((([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-9]|25[0-5]))$)|(^((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((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})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((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})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((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}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((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}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((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}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((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}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){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}))|:)))(%.+)?$))/;
57     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])$/;
58     this.ranDialogForm = new FormGroup({
59       ranType: new FormControl('endc'),
60       ranName: new FormControl('', [Validators.required, Validators.pattern(namePattern)]),
61       ranIp: new FormControl('', [Validators.required, Validators.pattern(ipPattern)]),
62       ranPort: new FormControl('', [Validators.required, Validators.pattern(portPattern)])
63     });
64   }
65
66   onCancel() {
67     this.dialogRef.close(false);
68   }
69
70   setupConnection = (ranFormValue: RanDialogFormData) => {
71     if (!this.ranDialogForm.valid) {
72       // should never happen
73       return;
74     }
75     this.processing = true;
76     const setupRequest: E2SetupRequest = {
77       ranName: ranFormValue.ranName.trim(),
78       ranIp: ranFormValue.ranIp.trim(),
79       ranPort: ranFormValue.ranPort.trim()
80     };
81     this.loadingDialogService.startLoading('Setting up connection');
82     let observable: Observable<HttpResponse<Object>>;
83     if (ranFormValue.ranType === 'endc') {
84       observable = this.service.endcSetup(this.data.instanceKey, setupRequest);
85     } else {
86       observable = this.service.x2Setup(this.data.instanceKey, setupRequest);
87     }
88     observable
89       .pipe(
90         finalize(() => this.loadingDialogService.stopLoading())
91       )
92       .subscribe(
93         (response: any) => {
94           this.processing = false;
95           this.notifService.success('Connect request sent!');
96           this.dialogRef.close(true);
97         },
98         ((her: HttpErrorResponse) => {
99           this.processing = false;
100           // the error field carries the server's response
101           let msg = her.message;
102           if (her.error && her.error.message) {
103             msg = her.error.message;
104           }
105           this.errorService.displayError('Connect request failed: ' + msg);
106           // keep the dialog open
107         })
108       );
109   }
110
111   hasError(controlName: string, errorName: string) {
112     if (this.ranDialogForm.controls[controlName].hasError(errorName)) {
113       return true;
114     }
115     return false;
116   }
117
118   validateControl(controlName: string) {
119     if (this.ranDialogForm.controls[controlName].invalid && this.ranDialogForm.controls[controlName].touched) {
120       return true;
121     }
122     return false;
123   }
124
125 }