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.springframework.http.HttpStatus;
31 import org.springframework.web.reactive.function.BodyInserters;
32 import org.springframework.web.reactive.function.client.WebClient;
33 import reactor.core.publisher.Mono;
36 public class AaiProducerReactiveHttpClient {
38 private WebClient webClient;
39 private final String aaiHost;
40 private final String aaiProtocol;
41 private final Integer aaiHostPortNumber;
42 private final String aaiBasePath;
46 * Constructor of AaiProducerReactiveHttpClient.
48 * @param configuration - AAI producer configuration object
50 public AaiProducerReactiveHttpClient(AaiClientConfiguration configuration) {
51 this.aaiHost = configuration.aaiHost();
52 this.aaiProtocol = configuration.aaiProtocol();
53 this.aaiHostPortNumber = configuration.aaiPort();
54 this.aaiBasePath = configuration.aaiBasePath();
58 * Function for calling AAI Http producer - patch request to AAI database.
60 * @param consumerDmaapModelMono - object which will be sent to AAI database
61 * @return status code of operation
63 public Mono<Integer> getAaiProducerResponse(Mono<ConsumerDmaapModel> consumerDmaapModelMono) {
64 return consumerDmaapModelMono.flatMap(this::patchAaiRequest);
67 public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) {
68 this.webClient = webClient;
72 private Mono<Integer> patchAaiRequest(ConsumerDmaapModel dmaapModel) {
74 return webClient.patch()
75 .uri(getUri(dmaapModel.getPnfName()))
76 .body(BodyInserters.fromObject(dmaapModel))
79 HttpStatus::is4xxClientError,
80 clientResponse -> Mono.error(new AaiRequestException("HTTP 400"))
82 .onStatus(HttpStatus::is5xxServerError,
83 clientResponse -> Mono.error(new AaiRequestException("HTTP 500")))
84 .bodyToMono(Integer.class);
85 } catch (URISyntaxException e) {
90 URI getUri(String pnfName) throws URISyntaxException {
91 return new URIBuilder()
92 .setScheme(aaiProtocol)
94 .setPort(aaiHostPortNumber)
95 .setPath(aaiBasePath + "/" + pnfName)