Update pattern to match ANR app by name
[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 { Component, OnInit, ViewChild } from '@angular/core';
21 import { MatSort } from '@angular/material/sort';
22 import { Router } from '@angular/router';
23 import { XappControlRow } from '../interfaces/app-mgr.types';
24 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
25 import { ConfirmDialogService } from '../services/ui/confirm-dialog.service';
26 import { ErrorDialogService } from '../services/ui/error-dialog.service';
27 import { NotificationService } from '../services/ui/notification.service';
28 import { AppControlAnimations } from './app-control.animations';
29 import { AppControlDataSource } from './app-control.datasource';
30
31 @Component({
32   selector: 'control-app-control',
33   templateUrl: './app-control.component.html',
34   styleUrls: ['./app-control.component.css'],
35   animations: [AppControlAnimations.messageTrigger]
36 })
37 export class AppControlComponent implements OnInit {
38
39   displayedColumns: string[] = ['xapp', 'name', 'status', 'ip', 'port', 'action'];
40   dataSource: AppControlDataSource;
41   @ViewChild(MatSort) sort: MatSort;
42
43   constructor(
44     private appMgrSvc: AppMgrService,
45     private router: Router,
46     private confirmDialogService: ConfirmDialogService,
47     private errorDialogService: ErrorDialogService,
48     private notification: NotificationService) { }
49
50   ngOnInit() {
51     this.dataSource = new AppControlDataSource(this.appMgrSvc, this.sort);
52     this.dataSource.loadTable();
53   }
54
55   controlApp(app: XappControlRow): void {
56     // TODO: identify apps without hardcoding to names
57     const acAppPattern =  /[Aa][Dd][Mm][Ii][Ss]{2}[Ii][Oo][Nn]/;
58     const anrAppPattern0 = /ANR/;
59     const anrAppPattern1 = /[Aa][Uu][Tt][Oo][Mm][Aa][Tt][Ii][Cc]/;
60     const anrAppPattern2 = /[Nn][Ee][Ii][Gg][Hh][Bb][Oo][Rr]/;
61     if (acAppPattern.test(app.xapp)) {
62       this.router.navigate(['/ac']);
63     } else if (anrAppPattern0.test(app.xapp) || (anrAppPattern1.test(app.xapp) && anrAppPattern2.test(app.xapp))) {
64       this.router.navigate(['/anr']);
65     } else {
66       this.errorDialogService.displayError('No control available for ' + app.xapp + ' (yet)');
67     }
68   }
69
70   undeployApp(app: XappControlRow): void {
71     this.confirmDialogService.openConfirmDialog('Are you sure you want to undeploy xApp ' + app.xapp + '?')
72       .afterClosed().subscribe(res => {
73         if (res) {
74           this.appMgrSvc.undeployXapp(app.xapp).subscribe(
75             response => {
76               this.dataSource.loadTable();
77               switch (response.status) {
78                 case 200:
79                   this.notification.success('xApp undeployed successfully!');
80                   break;
81                 default:
82                   this.notification.warn('xApp undeploy failed.');
83               }
84             }
85           );
86         }
87       });
88   }
89
90 }