2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
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.services.prh.service.producer;
24 import java.net.URISyntaxException;
26 import org.apache.http.client.utils.URIBuilder;
27 import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
28 import org.onap.dcaegen2.services.prh.exceptions.AaiRequestException;
29 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.web.reactive.function.BodyInserters;
34 import org.springframework.web.reactive.function.client.WebClient;
35 import reactor.core.publisher.Mono;
38 public class AaiProducerReactiveHttpClient {
40 private WebClient webClient;
41 private final String aaiHost;
42 private final String aaiProtocol;
43 private final Integer aaiHostPortNumber;
44 private final String aaiBasePath;
45 private final Logger logger = LoggerFactory.getLogger(this.getClass());
49 * Constructor of AaiProducerReactiveHttpClient.
51 * @param configuration - AAI producer configuration object
53 public AaiProducerReactiveHttpClient(AaiClientConfiguration configuration) {
54 this.aaiHost = configuration.aaiHost();
55 this.aaiProtocol = configuration.aaiProtocol();
56 this.aaiHostPortNumber = configuration.aaiPort();
57 this.aaiBasePath = configuration.aaiBasePath();
61 * Function for calling AAI Http producer - patch request to AAI database.
63 * @param consumerDmaapModelMono - object which will be sent to AAI database
64 * @return status code of operation
66 public Mono<Integer> getAaiProducerResponse(Mono<ConsumerDmaapModel> consumerDmaapModelMono) {
67 return consumerDmaapModelMono
68 .doOnNext(consumerDmaapModel -> logger.info("Sending PNF model to AAI {}", consumerDmaapModel))
69 .flatMap(this::patchAaiRequest);
72 public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) {
73 this.webClient = webClient;
77 private Mono<Integer> patchAaiRequest(ConsumerDmaapModel dmaapModel) {
79 return webClient.patch()
80 .uri(getUri(dmaapModel.getSourceName()))
81 .body(BodyInserters.fromObject(dmaapModel))
84 HttpStatus::is4xxClientError,
85 clientResponse -> Mono
86 .error(new AaiRequestException("AaiProducer HTTP " + clientResponse.statusCode()))
88 .onStatus(HttpStatus::is5xxServerError,
89 clientResponse -> Mono
90 .error(new AaiRequestException("AaiProducer HTTP " + clientResponse.statusCode())))
91 .bodyToMono(Integer.class);
92 } catch (URISyntaxException e) {
97 URI getUri(String pnfName) throws URISyntaxException {
98 return new URIBuilder()
99 .setScheme(aaiProtocol)
101 .setPort(aaiHostPortNumber)
102 .setPath(aaiBasePath + "/" + pnfName)