Reorganize dashboard into subfolders
[portal/ric-dashboard.git] / dashboard / webapp-frontend / src / app / app-control / app-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, HttpResponse } from '@angular/common/http';
21 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
22 import { MatSort } from '@angular/material/sort';
23 import { Router } from '@angular/router';
24 import { Subscription } from 'rxjs';
25 import { finalize } from 'rxjs/operators';
26 import { XappControlRow } from '../interfaces/app-mgr.types';
27 import { RicInstance } from '../interfaces/dashboard.types';
28 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
29 import { InstanceSelectorService } from '../services/instance-selector/instance-selector.service';
30 import { ConfirmDialogService } from '../services/ui/confirm-dialog.service';
31 import { ErrorDialogService } from '../services/ui/error-dialog.service';
32 import { LoadingDialogService } from '../services/ui/loading-dialog.service';
33 import { NotificationService } from '../services/ui/notification.service';
34 import { AppControlAnimations } from './app-control.animations';
35 import { AppControlDataSource } from './app-control.datasource';
36
37 @Component({
38   selector: 'rd-app-control',
39   templateUrl: './app-control.component.html',
40   styleUrls: ['./app-control.component.scss'],
41   animations: [AppControlAnimations.messageTrigger]
42 })
43 export class AppControlComponent implements OnInit, OnDestroy {
44
45   displayedColumns: string[] = ['xapp', 'name', 'status', 'ip', 'port', 'action'];
46   dataSource: AppControlDataSource;
47   @ViewChild(MatSort, { static: true }) sort: MatSort;
48   private instanceChange: Subscription;
49   private instanceKey: string;
50
51   constructor(
52     private appMgrSvc: AppMgrService,
53     private router: Router,
54     private confirmDialogService: ConfirmDialogService,
55     private errorDialogService: ErrorDialogService,
56     private loadingDialogService: LoadingDialogService,
57     public instanceSelectorService: InstanceSelectorService,
58     private notificationService: NotificationService) { }
59
60
61   ngOnInit() {
62     this.dataSource = new AppControlDataSource(this.appMgrSvc, this.sort, this.notificationService);
63     this.instanceChange = this.instanceSelectorService.getSelectedInstance().subscribe((instance: RicInstance) => {
64       if (instance.key) {
65         this.instanceKey = instance.key;
66         this.dataSource.loadTable(instance.key);
67       }
68     });
69   }
70
71   ngOnDestroy() {
72     this.instanceChange.unsubscribe();
73   }
74
75   controlApp(app: XappControlRow): void {
76     // TODO: identify apps without hardcoding to names
77     const acAppPattern0 = /[Aa][Dd][Mm][Ii][Nn]/;
78     const acAppPattern1 = /[Aa][Dd][Mm][Ii][Ss]{2}[Ii][Oo][Nn]/;
79     if (acAppPattern0.test(app.xapp) || acAppPattern1.test(app.xapp)) {
80       this.router.navigate(['/ac']);
81     } else {
82       this.errorDialogService.displayError('No control available for ' + app.xapp + ' (yet)');
83     }
84   }
85
86   onUndeployApp(app: XappControlRow): void {
87     this.confirmDialogService.openConfirmDialog('Are you sure you want to undeploy App ' + app.xapp + '?')
88       .afterClosed().subscribe((res: boolean) => {
89         if (res) {
90           this.loadingDialogService.startLoading('Undeploying ' + app.xapp);
91           this.appMgrSvc.undeployXapp(this.instanceKey, app.xapp)
92             .pipe(
93               finalize(() => this.loadingDialogService.stopLoading())
94             )
95             .subscribe(
96               (httpResponse: HttpResponse<Object>) => {
97                 // Answers 204/No content on success
98                 this.notificationService.success('App undeployed successfully!');
99                 this.dataSource.loadTable(this.instanceKey);
100               },
101               ((her: HttpErrorResponse) => {
102                 // the error field should have an ErrorTransport object
103                 let msg = her.message;
104                 if (her.error && her.error.message) {
105                   msg = her.error.message;
106                 }
107                 this.notificationService.warn('App undeploy failed: ' + msg);
108               })
109             );
110         }
111       });
112   }
113
114 }