2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 import { Injectable } from "@angular/core";
22 import { HttpClient } from "@angular/common/http";
23 import { Observable } from "rxjs";
32 } from "@interfaces/policy.types";
33 import { Rics } from "@interfaces/ric";
34 import { RicConfig } from "@app/interfaces/ric.config";
35 import { HttpHeaders } from "@angular/common/http";
38 * Services for calling the policy endpoints.
43 export class PolicyService {
44 private apiVersion2 = "/v2";
45 private basePath = "/a1-policy";
46 policyTypesPath = "policy-types";
47 policyPath = "policies";
49 private buildPath(...args: any[]) {
50 let result = this.basePath + this.apiVersion2;
51 args.forEach((part) => {
52 result = result + "/" + part;
57 constructor(private httpClient: HttpClient) {
58 // injects to variable httpClient
61 getPolicyTypes(): Observable<PolicyTypes> {
62 const url = this.buildPath(this.policyTypesPath);
63 return this.httpClient.get<PolicyTypes>(url);
66 getPolicyType(policyTypeId: string): Observable<PolicyType> {
67 const url = this.buildPath(this.policyTypesPath + "/" + policyTypeId);
68 return this.httpClient.get<PolicyType>(url);
71 getPolicyInstancesByType(policyTypeId: string): Observable<PolicyInstances> {
72 const url = this.buildPath(
73 this.policyPath + "?" + "policytype_id=" + policyTypeId
75 return this.httpClient.get<PolicyInstances>(url);
78 getPolicyInstance(policyId: string): Observable<PolicyInstance> {
79 const url = this.buildPath(this.policyPath) + "/" + policyId;
80 return this.httpClient.get<PolicyInstance>(url);
83 getPolicyStatus(policyId: string): Observable<PolicyStatus> {
84 const url = this.buildPath(this.policyPath) + "/" + policyId + "/status";
85 return this.httpClient.get<PolicyStatus>(url);
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
95 putPolicy(createPolicyInstance: CreatePolicyInstance): Observable<any> {
96 const url = this.buildPath(this.policyPath);
97 return this.httpClient.put<PolicyInstanceAck>(url, createPolicyInstance, {
103 * Deletes a policy instance.
104 * @param policyTypeId ID of the policy type that the instance belong to
105 * @param policyInstanceId ID of the instance
106 * @returns Observable that should yield a response code, no data
108 deletePolicy(policyInstanceId: string): Observable<any> {
109 const url = this.buildPath(this.policyPath, policyInstanceId);
110 return this.httpClient.delete(url, { observe: "response" });
113 getRics(policyTypeId: string): Observable<Rics> {
114 const url = this.buildPath("rics") + "?policytype_id=" + policyTypeId;
115 return this.httpClient.get<any>(url);
118 getConfiguration(): Observable<RicConfig> {
119 const url = this.buildPath("configuration");
120 return this.httpClient.get<RicConfig>(url);
123 updateConfiguration(ricConfig: RicConfig): Observable<RicConfig> {
124 const httpOptions = {
125 headers: new HttpHeaders({'Content-Type': 'application/json'}),
126 observe: 'response' as 'body'
128 const url = this.buildPath("configuration");
129 return this.httpClient.put<RicConfig>(url, ricConfig, httpOptions);