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.Arrays;
24 import java.util.List;
26 import org.oransc.policyagent.configuration.RicConfig;
27 import org.oransc.policyagent.repository.Policy;
28 import org.springframework.web.util.UriComponentsBuilder;
30 import reactor.core.publisher.Flux;
31 import reactor.core.publisher.Mono;
33 public class StdA1ClientVersion1 implements A1Client {
34 private final AsyncRestClient restClient;
36 public StdA1ClientVersion1(RicConfig ricConfig) {
37 final String urlPrefix = "/A1-P/v1";
38 String baseUrl = ricConfig.baseUrl() + urlPrefix;
39 this.restClient = new AsyncRestClient(baseUrl);
42 public StdA1ClientVersion1(AsyncRestClient restClient) {
43 this.restClient = restClient;
47 public Mono<List<String>> getPolicyIdentities() {
48 return getPolicyIds() //
53 public Mono<String> putPolicy(Policy policy) {
54 final UriComponentsBuilder policyUri = UriComponentsBuilder.fromPath("/policies/{policy-id}");
55 final String uri = policyUri.buildAndExpand(policy.id()).toUriString();
56 return restClient.put(uri, policy.json()) //
57 .flatMap(JsonHelper::validateJson);
61 public Mono<List<String>> getPolicyTypeIdentities() {
62 return Mono.just(Arrays.asList(""));
66 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
67 return Mono.just("{}");
71 public Mono<String> deletePolicy(Policy policy) {
72 return deletePolicyById(policy.id());
76 public Flux<String> deleteAllPolicies() {
77 return getPolicyIds() //
78 .flatMap(this::deletePolicyById); //
82 public Mono<A1ProtocolType> getProtocolVersion() {
83 return getPolicyTypeIdentities() //
84 .flatMap(x -> Mono.just(A1ProtocolType.STD_V1_1));
88 public Mono<String> getPolicyStatus(Policy policy) {
89 final UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/policies/{policy-id}/status");
90 String uri = builder.buildAndExpand(policy.id()).toUriString();
91 return restClient.get(uri);
94 private Flux<String> getPolicyIds() {
95 final String uri = "/policies";
96 return restClient.get(uri) //
97 .flatMapMany(JsonHelper::parseJsonArrayOfString);
100 private Mono<String> deletePolicyById(String policyId) {
101 final UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/policies/{policy-id}");
102 String uri = builder.buildAndExpand(policyId).toUriString();
103 return restClient.delete(uri);