update onboard workflow
[portal/ric-dashboard.git] / dashboard / webapp-frontend / src / app / onboard / onboard.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 { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
23 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
24 import { finalize } from 'rxjs/operators';
25 import { XappOnboarderService } from '../services/xapp-onboarder/xapp-onboarder.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 { FormControl, FormGroup, Validators } from '@angular/forms';
31
32 @Component({
33   selector: 'rd-onboard',
34   templateUrl: './onboard.component.html',
35   styleUrls: ['./onboard.component.scss']
36 })
37 export class OnboardComponent implements OnInit {
38
39   private loadingSubject = new BehaviorSubject<boolean>(false);
40   public loading$ = this.loadingSubject.asObservable();
41   public urlOnboardForm: FormGroup;
42
43   constructor(
44     private dialogRef: MatDialogRef<OnboardComponent>,
45     private xappOnboarderService: XappOnboarderService,
46     private errorDiaglogService: ErrorDialogService,
47     private loadingDialogService: LoadingDialogService,
48     private notificationService: NotificationService,
49     @Inject(MAT_DIALOG_DATA) private data
50   ) { }
51
52   ngOnInit(): void {
53     this.urlOnboardForm = new FormGroup({
54       configURL: new FormControl('', [Validators.required]),
55       schemaURL: new FormControl('')
56     })
57   }
58   ;
59   configFile: File;
60   controlsSchema: File;
61   descriptor = {
62     "config-file.json": {},
63   }
64   descriptor_url = {
65     "config-file.json_url": "",
66   }
67
68   uploadFromLocal() {
69     this.loadingDialogService.startLoading('Onboarding xApp');
70     this.xappOnboarderService.onboardXappFile(this.descriptor, this.data.instanceKey)
71       .pipe(
72         finalize(() => {
73           this.loadingDialogService.stopLoading();
74           this.dialogRef.close();
75         })
76       )
77       .subscribe(
78         (response: HttpResponse<Object>) => {
79           this.notificationService.success('Onboard succeeded!');
80         },
81         ((her: HttpErrorResponse) => {
82           let msg = her.message;
83           if (her.error && her.error.message) {
84             msg = her.error.message;
85           }
86           this.notificationService.warn('Onboard failed: ' + msg);
87         })
88       );
89   }
90
91   uploadFromURL(data) {
92     this.descriptor_url["config-file.json_url"] = data.configURL;
93     if (data.schemaURL) {
94       this.descriptor_url["controls-schema.json_url"] = data.schemaURL;
95     }
96     this.loadingDialogService.startLoading('Onboarding xApp');
97     this.xappOnboarderService.onboardXappURL(this.descriptor_url, this.data.instanceKey)
98       .pipe(
99         finalize(() => {
100           this.loadingDialogService.stopLoading();
101           this.dialogRef.close();
102         })
103       )
104       .subscribe(
105         (response: HttpResponse<Object>) => {
106           this.notificationService.success('Onboard succeeded!');
107         },
108         ((her: HttpErrorResponse) => {
109           let msg = her.message;
110           if (her.error && her.error.message) {
111             msg = her.error.message;
112           }
113           this.notificationService.warn('Onboard failed: ' + msg);
114         })
115       );
116   }
117
118
119   selectConfigFile(event) {
120     if (event.target.files.length) {
121       this.configFile = event.target.files[0];
122       let fileReader = new FileReader();
123       fileReader.onload = (e) => {
124         this.descriptor["config-file.json"] = JSON.parse(fileReader.result as string);
125       }
126       fileReader.readAsText(this.configFile);
127     }
128   }
129
130   selectControlsSchema(event) {
131     if (event.target.files.length) {
132       this.controlsSchema = event.target.files[0];
133       let fileReader = new FileReader();
134       fileReader.onload = (e) => {
135         this.descriptor["controls-schema.json"] = JSON.parse(fileReader.result as string);
136       }
137       fileReader.readAsText(this.controlsSchema);
138     }
139   }
140 }