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;
29 import reactor.core.publisher.Flux;
30 import reactor.core.publisher.Mono;
33 * Client for accessing standard A1 REST API version 1.1
35 public class StdA1ClientVersion1 implements A1Client {
37 public static class UriBuilder implements A1UriBuilder {
39 private final RicConfig ricConfig;
41 public UriBuilder(RicConfig ricConfig) {
42 this.ricConfig = ricConfig;
46 * /A1-P/v1/policies/{policyId}
49 public String createPutPolicyUri(String type, String policyId) {
50 return policiesBaseUri() + policyId;
56 public String createGetPolicyIdsUri() {
57 return baseUri() + "/policies";
61 * /A1-P/v1/policies/{policyId}
64 public String createDeleteUri(String type, String policyId) {
65 return policiesBaseUri() + policyId;
69 * /A1-P/v1/policies/{policyId}/status
71 public String createGetPolicyStatusUri(String type, String policyId) {
72 return policiesBaseUri() + policyId + "/status";
75 private String baseUri() {
76 return ricConfig.baseUrl() + "/A1-P/v1";
79 private String policiesBaseUri() {
80 return createGetPolicyIdsUri() + "/";
84 private final AsyncRestClient restClient;
85 private final UriBuilder uri;
87 public StdA1ClientVersion1(RicConfig ricConfig) {
88 this(new AsyncRestClient(""), ricConfig);
91 public StdA1ClientVersion1(AsyncRestClient restClient, RicConfig ricConfig) {
92 this.restClient = restClient;
93 this.uri = new UriBuilder(ricConfig);
97 public Mono<List<String>> getPolicyIdentities() {
98 return getPolicyIds() //
103 public Mono<String> putPolicy(Policy policy) {
104 return restClient.put(uri.createPutPolicyUri(policy.type().name(), policy.id()), policy.json());
108 public Mono<List<String>> getPolicyTypeIdentities() {
109 return Mono.just(Arrays.asList(""));
113 public Mono<String> getPolicyTypeSchema(String policyTypeId) {
114 return Mono.just("{}");
118 public Mono<String> deletePolicy(Policy policy) {
119 return deletePolicyById(policy.id());
123 public Flux<String> deleteAllPolicies() {
124 return getPolicyIds() //
125 .flatMap(this::deletePolicyById); //
129 public Mono<A1ProtocolType> getProtocolVersion() {
130 return getPolicyIdentities() //
131 .flatMap(x -> Mono.just(A1ProtocolType.STD_V1_1));
135 public Mono<String> getPolicyStatus(Policy policy) {
136 return restClient.get(uri.createGetPolicyStatusUri(policy.type().name(), policy.id()));
139 private Flux<String> getPolicyIds() {
140 return restClient.get(uri.createGetPolicyIdsUri()) //
141 .flatMapMany(SdncJsonHelper::parseJsonArrayOfString);
144 private Mono<String> deletePolicyById(String policyId) {
145 return restClient.delete(uri.createDeleteUri("", policyId));