07e0e020e9cd429f5c7b4b34efaf5b62be40a6e4
[portal/ric-dashboard.git] / webapp-frontend / src / app / catalog / catalog.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 { MatDialog } from '@angular/material/dialog';
23 import { MatSort } from '@angular/material/sort';
24 import { finalize } from 'rxjs/operators';
25 import { XMDeployableApp } from '../interfaces/app-mgr.types';
26 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
27 import { ErrorDialogService } from '../services/ui/error-dialog.service';
28 import { LoadingDialogService } from '../services/ui/loading-dialog.service';
29 import { AppConfigurationComponent } from './../app-configuration/app-configuration.component';
30 import { ConfirmDialogService } from './../services/ui/confirm-dialog.service';
31 import { NotificationService } from './../services/ui/notification.service';
32 import { CatalogDataSource } from './catalog.datasource';
33 import { UiService } from '../services/ui/ui.service';
34
35 @Component({
36   selector: 'rd-app-catalog',
37   templateUrl: './catalog.component.html',
38   styleUrls: ['./catalog.component.scss'],
39 })
40 export class CatalogComponent implements OnInit {
41
42   darkMode: boolean;
43   panelClass: string = "";
44   displayedColumns: string[] = ['name', 'version', 'action'];
45   dataSource: CatalogDataSource;
46   @ViewChild(MatSort, { static: true }) sort: MatSort;
47
48   constructor(
49     private appMgrService: AppMgrService,
50     private confirmDialogService: ConfirmDialogService,
51     private dialog: MatDialog,
52     private errorDiaglogService: ErrorDialogService,
53     private loadingDialogService: LoadingDialogService,
54     private notificationService: NotificationService,
55     public ui: UiService) { }
56
57   ngOnInit() {
58     this.dataSource = new CatalogDataSource(this.appMgrService, this.sort, this.notificationService);
59     this.dataSource.loadTable();
60     this.ui.darkModeState.subscribe((isDark) => {
61       this.darkMode = isDark;
62     });
63   }
64
65   onConfigureApp(xapp: XMDeployableApp): void {
66     if (this.darkMode) {
67       this.panelClass = "dark-theme";
68     } else {
69       this.panelClass = "";
70     }
71     const dialogRef = this.dialog.open(AppConfigurationComponent, {
72       panelClass: this.panelClass,
73       width: '40%',
74       maxHeight: '500px',
75       position: {
76         top: '10%'
77       },
78       data: xapp
79     })
80   }
81
82   onDeployApp(app: XMDeployableApp): void {
83     this.confirmDialogService.openConfirmDialog('Deploy application ' + app.name + '?')
84       .afterClosed().subscribe((res: boolean) => {
85         if (res) {
86           this.loadingDialogService.startLoading('Deploying ' + app.name);
87           this.appMgrService.deployXapp(app.name)
88             .pipe(
89               finalize(() => this.loadingDialogService.stopLoading())
90             )
91             .subscribe(
92               (response: HttpResponse<Object>) => {
93                 this.notificationService.success('App deploy succeeded!');
94               },
95               ((her: HttpErrorResponse) => {
96                 // the error field should have an ErrorTransport object
97                 let msg = her.message;
98                 if (her.error && her.error.message) {
99                   msg = her.error.message;
100                 }
101                 this.notificationService.warn('App deploy failed: ' + msg);
102               })
103             );
104         }
105       }
106       );
107   }
108
109 }