be6c63e023912a6b68a380c9d4dd2f418024dc77
[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 static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.REQUEST_ID;
24 import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_INVOCATION_ID;
25 import static org.onap.dcaegen2.services.prh.model.logging.MDCVariables.X_ONAP_REQUEST_ID;
26
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.util.UUID;
30 import org.apache.http.client.utils.URIBuilder;
31 import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
32 import org.onap.dcaegen2.services.prh.exceptions.AaiRequestException;
33 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.slf4j.MDC;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.web.reactive.function.BodyInserters;
39 import org.springframework.web.reactive.function.client.WebClient;
40 import reactor.core.publisher.Mono;
41
42
43 public class AaiProducerReactiveHttpClient {
44
45     private final String aaiHost;
46     private final String aaiProtocol;
47     private final Integer aaiHostPortNumber;
48     private final String aaiBasePath;
49     private final Logger logger = LoggerFactory.getLogger(this.getClass());
50     private WebClient webClient;
51
52
53     /**
54      * Constructor of AaiProducerReactiveHttpClient.
55      *
56      * @param configuration - AAI producer configuration object
57      */
58     public AaiProducerReactiveHttpClient(AaiClientConfiguration configuration) {
59         this.aaiHost = configuration.aaiHost();
60         this.aaiProtocol = configuration.aaiProtocol();
61         this.aaiHostPortNumber = configuration.aaiPort();
62         this.aaiBasePath = configuration.aaiBasePath();
63     }
64
65     /**
66      * Function for calling AAI Http producer - patch request to AAI database.
67      *
68      * @param consumerDmaapModelMono - object which will be sent to AAI database
69      * @return status code of operation
70      */
71     public Mono<Integer> getAaiProducerResponse(Mono<ConsumerDmaapModel> consumerDmaapModelMono) {
72         return consumerDmaapModelMono
73             .doOnNext(consumerDmaapModel -> logger.info("Sending PNF model to AAI {}", consumerDmaapModel))
74             .flatMap(this::patchAaiRequest);
75     }
76
77     public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) {
78         this.webClient = webClient;
79         return this;
80     }
81
82     private Mono<Integer> patchAaiRequest(ConsumerDmaapModel dmaapModel) {
83         try {
84             return webClient.patch()
85                 .uri(getUri(dmaapModel.getSourceName()))
86                 .header(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID))
87                 .header(X_INVOCATION_ID, UUID.randomUUID().toString())
88                 .body(BodyInserters.fromObject(dmaapModel))
89                 .retrieve()
90                 .onStatus(
91                     HttpStatus::is4xxClientError,
92                     clientResponse -> Mono
93                         .error(new AaiRequestException("AaiProducer HTTP " + clientResponse.statusCode()))
94                 )
95                 .onStatus(HttpStatus::is5xxServerError,
96                     clientResponse -> Mono
97                         .error(new AaiRequestException("AaiProducer HTTP " + clientResponse.statusCode())))
98                 .bodyToMono(Integer.class);
99         } catch (URISyntaxException e) {
100             return Mono.error(e);
101         }
102     }
103
104     URI getUri(String pnfName) throws URISyntaxException {
105         return new URIBuilder()
106             .setScheme(aaiProtocol)
107             .setHost(aaiHost)
108             .setPort(aaiHostPortNumber)
109             .setPath(aaiBasePath + "/" + pnfName)
110             .build();
111     }
112 }