Add multi-layer RIC instance selector
[portal/ric-dashboard.git] / webapp-frontend / src / app / services / instance-selector / instance-selector.service.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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 { HttpClient } from '@angular/common/http';
22 import { Injectable } from '@angular/core';
23 import { BehaviorSubject, Observable } from 'rxjs';
24 import { shareReplay, tap } from 'rxjs/operators';
25 import { RicInstance, RicRegion } from '../../interfaces/dashboard.types';
26 import { DashboardService } from '../dashboard/dashboard.service';
27
28 @Injectable({
29   providedIn: 'root'
30 })
31 export class InstanceSelectorService {
32
33   private allInstances: Observable<RicRegion[]>;
34   private selectedInstance: BehaviorSubject<RicInstance> = new BehaviorSubject<RicInstance>({ key: '', name: '' });
35
36   constructor(
37     private dashboardSvc: DashboardService,
38     private httpClient: HttpClient) {
39   }
40
41   getAllInstances(): Observable<RicRegion[]> {
42     if (this.allInstances) {
43       return this.allInstances;
44     }
45     const path = this.dashboardSvc.buildPath('admin', null, 'instance');
46     return this.allInstances = this.httpClient.get<RicRegion[]>(path)
47       .pipe(
48         shareReplay(1)
49       );
50   }
51
52   // This method may return the BehaviorSubject with empty value
53   // Afther subscribe that BehaviorSubject
54   // Please make sure this BehaviorSubject has non empty value
55   getSelectedInstance(): BehaviorSubject<RicInstance> {
56     return this.selectedInstance;
57   }
58
59   updateSelectedInstance(instance: RicInstance) {
60     this.selectedInstance.next(instance);
61   }
62
63 }