65ae40b8fc6dd86152df2f60a31362943d8cf112
[dcaegen2/collectors/datafile.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Datafile Collector Service
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.collectors.datafile.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.collectors.datafile.config.AaiClientConfiguration;
28 import org.onap.dcaegen2.collectors.datafile.exceptions.AaiRequestException;
29 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
30 import org.onap.dcaegen2.collectors.datafile.service.producer.AaiProducerReactiveHttpClient;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.web.reactive.function.BodyInserters;
33 import org.springframework.web.reactive.function.client.WebClient;
34 import reactor.core.publisher.Mono;
35
36
37 public class AaiProducerReactiveHttpClient {
38
39     private WebClient webClient;
40     private final String aaiHost;
41     private final String aaiProtocol;
42     private final Integer aaiHostPortNumber;
43     private final String aaiBasePath;
44
45
46     /**
47      * Constructor of AaiProducerReactiveHttpClient.
48      *
49      * @param configuration - AAI producer configuration object
50      */
51     public AaiProducerReactiveHttpClient(AaiClientConfiguration configuration) {
52         this.aaiHost = configuration.aaiHost();
53         this.aaiProtocol = configuration.aaiProtocol();
54         this.aaiHostPortNumber = configuration.aaiPort();
55         this.aaiBasePath = configuration.aaiBasePath();
56     }
57
58     /**
59      * Function for calling AAI Http producer - patch request to AAI database.
60      *
61      * @param consumerDmaapModelMono - object which will be sent to AAI database
62      * @return status code of operation
63      */
64     public Mono<Integer> getAaiProducerResponse(Mono<ConsumerDmaapModel> consumerDmaapModelMono) {
65         return consumerDmaapModelMono.flatMap(this::patchAaiRequest);
66     }
67
68     public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) {
69         this.webClient = webClient;
70         return this;
71     }
72
73     private Mono<Integer> patchAaiRequest(ConsumerDmaapModel dmaapModel) {
74         try {
75             return webClient.patch()
76                 .uri(getUri(dmaapModel.getPnfName()))
77                 .body(BodyInserters.fromObject(dmaapModel))
78                 .retrieve()
79                 .onStatus(
80                     HttpStatus::is4xxClientError,
81                     clientResponse -> Mono.error(new AaiRequestException("HTTP 400"))
82                 )
83                 .onStatus(HttpStatus::is5xxServerError,
84                     clientResponse -> Mono.error(new AaiRequestException("HTTP 500")))
85                 .bodyToMono(Integer.class);
86         } catch (URISyntaxException e) {
87             return Mono.error(e);
88         }
89     }
90
91     URI getUri(String pnfName) throws URISyntaxException {
92         return new URIBuilder()
93             .setScheme(aaiProtocol)
94             .setHost(aaiHost)
95             .setPort(aaiHostPortNumber)
96             .setPath(aaiBasePath + "/" + pnfName)
97             .build();
98     }
99 }