7f609a621cbc61c00f984b878d38e195d9162c67
[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('', [Validators.required])
56     })
57   }
58   ;
59   configFile: File;
60   controlsSchema: File;
61   descriptor = {
62     "config-file.json": {},
63     "controls-schema.json": {}
64   }
65   descriptor_url = {
66     "config-file.json_url": "",
67     "controls-schema.json_url": ""
68   }
69
70   uploadFromLocal() {
71     this.loadingDialogService.startLoading('Onboarding xApp');
72     this.xappOnboarderService.onboardXappFile(this.descriptor, this.data.instanceKey)
73       .pipe(
74         finalize(() => {
75           this.loadingDialogService.stopLoading();
76           this.dialogRef.close();
77         })
78       )
79       .subscribe(
80         (response: HttpResponse<Object>) => {
81           this.notificationService.success('Onboard succeeded!');
82         },
83         ((her: HttpErrorResponse) => {
84           let msg = her.message;
85           if (her.error && her.error.message) {
86             msg = her.error.message;
87           }
88           this.notificationService.warn('Onboard failed: ' + msg);
89         })
90       );
91   }
92
93   uploadFromURL(data) {
94     this.descriptor_url["config-file.json_url"] = data.configURL;
95     this.descriptor_url["controls-schema.json_url"] = data.schemaURL;
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     this.configFile = event.target.files[0];
121     let fileReader = new FileReader();
122     fileReader.onload = (e) => {
123       this.descriptor["config-file.json"] = JSON.parse(fileReader.result as string);
124     }
125     fileReader.readAsText(this.configFile);
126   }
127
128   selectControlsSchema(event) {
129     this.controlsSchema = event.target.files[0];
130     let fileReader = new FileReader();
131     fileReader.onload = (e) => {
132       this.descriptor["controls-schema.json"] = JSON.parse(fileReader.result as string);
133     }
134     fileReader.readAsText(this.controlsSchema);
135
136   }
137 }