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