51000b09c9248b2e7328ef69c31221a1d56196dc
[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 import io.netty.handler.codec.http.HttpHeaders;
24 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.config.AaiClientConfiguration;
25 import org.onap.dcaegen2.services.sdk.rest.services.aai.client.service.http.AaiHttpClient;
26 import org.onap.dcaegen2.services.sdk.rest.services.model.AaiModel;
27 import org.onap.dcaegen2.services.sdk.rest.services.model.JsonBodyBuilder;
28 import org.onap.dcaegen2.services.sdk.rest.services.uri.URI;
29 import org.slf4j.MDC;
30 import reactor.core.publisher.Mono;
31 import reactor.netty.ByteBufFlux;
32 import reactor.netty.http.client.HttpClient;
33
34 import java.util.UUID;
35 import java.util.function.Consumer;
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 public final class AaiHttpPatchClient implements AaiHttpClient<Integer> {
42
43     private HttpClient httpClient;
44     private final AaiClientConfiguration configuration;
45     private final JsonBodyBuilder jsonBodyBuilder;
46
47
48     public AaiHttpPatchClient(final AaiClientConfiguration configuration, JsonBodyBuilder jsonBodyBuilder) {
49         this.configuration = configuration;
50         this.jsonBodyBuilder = jsonBodyBuilder;
51     }
52
53
54     public Mono<Integer> getAaiResponse(AaiModel aaiModel) {
55         return httpClient
56                 .headers(addHeaders())
57                 .baseUrl(getUri(aaiModel.getCorrelationId()))
58                 .patch()
59                 .send(ByteBufFlux.fromString(Mono.just(jsonBodyBuilder.createJsonBody(aaiModel))))
60                 .responseSingle((res, content) -> Mono.just(res.status().code()));
61     }
62
63     public AaiHttpPatchClient createAaiHttpClient(HttpClient httpClient) {
64         this.httpClient = httpClient;
65         return this;
66     }
67
68     String getUri(String pnfName) {
69         return new URI.URIBuilder()
70                 .scheme(configuration.aaiProtocol())
71                 .host(configuration.aaiHost())
72                 .port(configuration.aaiPort())
73                 .path(configuration.aaiBasePath() + configuration.aaiPnfPath() + "/" + pnfName).build().toString();
74     }
75
76     private Consumer<? super HttpHeaders> addHeaders() {
77         return h -> {
78             h.add(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID));
79             h.add(X_INVOCATION_ID, UUID.randomUUID().toString());
80         };
81     }
82 }