4e0758c132ab34f11733209fbdd51c1d189fa407
[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.net.URISyntaxException;
30 import java.util.UUID;
31 import org.apache.http.client.utils.URIBuilder;
32 import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration;
33 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
34 import org.slf4j.MDC;
35 import org.springframework.web.reactive.function.client.ClientResponse;
36 import org.springframework.web.reactive.function.client.WebClient;
37 import reactor.core.publisher.Mono;
38
39
40 public class AaiProducerReactiveHttpClient {
41
42     private WebClient webClient;
43     private final String aaiHost;
44     private final String aaiProtocol;
45     private final Integer aaiHostPortNumber;
46     private final String aaiBasePath;
47     private final String aaiPnfPath;
48
49     /**
50      * Constructor of AaiProducerReactiveHttpClient.
51      *
52      * @param configuration - AAI producer configuration object
53      */
54     public AaiProducerReactiveHttpClient(AaiClientConfiguration configuration) {
55         this.aaiHost = configuration.aaiHost();
56         this.aaiProtocol = configuration.aaiProtocol();
57         this.aaiHostPortNumber = configuration.aaiPort();
58         this.aaiBasePath = configuration.aaiBasePath();
59         this.aaiPnfPath = configuration.aaiPnfPath();
60     }
61
62     /**
63      * Function for calling AAI Http producer - patch request to AAI database.
64      *
65      * @param consumerDmaapModelMono - object which will be sent to AAI database
66      * @return status code of operation
67      */
68     public Mono<ClientResponse> getAaiProducerResponse(ConsumerDmaapModel consumerDmaapModelMono) {
69         try {
70             return patchAaiRequest(consumerDmaapModelMono);
71         } catch (URISyntaxException e) {
72             return Mono.error(e);
73         }
74     }
75
76     public AaiProducerReactiveHttpClient createAaiWebClient(WebClient webClient) {
77         this.webClient = webClient;
78         return this;
79     }
80
81     private Mono<ClientResponse> patchAaiRequest(ConsumerDmaapModel dmaapModel) throws URISyntaxException {
82         return
83             webClient.patch()
84                 .uri(getUri(dmaapModel.getSourceName()))
85                 .header(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID))
86                 .header(X_INVOCATION_ID, UUID.randomUUID().toString())
87                 .body(Mono.just(createJsonBody(dmaapModel)), String.class)
88                 .exchange();
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 + aaiPnfPath + "/" + pnfName)
97             .build();
98     }
99 }