Revise front-end buildPath support
[portal/ric-dashboard.git] / webapp-frontend / src / app / services / ac-xapp / ac-xapp.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 { Observable } from 'rxjs';
24 import { map } from 'rxjs/operators';
25 import { ACAdmissionIntervalControl, ACAdmissionIntervalControlAck } from '../../interfaces/ac-xapp.types';
26 import { DashboardSuccessTransport } from '../../interfaces/dashboard.types';
27 import { DashboardService } from '../dashboard/dashboard.service';
28
29 /**
30  * Services for calling the Dashboard's A1 endpoints to get/put AC policies.
31  */
32 @Injectable({
33   providedIn: 'root'
34 })
35 export class ACXappService {
36
37   private component = 'a1-p';
38   private policyPath = 'policies';
39   private acPolicyName = 'admission_control_policy';
40
41   constructor(
42     private dashboardSvc: DashboardService,
43     private httpClient: HttpClient) {
44   }
45
46   /**
47    * Gets AC client version details
48    * @returns Observable that yields a String
49    */
50   getVersion(instanceKey: string): Observable<string> {
51     const path = this.dashboardSvc.buildPath(this.component, null, 'version');
52     return this.httpClient.get<DashboardSuccessTransport>(path).pipe(
53       // Extract the string here
54       map(res => res['data'])
55     );
56   }
57
58   /**
59    * Gets admission control policy.
60    * @returns Observable that yields an ACAdmissionIntervalControl
61    */
62   getPolicy(instanceKey: string): Observable<ACAdmissionIntervalControl> {
63     const path = this.dashboardSvc.buildPath(this.component, instanceKey, this.policyPath, this.acPolicyName);
64     return this.httpClient.get<ACAdmissionIntervalControl>(path);
65   }
66
67   /**
68    * Puts admission control policy.
69    * @param policy an instance of ACAdmissionIntervalControl
70    * @returns Observable that yields a response code, no data
71    */
72   putPolicy(instanceKey: string, policy: ACAdmissionIntervalControl): Observable<any> {
73     const path = this.dashboardSvc.buildPath(this.component, instanceKey, this.policyPath, this.acPolicyName);
74     return this.httpClient.put<ACAdmissionIntervalControlAck>(path, policy, { observe: 'response' });
75   }
76
77 }