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