Move Enrichment Producer Logic from backend
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / ei-producer.datasource.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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 { HttpErrorResponse } from '@angular/common/http';
22 import { Injectable } from '@angular/core';
23 import { MatTableDataSource } from '@angular/material';
24
25 import { Observable } from 'rxjs/Observable';
26 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
27 import { of } from 'rxjs/observable/of';
28 import { catchError, finalize, tap } from 'rxjs/operators';
29
30 import { EIProducer } from '../interfaces/ei.types';
31 import { EIService } from '../services/ei/ei.service';
32 import { NotificationService } from '../services/ui/notification.service';
33
34 @Injectable({
35     providedIn: 'root'
36 })
37
38 export class EIProducerDataSource extends MatTableDataSource<EIProducer> {
39
40     producerSubject = new BehaviorSubject<EIProducer[]>([]);
41
42     private loadingSubject = new BehaviorSubject<boolean>(false);
43
44     public loading$ = this.loadingSubject.asObservable();
45
46     public rowCount = 1; // hide footer during intial load
47
48     constructor(
49         private eiSvc: EIService,
50         private notificationService: NotificationService) {
51         super();
52     }
53
54     loadProducers(): Observable<EIProducer[]> {
55         this.loadingSubject.next(true);
56         let producers: Array<EIProducer> = [];
57         this.eiSvc.getProducerIds()
58             .pipe(
59                 catchError((her: HttpErrorResponse) => {
60                     this.notificationService.error('Failed to get producers: ' + her.error);
61                     return of([]);
62                 }),
63                 finalize(() => this.loadingSubject.next(false))
64             )
65             .subscribe((prodIds: string[]) => {
66                 console.log("ProducerIds: " + prodIds);
67                 prodIds.forEach(id => {
68                     let eiProducer = <EIProducer>{};
69                     eiProducer.ei_producer_id = id;
70                     this.eiSvc.getProducer(id).subscribe(producer => {
71                         eiProducer.ei_producer_types = producer.supported_ei_types;
72                     });
73                     this.eiSvc.getProducerStatus(id).subscribe(prodStatus => {
74                         eiProducer.status = prodStatus.opState.toString();
75                     });
76                     this.addProducerToSubject(eiProducer);
77                     producers.push(eiProducer);
78                 });
79                 this.rowCount = this.producerSubject.value.length;
80             });
81         return of(producers);
82     }
83
84     private addProducerToSubject(producer: EIProducer) {
85         const currentValue = this.producerSubject.value;
86         const updatedValue = [...currentValue, producer];
87         this.producerSubject.next(updatedValue);
88     }
89
90     connect(): BehaviorSubject<EIProducer[]> {
91         return this.producerSubject;
92     }
93
94     disconnect(): void {
95         this.producerSubject.complete();
96         this.loadingSubject.complete();
97     }
98 }