aboutsummaryrefslogtreecommitdiffstats
path: root/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/clients/CcsdkA1AdapterClient.java
blob: c3f00407bb00a25d697c91b3665ca9624b4ae0e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*-
 * ========================LICENSE_START=================================
 * ONAP : ccsdk oran
 * ======================================================================
 * Copyright (C) 2020 Nordix Foundation. All rights reserved.
 * ======================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================LICENSE_END===================================
 */

package org.onap.ccsdk.oran.a1policymanagementservice.clients;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.GsonBuilder;

import java.lang.invoke.MethodHandles;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.immutables.value.Value;
import org.json.JSONObject;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ControllerConfig;
import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClientResponseException;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * Client for accessing the A1 adapter in the CCSDK in ONAP.
 */
@SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
public class CcsdkA1AdapterClient implements A1Client {

    static final int CONCURRENCY_RIC = 1; // How many paralell requests that is sent to one NearRT RIC

    @Value.Immutable
    @org.immutables.gson.Gson.TypeAdapters
    public interface AdapterRequest {
        public String nearRtRicUrl();

        public Optional<String> body();
    }

    @Value.Immutable
    @org.immutables.gson.Gson.TypeAdapters
    public interface AdapterOutput {
        public Optional<String> body();

        public int httpStatus();
    }

