2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
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';
31 export class InstanceSelectorService {
32 private selectedInstanceKey: BehaviorSubject<string> = new BehaviorSubject<string>('');
33 private instanceArray: Observable<RicInstance[]>;
36 private dashboardSvc: DashboardService,
37 private httpClient: HttpClient) {
40 getInstanceArray(): Observable<RicInstance[]> {
41 if (this.instanceArray) {
42 return this.instanceArray;
44 const path = this.dashboardSvc.buildPath('admin', null, 'instance');
45 return this.instanceArray = this.httpClient.get<RicInstance[]>(path)
47 tap(ricInstanceArray => {
48 this.initselectedInstanceKey(ricInstanceArray[0].key);
54 private initselectedInstanceKey(instanceKey: string) {
55 if (!this.selectedInstanceKey.value) {
56 this.selectedInstanceKey.next(instanceKey)
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;
67 updateSelectedInstance(instanceKey: string) {
68 this.selectedInstanceKey.next(instanceKey)