b43c216410ea2b6c78cbb77bf049df3cce2bb718
[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.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;
34
35
36 public class AaiProducerReactiveHttpClient {
37
38     private WebClient webClient;
39     private final String aaiHost;
40     private final String aaiProtocol;
41     private final Integer aaiHostPortNumber;
42     private final String aaiBasePath;
43
44
45     /**
46      * Constructor of AaiProducerReactiveHttpClient.
47      *
48      * @param configuration - AAI producer configuration object
49      */
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();
55     }
56
57     /**
58      * Function for calling AAI Http producer - patch request to AAI database.
59      *
60      * @param consumerDmaapModelMono - object which will be sent to AAI database
61      * @return status code of operation
62      */
63     public Mono<Integer> getAaiProducerResponse(Mono<ConsumerDmaapModel> consumerDmaapModelMono) {
64         return consumerDmaapModelMono.flatMap(this::patchAaiRequest);
65     }
66
67     public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) {
68         this.webClient = webClient;
69         return this;
70     }
71
72     private Mono<Integer> patchAaiRequest(ConsumerDmaapModel dmaapModel) {
73         try {
74             return webClient.patch()
75                 .uri(getUri(dmaapModel.getPnfName()))
76                 .body(BodyInserters.fromObject(dmaapModel))
77                 .retrieve()
78                 .onStatus(
79                     HttpStatus::is4xxClientError,
80                     clientResponse -> Mono.error(new AaiRequestException("HTTP 400"))
81                 )
82                 .onStatus(HttpStatus::is5xxServerError,
83                     clientResponse -> Mono.error(new AaiRequestException("HTTP 500")))
84                 .bodyToMono(Integer.class);
85         } catch (URISyntaxException e) {
86             return Mono.error(e);
87         }
88     }
89
90     URI getUri(String pnfName) throws URISyntaxException {
91         return new URIBuilder()
92             .setScheme(aaiProtocol)
93             .setHost(aaiHost)
94             .setPort(aaiHostPortNumber)
95             .setPath(aaiBasePath + "/" + pnfName)
96             .build();
97     }
98 }