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