Create typed policy editor component
[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 { Injectable } from '@angular/core';
22
23 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
24 import { mergeMap, finalize } from 'rxjs/operators';
25 import { Observable, forkJoin, of } from 'rxjs';
26
27 import { EIProducer } from '../interfaces/ei.types';
28 import { EIService } from '../services/ei/ei.service';
29
30 @Injectable({
31     providedIn: 'root'
32 })
33
34 export class EIProducerDataSource {
35
36     private producers: Array<EIProducer> = [];
37
38     public eiProducers(): EIProducer[] {
39         return this.producers;
40     }
41
42     public eiProducersSubject(): Observable<EIProducer[]> {
43         return this.producersSubject.asObservable() as Observable<EIProducer[]>;
44     }
45
46     private loadingSubject = new BehaviorSubject<boolean>(false);
47     private producersSubject = new BehaviorSubject<EIProducer[]>([]);
48
49     public loading$ = this.loadingSubject.asObservable();
50
51     public rowCount = 1; // hide footer during intial load
52
53     constructor(
54         private eiSvc: EIService) {
55     }
56
57     loadProducers() {
58         this.loadingSubject.next(true);
59         this.producers = [];
60
61         this.eiSvc.getProducerIds().pipe(
62             mergeMap(prodIds =>
63                 forkJoin(prodIds.map(id => {
64                     return forkJoin([
65                         of(id),
66                         this.eiSvc.getProducer(id),
67                         this.eiSvc.getProducerStatus(id)
68                     ])
69                 })
70                 )),
71             finalize(() => this.loadingSubject.next(false))
72         ).subscribe(result => {
73             this.producers = result.map(producer => {
74                 let eiProducer = <EIProducer>{};
75                 eiProducer.ei_producer_id = producer[0];
76                 eiProducer.ei_producer_types = producer[1].supported_ei_types;
77                 eiProducer.status = producer[2].operational_state.toString();
78                 return eiProducer;
79             });
80             this.producersSubject.next(this.producers);
81         });
82         this.rowCount = this.producers.length;
83     }
84 }