support-multiple-ric-instances
[portal/ric-dashboard.git] / webapp-frontend / src / app / ran-control / ran-control.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 } from '@angular/common/http';
21 import { Component, OnInit } from '@angular/core';
22 import { MatDialog } from '@angular/material/dialog';
23 import { Subscription } from 'rxjs';
24 import { finalize } from 'rxjs/operators';
25 import { E2ManagerService } from '../services/e2-mgr/e2-mgr.service';
26 import { InstanceSelectorService } from '../services/instance-selector/instance-selector.service';
27 import { ConfirmDialogService } from '../services/ui/confirm-dialog.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 import { UiService } from '../services/ui/ui.service';
32 import { RanControlConnectDialogComponent } from './ran-connection-dialog.component';
33 import { RANControlDataSource } from './ran-control.datasource';
34
35 @Component({
36   selector: 'rd-ran-control',
37   templateUrl: './ran-control.component.html',
38   styleUrls: ['./ran-control.component.scss']
39 })
40 export class RanControlComponent implements OnInit {
41
42   darkMode: boolean;
43   panelClass: string = "";
44   displayedColumns: string[] = ['nbId', 'nodeType', 'ranName', 'ranIp', 'ranPort', 'connectionStatus'];
45   dataSource: RANControlDataSource;
46   private instanceChange: Subscription;
47   private instanceKey: string;
48
49   constructor(private e2MgrSvc: E2ManagerService,
50     private errorDialogService: ErrorDialogService,
51     private confirmDialogService: ConfirmDialogService,
52     private notificationService: NotificationService,
53     private loadingDialogService: LoadingDialogService,
54     public instanceSelectorService: InstanceSelectorService,
55     public dialog: MatDialog,
56     public ui: UiService) { }
57
58   ngOnInit() {
59     this.dataSource = new RANControlDataSource(this.e2MgrSvc, this.notificationService);
60
61     this.ui.darkModeState.subscribe((isDark) => {
62       this.darkMode = isDark;
63     });
64
65     this.instanceChange = this.instanceSelectorService.getSelectedInstancekey().subscribe((instanceKey: string) => {
66       if (instanceKey) {
67         this.instanceKey = instanceKey;
68         this.dataSource.loadTable(instanceKey);
69       }
70     })
71   }
72
73   ngOnDestroy() {
74     this.instanceChange.unsubscribe();
75   }
76
77   setupRANConnection() {
78     if (this.darkMode) {
79       this.panelClass = "dark-theme";
80     } else {
81       this.panelClass = "";
82     }
83     const dialogRef = this.dialog.open(RanControlConnectDialogComponent, {
84       panelClass: this.panelClass,
85       width: '450px',
86       data: {
87         instanceKey: this.instanceKey
88       }
89     });
90     dialogRef.afterClosed()
91       .subscribe((result: boolean) => {
92         if (result) {
93           this.dataSource.loadTable(this.instanceKey);
94         }
95       });
96   }
97
98   disconnectAllRANConnections() {
99     const aboutError = 'Disconnect all RAN Connections Failed: ';
100     this.confirmDialogService.openConfirmDialog('Are you sure you want to disconnect all RAN connections?')
101       .afterClosed().subscribe((res: boolean) => {
102         if (res) {
103           this.loadingDialogService.startLoading("Disconnecting");
104           this.e2MgrSvc.nodebPut(this.instanceKey)
105             .pipe(
106               finalize(() => this.loadingDialogService.stopLoading())
107             )
108             .subscribe(
109               (body: any) => {
110                 this.notificationService.success('Disconnect succeeded!');
111                 this.dataSource.loadTable(this.instanceKey);
112               },
113               (her: HttpErrorResponse) => {
114                 // the error field should have an ErrorTransport object
115                 let msg = her.message;
116                 if (her.error && her.error.message) {
117                   msg = her.error.message;
118                 }
119                 this.errorDialogService.displayError('Disconnect failed: ' + msg);
120               }
121             );
122         }
123       });
124   }
125
126 }