support-multiple-ric-instances
[portal/ric-dashboard.git] / webapp-frontend / src / app / app-configuration / app-configuration.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
21 import { Component, Inject, OnInit } from '@angular/core';
22 import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
23 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
24 import { finalize } from 'rxjs/operators';
25 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
26 import { ErrorDialogService } from '../services/ui/error-dialog.service';
27 import { LoadingDialogService } from '../services/ui/loading-dialog.service';
28 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
29 import { NotificationService } from './../services/ui/notification.service';
30
31 @Component({
32   selector: 'rd-app-configuration',
33   templateUrl: './app-configuration.component.html',
34   styleUrls: ['./app-configuration.component.scss']
35 })
36 export class AppConfigurationComponent implements OnInit {
37
38   private loadingSubject = new BehaviorSubject<boolean>(false);
39   public loading$ = this.loadingSubject.asObservable();
40
41   constructor(
42     private dialogRef: MatDialogRef<AppConfigurationComponent>,
43     private appMgrService: AppMgrService,
44     private errorDiaglogService: ErrorDialogService,
45     private loadingDialogService: LoadingDialogService,
46     private notificationService: NotificationService,
47     @Inject(MAT_DIALOG_DATA) private data
48   ) { }
49
50   xappMetadata: any;
51   xappConfigSchema: any;
52   xappConfigData: any;
53   xappLayout: any;
54   ngOnInit() {
55     this.loadingSubject.next(true);
56     this.appMgrService.getConfig(this.data.instanceKey)
57       .pipe(
58         finalize(() => this.loadingSubject.next(false))
59       )
60       .subscribe(
61         (allConfig: any) => {
62           this.loadConfigForm(this.data.xapp.name, allConfig)
63         }
64       );
65   }
66
67   updateconfig(event) {
68     var config = {
69       metadata: this.xappMetadata,
70       descriptor: this.xappConfigSchema,
71       config: event,
72       layout: this.xappLayout
73     }
74     this.loadingDialogService.startLoading("Updating " + this.data.xapp.name + " configuration");
75     this.appMgrService.putConfig(this.data.instanceKey, config)
76       .pipe(
77         finalize(() => {
78           this.loadingDialogService.stopLoading();
79           this.dialogRef.close();
80         })
81       )
82       .subscribe(
83         (response: HttpResponse<Object>) => {
84           this.notificationService.success('Configuration update succeeded!');
85         },
86         ((her: HttpErrorResponse) => {
87           let msg = her.message;
88           if (her.error && her.error.message) {
89             msg = her.error.message;
90           }
91           this.notificationService.warn('Configuration update failed: ' + msg);
92         })
93       );
94   }
95
96   loadConfigForm(name: string, allConfig: any) {
97     var xappConfig = allConfig.find(xapp => xapp.metadata.name == name)
98     if (xappConfig != null) {
99       this.xappMetadata = xappConfig.metadata
100       this.xappConfigSchema = xappConfig.descriptor;
101       this.xappConfigData = xappConfig.config;
102       this.xappLayout = xappConfig.layout;
103     } else {
104       this.errorDiaglogService.displayError("Cannot find configration data for " + name);
105       this.dialogRef.close();
106     }
107   }
108 }