update configure workflow
[portal/ric-dashboard.git] / dashboard / 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 import { XMXappConfig } from "../interfaces/app-mgr.types"
31
32 @Component({
33   selector: 'rd-app-configuration',
34   templateUrl: './app-configuration.component.html',
35   styleUrls: ['./app-configuration.component.scss']
36 })
37 export class AppConfigurationComponent implements OnInit {
38
39   private loadingSubject = new BehaviorSubject<boolean>(false);
40   public loading$ = this.loadingSubject.asObservable();
41
42   constructor(
43     private dialogRef: MatDialogRef<AppConfigurationComponent>,
44     private appMgrService: AppMgrService,
45     private errorDiaglogService: ErrorDialogService,
46     private loadingDialogService: LoadingDialogService,
47     private notificationService: NotificationService,
48     @Inject(MAT_DIALOG_DATA) private data
49   ) { }
50
51   xappConfig: XMXappConfig
52
53   ngOnInit() {
54     this.loadingSubject.next(true);
55     this.appMgrService.getConfig(this.data.instanceKey)
56       .pipe(
57         finalize(() => this.loadingSubject.next(false))
58       )
59       .subscribe(
60         (allConfig: any) => {
61           this.loadConfigForm(this.data.xapp, allConfig);
62         }
63       );
64   }
65
66   updateconfig(config: any) {
67     this.loadingDialogService.startLoading('Updating ' + this.data.xapp + ' configuration');
68     this.appMgrService.putConfig(this.data.instanceKey, JSON.parse(config))
69       .pipe(
70         finalize(() => {
71           this.loadingDialogService.stopLoading();
72           this.dialogRef.close();
73         })
74       )
75       .subscribe(
76         (response: HttpResponse<Object>) => {
77           this.notificationService.success('Configuration update succeeded!');
78         },
79         ((her: HttpErrorResponse) => {
80           let msg = her.message;
81           if (her.error && her.error.message) {
82             msg = her.error.message;
83           }
84           this.notificationService.warn('Configuration update failed: ' + msg);
85         })
86       );
87   }
88
89   loadConfigForm(name: string, allConfig: any) {
90     const xappConfig = allConfig.find(xapp => xapp.metadata.name === name);
91     if (xappConfig != null) {
92       this.xappConfig = xappConfig
93     } else {
94       this.errorDiaglogService.displayError('Cannot find configration data for ' + name);
95       this.dialogRef.close();
96     }
97   }
98 }