Fix problem with double load of EI tables
[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
25 import { EIProducer } from '../interfaces/ei.types';
26 import { EIService } from '../services/ei/ei.service';
27
28 @Injectable({
29     providedIn: 'root'
30 })
31
32 export class EIProducerDataSource {
33
34     private producers: Array<EIProducer> = [];
35
36     public eiProducers(): EIProducer[] {
37         return this.producers;
38     }
39
40     private loadingSubject = new BehaviorSubject<boolean>(false);
41
42     public loading$ = this.loadingSubject.asObservable();
43
44     public rowCount = 1; // hide footer during intial load
45
46     constructor(
47         private eiSvc: EIService) {
48     }
49
50     loadProducers() {
51         this.loadingSubject.next(true);
52         this.producers = [];
53         this.eiSvc.getProducerIds()
54             .subscribe((prodIds: string[]) => {
55                 console.log("ProducerIds: " + prodIds);
56                 prodIds.forEach(id => {
57                     let eiProducer = <EIProducer>{};
58                     eiProducer.ei_producer_id = id;
59                     this.eiSvc.getProducer(id).subscribe(producer => {
60                         eiProducer.ei_producer_types = producer.supported_ei_types;
61                     });
62                     this.eiSvc.getProducerStatus(id).subscribe(prodStatus => {
63                         eiProducer.status = prodStatus.opState.toString();
64                     });
65                     this.producers.push(eiProducer);
66                 });
67                 this.rowCount = this.producers.length;
68             });
69     }
70 }