diff options
Diffstat (limited to 'dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service')
6 files changed, 526 insertions, 0 deletions
diff --git a/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIEnrichmentClient.java b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIEnrichmentClient.java new file mode 100644 index 0000000..8813cdf --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIEnrichmentClient.java @@ -0,0 +1,45 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================LICENSE_END=========================================== + */ + +package org.openecomp.dcae.apod.analytics.aai.service; + +import java.util.Map; + +/** + * <p> + * A client used to get enrichment details from A&AI + * </p> + * + * @author Rajiv Singla . Creation Date: 9/15/2017. + */ +public interface AAIEnrichmentClient { + + /** + * Provides enrichment details from A&AI API and returns them as string. If no enrichment lookup fails returns null + * + * @param aaiAPIPath A&AI API Path + * @param queryParams A&AI Query Params map + * @param headers A&AI HTTP Headers + * + * @return Enrichment details from A&AI API and returns them as string. If enrichment lookup fails returns null + */ + String getEnrichmentDetails(String aaiAPIPath, Map<String, String> queryParams, Map<String, String> headers); + +} diff --git a/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIEnrichmentClientFactory.java b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIEnrichmentClientFactory.java new file mode 100644 index 0000000..bd4a4f2 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIEnrichmentClientFactory.java @@ -0,0 +1,40 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================LICENSE_END=========================================== + */ + +package org.openecomp.dcae.apod.analytics.aai.service; + +import org.openecomp.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig; + +/** + * Factory to initialize instance of {@link AAIEnrichmentClient} for Guice DI injection purposes. + * + * @author Rajiv Singla . Creation Date: 9/19/2017. + */ +public interface AAIEnrichmentClientFactory { + + /** + * Provides an instance of A&AI Enrichment Client used to get details from A&AI API + * + * @param aaiHttpClientConfig A&AI Http Client config used to create A&AI Enrichment client + * + * @return an instance of A&AI Enrichment Client used to get details from A&AI API + */ + AAIEnrichmentClient create(AAIHttpClientConfig aaiHttpClientConfig); +} diff --git a/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIEnrichmentClientImpl.java b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIEnrichmentClientImpl.java new file mode 100644 index 0000000..229ab17 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIEnrichmentClientImpl.java @@ -0,0 +1,202 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================LICENSE_END=========================================== + */ + +package org.openecomp.dcae.apod.analytics.aai.service; + +import com.google.common.base.Optional; +import com.google.inject.Inject; +import com.google.inject.assistedinject.Assisted; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; +import org.openecomp.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig; +import org.openecomp.dcae.apod.analytics.common.utils.HTTPUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Iterator; +import java.util.Map; + +import javax.annotation.Nonnull; + + +/** + * A concrete implementation for {@link AAIEnrichmentClient} which uses A&AI REST API to get A&AI Enrichment details + * + * @author Rajiv Singla . Creation Date: 9/18/2017. + */ +public class AAIEnrichmentClientImpl implements AAIEnrichmentClient { + + private static final Logger LOG = LoggerFactory.getLogger(AAIEnrichmentClientImpl.class); + + private final CloseableHttpClient closeableHttpClient; + private final String aaiProtocol; + private final String aaiHost; + private final Integer aaiHostPortNumber; + + @Inject + public AAIEnrichmentClientImpl(@Assisted final AAIHttpClientConfig aaiHttpClientConfig, + final AAIHttpClientFactory aaiHttpClientFactory) { + final AAIHttpClient aaiHttpClient = aaiHttpClientFactory.create(aaiHttpClientConfig); + closeableHttpClient = aaiHttpClient.getAAIHttpClient(); + aaiProtocol = aaiHttpClientConfig.getAaiProtocol(); + aaiHost = aaiHttpClientConfig.getAaiHost(); + aaiHostPortNumber = aaiHttpClientConfig.getAaiHostPortNumber(); + } + + + /** + * Provides enrichment details from A&AI API and returns them as string. If no enrichment lookup fails returns null + * + * @param aaiAPIPath A&AI API Path + * @param queryParams A&AI Query Params map + * @param headers A&AI HTTP Headers + * + * @return Enrichment details from A&AI API and returns them as string. If enrichment lookup fails returns null + */ + @Override + public String getEnrichmentDetails(final String aaiAPIPath, final Map<String, String> queryParams, + final Map<String, String> headers) { + + final URI enrichmentURI = + createAAIEnrichmentURI(aaiProtocol, aaiHost, aaiHostPortNumber, aaiAPIPath, queryParams); + + if (enrichmentURI == null) { + return null; + } + + // create new get request + final HttpGet getRequest = new HttpGet(enrichmentURI); + // add http headers + for (Map.Entry<String, String> headersEntry : headers.entrySet()) { + getRequest.addHeader(headersEntry.getKey(), headersEntry.getValue()); + } + + Optional<String> enrichmentDetails = Optional.absent(); + // execute http get request + try { + enrichmentDetails = closeableHttpClient.execute(getRequest, aaiResponseHandler()); + } catch (IOException ex) { + LOG.error("Failed to get A&AI Enrichment Details for A&AI Enrichment URI: {} A&AI Error: {}", + enrichmentURI, ex); + } + + // return response + if (enrichmentDetails.isPresent()) { + return enrichmentDetails.get(); + } else { + return null; + } + } + + /** + * Create A&AI API Enrichment URI. If invalid URI - null will be returned + * + * @param protocol A&AI API protocol + * @param hostName A&AI API hostname + * @param portNumber A&AI API port number + * @param path A&AI API path + * @param queryParams A&AI API query parameters + * + * @return A&AI API Enrichment URI + */ + private URI createAAIEnrichmentURI(final String protocol, final String hostName, + final Integer portNumber, final String path, + Map<String, String> queryParams) { + + final URIBuilder uriBuilder = new URIBuilder().setScheme(protocol).setHost(hostName).setPort(portNumber) + .setPath(path); + + // creates custom query string which is not encoded + final String customQuery = createCustomQuery(queryParams); + if (StringUtils.isNoneBlank(customQuery)) { + uriBuilder.setCustomQuery(customQuery); + } + + URI enrichmentURI = null; + try { + enrichmentURI = uriBuilder.build(); + } catch (URISyntaxException e) { + LOG.error("URI Syntax Exception when creating A&AI Enrichment URI. " + + "Protocol: {}, HostName: {}, Port: {}, Path: {}, Custom Query String: {}, Exception: {}", + protocol, hostName, portNumber, path, customQuery, e); + } + + LOG.trace("Created A&AI Enrichment URI: {}", enrichmentURI); + return enrichmentURI; + } + + /** + * Creates Custom Query string to be used for A&AI API URI as A&AI currently does not expect encoded + * query params. + * + * @param queryParams query param map + * + * @return custom query string which does not encode query param values + */ + private static String createCustomQuery(@Nonnull final Map<String, String> queryParams) { + final StringBuilder queryStringBuilder = new StringBuilder(""); + final Iterator<Map.Entry<String, String>> queryParamIterator = queryParams.entrySet().iterator(); + while (queryParamIterator.hasNext()) { + final Map.Entry<String, String> queryParamsEntry = queryParamIterator.next(); + queryStringBuilder.append(queryParamsEntry.getKey()); + queryStringBuilder.append("="); + queryStringBuilder.append(queryParamsEntry.getValue()); + if (queryParamIterator.hasNext()) { + queryStringBuilder.append("&"); + } + } + return queryStringBuilder.toString(); + } + + /** + * Response Handler for A&AI Enrichment API + * + * @return Response Handler that + */ + static ResponseHandler<Optional<String>> aaiResponseHandler() { + return new ResponseHandler<Optional<String>>() { + @Override + public Optional<String> handleResponse(final HttpResponse response) throws IOException { + final int responseCode = response.getStatusLine().getStatusCode(); + final HttpEntity responseEntity = response.getEntity(); + if (HTTPUtils.isSuccessfulResponseCode(responseCode) && null != responseEntity) { + final String aaiResponse = EntityUtils.toString(responseEntity); + return Optional.of(aaiResponse); + } else { + String aaiResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : ""; + LOG.error("Unable to fetch response from A&AI API. A&AI Response Code: {}, " + + "A&AI Response Message: {}", responseCode, aaiResponse); + return Optional.absent(); + } + } + }; + } + +} + diff --git a/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIHttpClient.java b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIHttpClient.java new file mode 100644 index 0000000..a4adad9 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIHttpClient.java @@ -0,0 +1,39 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================LICENSE_END=========================================== + */ + +package org.openecomp.dcae.apod.analytics.aai.service; + +import org.apache.http.impl.client.CloseableHttpClient; + +/** + * An HTTP Client used to make REST calls to A&AI Enrichment API + * + * @author Rajiv Singla . Creation Date: 9/19/2017. + */ +public interface AAIHttpClient { + + /** + * Provides an instance of {@link CloseableHttpClient} used to make REST calls to A&AI Enrichment API + * + * @return An instance of Closeable HTTP Client used to make A&AI API Rest calls + */ + CloseableHttpClient getAAIHttpClient(); + +} diff --git a/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIHttpClientFactory.java b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIHttpClientFactory.java new file mode 100644 index 0000000..e977639 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIHttpClientFactory.java @@ -0,0 +1,41 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================LICENSE_END=========================================== + */ + +package org.openecomp.dcae.apod.analytics.aai.service; + +import org.openecomp.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig; + +/** + * Factory to initialize instance of {@link AAIHttpClient} for Guice DI injection purposes. + * + * @author Rajiv Singla . Creation Date: 9/22/2017. + */ +public interface AAIHttpClientFactory { + + /** + * Provides an instance of A&AI HTTP Client + * + * @param aaiHttpClientConfig A&AI HTTP Client Config + * + * @return An instance of A&AI HTTP Client + */ + AAIHttpClient create(AAIHttpClientConfig aaiHttpClientConfig); + +} diff --git a/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIHttpClientImpl.java b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIHttpClientImpl.java new file mode 100644 index 0000000..34631b1 --- /dev/null +++ b/dcae-analytics-aai/src/main/java/org/openecomp/dcae/apod/analytics/aai/service/AAIHttpClientImpl.java @@ -0,0 +1,159 @@ +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================LICENSE_END=========================================== + */ + +package org.openecomp.dcae.apod.analytics.aai.service; + +import com.google.inject.Inject; +import com.google.inject.assistedinject.Assisted; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.DefaultProxyRoutePlanner; +import org.apache.http.ssl.SSLContextBuilder; +import org.openecomp.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig; +import org.openecomp.dcae.apod.analytics.aai.utils.ssl.AlwaysTrustingTrustStrategy; +import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URL; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; + +/** + * <p> + * A concrete implementation of {@link AAIHttpClient} which provides Apache {@link CloseableHttpClient} for + * making rest calls to A&AI Enrichment API. + * </p> + * + * @author Rajiv Singla . Creation Date: 9/19/2017. + */ +public class AAIHttpClientImpl implements AAIHttpClient { + + private static final Logger LOG = LoggerFactory.getLogger(AAIHttpClientImpl.class); + + private final AAIHttpClientConfig aaiHttpClientConfig; + + @Inject + public AAIHttpClientImpl(@Assisted final AAIHttpClientConfig aaiHttpClientConfig) { + this.aaiHttpClientConfig = aaiHttpClientConfig; + } + + /** + * Provides an instance of {@link CloseableHttpClient} used to make REST calls to A&AI Enrichment API + * + * @return An instance of Closeable HTTP Client used to make A&AI API Rest calls + */ + @Override + public CloseableHttpClient getAAIHttpClient() { + + final HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); + final boolean aaiIgnoreSSLCertificateErrors = aaiHttpClientConfig.isAaiIgnoreSSLCertificateErrors(); + + // Setup SSL Context to ignore SSL certificate issues if ignoreSSLCertificateErrors is true + LOG.info("SSL Certificate Errors attributed is set to : {}", aaiIgnoreSSLCertificateErrors); + + if (aaiIgnoreSSLCertificateErrors) { + LOG.warn("SSL Certificate Errors will be ignored for this A&AI Http Client Instance"); + try { + SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); + sslContextBuilder.loadTrustMaterial(null, new AlwaysTrustingTrustStrategy()); + httpClientBuilder.setSSLContext(sslContextBuilder.build()); + } catch (NoSuchAlgorithmException e) { + final String errorMessage = "NoSuchAlgorithmException while setting SSL Context for AAI HTTP Client."; + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } catch (KeyStoreException e) { + final String errorMessage = "KeyStoreException while setting SSL Context for AAI HTTP Client."; + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } catch (KeyManagementException e) { + final String errorMessage = "KeyManagementException while setting SSL Context for AAI HTTP Client."; + throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e); + } + + httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE); + + } else { + LOG.info("SSL Certification Errors will be enforced for A&AI Http Client instance"); + } + + // Setup credentials and proxy + final String aaiUserName = aaiHttpClientConfig.getAaiUserName(); + + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + + if (aaiUserName != null) { + final String aaiHost = aaiHttpClientConfig.getAaiHost(); + final Integer aaiHostPortNumber = aaiHttpClientConfig.getAaiHostPortNumber(); + final String aaiUserPassword = aaiHttpClientConfig.getAaiUserPassword(); + LOG.info("Setting A&AI host credentials for AAI Host: {}", aaiHost); + final AuthScope aaiHostPortAuthScope = new AuthScope(aaiHost, aaiHostPortNumber); + final Credentials aaiCredentials = new UsernamePasswordCredentials(aaiUserName, aaiUserPassword); + credentialsProvider.setCredentials(aaiHostPortAuthScope, aaiCredentials); + } else { + LOG.warn("A&AI userName not present. No credentials set for A&AI authentication"); + } + + final URL aaiProxyURL = aaiHttpClientConfig.getAaiProxyURL(); + + if (aaiProxyURL != null) { + final String aaiProxyHost = aaiProxyURL.getHost(); + final Integer aaiProxyPortNumber = aaiProxyURL.getPort(); + final String aaiProxyProtocol = aaiProxyURL.getProtocol(); + final HttpHost proxy = new HttpHost(aaiProxyHost, aaiProxyPortNumber, aaiProxyProtocol); + LOG.info("Setting A&AI Http Client default proxy as: {}", proxy); + final DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); + httpClientBuilder.setRoutePlanner(routePlanner); + + final String userInfo = aaiProxyURL.getUserInfo(); + if (StringUtils.isNotBlank(userInfo)) { + final String[] userInfoArray = userInfo.split(":"); + final String aaiProxyUsername = userInfoArray[0]; + String aaiProxyPassword = null; + if (userInfoArray.length > 1) { + aaiProxyPassword = userInfoArray[1]; + } + LOG.info("Setting A&AI Http Client proxy credentials with username: {}", aaiProxyUsername); + final AuthScope aaiProxyAuthScope = new AuthScope(aaiProxyHost, aaiProxyPortNumber); + final Credentials aaiProxyCredentials = new UsernamePasswordCredentials(aaiProxyUsername, + aaiProxyPassword); + credentialsProvider.setCredentials(aaiProxyAuthScope, aaiProxyCredentials); + } else { + LOG.debug("NO A&AI Proxy Username present.Bypassing setting up A&AI Proxy authentication credentials"); + } + } else { + LOG.debug("A&AI proxy not Enabled - bypassing setting A&AI Proxy settings"); + } + + // setup credentials provider + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + + return httpClientBuilder.build(); + } + +} |