    static com.google.gson.Gson gson = new GsonBuilder() //
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES) //
            .create(); //

    private static final String GET_POLICY_RPC = "getA1Policy";
    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
    private final ControllerConfig controllerConfig;
    private final AsyncRestClient restClient;
    private final RicConfig ricConfig;
    private final A1ProtocolType protocolType;

    /**
     * Constructor that creates the REST client to use.
     *
     * @param protocolType the southbound protocol of the controller. Supported
     *        protocols are CCSDK_A1_ADAPTER_STD_V1_1,
     *        CCSDK_A1_ADAPTER_OSC_V1 and
     *        CCSDK_A1_ADAPTER_STD_V2_0_0 with
     * @param ricConfig the configuration of the Near-RT RIC to communicate
     *        with
     * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
     *
     * @throws IllegalArgumentException when the protocolType is wrong.
     */
    public CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig,
            AsyncRestClientFactory restClientFactory) {
        this(protocolType, ricConfig, controllerConfig,
                restClientFactory.createRestClientNoHttpProxy(controllerConfig.baseUrl() + "/restconf/operations"));
    }

    /**
     * Constructor where the REST client to use is provided.
     *
     * @param protocolType the southbound protocol of the controller. Supported
     *        protocols are CCSDK_A1_ADAPTER_STD_V1_1,
     *        CCSDK_A1_ADAPTER_OSC_V1 and
     *        CCSDK_A1_ADAPTER_STD_V2_0_0 with
     * @param ricConfig the configuration of the Near-RT RIC to communicate
     *        with
     * @param controllerConfig the configuration of the CCSDK A1 Adapter to use
     * @param restClient the REST client to use
     *
     * @throws IllegalArgumentException when the protocolType is illegal.
     */
    CcsdkA1AdapterClient(A1ProtocolType protocolType, RicConfig ricConfig, ControllerConfig controllerConfig,
            AsyncRestClient restClient) {
        if (A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1.equals(protocolType) //
                || A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1.equals(protocolType) //
                || A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0.equals(protocolType)) {
            this.restClient = restClient;
            this.ricConfig = ricConfig;
            this.protocolType = protocolType;
            this.controllerConfig = controllerConfig;
            logger.debug("CcsdkA1AdapterClient for ric: {}, a1Controller: {}", ricConfig.ricId(), controllerConfig);
        } else {
            throw new IllegalArgumentException("Not handeled protocolversion: " + protocolType);
        }

    }

    @Override
    public Mono<List<String>> getPolicyTypeIdentities() {
        if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
            return Mono.just(Arrays.asList(""));
        } else {
            return post(GET_POLICY_RPC, getUriBuilder().createPolicyTypesUri(), Optional.empty()) //
                    .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString) //
                    .collectList();
        }

    }

    @Override
    public Mono<List<String>> getPolicyIdentities() {
        return getPolicyIds() //
                .collectList();
    }

    @Override
    public Mono<String> getPolicyTypeSchema(String policyTypeId) {
        if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
            return Mono.just("{}");
        } else {
            A1UriBuilder uri = this.getUriBuilder();
            final String ricUrl = uri.createGetSchemaUri(policyTypeId);
            return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
                    .flatMap(response -> extractCreateSchema(response, policyTypeId));
        }
    }

    private Mono<String> extractCreateSchema(String controllerResponse, String policyTypeId) {
        if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) {
            return OscA1Client.extractCreateSchema(controllerResponse, policyTypeId);
        } else if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
            return StdA1ClientVersion2.extractPolicySchema(controllerResponse, policyTypeId);
        } else {
            throw new NullPointerException("Not supported");
        }
    }

    @Override
    public Mono<String> putPolicy(Policy policy) {
        String ricUrl = getUriBuilder().createPutPolicyUri(policy.getType().getId(), policy.getId(),
                policy.getStatusNotificationUri());
        return post("putA1Policy", ricUrl, Optional.of(policy.getJson()));
    }

    @Override
    public Mono<String> deletePolicy(Policy policy) {
        return deletePolicyById(policy.getType().getId(), policy.getId());
    }

    @Override
    public Flux<String> deleteAllPolicies() {
        if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
            return getPolicyIds() //
                    .flatMap(policyId -> deletePolicyById("", policyId), CONCURRENCY_RIC); //
        } else {
            A1UriBuilder uriBuilder = this.getUriBuilder();
            return getPolicyTypeIdentities() //
                    .flatMapMany(Flux::fromIterable) //
                    .flatMap(type -> deleteAllInstancesForType(uriBuilder, type), CONCURRENCY_RIC);
        }
    }

    private Flux<String> getInstancesForType(A1UriBuilder uriBuilder, String type) {
        return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(type), Optional.empty()) //
                .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
    }

    private Flux<String> deleteAllInstancesForType(A1UriBuilder uriBuilder, String type) {
        return getInstancesForType(uriBuilder, type) //
                .flatMap(instance -> deletePolicyById(type, instance), CONCURRENCY_RIC);
    }

    @Override
    public Mono<A1ProtocolType> getProtocolVersion() {
        return tryStdProtocolVersion2() //
                .onErrorResume(t -> tryStdProtocolVersion1()) //
                .onErrorResume(t -> tryOscProtocolVersion());
    }

    @Override
    public Mono<String> getPolicyStatus(Policy policy) {
        String ricUrl = getUriBuilder().createGetPolicyStatusUri(policy.getType().getId(), policy.getId());
        return post("getA1PolicyStatus", ricUrl, Optional.empty());

    }

    private A1UriBuilder getUriBuilder() {
        if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
            return new StdA1ClientVersion1.UriBuilder(ricConfig);
        } else if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0) {
            return new StdA1ClientVersion2.OranV2UriBuilder(ricConfig);
        } else if (protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1) {
            return new OscA1Client.UriBuilder(ricConfig);
        }
        throw new NullPointerException();
    }

    private Mono<A1ProtocolType> tryOscProtocolVersion() {
        OscA1Client.UriBuilder oscApiuriBuilder = new OscA1Client.UriBuilder(ricConfig);
        return post(GET_POLICY_RPC, oscApiuriBuilder.createHealtcheckUri(), Optional.empty()) //
                .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_OSC_V1));
    }

    private Mono<A1ProtocolType> tryStdProtocolVersion1() {
        StdA1ClientVersion1.UriBuilder uriBuilder = new StdA1ClientVersion1.UriBuilder(ricConfig);
        return post(GET_POLICY_RPC, uriBuilder.createGetPolicyIdsUri(""), Optional.empty()) //
                .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1));
    }

    private Mono<A1ProtocolType> tryStdProtocolVersion2() {
        StdA1ClientVersion2.OranV2UriBuilder uriBuilder = new StdA1ClientVersion2.OranV2UriBuilder(ricConfig);
        return post(GET_POLICY_RPC, uriBuilder.createPolicyTypesUri(), Optional.empty()) //
                .flatMap(x -> Mono.just(A1ProtocolType.CCSDK_A1_ADAPTER_STD_V2_0_0));
    }

    private Flux<String> getPolicyIds() {
        if (this.protocolType == A1ProtocolType.CCSDK_A1_ADAPTER_STD_V1_1) {
            StdA1ClientVersion1.UriBuilder uri = new StdA1ClientVersion1.UriBuilder(ricConfig);
            final String ricUrl = uri.createGetPolicyIdsUri("");
            return post(GET_POLICY_RPC, ricUrl, Optional.empty()) //
                    .flatMapMany(A1AdapterJsonHelper::parseJsonArrayOfString);
        } else {
            A1UriBuilder uri = this.getUriBuilder();
            return getPolicyTypeIdentities() //
                    .flatMapMany(Flux::fromIterable)
                    .flatMap(type -> post(GET_POLICY_RPC, uri.createGetPolicyIdsUri(type), Optional.empty())) //
                    .flatMap(A1AdapterJsonHelper::parseJsonArrayOfString);
        }
    }

    private Mono<String> deletePolicyById(String type, String policyId) {
        String ricUrl = getUriBuilder().createDeleteUri(type, policyId);
        return post("deleteA1Policy", ricUrl, Optional.empty());
    }

    private Mono<String> post(String rpcName, String ricUrl, Optional<String> body) {
        AdapterRequest inputParams = ImmutableAdapterRequest.builder() //
                .nearRtRicUrl(ricUrl) //
                .body(body) //
                .build();
        final String inputJsonString = A1AdapterJsonHelper.createInputJsonString(inputParams);
        logger.debug("POST inputJsonString = {}", inputJsonString);

        return restClient
                .postWithAuthHeader(controllerUrl(rpcName), inputJsonString, this.controllerConfig.userName(),
                        this.controllerConfig.password()) //
                .flatMap(this::extractResponseBody);
    }

    private Mono<String> extractResponse(JSONObject responseOutput) {
        AdapterOutput output = gson.fromJson(responseOutput.toString(), ImmutableAdapterOutput.class);
        Optional<String> optionalBody = output.body();
        String body = optionalBody.isPresent() ? optionalBody.get() : "";
        if (HttpStatus.valueOf(output.httpStatus()).is2xxSuccessful()) {
            return Mono.just(body);
        } else {
            logger.debug("Error response: {} {}", output.httpStatus(), body);
            byte[] responseBodyBytes = body.getBytes(StandardCharsets.UTF_8);
            HttpStatus httpStatus = HttpStatus.valueOf(output.httpStatus());
            WebClientResponseException responseException = new WebClientResponseException(httpStatus.value(),
                    httpStatus.getReasonPhrase(), null, responseBodyBytes, StandardCharsets.UTF_8, null);

            return Mono.error(responseException);
        }
    }

    private Mono<String> extractResponseBody(String responseStr) {
        return A1AdapterJsonHelper.getOutput(responseStr) //
                .flatMap(this::extractResponse);
    }

    private String controllerUrl(String rpcName) {
        return "/A1-ADAPTER-API:" + rpcName;
    }
}