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 package org.oransc.policyagent.clients;
23 import java.util.List;
25 import org.oransc.policyagent.configuration.RicConfig;
26 import org.oransc.policyagent.repository.Policy;
27 import org.springframework.web.util.UriComponentsBuilder;
29 import reactor.core.publisher.Flux;
30 import reactor.core.publisher.Mono;
32 public class StdA1Client implements A1Client {
33 private static final String URL_PREFIX = "/A1-P/v1";
35 private static final String POLICY_TYPES_URI = "/policytypes";
36 private static final String POLICY_TYPE_ID = "policyTypeId";
38 private static final String POLICIES_URI = "/policies";
40 private static final UriComponentsBuilder POLICY_TYPE_SCHEMA_URI =
41 UriComponentsBuilder.fromPath("/policytypes/{policy-type-name}");
43 private static final UriComponentsBuilder POLICY_URI =
44 UriComponentsBuilder.fromPath("/policies/{policy-id}").queryParam(POLICY_TYPE_ID, "{policy-type-name}");
46 private static final UriComponentsBuilder POLICY_DELETE_URI =
47 UriComponentsBuilder.fromPath("/policies/{policy-id}");
49 private static final UriComponentsBuilder POLICY_STATUS_URI =
50 UriComponentsBuilder.fromPath("/policies/{policy-id}/status");
52 private final AsyncRestClient restClient;
54 public StdA1Client(RicConfig ricConfig) {
55 String baseUrl = ricConfig.baseUrl() + URL_PREFIX;
56 this.restClient = new AsyncRestClient(baseUrl);
59 public StdA1Client(AsyncRestClient restClient) {
60 this.restClient = restClient;
64 public Mono<List<String>> getPolicyIdentities() {
65 return getPolicyIds() //
70 public Mono<String> putPolicy(Policy policy) {
71 String uri = POLICY_URI.buildAndExpand(policy.id(), policy.type().name()).toUriString();
72 return restClient.put(uri, policy.json()) //
73 .flatMap(JsonHelper::validateJson);
77 public Mono<List<String>> getPolicyTypeIdentities() {
78 return restClient.get(POLICY_TYPES_URI) //
79 .flatMapMany(JsonHelper::parseJsonArrayOfString) //
84 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
85 String uri = POLICY_TYPE_SCHEMA_URI.buildAndExpand(policyTypeId).toUriString();
86 return restClient.get(uri) //
87 .flatMap(JsonHelper::extractPolicySchema);
91 public Mono<String> deletePolicy(Policy policy) {
92 return deletePolicyById(policy.id());
96 public Flux<String> deleteAllPolicies() {
97 return getPolicyIds() //
98 .flatMap(this::deletePolicyById); //
102 public Mono<A1ProtocolType> getProtocolVersion() {
103 return getPolicyTypeIdentities() //
104 .flatMap(x -> Mono.just(A1ProtocolType.STD_V1));
108 public Mono<String> getPolicyStatus(Policy policy) {
109 String uri = POLICY_STATUS_URI.buildAndExpand(policy.id()).toUriString();
110 return restClient.get(uri);
113 private Flux<String> getPolicyIds() {
114 return restClient.get(POLICIES_URI) //
115 .flatMapMany(JsonHelper::parseJsonArrayOfString);
118 private Mono<String> deletePolicyById(String policyId) {
119 String uri = POLICY_DELETE_URI.buildAndExpand(policyId).toUriString();
120 return restClient.delete(uri);