Reorganize dashboard into subfolders
[portal/ric-dashboard.git] / dashboard / webapp-frontend / src / app / services / dashboard / dashboard.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 import { HttpClient } from '@angular/common/http';
21 import { Injectable } from '@angular/core';
22 import { Observable } from 'rxjs';
23 import { DashboardSuccessTransport, EcompUser } from '../../interfaces/dashboard.types';
24
25 @Injectable({
26   providedIn: 'root'
27 })
28
29 /**
30  * Services to query the dashboard's admin endpoints.
31  */
32 export class DashboardService {
33
34   private adminPath = 'admin';
35
36   constructor(private httpClient: HttpClient) {
37     // injects to variable httpClient
38   }
39
40   /**
41    * Builds the path for a controller method (including arguments) to use as the
42    * first argument to a HTTP client method.
43    * This function encapsulates the API prefix and RIC instance constants.
44    * @param component Controller method prefix; e.g., "admin"
45    * @param instanceKey RIC instance key; e.g., "i1" (optional).
46    * If null or empty, adds no RIC instance path components.
47    * @param args List of method path components, argument keys and values
48    * @returns Path string; e.g., "api/admin/method2/arg1/foo"
49    */
50   buildPath(component: string, instanceKey: string, ...args: any[]) {
51     let result = 'api/' + component;
52     if (instanceKey) {
53       result = result + '/ric/' + instanceKey;
54     }
55     args.forEach(part => {
56       result = result + '/' + part;
57     });
58     return result;
59   }
60
61   /**
62     * Checks app health
63     * @returns Observable that yields a DashboardSuccessTransport
64     */
65   getHealth(): Observable<DashboardSuccessTransport> {
66     const path = this.buildPath(this.adminPath, null, 'health');
67     return this.httpClient.get<DashboardSuccessTransport>(path);
68   }
69
70   /**
71    * Gets Dashboard version details
72    * @returns Observable that yields a DashboardSuccessTransport object
73    */
74   getVersion(): Observable<DashboardSuccessTransport> {
75     const path = this.buildPath(this.adminPath, null, 'version');
76     return this.httpClient.get<DashboardSuccessTransport>(path);
77   }
78
79   /**
80    * Gets Dashboard users
81    * @returns Observable that yields an EcompUser array
82    */
83   getUsers(): Observable<EcompUser[]> {
84     const path = this.buildPath(this.adminPath, null, 'user');
85     return this.httpClient.get<EcompUser[]>(path);
86   }
87
88 }