354173c77dae9df81f86cee885f75512d0197a61
[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 { CreatePolicyInstance, PolicyInstance, PolicyInstanceAck, PolicyInstances, PolicyStatus, PolicyType, PolicyTypes } from '@interfaces/policy.types';
25 import { Rics } from '@interfaces/ric';
26
27 /**
28  * Services for calling the policy endpoints.
29  */
30 @Injectable({
31     providedIn: 'root'
32 })
33 export class PolicyService {
34
35     private apiVersion2 = '/v2'
36     private basePath = '/a1-policy';
37     policyTypesPath = 'policy-types';
38     policyPath = 'policies';
39
40     private buildPath(...args: any[]) {
41         let result = this.basePath + this.apiVersion2;
42         args.forEach(part => {
43             result = result + '/' + part;
44         });
45         return result;
46     }
47
48     constructor(private httpClient: HttpClient) {
49         // injects to variable httpClient
50     }
51
52     getPolicyTypes(): Observable<PolicyTypes> {
53         const url = this.buildPath(this.policyTypesPath);
54         return this.httpClient.get<PolicyTypes>(url);
55     }
56
57     getPolicyType(policyTypeId: string): Observable<PolicyType> {
58         const url = this.buildPath(this.policyTypesPath + '/' + policyTypeId);
59         return this.httpClient.get<PolicyType>(url);
60     }
61
62     getPolicyInstancesByType(policyTypeId: string): Observable<PolicyInstances> {
63         const url = this.buildPath(this.policyPath + '?' + 'policytype_id=' + policyTypeId);
64         return this.httpClient.get<PolicyInstances>(url);
65     }
66
67     getPolicyInstance(policyId: string): Observable<PolicyInstance> {
68         const url = this.buildPath(this.policyPath) + '/' + policyId;
69         return this.httpClient.get<PolicyInstance>(url);
70     }
71
72     getPolicyStatus(policyId: string): Observable<PolicyStatus> {
73         const url = this.buildPath(this.policyPath) + '/' + policyId + '/status';
74         return this.httpClient.get<PolicyStatus>(url);
75     }
76
77     /**
78      * Gets policy parameters.
79      * @returns Observable that should yield a policy instance
80      */
81     getPolicy(policyTypeId: string, policyInstanceId: string): Observable<any> {
82         const url = this.buildPath(this.policyPath, policyInstanceId) + '?type=' + policyTypeId;
83         return this.httpClient.get<any>(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, { observe: 'response' });
96     }
97
98     /**
99      * Deletes a policy instance.
100      * @param policyTypeId ID of the policy type that the instance belong to
101      * @param policyInstanceId ID of the instance
102      * @returns Observable that should yield a response code, no data
103      */
104     deletePolicy(policyInstanceId: string): Observable<any> {
105         const url = this.buildPath(this.policyPath, policyInstanceId);
106         return this.httpClient.delete(url, { observe: 'response' });
107     }
108
109
110     getRics(policyTypeId: string): Observable<Rics> {
111         const url = this.buildPath('rics') + '?policytype_id=' + policyTypeId;
112         return this.httpClient.get<any>(url);
113     }
114 }