* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
- * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017-2018 European Software Marketing Ltd.
+ * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2019 European Software Marketing Ltd.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
* ============LICENSE_END=========================================================
*/
+
package org.onap.aai.modelloader.restclient;
import com.google.gson.Gson;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.aai.babel.service.data.BabelArtifact;
+import org.onap.aai.cl.api.LogFields;
+import org.onap.aai.cl.api.LogLine;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
+import org.onap.aai.cl.mdc.MdcContext;
+import org.onap.aai.cl.mdc.MdcOverride;
import org.onap.aai.modelloader.config.ModelLoaderConfig;
import org.onap.aai.modelloader.service.ModelLoaderMsgs;
public class HttpsBabelServiceClient implements BabelServiceClient {
private static final Logger logger = LoggerFactory.getInstance().getLogger(HttpsBabelServiceClient.class);
+ private static final Logger metricsLogger =
+ LoggerFactory.getInstance().getMetricsLogger(HttpsBabelServiceClient.class);
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
private static final String SSL_PROTOCOL = "TLS";
private static final String KEYSTORE_ALGORITHM = "SunX509";
this.config = config;
logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Creating Babel Service client");
+ //Initialize SSL Context only if SSL is enabled
+ if (config.useHttpsWithBabel()) {
+ SSLContext ctx = SSLContext.getInstance(SSL_PROTOCOL);
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEYSTORE_ALGORITHM);
+ KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);
- SSLContext ctx = SSLContext.getInstance(SSL_PROTOCOL);
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEYSTORE_ALGORITHM);
- KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);
-
- String clientCertPassword = config.getBabelKeyStorePassword();
+ String clientCertPassword = config.getBabelKeyStorePassword();
- char[] pwd = null;
- if (clientCertPassword != null) {
- pwd = clientCertPassword.toCharArray();
- }
+ char[] pwd = null;
+ if (clientCertPassword != null) {
+ pwd = clientCertPassword.toCharArray();
+ }
- TrustManager[] trustManagers = getTrustManagers();
+ TrustManager[] trustManagers = getTrustManagers();
- String clientCertFileName = config.getBabelKeyStorePath();
- if (clientCertFileName == null) {
- ctx.init(null, trustManagers, null);
- } else {
- InputStream fin = Files.newInputStream(Paths.get(clientCertFileName));
- keyStore.load(fin, pwd);
- kmf.init(keyStore, pwd);
- ctx.init(kmf.getKeyManagers(), trustManagers, null);
- }
+ String clientCertFileName = config.getBabelKeyStorePath();
+ if (clientCertFileName == null) {
+ ctx.init(null, trustManagers, null);
+ } else {
+ InputStream fin = Files.newInputStream(Paths.get(clientCertFileName));
+ keyStore.load(fin, pwd);
+ kmf.init(keyStore, pwd);
+ ctx.init(kmf.getKeyManagers(), trustManagers, null);
+ }
- logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Initialised context");
+ logger.debug(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Initialised context");
- HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
- HttpsURLConnection.setDefaultHostnameVerifier((host, session) -> true);
+ HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
+ HttpsURLConnection.setDefaultHostnameVerifier((host, session) -> true);
+ }
client = Client.create(new DefaultClientConfig());
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
try {
finalLocalTm.checkServerTrusted(chain, authType);
- } catch (CertificateException e) {
+ } catch (CertificateException e) { // NOSONAR
defaultTrustManager.checkServerTrusted(chain, authType);
}
}
+ " Artifact version: " + artifactVersion + " Artifact payload: " + encodedPayload);
}
+ MdcOverride override = new MdcOverride();
+ override.addAttribute(MdcContext.MDC_START_TIME, ZonedDateTime.now().format(formatter));
+
WebResource webResource = client.resource(config.getBabelBaseUrl() + config.getBabelGenerateArtifactsUrl());
ClientResponse response = webResource.type("application/json")
.header(AaiRestClient.HEADER_TRANS_ID, Collections.singletonList(transactionId))
"Babel response " + response.getStatus() + " " + sanitizedJson);
}
+ metricsLogger.info(ModelLoaderMsgs.BABEL_REST_REQUEST, new LogFields() //
+ .setField(LogLine.DefinedFields.TARGET_ENTITY, "Babel")
+ .setField(LogLine.DefinedFields.STATUS_CODE,
+ Response.Status.fromStatusCode(response.getStatus()).getFamily()
+ .equals(Response.Status.Family.SUCCESSFUL) ? "COMPLETE" : "ERROR")
+ .setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatus())
+ .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, response.getStatusInfo().toString()), //
+ override, response.toString());
+
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
throw new BabelServiceClientException(sanitizedJson);
}