Provide test coverage of PolicyService
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / services / policy / policy.service.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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 { Injectable } from "@angular/core";
22 import { HttpClient } from "@angular/common/http";
23 import { Observable } from "rxjs";
24 import {
25   CreatePolicyInstance,
26   PolicyInstance,
27   PolicyInstanceAck,
28   PolicyInstances,
29   PolicyStatus,
30   PolicyType,
31   PolicyTypes,
32 } from "@interfaces/policy.types";
33 import { Rics } from "@interfaces/ric";
34
35 /**
36  * Services for calling the policy endpoints.
37  */
38 @Injectable({
39   providedIn: "root",
40 })
41 export class PolicyService {
42   private apiVersion2 = "/v2";
43   private basePath = "/a1-policy";
44   policyTypesPath = "policy-types";
45   policyPath = "policies";
46
47   private buildPath(...args: any[]) {
48     let result = this.basePath + this.apiVersion2;
49     args.forEach((part) => {
50       result = result + "/" + part;
51     });
52     return result;
53   }
54
55   constructor(private httpClient: HttpClient) {
56     // injects to variable httpClient
57   }
58
59   getPolicyTypes(): Observable<PolicyTypes> {
60     const url = this.buildPath(this.policyTypesPath);
61     return this.httpClient.get<PolicyTypes>(url);
62   }
63
64   getPolicyType(policyTypeId: string): Observable<PolicyType> {
65     const url = this.buildPath(this.policyTypesPath + "/" + policyTypeId);
66     return this.httpClient.get<PolicyType>(url);
67   }
68
69   getPolicyInstancesByType(policyTypeId: string): Observable<PolicyInstances> {
70     const url = this.buildPath(
71       this.policyPath + "?" + "policytype_id=" + policyTypeId
72     );
73     return this.httpClient.get<PolicyInstances>(url);
74   }
75
76   getPolicyInstance(policyId: string): Observable<PolicyInstance> {
77     const url = this.buildPath(this.policyPath) + "/" + policyId;
78     return this.httpClient.get<PolicyInstance>(url);
79   }
80
81   getPolicyStatus(policyId: string): Observable<PolicyStatus> {
82     const url = this.buildPath(this.policyPath) + "/" + policyId + "/status";
83     return this.httpClient.get<PolicyStatus>(url);
84   }
85
86   /**
87    * Creates or replaces policy instance.
88    * @param policyTypeId ID of the policy type that the instance will have
89    * @param policyInstanceId ID of the instance
90    * @param policyJson Json with the policy content
91    * @returns Observable that should yield a response code, no data
92    */
93   putPolicy(createPolicyInstance: CreatePolicyInstance): Observable<any> {
94     const url = this.buildPath(this.policyPath);
95     return this.httpClient.put<PolicyInstanceAck>(url, createPolicyInstance, {
96       observe: "response",
97     });
98   }
99
100   /**
101    * Deletes a policy instance.
102    * @param policyTypeId ID of the policy type that the instance belong to
103    * @param policyInstanceId ID of the instance
104    * @returns Observable that should yield a response code, no data
105    */
106   deletePolicy(policyInstanceId: string): Observable<any> {
107     const url = this.buildPath(this.policyPath, policyInstanceId);
108     return this.httpClient.delete(url, { observe: "response" });
109   }
110
111   getRics(policyTypeId: string): Observable<Rics> {
112     const url = this.buildPath("rics") + "?policytype_id=" + policyTypeId;
113     return this.httpClient.get<any>(url);
114   }
115 }