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 { map } from 'rxjs/operators';
25 import { PolicyInstance, PolicyInstanceAck, PolicyType, PolicyTypes } from '../../interfaces/policy.types';
26 import { ControlpanelSuccessTransport } from '../../interfaces/controlpanel.types';
29 * Services for calling the policy endpoints.
34 export class PolicyService {
36 private apiVersion2 = 'v2'
37 private basePath = '';
38 policyTypesPath = 'policy-types';
39 policyPath = 'policies';
41 private buildPath(...args: any[]) {
42 let result = this.basePath + this.apiVersion2;
43 args.forEach(part => {
44 result = result + '/' + part;
49 constructor(private httpClient: HttpClient) {
50 // injects to variable httpClient
53 getPolicyTypes(): Observable<PolicyTypes> {
54 const url = this.buildPath(this.policyTypesPath);
55 return this.httpClient.get<PolicyTypes>(url);
58 getPolicyType(policyTypeId: string): Observable<PolicyType> {
59 const url = this.buildPath(this.policyTypesPath + '/' + policyTypeId);
60 return this.httpClient.get<PolicyType>(url);
63 getPolicyInstances(policyTypeId: string): Observable<PolicyInstance[]> {
64 const url = this.buildPath(this.policyPath) + '?type=' + policyTypeId;
65 return this.httpClient.get<PolicyInstance[]>(url);
69 * Gets policy parameters.
70 * @returns Observable that should yield a policy instance
72 getPolicy(policyTypeId: string, policyInstanceId: string): Observable<any> {
73 const url = this.buildPath(this.policyPath, policyInstanceId) + '?type=' + policyTypeId;
74 return this.httpClient.get<any>(url);
78 * Creates or replaces policy instance.
79 * @param policyTypeId ID of the policy type that the instance will have
80 * @param policyInstanceId ID of the instance
81 * @param policyJson Json with the policy content
82 * @returns Observable that should yield a response code, no data
84 putPolicy(policyTypeId: string, policyInstanceId: string, policyJson: string, ric: string): Observable<any> {
85 const url = this.buildPath(this.policyPath, policyInstanceId) + '?ric=' + ric + '&type=' + policyTypeId;
86 return this.httpClient.put<PolicyInstanceAck>(url, policyJson, { observe: 'response' });
90 * Deletes a policy instance.
91 * @param policyTypeId ID of the policy type that the instance belong to
92 * @param policyInstanceId ID of the instance
93 * @returns Observable that should yield a response code, no data
95 deletePolicy(policyTypeId: string, policyInstanceId: string): Observable<any> {
96 const url = this.buildPath(this.policyPath, policyInstanceId) + '?type=' + policyTypeId;
97 return this.httpClient.delete(url, { observe: 'response' });
101 getRics(policyTypeId: string): Observable<string[]> {
102 const url = this.buildPath('rics') + '?policyType=' + policyTypeId;
103 return this.httpClient.get<any>(url);