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