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";
36 * Services for calling the policy endpoints.
41 export class PolicyService {
42 private apiVersion2 = "/v2";
43 private basePath = "/a1-policy";
44 policyTypesPath = "policy-types";
45 policyPath = "policies";
47 private buildPath(...args: any[]) {
48 let result = this.basePath + this.apiVersion2;
49 args.forEach((part) => {
50 result = result + "/" + part;
55 constructor(private httpClient: HttpClient) {
56 // injects to variable httpClient
59 getPolicyTypes(): Observable<PolicyTypes> {
60 const url = this.buildPath(this.policyTypesPath);
61 return this.httpClient.get<PolicyTypes>(url);
64 getPolicyType(policyTypeId: string): Observable<PolicyType> {
65 const url = this.buildPath(this.policyTypesPath + "/" + policyTypeId);
66 return this.httpClient.get<PolicyType>(url);
69 getPolicyInstancesByType(policyTypeId: string): Observable<PolicyInstances> {
70 const url = this.buildPath(
71 this.policyPath + "?" + "policytype_id=" + policyTypeId
73 return this.httpClient.get<PolicyInstances>(url);
76 getPolicyInstance(policyId: string): Observable<PolicyInstance> {
77 const url = this.buildPath(this.policyPath) + "/" + policyId;
78 return this.httpClient.get<PolicyInstance>(url);
81 getPolicyStatus(policyId: string): Observable<PolicyStatus> {
82 const url = this.buildPath(this.policyPath) + "/" + policyId + "/status";
83 return this.httpClient.get<PolicyStatus>(url);
87 * Gets policy parameters.
88 * @returns Observable that should yield a policy instance
90 getPolicy(policyTypeId: string, policyInstanceId: string): Observable<any> {
92 this.buildPath(this.policyPath, policyInstanceId) +
95 return this.httpClient.get<any>(url);
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
105 putPolicy(createPolicyInstance: CreatePolicyInstance): Observable<any> {
106 const url = this.buildPath(this.policyPath);
107 return this.httpClient.put<PolicyInstanceAck>(url, createPolicyInstance, {
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
118 deletePolicy(policyInstanceId: string): Observable<any> {
119 const url = this.buildPath(this.policyPath, policyInstanceId);
120 return this.httpClient.delete(url, { observe: "response" });
123 getRics(policyTypeId: string): Observable<Rics> {
124 const url = this.buildPath("rics") + "?policytype_id=" + policyTypeId;
125 return this.httpClient.get<any>(url);