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';
24 import { CreatePolicyInstance, PolicyInstance, PolicyInstanceAck, PolicyInstances, PolicyStatus, PolicyType, PolicyTypes } from '../../interfaces/policy.types';
25 import { Rics } from 'src/app/interfaces/ric';
28 * Services for calling the policy endpoints.
33 export class PolicyService {
35 private apiVersion2 = '/v2'
36 private basePath = '/a1-policy';
37 policyTypesPath = 'policy-types';
38 policyPath = 'policies';
40 private buildPath(...args: any[]) {
41 let result = this.basePath + this.apiVersion2;
42 args.forEach(part => {
43 result = result + '/' + part;
48 constructor(private httpClient: HttpClient) {
49 // injects to variable httpClient
52 getPolicyTypes(): Observable<PolicyTypes> {
53 const url = this.buildPath(this.policyTypesPath);
54 return this.httpClient.get<PolicyTypes>(url);
57 getPolicyType(policyTypeId: string): Observable<PolicyType> {
58 const url = this.buildPath(this.policyTypesPath + '/' + policyTypeId);
59 return this.httpClient.get<PolicyType>(url);
62 getPolicyInstancesByType(policyTypeId: string): Observable<PolicyInstances> {
63 const url = this.buildPath(this.policyPath + '?' + 'policytype_id=' + policyTypeId);
64 return this.httpClient.get<PolicyInstances>(url);
67 getPolicyInstance(policyId: string): Observable<PolicyInstance> {
68 const url = this.buildPath(this.policyPath) + '/' + policyId;
69 return this.httpClient.get<PolicyInstance>(url);
72 getPolicyStatus(policyId: string): Observable<PolicyStatus> {
73 const url = this.buildPath(this.policyPath) + '/' + policyId + '/status';
74 return this.httpClient.get<PolicyStatus>(url);
78 * Gets policy parameters.
79 * @returns Observable that should yield a policy instance
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);
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
93 putPolicy(createPolicyInstance: CreatePolicyInstance): Observable<any> {
94 const url = this.buildPath(this.policyPath);
95 return this.httpClient.put<PolicyInstanceAck>(url, createPolicyInstance, { observe: 'response' });
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
104 deletePolicy(policyInstanceId: string): Observable<any> {
105 const url = this.buildPath(this.policyPath, policyInstanceId);
106 return this.httpClient.delete(url, { observe: 'response' });
110 getRics(policyTypeId: string): Observable<Rics> {
111 const url = this.buildPath('rics') + '?policytype_id=' + policyTypeId;
112 return this.httpClient.get<any>(url);