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