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