43f6ce91a234c102ce36701a561d99ea99557e8f
[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.CommonFunctions.createJsonBody;
24 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.REQUEST_ID;
25 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_INVOCATION_ID;
26 import static org.onap.dcaegen2.services.prh.model.logging.MdcVariables.X_ONAP_REQUEST_ID;
27
28 import java.net.URI;
29 import java.util.UUID;
30 import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
31 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
32 import org.slf4j.MDC;
33 import org.springframework.web.reactive.function.client.ClientResponse;
34 import org.springframework.web.reactive.function.client.WebClient;
35 import org.springframework.web.util.DefaultUriBuilderFactory;
36 import reactor.core.publisher.Mono;
37
38
39 public class AaiProducerReactiveHttpClient {
40
41     private WebClient webClient;
42     private final String aaiHost;
43     private final String aaiProtocol;
44     private final Integer aaiHostPortNumber;
45     private final String aaiBasePath;
46     private final String aaiPnfPath;
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         this.aaiPnfPath = configuration.aaiPnfPath();
59     }
60
61     /**
62      * Function for calling AAI Http producer - patch request to AAI database.
63      *
64      * @param consumerDmaapModelMono - object which will be sent to AAI database
65      * @return status code of operation
66      */
67     public Mono<ClientResponse> getAaiProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) {
68         return patchAaiRequest(consumerDmaapModelMono);
69     }
70
71     public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) {
72         this.webClient = webClient;
73         return this;
74     }
75
76     private Mono<ClientResponse> patchAaiRequest(ConsumerDmaapModel dmaapModel) {
77         return
78             webClient.patch()
79                 .uri(getUri(dmaapModel.getCorrelationId()))
80                 .header(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID))
81                 .header(X_INVOCATION_ID, UUID.randomUUID().toString())
82                 .body(Mono.just(createJsonBody(dmaapModel)), String.class)
83                 .exchange();
84     }
85
86     URI getUri(String pnfName) {
87         return new DefaultUriBuilderFactory().builder().scheme(aaiProtocol).host(aaiHost).port(aaiHostPortNumber)
88             .path(aaiBasePath + aaiPnfPath + "/" + pnfName).build();
89     }
90 }