Extend Xapp deploy workflow
[portal/ric-dashboard.git] / dashboard / webapp-frontend / src / app / ui / deploy-dialog / deploy-dialog.component.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 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 { FormControl, FormGroup, Validators } from '@angular/forms';
23 import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
25 import { finalize } from 'rxjs/operators';
26 import { XMXappDescriptor} from '../../interfaces/app-mgr.types';
27 import { AppMgrService } from '../../services/app-mgr/app-mgr.service';
28 import { NotificationService } from '../../services/ui/notification.service';
29 import { LoadingDialogService } from '../../services/ui/loading-dialog.service';
30 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
31
32 @Component({
33   selector: 'rd-deploy-dialog',
34   templateUrl: './deploy-dialog.component.html',
35   styleUrls: ['./deploy-dialog.component.scss']
36 })
37 export class DeployDialogComponent implements OnInit {
38
39   private loadingSubject = new BehaviorSubject<boolean>(false);
40   public loading$ = this.loadingSubject.asObservable();
41   public deployForm: FormGroup;
42   xappDescriptor: XMXappDescriptor ;
43   overrideFile: File;
44
45   constructor(
46     private dialogRef: MatDialogRef<DeployDialogComponent>,
47     private appMgrService: AppMgrService,
48     private loadingDialogService: LoadingDialogService,
49     private notificationService: NotificationService,
50     @Inject(MAT_DIALOG_DATA) private data
51   ) { }
52
53   ngOnInit(): void {
54     this.deployForm = new FormGroup({
55       xappName: new FormControl(this.data.xappName, [Validators.required]),
56       helmVersion: new FormControl(''),
57       releaseName: new FormControl(''),
58       namespace: new FormControl(''),
59       overrideFile: new FormControl({}),
60     })
61   }
62
63   selectoverrideFile(event) {
64     if (event.target.files.length) {
65       this.overrideFile = event.target.files[0];
66       let fileReader = new FileReader();
67       fileReader.onload = (e) => {
68         this.deployForm.value.overrideFile = JSON.parse(fileReader.result as string);
69       }
70       fileReader.readAsText(this.overrideFile);
71     }
72     else {
73       this.deployForm.value.overrideFile =null
74     }
75   }
76
77   deploy(xapp: XMXappDescriptor) {   
78     this.xappDescriptor = xapp
79     this.loadingDialogService.startLoading('Deploying ' + this.xappDescriptor.xappName);
80     this.appMgrService.deployXapp(this.data.instanceKey, this.xappDescriptor)
81       .pipe(
82         finalize(() => {
83           this.loadingDialogService.stopLoading();
84           this.dialogRef.close();
85         })
86       )
87       .subscribe(
88         (response: HttpResponse<Object>) => {
89           this.notificationService.success('App deploy succeeded!');
90         },
91         ((her: HttpErrorResponse) => {
92           // the error field should have an ErrorTransport object
93           let msg = her.message;
94           if (her.error && her.error.message) {
95             msg = her.error.message;
96           }
97           this.notificationService.warn('App deploy failed: ' + msg);
98         })
99     );
100   }
101 }