0e95b0b0bdd86fbfbf6fa061f253fd255f3fd883
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018 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 com.google.gson.JsonElement;
20 import com.google.gson.JsonParser;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URI;
25 import java.nio.charset.StandardCharsets;
26 import java.util.concurrent.Future;
27
28 import javax.net.ssl.SSLContext;
29
30 import org.apache.commons.codec.binary.Base64;
31 import org.apache.commons.io.IOUtils;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.client.methods.HttpPut;
34 import org.apache.http.conn.ssl.NoopHostnameVerifier;
35 import org.apache.http.entity.ByteArrayEntity;
36 import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
37 import org.apache.http.impl.nio.client.HttpAsyncClients;
38 import org.apache.http.ssl.SSLContextBuilder;
39 import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration;
40 import org.onap.dcaegen2.collectors.datafile.io.FileSystemResourceWrapper;
41 import org.onap.dcaegen2.collectors.datafile.io.IFileSystemResource;
42 import org.onap.dcaegen2.collectors.datafile.model.CommonFunctions;
43 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
44 import org.onap.dcaegen2.collectors.datafile.service.HttpUtils;
45 import org.onap.dcaegen2.collectors.datafile.web.PublishRedirectStrategy;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.http.HttpHeaders;
49 import org.springframework.web.util.DefaultUriBuilderFactory;
50
51 import reactor.core.publisher.Flux;
52
53 /**
54  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 7/4/18
55  * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a>
56  */
57 public class DmaapProducerReactiveHttpClient {
58
59     private static final String X_ATT_DR_META = "X-ATT-DR-META";
60     private static final String NAME_JSON_TAG = "name";
61     private static final String INTERNAL_LOCATION_JSON_TAG = "internalLocation";
62     private static final String URI_SEPARATOR = "/";
63     private static final String DEFAULT_FEED_ID = "1";
64
65     private final Logger logger = LoggerFactory.getLogger(this.getClass());
66
67     private final String dmaapHostName;
68     private final Integer dmaapPortNumber;
69     private final String dmaapTopicName;
70     private final String dmaapProtocol;
71     private final String dmaapContentType;
72     private final String user;
73     private final String pwd;
74
75     private IFileSystemResource fileResource;
76     private CloseableHttpAsyncClient webClient;
77
78     /**
79      * Constructor DmaapProducerReactiveHttpClient.
80      *
81      * @param dmaapPublisherConfiguration - DMaaP producer configuration object
82      */
83     public DmaapProducerReactiveHttpClient(DmaapPublisherConfiguration dmaapPublisherConfiguration) {
84         this.dmaapHostName = dmaapPublisherConfiguration.dmaapHostName();
85         this.dmaapPortNumber = dmaapPublisherConfiguration.dmaapPortNumber();
86         this.dmaapTopicName = dmaapPublisherConfiguration.dmaapTopicName();
87         this.dmaapProtocol = dmaapPublisherConfiguration.dmaapProtocol();
88         this.dmaapContentType = dmaapPublisherConfiguration.dmaapContentType();
89         this.user = dmaapPublisherConfiguration.dmaapUserName();
90         this.pwd = dmaapPublisherConfiguration.dmaapUserPassword();
91     }
92
93     /**
94      * Function for calling DMaaP HTTP producer - post request to DMaaP DataRouter.
95      *
96      * @param consumerDmaapModel - object which will be sent to DMaaP DataRouter
97      * @return status code of operation
98      */
99     public Flux<String> getDmaapProducerResponse(ConsumerDmaapModel consumerDmaapModel) {
100         logger.trace("Entering getDmaapProducerResponse with {}", consumerDmaapModel);
101         try {
102             logger.trace("Starting to publish to DR");
103
104             webClient = getWebClient();
105             webClient.start();
106
107             HttpPut put = new HttpPut();
108             prepareHead(consumerDmaapModel, put);
109             prepareBody(consumerDmaapModel, put);
110             addUserCredentialsToHead(put);
111
112             Future<HttpResponse> future = webClient.execute(put, null);
113             HttpResponse response = future.get();
114             logger.trace(response.toString());
115             webClient.close();
116             handleHttpResponse(response);
117             return Flux.just(response.toString());
118         } catch (Exception e) {
119             logger.error("Unable to send file to DataRouter. Data: {}", consumerDmaapModel, e);
120             return Flux.empty();
121         }
122     }
123
124     private void handleHttpResponse(HttpResponse response) {
125         int statusCode = response.getStatusLine().getStatusCode();
126         if (HttpUtils.isSuccessfulResponseCode(statusCode)) {
127             logger.trace("Publish to DR successful!");
128         } else {
129             logger.error("Publish to DR unsuccessful, response code: " + statusCode);
130         }
131     }
132
133     private void addUserCredentialsToHead(HttpPut put) {
134         String plainCreds = user + ":" + pwd;
135         byte[] plainCredsBytes = plainCreds.getBytes(StandardCharsets.ISO_8859_1);
136         byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
137         String base64Creds = new String(base64CredsBytes);
138         logger.trace("base64Creds...: {}", base64Creds);
139         put.addHeader("Authorization", "Basic " + base64Creds);
140     }
141
142     private void prepareHead(ConsumerDmaapModel model, HttpPut put) {
143         put.addHeader(HttpHeaders.CONTENT_TYPE, dmaapContentType);
144         JsonElement metaData = new JsonParser().parse(CommonFunctions.createJsonBody(model));
145         String name = metaData.getAsJsonObject().remove(NAME_JSON_TAG).getAsString();
146         metaData.getAsJsonObject().remove(INTERNAL_LOCATION_JSON_TAG);
147         put.addHeader(X_ATT_DR_META, metaData.toString());
148         put.setURI(getUri(name));
149     }
150
151     private void prepareBody(ConsumerDmaapModel model, HttpPut put) {
152         String fileLocation = model.getInternalLocation();
153         IFileSystemResource fileSystemResource = getFileSystemResource();
154         fileSystemResource.setPath(fileLocation);
155         InputStream fileInputStream = null;
156         try {
157             fileInputStream = fileSystemResource.getInputStream();
158         } catch (IOException e) {
159             logger.error("Unable to get stream from filesystem.", e);
160         }
161         try {
162             put.setEntity(new ByteArrayEntity(IOUtils.toByteArray(fileInputStream)));
163         } catch (IOException e) {
164             logger.error("Unable to set put request body from ByteArray.", e);
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     private IFileSystemResource getFileSystemResource() {
175         if (fileResource == null) {
176             fileResource = new FileSystemResourceWrapper();
177         }
178         return fileResource;
179     }
180
181     protected void setFileSystemResource(IFileSystemResource fileSystemResource) {
182         fileResource = fileSystemResource;
183     }
184
185     protected CloseableHttpAsyncClient getWebClient() {
186         if (webClient != null) {
187             return webClient;
188         }
189         SSLContext sslContext = null;
190         try {
191             sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();
192         } catch (Exception e) {
193             logger.trace("Unable to get sslContext.", e);
194         }
195         //@formatter:off
196         return HttpAsyncClients.custom()
197                 .setSSLContext(sslContext)
198                 .setSSLHostnameVerifier(new NoopHostnameVerifier())
199                 .setRedirectStrategy(PublishRedirectStrategy.INSTANCE)
200                 .build();
201         //@formatter:on
202     }
203
204     protected void setWebClient(CloseableHttpAsyncClient client) {
205         this.webClient = client;
206     }
207 }