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