8b429c6ef41eb2047a1625eeb04b84de8403613e
[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 and Nokia
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, OnInit, Inject } from '@angular/core';
22 import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
23 import { AppMgrService } from '../services/app-mgr/app-mgr.service';
24 import { ErrorDialogService } from '../services/ui/error-dialog.service';
25 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
26 import { finalize } from 'rxjs/operators';
27
28
29 @Component({
30   selector: 'rd-app-configuration',
31   templateUrl: './app-configuration.component.html',
32   styleUrls: ['./app-configuration.component.scss']
33 })
34 export class AppConfigurationComponent implements OnInit {
35
36   private loadingSubject = new BehaviorSubject<boolean>(false);
37   public loading$ = this.loadingSubject.asObservable();
38
39   constructor(
40     private dialogRef: MatDialogRef<AppConfigurationComponent>,
41     private appMgrService: AppMgrService,
42     private errorDiaglogService: ErrorDialogService,
43     @Inject(MAT_DIALOG_DATA) private data
44   ) { }
45
46   xappMetadata: any;
47   xappConfigSchema: any;
48   xappConfigData: any; 
49   xappLayout:any;
50   ngOnInit() {
51     this.loadingSubject.next(true);
52     this.appMgrService.getConfig()
53       .pipe(
54         finalize(() => this.loadingSubject.next(false))
55       )
56       .subscribe(
57       (allConfig: any) => {
58         this.loadConfigForm(this.data.name, allConfig)
59       }
60     )
61   }
62
63   updateconfig(event) {
64     var config = {
65       metadata: this.xappMetadata,
66       descriptor: this.xappConfigSchema,
67       config: event,
68       layout: this.xappLayout
69     }
70     this.appMgrService.putConfig(config)
71     this.dialogRef.close();
72   }
73
74   loadConfigForm(name: string, allConfig: any) {
75     var xappConfig = allConfig.find(xapp => xapp.metadata.name == name)
76     if (xappConfig != null) {
77       this.xappMetadata = xappConfig.metadata
78       this.xappConfigSchema = xappConfig.descriptor;
79       this.xappConfigData = xappConfig.config;
80       this.xappLayout= xappConfig.layout;
81     } else {
82       this.errorDiaglogService.displayError("Cannot find configration data for " + name);
83       this.dialogRef.close();
84     }
85   }
86 }