fa1248df3d8c47f3c92ca13de3a3242e165308f8
[dcaegen2/services/sdk.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
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.sdk.rest.services.aai.client.service.http.patch;
22
23
24 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration;
25 import org.onap.dcaegen2.services.sdk.rest.services.model.AaiModel;
26 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
27 import org.slf4j.MDC;
28 import org.springframework.web.reactive.function.client.ClientResponse;
29 import org.springframework.web.reactive.function.client.WebClient;
30 import org.springframework.web.util.DefaultUriBuilderFactory;
31 import reactor.core.publisher.Mono;
32
33 import java.net.URI;
34 import java.util.UUID;
35
36
37 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.REQUEST_ID;
38 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.X_INVOCATION_ID;
39 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.X_ONAP_REQUEST_ID;
40
41
42 public class AaiReactiveHttpPatchClient {
43
44     private WebClient webClient;
45     private final String aaiHost;
46     private final String aaiProtocol;
47     private final Integer aaiHostPortNumber;
48     private final String aaiBasePath;
49     private final String aaiPnfPath;
50
51     private final JsonBodyBuilder jsonBodyBuilder;
52
53     /**
54      * Constructor of AaiProducerReactiveHttpClient.
55      *
56      * @param configuration - AAI producer configuration object
57      */
58     public AaiReactiveHttpPatchClient(AaiClientConfiguration configuration, JsonBodyBuilder jsonBodyBuilder) {
59         this.aaiHost = configuration.aaiHost();
60         this.aaiProtocol = configuration.aaiProtocol();
61         this.aaiHostPortNumber = configuration.aaiPort();
62         this.aaiBasePath = configuration.aaiBasePath();
63         this.aaiPnfPath = configuration.aaiPnfPath();
64         this.jsonBodyBuilder = jsonBodyBuilder;
65     }
66
67     /**
68      * Function for calling AAI Http producer - patch request to AAI database.
69      *
70      * @param aaiModel - object which will be sent to AAI database
71      * @return status code of operation
72      */
73     public Mono<ClientResponse> getAaiProducerResponse(AaiModel aaiModel) {
74         return patchAaiRequest(aaiModel);
75     }
76
77     public AaiReactiveHttpPatchClient createAaiWebClient(WebClient webClient) {
78         this.webClient = webClient;
79         return this;
80     }
81
82     private Mono<ClientResponse> patchAaiRequest(AaiModel aaiModel) {
83         return
84             webClient.patch()
85                 .uri(getUri(aaiModel.getCorrelationId()))
86                 .header(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID))
87                 .header(X_INVOCATION_ID, UUID.randomUUID().toString())
88                 .body(Mono.just(jsonBodyBuilder.createJsonBody(aaiModel)), String.class)
89                 .exchange();
90     }
91
92     URI getUri(String pnfName) {
93         return new DefaultUriBuilderFactory().builder().scheme(aaiProtocol).host(aaiHost).port(aaiHostPortNumber)
94             .path(aaiBasePath + aaiPnfPath + "/" + pnfName).build();
95     }
96 }