2 * ============LICENSE_START=======================================================
3 * Datafile Collector Service
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
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.onap.dcaegen2.collectors.datafile.service.producer;
24 import java.net.URISyntaxException;
26 import org.apache.http.client.utils.URIBuilder;
27 import org.onap.dcaegen2.collectors.datafile.config.AaiClientConfiguration;
28 import org.onap.dcaegen2.collectors.datafile.exceptions.AaiRequestException;
29 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
30 import org.onap.dcaegen2.collectors.datafile.service.producer.AaiProducerReactiveHttpClient;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.web.reactive.function.BodyInserters;
33 import org.springframework.web.reactive.function.client.WebClient;
34 import reactor.core.publisher.Mono;
37 public class AaiProducerReactiveHttpClient {
39 private WebClient webClient;
40 private final String aaiHost;
41 private final String aaiProtocol;
42 private final Integer aaiHostPortNumber;
43 private final String aaiBasePath;
47 * Constructor of AaiProducerReactiveHttpClient.
49 * @param configuration - AAI producer configuration object
51 public AaiProducerReactiveHttpClient(AaiClientConfiguration configuration) {
52 this.aaiHost = configuration.aaiHost();
53 this.aaiProtocol = configuration.aaiProtocol();
54 this.aaiHostPortNumber = configuration.aaiPort();
55 this.aaiBasePath = configuration.aaiBasePath();
59 * Function for calling AAI Http producer - patch request to AAI database.
61 * @param consumerDmaapModelMono - object which will be sent to AAI database
62 * @return status code of operation
64 public Mono<Integer> getAaiProducerResponse(Mono<ConsumerDmaapModel> consumerDmaapModelMono) {
65 return consumerDmaapModelMono.flatMap(this::patchAaiRequest);
68 public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) {
69 this.webClient = webClient;
73 private Mono<Integer> patchAaiRequest(ConsumerDmaapModel dmaapModel) {
75 return webClient.patch()
76 .uri(getUri(dmaapModel.getPnfName()))
77 .body(BodyInserters.fromObject(dmaapModel))
80 HttpStatus::is4xxClientError,
81 clientResponse -> Mono.error(new AaiRequestException("HTTP 400"))
83 .onStatus(HttpStatus::is5xxServerError,
84 clientResponse -> Mono.error(new AaiRequestException("HTTP 500")))
85 .bodyToMono(Integer.class);
86 } catch (URISyntaxException e) {
91 URI getUri(String pnfName) throws URISyntaxException {
92 return new URIBuilder()
93 .setScheme(aaiProtocol)
95 .setPort(aaiHostPortNumber)
96 .setPath(aaiBasePath + "/" + pnfName)