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