0a6c9926ffd3dd4e53ae5619f5b0e689485df15c
[dcaegen2/services/prh.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.services.prh.service.producer;
22
23 import java.net.URI;
24 import java.net.URISyntaxException;
25
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;
36
37
38 public class AaiProducerReactiveHttpClient {
39
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());
46
47
48     /**
49      * Constructor of AaiProducerReactiveHttpClient.
50      *
51      * @param configuration - AAI producer configuration object
52      */
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();
58     }
59
60     /**
61      * Function for calling AAI Http producer - patch request to AAI database.
62      *
63      * @param consumerDmaapModelMono - object which will be sent to AAI database
64      * @return status code of operation
65      */
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);
70     }
71
72     public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) {
73         this.webClient = webClient;
74         return this;
75     }
76
77     private Mono<Integer> patchAaiRequest(ConsumerDmaapModel dmaapModel) {
78         try {
79             return webClient.patch()
80                 .uri(getUri(dmaapModel.getSourceName()))
81                 .body(BodyInserters.fromObject(dmaapModel))
82                 .retrieve()
83                 .onStatus(
84                     HttpStatus::is4xxClientError,
85                     clientResponse -> Mono
86                         .error(new AaiRequestException("AaiProducer HTTP " + clientResponse.statusCode()))
87                 )
88                 .onStatus(HttpStatus::is5xxServerError,
89                     clientResponse -> Mono
90                         .error(new AaiRequestException("AaiProducer HTTP " + clientResponse.statusCode())))
91                 .bodyToMono(Integer.class);
92         } catch (URISyntaxException e) {
93             return Mono.error(e);
94         }
95     }
96
97     URI getUri(String pnfName) throws URISyntaxException {
98         return new URIBuilder()
99             .setScheme(aaiProtocol)
100             .setHost(aaiHost)
101             .setPort(aaiHostPortNumber)
102             .setPath(aaiBasePath + "/" + pnfName)
103             .build();
104     }
105 }