Revise front-end buildPath support
[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 } from '../../interfaces/dashboard.types';
26 import { DashboardService } from '../dashboard/dashboard.service';
27
28 @Injectable({
29   providedIn: 'root'
30 })
31 export class InstanceSelectorService {
32   private selectedInstanceKey: BehaviorSubject<string> = new BehaviorSubject<string>('');
33   private instanceArray: Observable<RicInstance[]>;
34
35   constructor(
36     private dashboardSvc: DashboardService,
37     private httpClient: HttpClient) {
38   }
39
40   getInstanceArray(): Observable<RicInstance[]> {
41     if (this.instanceArray) {
42       return this.instanceArray;
43     }
44     const path = this.dashboardSvc.buildPath('admin', null, 'instance');
45     return this.instanceArray = this.httpClient.get<RicInstance[]>(path)
46       .pipe(
47         tap(ricInstanceArray => {
48           this.initselectedInstanceKey(ricInstanceArray[0].key);
49         }),
50         shareReplay(1)
51       );
52   }
53
54   private initselectedInstanceKey(instanceKey: string) {
55     if (!this.selectedInstanceKey.value) {
56       this.selectedInstanceKey.next(instanceKey)
57     }
58   }
59
60   // This method may return the BehaviorSubject with empty string
61   // Afther subscribe that BehaviorSubject
62   // Please make sure this BehaviorSubject has non empty value
63   getSelectedInstancekey(): BehaviorSubject<string> {
64     return this.selectedInstanceKey;
65   }
66
67   updateSelectedInstance(instanceKey: string) {
68     this.selectedInstanceKey.next(instanceKey)
69   }
70
71 }