9304688f22815548e9d34df30a322651022e0b41
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.service.producer;
18
19 import static org.onap.dcaegen2.collectors.datafile.model.logging.MdcVariables.REQUEST_ID;
20 import static org.onap.dcaegen2.collectors.datafile.model.logging.MdcVariables.X_INVOCATION_ID;
21 import static org.onap.dcaegen2.collectors.datafile.model.logging.MdcVariables.X_ONAP_REQUEST_ID;
22
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonParser;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URI;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.security.KeyManagementException;
33 import java.security.KeyStoreException;
34 import java.security.NoSuchAlgorithmException;
35 import java.util.Map;
36 import java.util.UUID;
37 import java.util.concurrent.Future;
38
39 import javax.net.ssl.SSLContext;
40
41 import org.apache.commons.codec.binary.Base64;
42 import org.apache.commons.io.IOUtils;
43 import org.apache.http.HttpResponse;
44 import org.apache.http.client.methods.HttpPut;
45 import org.apache.http.conn.ssl.NoopHostnameVerifier;
46 import org.apache.http.entity.ByteArrayEntity;
47 import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
48 import org.apache.http.impl.nio.client.HttpAsyncClients;
49 import org.apache.http.ssl.SSLContextBuilder;
50 import org.onap.dcaegen2.collectors.datafile.io.FileSystemResourceWrapper;
51 import org.onap.dcaegen2.collectors.datafile.io.IFileSystemResource;
52 import org.onap.dcaegen2.collectors.datafile.model.CommonFunctions;
53 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
54 import org.onap.dcaegen2.collectors.datafile.model.logging.MdcVariables;
55 import org.onap.dcaegen2.collectors.datafile.web.PublishRedirectStrategy;
56 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapPublisherConfiguration;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.slf4j.MDC;
60 import org.slf4j.Marker;
61 import org.slf4j.MarkerFactory;
62 import org.springframework.http.HttpHeaders;
63 import org.springframework.http.HttpStatus;
64 import org.springframework.web.util.DefaultUriBuilderFactory;
65
66 import reactor.core.publisher.Mono;
67
68 /**
69  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
70  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
71  */
72 public class DmaapProducerReactiveHttpClient {
73
74     private static final String X_DMAAP_DR_META = "X-DMAAP-DR-META";
75     private static final String NAME_JSON_TAG = "name";
76     private static final String INTERNAL_LOCATION_JSON_TAG = "internalLocation";
77     private static final String URI_SEPARATOR = "/";
78     private static final String DEFAULT_FEED_ID = "1";
79     private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
80     private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE_RETURN");
81
82     private final Logger logger = LoggerFactory.getLogger(this.getClass());
83
84     private final String dmaapHostName;
85     private final Integer dmaapPortNumber;
86     private final String dmaapTopicName;
87     private final String dmaapProtocol;
88     private final String dmaapContentType;
89     private final String user;
90     private final String pwd;
91
92     private IFileSystemResource fileResource = new FileSystemResourceWrapper();
93
94     /**
95      * Constructor DmaapProducerReactiveHttpClient.
96      *
97      * @param dmaapPublisherConfiguration - DMaaP producer configuration object
98      */
99     public DmaapProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) {
100         this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
101         this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
102         this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
103         this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
104         this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType();
105         this.user = dmaapPublisherConfiguration.dmaapUserName();
106         this.pwd = dmaapPublisherConfiguration.dmaapUserPassword();
107     }
108
109     /**
110      * Function for calling DMaaP HTTP producer - post request to DMaaP DataRouter.
111      *
112      * @param consumerDmaapModel - object which will be sent to DMaaP DataRouter
113      * @return status code of operation
114      */
115     public Mono<HttpStatus> getDmaapProducerResponse(ConsumerDmaapModel consumerDmaapModel,
116             Map<String, String> contextMap) {
117         MdcVariables.setMdcContextMap(contextMap);
118        try (CloseableHttpAsyncClient webClient = createWebClient()) {
119
120             HttpPut put = new HttpPut();
121             prepareHead(consumerDmaapModel, put);
122             prepareBody(consumerDmaapModel, put);
123             addUserCredentialsToHead(put);
124
125             logger.trace(INVOKE, "Starting to publish to DR {}", consumerDmaapModel.getInternalLocation());
126             Future<HttpResponse> future = webClient.execute(put, null);
127             HttpResponse response = future.get();
128             logger.trace(INVOKE_RETURN, "Response from DR {}", response);
129             return Mono.just(HttpStatus.valueOf(response.getStatusLine().getStatusCode()));
130         } catch (Exception e) {
131             logger.error("Unable to send file to DataRouter. Data: {}", consumerDmaapModel.getInternalLocation(), e);
132             return Mono.error(e);
133         }
134     }
135
136     private void addUserCredentialsToHead(HttpPut put) {
137         String plainCreds = user + ":" + pwd;
138         byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1);
139         byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
140         String base64Creds = new String(base64CredsBytes);
141         logger.trace("base64Creds...: {}", base64Creds);
142         put.addHeader("Authorization", "Basic " + base64Creds);
143     }
144
145     private void prepareHead(ConsumerDmaapModel model, HttpPut put) {
146         put.addHeader(HttpHeaders.CONTENT_TYPE, dmaapContentType);
147         JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(model));
148         String name = metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
149         metaData.getAsJsonObject().remove(INTERNAL_LOCATION_JSON_TAG);
150         put.addHeader(X_DMAAP_DR_META, metaData.toString());
151         put.setURI(getUri(name));
152
153         String requestID = MDC.get(REQUEST_ID);
154         put.addHeader(X_ONAP_REQUEST_ID, requestID);
155         String invocationID = UUID.randomUUID().toString();
156         put.addHeader(X_INVOCATION_ID, invocationID);
157     }
158
159     private void prepareBody(ConsumerDmaapModel model, HttpPut put) throws IOException {
160         Path fileLocation = Paths.get(model.getInternalLocation());
161         this.fileResource.setPath(fileLocation);
162         InputStream fileInputStream = fileResource.getInputStream();
163
164         put.setEntity(new ByteArrayEntity(IOUtils.toByteArray(fileInputStream)));
165
166     }
167
168     private URI getUri(String fileName) {
169         String path = dmaapTopicName + URI_SEPARATOR + DEFAULT_FEED_ID + URI_SEPARATOR + fileName;
170         return new DefaultUriBuilderFactory().builder().scheme(dmaapProtocol).host(dmaapHostName).port(dmaapPortNumber)
171                 .path(path).build();
172     }
173
174     void setFileSystemResource(IFileSystemResource fileSystemResource) {
175         fileResource = fileSystemResource;
176     }
177
178     protected CloseableHttpAsyncClient createWebClient()
179             throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
180
181         SSLContext sslContext = new SSLContextBuilder() //
182                 .loadTrustMaterial(null, (certificate, authType) -> true) //
183                 .build();
184
185         CloseableHttpAsyncClient webClient =  HttpAsyncClients.custom() //
186                 .setSSLContext(sslContext) //
187                 .setSSLHostnameVerifier(new NoopHostnameVerifier()) //
188                 .setRedirectStrategy(PublishRedirectStrategy.INSTANCE) //
189                 .build();
190         webClient.start();
191         return webClient;
192     }
193
194 }