c97a1950d7a761c09a07d2dfe178fd13edfef0b6
[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
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     if (acAppPattern0.test(app.xapp) || acAppPattern1.test(app.xapp)) {
64       this.router.navigate(['/ac']);
65     }  else {
66       this.errorDialogService.displayError('No control available for ' + app.xapp + ' (yet)');
67     }
68   }
69
70   onUndeployApp(app: XappControlRow): void {
71     this.confirmDialogService.openConfirmDialog('Are you sure you want to undeploy App ' + app.xapp + '?')
72       .afterClosed().subscribe( (res: boolean) => {
73         if (res) {
74           this.loadingDialogService.startLoading("Undeploying " + app.xapp);
75           this.appMgrSvc.undeployXapp(app.xapp)
76             .pipe(
77               finalize(() => this.loadingDialogService.stopLoading())
78             )
79             .subscribe(
80             ( httpResponse: HttpResponse<Object>) => {
81               // Answers 204/No content on success
82               this.notificationService.success('App undeployed successfully!');
83               this.dataSource.loadTable();
84             },
85             ( (her: HttpErrorResponse) => {
86               // the error field should have an ErrorTransport object
87               let msg = her.message;
88               if (her.error && her.error.message) {
89                 msg = her.error.message;
90               }
91               this.notificationService.warn('App undeploy failed: ' + msg);
92             })
93           );
94         }
95       });
96   }
97
98 }