Add error handling to HttpRequestInterceptor
[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 import { MatTableDataSource } from '@angular/material';
23
24 import { Observable } from 'rxjs/Observable';
25 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
26 import { of } from 'rxjs/observable/of';
27
28 import { EIProducer } from '../interfaces/ei.types';
29 import { EIService } from '../services/ei/ei.service';
30
31 @Injectable({
32     providedIn: 'root'
33 })
34
35 export class EIProducerDataSource extends MatTableDataSource<EIProducer> {
36
37     producerSubject = new BehaviorSubject<EIProducer[]>([]);
38
39     private loadingSubject = new BehaviorSubject<boolean>(false);
40
41     public loading$ = this.loadingSubject.asObservable();
42
43     public rowCount = 1; // hide footer during intial load
44
45     constructor(
46         private eiSvc: EIService) {
47         super();
48     }
49
50     loadProducers(): Observable<EIProducer[]> {
51         this.loadingSubject.next(true);
52         let producers: Array<EIProducer> = [];
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.addProducerToSubject(eiProducer);
66                     producers.push(eiProducer);
67                 });
68                 this.rowCount = this.producerSubject.value.length;
69             });
70         return of(producers);
71     }
72
73     private addProducerToSubject(producer: EIProducer) {
74         const currentValue = this.producerSubject.value;
75         const updatedValue = [...currentValue, producer];
76         this.producerSubject.next(updatedValue);
77     }
78
79     connect(): BehaviorSubject<EIProducer[]> {
80         return this.producerSubject;
81     }
82
83     disconnect(): void {
84         this.producerSubject.complete();
85         this.loadingSubject.complete();
86     }
87 }