Fix PolicyControlComponent
[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    * Gets policy parameters.
88    * @returns Observable that should yield a policy instance
89    */
90   getPolicy(policyTypeId: string, policyInstanceId: string): Observable<any> {
91     const url =
92       this.buildPath(this.policyPath, policyInstanceId) +
93       "?type=" +
94       policyTypeId;
95     return this.httpClient.get<any>(url);
96   }
97
98   /**
99    * Creates or replaces policy instance.
100    * @param policyTypeId ID of the policy type that the instance will have
101    * @param policyInstanceId ID of the instance
102    * @param policyJson Json with the policy content
103    * @returns Observable that should yield a response code, no data
104    */
105   putPolicy(createPolicyInstance: CreatePolicyInstance): Observable<any> {
106     const url = this.buildPath(this.policyPath);
107     return this.httpClient.put<PolicyInstanceAck>(url, createPolicyInstance, {
108       observe: "response",
109     });
110   }
111
112   /**
113    * Deletes a policy instance.
114    * @param policyTypeId ID of the policy type that the instance belong to
115    * @param policyInstanceId ID of the instance
116    * @returns Observable that should yield a response code, no data
117    */
118   deletePolicy(policyInstanceId: string): Observable<any> {
119     const url = this.buildPath(this.policyPath, policyInstanceId);
120     return this.httpClient.delete(url, { observe: "response" });
121   }
122
123   getRics(policyTypeId: string): Observable<Rics> {
124     const url = this.buildPath("rics") + "?policytype_id=" + policyTypeId;
125     return this.httpClient.get<any>(url);
126   }
127 }