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