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