55475902790cfb6d0e03e21b20471a919595cd7e
[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
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(HttpStatus::is4xxClientError, clientResponse -> Mono.error(new AaiRequestException("HTTP 400")))
79                     .onStatus(HttpStatus::is5xxServerError, clientResponse -> Mono.error(new AaiRequestException("HTTP 500")))
80                     .bodyToMono(Integer.class);
81         } catch (URISyntaxException e) {
82             return Mono.error(e);
83         }
84     }
85
86     URI getUri(String pnfName) throws URISyntaxException {
87         return new URIBuilder()
88                 .setScheme(aaiProtocol)
89                 .setHost(aaiHost)
90                 .setPort(aaiHostPortNumber)
91                 .setPath(aaiBasePath + "/" + pnfName)
92                 .build();
93     }
94 }